/// <summary> /// 根据主键删除实体对应数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public virtual DbEntity Delete <T>(object id) { DbEntity dbEntity = null; if (id == null) { return(dbEntity); } Type type = typeof(T); var attributeBuilder = new AttributeBuilder(); var tableEntity = attributeBuilder.GetTableInfo(type); var pkColumn = attributeBuilder.GetPkColumnInfo(type); if (pkColumn == null) { return(dbEntity); } pkColumn.ColumnValue = id; string dbOperator = DbFactory.GetDbParamOperator(); var dbParams = new List <IDbDataParameter>(); StringBuilder sqlBuild = new StringBuilder("delete from {tableName} where {pkColumn}={dbOperator}{pkColumn}"); sqlBuild.Replace("{tableName}", tableEntity.TableName); sqlBuild.Replace("{pkColumn}", pkColumn.ColumnName); sqlBuild.Replace("{dbOperator}", dbOperator); dbParams.Add(DbFactory.GetDbParam(pkColumn)); dbEntity = new DbEntity() { TableEntity = tableEntity, CommandText = sqlBuild.ToString(), DbParams = dbParams }; return(dbEntity); }
/// <summary> /// 根据修改字段参数(参数名和参数值)、主键值修改数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="updateParam">修改字段参数(参数名和参数值),例:new {Uname="joyet",Age = 110}</param> /// <param name="id"></param> /// <returns></returns> public virtual DbEntity Update <T>(object updateParam, object id) { DbEntity dbEntity = null; if (updateParam == null || id == null) { return(dbEntity); } Type type = typeof(T); var attributeBuilder = new AttributeBuilder(); var tableEntity = attributeBuilder.GetTableInfo(type); var updateColumns = attributeBuilder.GetColumnInfos(updateParam); var pkColumn = attributeBuilder.GetPkColumnInfo(type); if (updateColumns.Count < 0 || pkColumn == null) { return(dbEntity); } pkColumn.ColumnValue = id; var dbOperator = DbFactory.GetDbParamOperator(); var dbParams = new List <IDbDataParameter>(); var sqlBuilder = new StringBuilder("update {tableName} set {updateCriteria} {whereCriteria}"); sqlBuilder.Replace("{tableName}", tableEntity.TableName); //处理更改字段参数 var updateItem = HandleUpdateParam(updateColumns, sqlBuilder, dbParams); sqlBuilder = updateItem.Item1; dbParams = updateItem.Item2; //处理过滤字段参数 var whereItem = HandleWhereParam("", new List <TableColumnAttribute>() { pkColumn }, sqlBuilder, dbParams); sqlBuilder = whereItem.Item1; dbParams = whereItem.Item2; dbEntity = new DbEntity() { TableEntity = tableEntity, CommandText = sqlBuilder.ToString(), DbParams = dbParams, }; return(dbEntity); }
/// <summary> /// 根据主键值删除数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public virtual DbEntity Delete <T>(object id) { DbEntity dbEntity = null; if (id == null) { return(dbEntity); } Type type = typeof(T); var attributeBuilder = new AttributeBuilder(); var tableEntity = attributeBuilder.GetTableInfo(type); var pkColumn = attributeBuilder.GetPkColumnInfo(type); if (pkColumn == null) { return(dbEntity); } pkColumn.ColumnValue = id; string dbOperator = DbFactory.GetDbParamOperator(); var dbParams = new List <IDbDataParameter>(); StringBuilder sqlBuilder = new StringBuilder("delete from {tableName} {whereCriteria}"); sqlBuilder.Replace("{tableName}", tableEntity.TableName); //处理过滤字段参数 var whereItem = HandleWhereParam("", new List <TableColumnAttribute>() { pkColumn }, sqlBuilder, dbParams); sqlBuilder = whereItem.Item1; dbParams = whereItem.Item2; dbEntity = new DbEntity() { TableEntity = tableEntity, CommandText = sqlBuilder.ToString(), DbParams = dbParams }; return(dbEntity); }
/// <summary> /// 修改字段名及字段值参数、主键值修改实体对应数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="updateParam">修改字段名及字段值参数,例:new {Uname="joyet",Age = 110}</param> /// <param name="id"></param> /// <returns></returns> public virtual DbEntity Update <T>(object updateParam, object id) { DbEntity dbEntity = null; if (updateParam == null || id == null) { return(dbEntity); } Type type = typeof(T); var attributeBuilder = new AttributeBuilder(); var tableEntity = attributeBuilder.GetTableInfo(type); var updateColumns = attributeBuilder.GetColumnInfos(updateParam); var pkColumn = attributeBuilder.GetPkColumnInfo(type); if (updateColumns.Count < 0 || pkColumn == null) { return(dbEntity); } pkColumn.ColumnValue = id; var dbOperator = DbFactory.GetDbParamOperator(); var dbParams = new List <IDbDataParameter>(); StringBuilder sqlBuild = new StringBuilder("update {tableName} set {updateCriteria} where {pkColumn}={dbOperator}{pkColumn}"); sqlBuild.Replace("{tableName}", tableEntity.TableName); sqlBuild.Replace("{pkColumn}", pkColumn.ColumnName); sqlBuild.Replace("{dbOperator}", dbOperator); HandleUpdateParam(updateColumns, ref sqlBuild, ref dbParams); dbParams.Add(DbFactory.GetDbParam(pkColumn)); dbEntity = new DbEntity() { TableEntity = tableEntity, CommandText = sqlBuild.ToString(), DbParams = dbParams, }; return(dbEntity); }