public DbCommand Get(Type type, Database db, Command cmdWrapper) { var map = MapConverter.GetPropertyData(type); DbCommand cmd = null; if (cmdWrapper.Procedures.IsNotNullOrEmpty()) { var table = cmdWrapper.Procedures.FirstOrDefault(p => p.Table == map.Table); if (table != null) { map.InsertSP = table.InsertSP; map.UpdateSP = table.UpdateSP; map.DeleteSP = table.DeleteSP; } } if (map.DeleteSP.IsNotNullOrEmpty()) { cmd = db.GetStoredProcCommand(map.DeleteSP); } else { cmd = db.GetSqlStringCommand(string.Format(SQL_DELETE, map.Table, PK_ID)); //数据版本管理 if (map.IsRowVersion) { cmd.CommandText += string.Format(" AND {0}=@{0} ", Constant.FieldName_RowVersion); db.AddInParameter(cmd, Tool.Str_Alpha + Constant.FieldName_RowVersion, DbType.Int32, Constant.FieldName_RowVersion, DataRowVersion.Current); } } db.AddInParameter(cmd, Tool.Str_Alpha + PK_ID, DbType.String, PK_ID, DataRowVersion.Current); return(cmd); }
public override Response Execute(Command wrapper) { var result = new Response() { IsSuccess = true, Entities = new List <EntityBase>() }; try { var db = CreateDatabase(); using (var conn = db.CreateConnection()) { if (conn.State != ConnectionState.Open) { conn.Open(); } var cmd = CommandConvert.ToSelectSelfCommand(db, conn, null, wrapper.Text, wrapper.Params, Type.GetType(wrapper.EntityType), wrapper); var ds = db.ExecuteDataSet(cmd); if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { result.Entities = new List <EntityBase>(); MapConverter.ToList(ds.Tables[0], Type.GetType(wrapper.EntityType), result.Entities); } conn.Close(); } } catch (Exception ex) { result.IsSuccess = false; result.Message = ex.Message; } return(result); }
public DbCommand Get(Type type, Database db, Command cmdWrapper) { DbCommand cmd = null; var map = MapConverter.GetPropertyData(type); var key = map.Fields.FirstOrDefault(p => p.IsKey); if (key != null) { var sb = new StringBuilder(); foreach (var field in map.Fields) { if (field.IsUpdate) { if (sb.Length > 0) { sb.Append(Tool.Str_Comma); } sb.Append(field.Field); } } if (sb.Length > 0) { cmd = db.GetSqlStringCommand(string.Format(SQL_SELECT, map.Table)); } } return(cmd); }
public DbCommand Get(Type type, Database db, Command cmdWrapper) { var map = MapConverter.GetPropertyData(type); List <FieldInfo> fieldList = GetFieldList(type, cmdWrapper, map); DbCommand cmd = null; if (cmdWrapper.Procedures.IsNotNullOrEmpty()) { var table = cmdWrapper.Procedures.FirstOrDefault(p => p.Table == map.Table); if (table != null) { map.InsertSP = table.InsertSP; map.UpdateSP = table.UpdateSP; map.DeleteSP = table.DeleteSP; } } if (map.UpdateSP.IsNotNullOrEmpty()) { cmd = db.GetStoredProcCommand(map.UpdateSP); SetUpdateFieldsForSP(fieldList, db, cmd); } else { cmd = db.GetSqlStringCommand(SQL_NULL); cmd.CommandText = null; if (fieldList.IsNotNullOrEmpty()) { var updateSql = new StringBuilder(); updateSql.Append(string.Format(SQL_UPDATE_PART1, map.Table)); SetUpdateFields(fieldList, updateSql, db, cmd); string whereText = string.Format(SQL_UPDATE_PART2, PK_ID); //数据版本管理 if (map.IsRowVersion) { whereText += string.Format(" AND {0}=@{0} ", Constant.FieldName_RowVersion); db.AddInParameter(cmd, Tool.Str_Alpha + Constant.FieldName_RowVersion, DbType.Int32, Constant.FieldName_RowVersion, DataRowVersion.Current); } cmd.CommandText = updateSql.ToString() + whereText; AddPkParameter(fieldList, db, cmd); } } return(cmd); }
public DbCommand Get(Type type, Database db, Command cmdWrapper) { var map = MapConverter.GetPropertyData(type); var properties = map.Fields.Where(p => p.Field.IsNotNullOrEmpty() && p.IsUpdate).ToList(); if (properties != null && properties.Count > 0) { DbCommand cmd = null; if (cmdWrapper.Procedures.IsNotNullOrEmpty()) { var table = cmdWrapper.Procedures.FirstOrDefault(p => p.Table == map.Table); if (table != null) { map.InsertSP = table.InsertSP; map.UpdateSP = table.UpdateSP; map.DeleteSP = table.DeleteSP; } } if (map.InsertSP.IsNotNullOrEmpty()) { cmd = db.GetStoredProcCommand(map.InsertSP); SetUpdateFieldsForSP(properties, db, cmd); } else { cmd = db.GetSqlStringCommand(SQL_NULL); cmd.CommandText = null; var fields = new StringBuilder(); fields.Append(string.Format(SQL_INSERT_PART1, map.Table)); var values = new StringBuilder(); values.Append(SQL_INSERT_PART2); SetUpdateFields(properties, db, cmd, fields, values); fields.Append(RIGHT_BRACKET); values.Append(RIGHT_BRACKET); cmd.CommandText = fields.ToString() + values.ToString(); } return(cmd); } return(null); }
private int ToDb(Database db, DbTransaction tran, IEnumerable <object> list, Command wrapper) { int result = 0; if (list.IsNotNullOrEmpty()) { var type = list.First().GetType(); var builder = new CommandBuilder { Type = type, Db = db, CmdWrapper = wrapper }; var insertCommand = builder.Get(new InsertCommand()); var updateCommand = builder.Get(new UpdateCommand()); var deleteCommand = builder.Get(new DeleteCommand()); DataSet ds = new DataSet(); ds.Tables.Add(MapConverter.ToDataTable(list)); result = db.UpdateDataSet(ds, ds.Tables[0].TableName, insertCommand, updateCommand, deleteCommand, tran); } return(result); }