protected override void ApplyRules(UpdateContext updateContext, UpdateConfig config, IDictionary <string, string> cols) { var uParams = updateContext.Params; IValueSetter valueSetter = ValueSetterCreater.Create(uParams); UpdateTrigger.TriggeValuesChecked(updateContext, uParams, config, cols, ActionType.Update, valueSetter, uParams.Keys); }
protected override object DoUpdate(UpdateContext context, UpdateConfig config, IEnumerable <object> datas, object complexData) { var sql = config.Sql; var cols = GetCols(config); var key = GetKeyName(config, cols); IValueSetter valueSetter = ValueSetterCreater.Create(datas.First()); //1.自定义SQl一条条插入 if (!string.IsNullOrEmpty(sql) || !string.IsNullOrEmpty(config.Filter)) { if (string.IsNullOrEmpty(sql)) { config.Sql = DBHelper.BuildUpdateSql(cols, config, key, SqlKeyWorld.ComplexDataObjectStart); } config.ReturnId = true; var keys = valueSetter.GetFields(datas.First()); foreach (var data in datas) { context.ContentParams.ReplaceOrInsert(SqlKeyWorld.ComplexData, data); UpdateTrigger.TriggeValuesChecked(context, data, config, cols, ActionType.Update, valueSetter, keys); SqlExcuter.ExcuteTrann(context); ExcuteSubUpdate(context, config, data); } return(datas.Count()); } return(UpdateOnOneTime(context, config, cols, datas, valueSetter, key)); }
protected override void ApplyRules(UpdateContext updateContext, UpdateConfig config, IDictionary <string, string> cols) { var cParams = updateContext.Params; IValueSetter valueSetter = ValueSetterCreater.Create(cParams); var keys = cParams.Keys; UpdateTrigger.TriggeDefaultValues(updateContext, cParams, config, cols, valueSetter, keys); UpdateTrigger.TriggeValuesChecked(updateContext, cParams, config, cols, ActionType.Insert, valueSetter, keys); config.ReturnId = true; var key = GetKeyName(config, cols); object id; if (updateContext.Params.TryGetValue(key, out id) == false) { return; } updateContext.ContentParams.Add(SqlKeyWorld.ReturnKey, id); }
protected override object DoUpdate(UpdateContext context, UpdateConfig config, IEnumerable <object> datas, object complexData) { var sql = config.Sql; var ingoreKey = config.Config[IngoreKey].ToSingleData <bool>(); var cols = GetCols(config); var key = GetKeyName(config, cols); IValueSetter valueSetter = ValueSetterCreater.Create(datas.First()); //1.自定义SQl一条条插入 //2.未包含Key又没有忽略产生Key说明是要数据库自动生成 if (!string.IsNullOrEmpty(sql) || (!cols.ContainsKey(key) && !ingoreKey)) { if (string.IsNullOrEmpty(sql)) { config.Sql = DBHelper.BuildInsertSql(cols, config.Table, SqlKeyWorld.ComplexDataObjectStart); } config.ReturnId = true; var keys = valueSetter.GetFields(datas.First()); foreach (var data in datas) { context.ContentParams.ReplaceOrInsert(SqlKeyWorld.ComplexData, data); UpdateTrigger.TriggeDefaultValues(context, data, config, cols, valueSetter, keys); UpdateTrigger.TriggeValuesChecked(context, data, config, cols, ActionType.Insert, valueSetter, keys); var result = SqlExcuter.ExcuteTrann(context); long id = 0; if (result is long || result is int) { id = long.Parse(result.ToString()); valueSetter.SetField(data, id, key); } ExcuteSubUpdate(context, config, data); } return(datas.Count()); } return(InsertOnOneTime(context, config, cols, datas, valueSetter)); }
private object InsertOnOneTime(UpdateContext context, UpdateConfig config, IDictionary <string, string> cols, IEnumerable <object> datas, IValueSetter valueSetter) { StringBuilder sb = new StringBuilder(); var columnInfos = TableColumnQueryHandler.QueryColumns(config).Where(c => cols.Keys.Contains(c.ColName) && DBHelper.SpecailColumn(c)).ToArray(); sb.AppendLine($"insert into {config.Table} ({string.Join(SqlKeyWorld.Split1, cols.Keys)}) values "); var len = datas.Count(); var keys = valueSetter.GetFields(datas.First()); for (var i = 0; i < len; i++) { var data = datas.ElementAt(i); context.ContentParams.ReplaceOrInsert(SqlKeyWorld.ComplexData, data); UpdateTrigger.TriggeDefaultValues(context, data, config, cols, valueSetter, keys); UpdateTrigger.TriggeValuesChecked(context, data, config, cols, ActionType.Insert, valueSetter, keys); sb.AppendLine(string.Intern("(")); var colVals = cols.Select(c => DBHelper.BuildColVal(c, valueSetter, data, columnInfos)); sb.Append(string.Join(SqlKeyWorld.Split1, colVals)); sb.Append(string.Intern(")")); if (i != len - 1) { sb.Append(SqlKeyWorld.Split1); } } config.Sql = sb.ToString(); object result = SqlExcuter.ExcuteTrann(context); foreach (var data in datas) { ExcuteSubUpdate(context, config, data); } return(result); }
private object UpdateOnOneTime(UpdateContext context, UpdateConfig config, IDictionary <string, string> cols, IEnumerable <object> datas, IValueSetter valueSetter, string key) { var columnInfos = TableColumnQueryHandler.QueryColumns(config).Where(c => cols.Keys.Contains(c.ColName) && DBHelper.SpecailColumn(c)).ToArray(); StringBuilder sb = new StringBuilder(); var len = datas.Count(); var keys = valueSetter.GetFields(datas.First()); var page = len / PerCount; if (page % PerCount != 0) { page++; } if (page == 0 && len > 0) { page = 1; } var cCount = cols.Count(); for (var p = 0; p < page; p++) { var currentIndex = p * PerCount; ICollection <object> ids = new List <object>(); IDictionary <string, StringBuilder> dictSbs = new Dictionary <string, StringBuilder>(); for (var i = currentIndex; i < currentIndex + PerCount; i++) { if (i >= len) { break; } var data = datas.ElementAt(i); context.ContentParams.ReplaceOrInsert(SqlKeyWorld.ComplexData, data); UpdateTrigger.TriggeValuesChecked(context, data, config, cols, ActionType.Update, valueSetter, keys); var id = valueSetter.GetValue(data, key); ids.Add(id); for (var c = 0; c < cCount; c++) { var col = cols.ElementAt(c); if (col.Key.Equals(key, StringComparison.OrdinalIgnoreCase)) { continue; } StringBuilder colSb; if (!dictSbs.TryGetValue(col.Key, out colSb)) { colSb = new StringBuilder(); dictSbs.Add(col.Key, colSb); } colSb.Append($" WHEN '{id}' THEN {DBHelper.BuildColVal(col, valueSetter, data, columnInfos)} "); } } if (ids.Any() == false) { break; } sb.AppendFormat(DBHelper.UpdateFormatter, config.Table); var index = 0; foreach (var colSb in dictSbs) { sb.Append(string.Concat("`", colSb.Key, "` = CASE `", key, "` ")); sb.Append(colSb.Value.ToString()); sb.Append(End); index++; if (index < cCount) { sb.Append(SqlKeyWorld.Split3); } } sb.Append($"{DBHelper.Where}{key} {SqlKeyWorld.In} ({string.Join(SqlKeyWorld.Split1, ids)});"); } config.Sql = sb.ToString(); object result = SqlExcuter.ExcuteTrann(context); foreach (var data in datas) { ExcuteSubUpdate(context, config, data); } return(result); }