/// <summary> /// 执行查询获取第一行第一列的值 /// </summary> /// <returns>第一行第一列的值</returns> public object Execute() { //创建CommandTree ICommandTree queryTree = base.CommandTreeFactory.GetQueryTree(base.NodeBuilders); //执行查询获取第一行,第一列的值 return(base.DbHelper.GetScalar(queryTree.Compile(), parameters: base.Parameters.ToArray())); }
/// <summary> /// 获取update所有非主键列的sql /// </summary> /// <returns>update所有非主键列的sql</returns> private string GetUpdateAllSql() { //获取TableHeader对象 ITableHeader tableHeader = this.TableMapper.Header; //获取命令生成树 ICommandTree commandTree = this.CommandTreeFactory.GetUpdateTree(tableHeader.SchemaName, tableHeader.TableName, tableHeader.TableAlias, this.GetUpdateAllChildNodes()); //获取Insert命令 return(commandTree.Compile()); }
/// <summary> /// 获取枚举器 /// </summary> /// <returns>枚举器</returns> public IEnumerator <TEntity> GetEnumerator() { //创建CommandTree ICommandTree queryTree = base.CommandTreeFactory.GetQueryTree(this.GetNodeBuilders()); //执行查询 foreach (TEntity entity in base.DbHelper.GetResults(this.CreateEntity, queryTree.Compile(), parameters: base.Parameters.ToArray())) { yield return(entity); } }
/// <summary> /// 获取TEntity类型的枚举器 /// </summary> /// <returns>TEntity类型的枚举器</returns> public IEnumerator <TEntity> GetEnumerator() { //获取查询命令生成树 ICommandTree queryTree = base.CommandTreeFactory.GetQueryTree(this.NodeBuilders); //执行查询获取结果 foreach (TEntity entity in base.DbHelper.GetResults(this.CreateEntity, queryTree.Compile())) { yield return(entity); } }
/// <summary> /// 获取枚举器 /// </summary> /// <returns>枚举器</returns> public IEnumerator <TModel> GetEnumerator() { //创建CommandTree ICommandTree queryTree = this.commandTreeFactory.GetWithAsQueryTree(this.InnerQuerySql, this.NodeBuilders); //执行查询 foreach (TModel model in this.dbHelper.GetResults(this.CreateModel, queryTree.Compile(), parameters: this.Parameters.ToArray())) { yield return(model); } }
/// <summary> /// 判断当前ID的实体是否存在 /// </summary> /// <param name="id">实体的ID</param> /// <returns>1:存在 0:不存在</returns> public int Exist(object id) { //非空检查 Check.ArgumentNull(id, nameof(id)); //获取CommandTree ICommandTree queryTree = this.CommandTreeFactory.GetQueryTree(this.GetQueryCountByIdChildBuilders()); //获取Sql参数数组 IDbDataParameter[] parameters = { base.DbHelper.Parameter(base.TableMapper.KeyMapper.Property.Name, id) }; //执行查询获取第一行第一列的数据 return(TypeHelper.ConvertTo <int>(base.DbHelper.GetScalar(queryTree.Compile(), parameters: parameters))); }
/// <summary> /// 创建表 /// </summary> /// <param name="dbHelper">操作数据库的Helper</param> /// <param name="commandTreeFactory">创建CommandTree的工厂</param> /// <param name="tableMapper">Table元数据解析器</param> public int CreateTable(DbHelper dbHelper, ICommandTreeFactory commandTreeFactory, ITableMapper tableMapper) { //获取TableHeader ITableHeader tableHeader = tableMapper.Header; //获取ColumnNodes IEnumerable <IColumnNode> columnNodes = tableMapper.GetColumnMappers().Select(m => m.ToColumnNode()); //获取建表语句生成树 ICommandTree buildTableTree = commandTreeFactory.GetBuildTableTree(tableHeader.SchemaName, tableHeader.TableName, columnNodes); //创建建表语句并执行 return(dbHelper.ExecuteUpdate(buildTableTree.Compile())); }
/// <summary> /// 获取当前实体对象 /// </summary> /// <param name="id">实体的ID</param> /// <returns>实体对象</returns> public TEntity this[object id] { get { //非空检查 Check.ArgumentNull(id, nameof(id)); //获取查询命令生成树 ICommandTree queryTree = base.CommandTreeFactory.GetQueryTree(this.GetQueryByIdChildBuilders()); //获取Sql参数数组 IDbDataParameter[] parameters = { base.DbHelper.Parameter(base.TableMapper.KeyMapper.Property.Name, id) }; //执行查询 return(base.DbHelper.GetResults(this.CreateEntity, queryTree.Compile(), parameters: parameters).SingleOrDefault()); } }
/// <summary> /// 批量删除数据库中的实体对象 /// </summary> /// <param name="entities">待删除的实体对象数据源</param> /// <returns>删除的实体对象的数量</returns> public int RemoveAll(IDbQuery <TEntity> entities) { //非空检查 Check.ArgumentNull(entities, nameof(entities)); //创建Delete命令生成树 ICommandTree deleteTree = this.CommandTreeFactory.GetDeleteTree( this.TableMapper.Header.SchemaName, this.TableMapper.Header.TableName, this.TableMapper.Header.TableAlias, entities.NodeBuilders.Where(b => b.ParentNodeType == SqlType.Where)); //执行并返回DB受影响行数 return(this.ExecuteUpdate(deleteTree.Compile(), entities.Parameters.ToArray())); }
/// <summary> /// 获取枚举器 /// </summary> /// <returns>枚举器</returns> public IEnumerator <TEntity> GetEnumerator() { //获取查询命令生成树(当前可默认获取到分页查询命令生成树) ICommandTree queryTree = base.CommandTreeFactory.GetPagingQueryTree(base.NodeBuilders, this.SortBuilders); //获取sql参数集合 IList <IDbDataParameter> parameters = base.Parameters.ToList(); parameters.Add(base.DbHelper.Parameter("SkipCount", this.PageSize * (this.PageIndex - 1))); parameters.Add(base.DbHelper.Parameter("NextCount", this.PageSize)); //执行查询 foreach (TEntity entity in base.DbHelper.GetResults(this.CreateEntity, queryTree.Compile(), parameters: parameters.ToArray())) { yield return(entity); } }
/// <summary> /// 为当前实体所Mapping的Table添加没有添加的列 /// </summary> /// <param name="dbHelper">操作数据库的Helper对象</param> /// <param name="commandTreeFactory">创建CommandTree的工厂</param> /// <param name="tableMapper">Table元数据解析器</param> public void AlterTableAddColumns(DbHelper dbHelper, ICommandTreeFactory commandTreeFactory, ITableMapper tableMapper) { //获取当前实体元数据解析器中某些属性Mapping的Column未包含在Table中的属性对应的ColumnMapper,并转换获取Column节点 string[] columnNames = this.GetColumns(dbHelper, tableMapper.Header).ToArray(); IColumnNode[] columnNodes = (from columnMapper in tableMapper.GetColumnMappers() where !columnNames.Contains(columnMapper.ColumnName) select columnMapper.ToColumnNode()).ToArray(); //若实体元数据解析器中所有属性Mapping的列都在当前Table中,直接退出 if (columnNodes.Length == 0) { return; } //获取TableHeader ITableHeader tableHeader = tableMapper.Header; //创建Alter Table Columns语句生成树 ICommandTree alterTableAddColumnsTree = commandTreeFactory.GetAlterTableAddColumnsTree(tableHeader.SchemaName, tableHeader.TableName, columnNodes); //生成并执行Alter Table Columns语句,为当前实体Mapping的Table添加为注册的列 dbHelper.ExecuteUpdate(alterTableAddColumnsTree.Compile()); }
/// <summary> /// 批量修改符合条件的实体的信息 /// </summary> /// <param name="setParameters">属性名及属性值字典</param> /// <param name="entities">待修改的实体的数据源</param> /// <returns>修改的实体对象的数量</returns> public int SaveAll(IDictionary <string, object> setParameters, IDbQuery <TEntity> entities) { //非空检查 Check.ArgumentNull(setParameters, nameof(setParameters)); Check.ArgumentNull(entities, nameof(entities)); //初始化sql条件表达式集合及其sql参数集合 List <INodeBuilder> nodeBuilders = new List <INodeBuilder>(); List <IDbDataParameter> sqlParameters = new List <IDbDataParameter>(); //Set 添加Update Set表达式子节点及其附属参数 this.LoadUpdateChildNodesAndParameters(setParameters, nodeBuilders, sqlParameters); //Where 获取entitis中的Where表达式子节点及其附属的sql参数填充至nodeBuilders 和 sqlParameters nodeBuilders.AddRange(entities.NodeBuilders.Where(n => n.ParentNodeType == SqlType.Where)); sqlParameters.AddRange(entities.Parameters); //创建Update命令生成树 ICommandTree updateTree = this.CommandTreeFactory.GetUpdateTree( this.TableMapper.Header.SchemaName, this.TableMapper.Header.TableName, this.TableMapper.Header.TableAlias, nodeBuilders); //执行并返回DB受影响行数 return(this.ExecuteUpdate(updateTree.Compile(), sqlParameters.ToArray())); }
/// <summary> /// 获取条件查询Sql /// </summary> /// <returns>条件查询Sql</returns> public string ToWhereSqlString() { ICommandTree queryTree = this.CreateQueryTree(); return(queryTree.Compile()); }
/// <summary> /// 获取枚举器 /// </summary> /// <returns>枚举器</returns> public IEnumerator <TElement> GetEnumerator() { //获取查询命令生成树 ICommandTree queryTree = this.CreateQueryTree(); //执行查询获取TElement类型的枚举器 foreach (TElement element in base.DbHelper.GetResults(this.CreateElement, queryTree.Compile(), parameters: base.Parameters.ToArray())) { yield return(element); } }