/// <summary> /// 生成删除的SQL声明 /// </summary> /// <param name="dbTranslator"></param> /// <param name="entityInfo"></param> /// <param name="entity"></param> /// <returns></returns> public static SqlStatement BuildDeleteSqlStatement(IDbTranslator dbTranslator, IEntityMapper entityInfo, object entity) { if (entityInfo.PKeyProperties.Count == 0) { throw new MapleException("当前实体中未设置主键"); } //DELETE [Customers] WHERE CustomerID=@CustomerID and City=@City StringBuilder sBuilderWhere = new StringBuilder(); var dpc = new DataParameterCollection(); for (int i = 0; i < entityInfo.PKeyProperties.Count; i++) { var item = entityInfo.PKeyProperties[i]; sBuilderWhere.Append(dbTranslator.Quote(item.ColumnName)); sBuilderWhere.Append(" = "); sBuilderWhere.Append(dbTranslator.QuoteParameter(item.ColumnName)); if (i < entityInfo.PKeyProperties.Count - 1) { sBuilderWhere.Append(" AND "); } dpc.Add(getDataParameter(item, item.FastGetValue(entity))); } string sql = string.Format("DELETE FROM {0} WHERE {1};\n", dbTranslator.Quote(entityInfo.TableName), sBuilderWhere.ToString()); return(new SqlStatement(System.Data.CommandType.Text, sql, dpc)); }
/// <summary> /// 生成更新的SQL声明 /// </summary> /// <param name="dbTranslator"></param> /// <param name="entityInfo"></param> /// <param name="entity"></param> /// <returns></returns> public static SqlStatement BuildUpdateSqlStatement(IDbTranslator dbTranslator, IEntityMapper entityInfo, object entity) { if (entityInfo.PKeyProperties.Count == 0) { throw new MapleException("当前实体中未设置主键"); } if (entityInfo.OtherProperties.Count == 0) { throw new MapleException("当前实体中除了主键以外无其他字段"); } // UPDATE [Test] SET [name] = @name WHERE id=@id StringBuilder sBuilderSet = new StringBuilder(); StringBuilder sBuilderWhere = new StringBuilder(); var dpc = new DataParameterCollection(); foreach (var item in entityInfo.OtherProperties) { sBuilderSet.Append(dbTranslator.Quote(item.ColumnName)); sBuilderSet.Append(" = "); sBuilderSet.Append(dbTranslator.QuoteParameter(item.ColumnName)); sBuilderSet.Append(","); dpc.Add(getDataParameter(item, item.FastGetValue(entity))); } for (int i = 0; i < entityInfo.PKeyProperties.Count; i++) { var item = entityInfo.PKeyProperties[i]; sBuilderWhere.Append(dbTranslator.Quote(item.ColumnName)); sBuilderWhere.Append(" = "); sBuilderWhere.Append(dbTranslator.QuoteParameter(item.ColumnName)); if (i < entityInfo.PKeyProperties.Count - 1) { sBuilderWhere.Append(" AND "); } dpc.Add(getDataParameter(item, item.FastGetValue(entity))); } string sql = string.Format("UPDATE {0} SET {1} WHERE {2};\n", dbTranslator.Quote(entityInfo.TableName), sBuilderSet.ToString().TrimEnd(','), sBuilderWhere.ToString()); return(new SqlStatement(System.Data.CommandType.Text, sql, dpc)); }
/// <summary> /// 生成插入的SQL声明 /// </summary> /// <param name="dbTranslator"></param> /// <param name="entityInfo"></param> /// <param name="entity"></param> /// <returns></returns> public static SqlStatement BuildInsertSqlStatement(IDbTranslator dbTranslator, IEntityMapper entityInfo, object entity) { if (entityInfo.PKeyProperties.Count == 0) { throw new MapleException("当前实体中未设置主键"); } //插入的SQL语句 StringBuilder sBuilderFiled = new StringBuilder(); StringBuilder sBuilderValue = new StringBuilder(); var dpc = new DataParameterCollection(); foreach (var propertyMapper in entityInfo.PKeyProperties) { sBuilderFiled.Append(dbTranslator.Quote(propertyMapper.ColumnName)); sBuilderFiled.Append(","); sBuilderValue.Append(dbTranslator.QuoteParameter(propertyMapper.ColumnName)); sBuilderValue.Append(","); dpc.Add(getDataParameter(propertyMapper, propertyMapper.FastGetValue(entity))); } foreach (var propertyMapper in entityInfo.OtherProperties) { sBuilderFiled.Append(dbTranslator.Quote(propertyMapper.ColumnName)); sBuilderFiled.Append(","); sBuilderValue.Append(dbTranslator.QuoteParameter(propertyMapper.ColumnName)); sBuilderValue.Append(","); dpc.Add(getDataParameter(propertyMapper, propertyMapper.FastGetValue(entity))); } string sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2});\n", dbTranslator.Quote(entityInfo.TableName), sBuilderFiled.ToString().TrimEnd(','), sBuilderValue.ToString().TrimEnd(',')); return(new SqlStatement(System.Data.CommandType.Text, sql, dpc)); }