/// <summary> /// 如果存在的话读取首行 /// </summary> public TEntity LoadFirst(string condition, params SQLiteParameter[] args) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.LoadFirstInner(condition, args)); } }
/// <summary> /// 执行SQL /// </summary> /// <param name="sql">SQL语句</param> /// <returns>被影响的行数</returns> /// <remarks> /// 注意,如果有参数时,都是匿名参数,请使用?序号的形式访问参数 /// </remarks> public int Execute(string sql) { using (SqliteDataBaseScope.CreateScope(this)) { return(this.ExecuteInner(sql)); } }
/// <summary> /// 总数 /// </summary> public long Count(string condition, params SQLiteParameter[] args) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.CountInner(condition, args)); } }
/// <summary> /// 主键数据是否存在 /// </summary> public bool ExistPrimaryKey(object key) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.ExistInner(this.PrimaryKeyConditionSQL, CreatePimaryKeyParameter(key))); } }
/// <summary> /// 总数 /// </summary> public long Count() { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.CountInner()); } }
/// <summary> /// 表是否存在 /// </summary> /// <param name="table">表名</param> /// <returns>True表示表已存在</returns> public bool IsExist(string table) { using (SqliteDataBaseScope.CreateScope(this)) { return(IsExistTable(table)); } }
/// <summary> /// 是否存在数据 /// </summary> /// <returns>是否存在数据</returns> public bool Any() { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.ExistInner()); } }
/// <summary> /// 执行SQL /// </summary> /// <param name="sql">SQL语句</param> /// <param name="args">参数</param> /// <returns>操作的第一行第一列或空</returns> /// <remarks> /// 注意,如果有参数时,都是匿名参数,请使用?序号的形式访问参数 /// </remarks> public T ExecuteScalar <T>(string sql, params object[] args) { using (SqliteDataBaseScope.CreateScope(this)) { return(ExecuteScalarInner <T>(sql, args)); } }
/// <summary> /// 全表读取 /// </summary> public List <TEntity> LoadData() { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.LoadDataInner()); } }
/// <summary> /// 主键读取 /// </summary> public TEntity LoadByPrimaryKey(object key) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.LoadFirstInner(this.PrimaryKeyConditionSQL, CreatePimaryKeyParameter(key))); } }
/// <summary> /// 如果存在的话读取首行 /// </summary> public List <TEntity> LoadByForeignKey(string foreignKey, object key) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.LoadDataInner(FieldConditionSQL(foreignKey), CreateFieldParameter(foreignKey, key))); } }
/// <summary> /// 保存 /// </summary> public void Save(TEntity entity) { using (SqliteDataBaseScope.CreateScope(this.DataBase, true)) { this.SaveInner(entity); } }
/// <summary> /// 重新读取 /// </summary> public void ReLoad(TEntity entity) { using (SqliteDataBaseScope.CreateScope(this.DataBase, true)) { this.ReLoadInner(entity); } }
/// <summary> /// 保存值 /// </summary> public void SaveValue(string field, object value, string[] conditionFiles, object[] values) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { this.SaveValueInner(field, value, conditionFiles, values); } }
/// <summary> /// 执行SQL /// </summary> /// <param name="sql">SQL语句</param> /// <returns>操作的第一行第一列或空</returns> /// <remarks> /// 注意,如果有参数时,都是匿名参数,请使用?序号的形式访问参数 /// </remarks> public T ExecuteScalar <T>(string sql) { using (SqliteDataBaseScope.CreateScope(this)) { return(ExecuteScalarInner <T>(sql)); } }
/// <summary> /// 如果存在的话读取首行 /// </summary> public Object LoadValue(string field, string condition, params SQLiteParameter[] args) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.LoadValueInner(field, condition, args)); } }
/// <summary> /// 如果存在的话读取首行 /// </summary> public TEntity LoadFirst(string condition = null) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.LoadFirstInner(condition)); } }
/// <summary> /// 删除所有数据 /// </summary> public void Clear() { using (SqliteDataBaseScope.CreateScope(this.DataBase, true)) { this.DeleteInner(); return; } }
/// <summary> /// 删除 /// </summary> public void Delete(TEntity entity) { using (SqliteDataBaseScope.CreateScope(this.DataBase, true)) { this.DeleteInner(entity); } //entity.RaiseStatusChanged(NotificationStatusType.Deleted); }
/// <summary> /// 新增 /// </summary> public void Insert(TEntity entity) { using (SqliteDataBaseScope.CreateScope(this.DataBase, true)) { this.InsertInner(entity); this.ReLoadInner(entity); } //entity.RaiseStatusChanged(NotificationStatusType.Saved); }
/// <summary> /// 是否存在数据 /// </summary> /// <param name="lambda">查询表达式</param> /// <returns>是否存在数据</returns> public bool Any(Expression <Func <TEntity, bool> > lambda) { var convert = PredicateConvert.Convert(Fields, lambda); using (SqliteDataBaseScope.CreateScope(this.DataBase)) { return(this.ExistInner(convert.ConditionSql, convert.Parameters)); } }
/// <summary> /// 执行SQL /// </summary> /// <param name="sql">SQL语句</param> /// <param name="args">参数</param> /// <returns>操作的第一行第一列或空</returns> /// <remarks> /// 注意,如果有参数时,都是匿名参数,请使用?序号的形式访问参数 /// </remarks> public object ExecuteScalar(string sql, params SQLiteParameter[] args) { using (SqliteDataBaseScope.CreateScope(this)) { return(args.Length == 0 ? this.ExecuteScalarInner(sql) : this.ExecuteScalarInner(sql, args)); } }
/// <summary> /// 总数据量 /// </summary> protected long CountInner(string condition, SQLiteParameter[] args) { using (SqliteDataBaseScope.CreateScope(this.DataBase)) { var sql = string.IsNullOrWhiteSpace(condition) ? string.Format(@"SELECT COUNT(*) FROM [{0}];", this.TableName) : string.Format(@"SELECT COUNT(*) FROM [{0}] WHERE {1};", this.TableName, condition); return((long)this.DataBase.ExecuteScalar(sql, args)); } }
/// <summary> /// 如果表不存在,就创建它 /// </summary> /// <param name="table">表名</param> /// <remarks>TableSql中必须存在对应的表的创建SQL,否则会因为在字典中找不到对象或执行SQL为空而发生异常</remarks> public void CheckTable(string table) { using (SqliteDataBaseScope.CreateScope(this)) { if (IsExistTable(table)) { return; } CreateTableInner(table); } }
/// <summary> /// 条件删除 /// </summary> public int Delete(string condition, params SQLiteParameter[] args) { if (string.IsNullOrWhiteSpace(condition)) { throw new ArgumentException(@"删除条件不能为空,因为不允许执行全表删除", "condition"); } using (SqliteDataBaseScope.CreateScope(this.DataBase, true)) { return(this.DeleteInner(condition, args)); } }
/// <summary> /// 执行SQL /// </summary> /// <param name="sql">SQL语句</param> /// <param name="args">参数</param> /// <returns>操作的第一行第一列或空</returns> /// <remarks> /// 注意,如果有参数时,都是匿名参数,请使用?序号的形式访问参数 /// </remarks> public object ExecuteScalar(string sql, params object[] args) { using (SqliteDataBaseScope.CreateScope(this)) { return(args.Length == 0 ? this.ExecuteScalarInner(sql) : this.ExecuteScalarInner(sql, args.Select(p => p is SQLiteParameter ? p as SQLiteParameter : new SQLiteParameter { Value = p }).ToArray())); } }
/// <summary> /// 删除 /// </summary> public void Delete(IEnumerable <TEntity> entities) { using (SqliteDataBaseScope.CreateScope(this.DataBase, true)) { foreach (var entity in entities) { this.DeleteInner(entity); //entity.RaiseStatusChanged(NotificationStatusType.Deleted); } } }
/// <summary> /// 读取全部 /// </summary> protected List <TEntity> LoadDataInner(string condition, SQLiteParameter[] args) { var results = new List <TEntity>(); using (SqliteDataBaseScope.CreateScope(this.DataBase)) { var cmd = CreateLoadCommand(condition, args); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { //lock (DataBase) { results.Add(this.LoadEntity(reader)); } } } } return(results); }