コード例 #1
0
ファイル: DBHelper.cs プロジェクト: nGenieDeveloper/SqlEngine
        public static string BuildColVal(KeyValuePair <string, string> col, IValueSetter valueSetter, object data, IEnumerable <Column> columns)
        {
            var v = col.Value;

            if (v.StartsWith(SqlKeyWorld.ParamStart))
            {
                return(v);
            }

            var val = valueSetter.GetValue(data, v);

            return(DBHelper.GetDBValue(val, columns));
        }
コード例 #2
0
        public static void TriggeValuesChecked(UpdateContext updateContext, object data, UpdateConfig config, IDictionary <string, string> cols, ActionType actionType, IValueSetter valueSetter, IEnumerable <string> keys)
        {
            var rules = SqlEnginerConfig.GetMatchRules(config.Connection, config.Table, actionType, UpdateType.CheckValue).OrderBy(r => r.RangeType).ToArray();

            if (rules.Any() == false)
            {
                return;
            }

            var columns = rules.SelectMany(r => r.Columns).ToArray();

            foreach (var key in keys)
            {
                if (!cols.Any(c => c.Value == key) || key.StartsWith(SqlKeyWorld.ParamStart))
                {
                    continue;
                }

                var realKey      = cols.First(c => c.Value == key).Key;
                var matchColumns = columns.Where(c => c.Name == realKey).ToArray();
                if (matchColumns.Any() == false)
                {
                    continue;
                }

                foreach (var mc in matchColumns)
                {
                    IValueChecked valueChecked = ValueCheckedFactory.Create(mc.ValueType);
                    if (valueChecked == null || mc.Value == null)
                    {
                        continue;
                    }

                    var value = valueSetter.GetValue(data, key);
                    if (valueChecked.Checked(updateContext, mc, value, key, realKey))
                    {
                        continue;
                    }

                    var msg = mc.Name;
                    if (!string.IsNullOrEmpty(mc.Error))
                    {
                        msg = mc.Error;
                    }

                    throw new ArgumentException(msg);
                }
            }
        }
コード例 #3
0
        private object DeleteOnOneTime(UpdateContext context, UpdateConfig config, IDictionary <string, string> cols, IEnumerable <object> datas, IValueSetter valueSetter, string key)
        {
            ICollection <object> ids = new List <object>();

            foreach (var data in datas)
            {
                var id = valueSetter.GetValue(data, key);
                ids.Add(id);
                ExcuteSubUpdate(context, config, data);
            }

            var sql = $"DELETE FROM {config.Table} where {key} in ({string.Join(SqlKeyWorld.Split1, ids)});";

            config.Sql = sql;
            object result = SqlExcuter.ExcuteTrann(context);

            return(result);
        }
コード例 #4
0
        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);
        }