using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace FLY.OBJComponents.Common.SQLite { public class DBTable : IDBTable where T : new() { public List ArrayFieldTypeInfos = new List(); public string TableName { get; private set; } public string DDL { get { return SQLiteHelper.GetCreateTableCommandText(typeof(T), ArrayFieldTypeInfos.ToArray()); } } SQLiteHelper sqliteHelper; public DBTable() { TableName = SQLiteHelper.GetTableName(typeof(T)); } public void Init(string connectionString) { sqliteHelper = new SQLiteHelper { ConnectionString = connectionString }; } public void Create() { sqliteHelper.ExecuteNonQuery(SQLiteHelper.GetCreateTableCommandText(typeof(T), ArrayFieldTypeInfos.ToArray())); } public void Add(T t) { sqliteHelper.ExecuteNonQuery(SQLiteHelper.GetInsertCommandText(t)); } public void Update(T t, string condition) { sqliteHelper.ExecuteNonQuery(SQLiteHelper.GetUpdateCommandText(t, condition)); } public bool AddRange(IEnumerable array) { List querys = new List(); foreach (T t in array) { querys.Add(SQLiteHelper.GetInsertCommandText(t)); } return sqliteHelper.QueryTran(querys); } public List Find(string condition) { string sql = $"SELECT * FROM {TableName}"; if (!string.IsNullOrEmpty(condition)) sql += " " + condition; DataTable dataTable = sqliteHelper.ExecuteReader(sql); return SQLiteHelper.ToObjs(dataTable, ArrayFieldTypeInfos.ToArray()); } /// /// 获取最后N行数据 /// /// /// public List Last(int count) { return Find($"LIMIT (SELECT COUNT()-{count} FROM {TableName}),{count}"); } /// /// 获取前面N行数据 /// /// /// public List First(int count) { return Find($"LIMIT {count}"); } public void Remove() { } } }