Exemple #1
0
        //============================
        public static IDataItem create(IDataItem schema, GetHandler source)
        {
            DataItem item = new DataItem();

            foreach (var key in schema.keys())
            {
                object val = source(key);
                if (val != null)
                {
                    item.set(key, val);
                }
            }
            return(item);
        }
Exemple #2
0
        protected bool updateList(String pk, IDataItem cols, List <GetHandler> valuesList)
        {
            if (valuesList == null || valuesList.Count == 0)
            {
                return(false);
            }

            if (cols == null || cols.count() == 0)
            {
                return(false);
            }

            List <Object> args = new List <Object>();
            StringBuilder sb   = new StringBuilder();

            sb.Append(" INSERT INTO ").Append(_table).Append(" (");
            foreach (String key in cols.keys())
            {
                sb.Append(_context.field(key)).Append(",");
            }
            sb.Remove(sb.Length - 1, 1);

            sb.Append(") ");

            sb.Append("VALUES");

            foreach (GetHandler item in valuesList)
            {
                sb.Append("(");

                foreach (String key in cols.keys())
                {
                    Object val = item(key);

                    if (val is String)
                    {
                        String val2 = (String)val;
                        if (val2.IndexOf(DataFlagConfig.sqlFunctionFlag[0]) == 0)
                        { //说明是SQL函数
                            sb.Append(val2.Substring(1)).Append(",");
                        }
                        else
                        {
                            sb.Append(DataFlagConfig.paramReplaceFlag).Append(",");
                            args.Add(val);
                        }
                    }
                    else
                    {
                        sb.Append(DataFlagConfig.paramReplaceFlag).Append(",");
                        args.Add(val);
                    }
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("),");
            }

            sb.Remove(sb.Length - 1, 1);
            sb.Append(" ON DUPLICATE KEY UPDATE");
            foreach (String key in cols.keys())
            {
                if (pk.Equals(key))
                {
                    continue;
                }

                sb.Append(" ").Append(key).Append("=VALUES(").Append(key).Append("),");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append(";");

            _builder.append(sb.ToString(), args.ToArray());

            return(compile().execute() > 0);
        }
Exemple #3
0
        protected bool insertList(IDataItem cols, List <GetHandler> valuesList)
        {
            if (valuesList == null || valuesList.Count == 0)
            {
                return(false);
            }

            if (cols == null || cols.count() == 0)
            {
                return(false);
            }

            List <Object> args = new List <Object>();
            StringBuilder sb   = new StringBuilder();

            sb.Append(" INSERT INTO ").Append(_table).Append(" (");
            foreach (String key in cols.keys())
            {
                sb.Append(_context.field(key)).Append(",");
            }
            sb.DeleteCharAt(sb.Length - 1);

            sb.Append(") ");

            sb.Append("VALUES");

            foreach (GetHandler item in valuesList)
            {
                sb.Append("(");

                foreach (String key in cols.keys())
                {
                    Object val = item(key);

                    if (val is String)
                    {
                        String val2 = (String)val;
                        if (val2.IndexOf('$') == 0)   //说明是SQL函数
                        {
                            sb.Append(val2.Substring(1)).Append(",");
                        }
                        else
                        {
                            sb.Append("?,");
                            args.Add(val);
                        }
                    }
                    else
                    {
                        sb.Append("?,");
                        args.Add(val);
                    }
                }
                sb.DeleteCharAt(sb.Length - 1);
                sb.Append("),");
            }

            sb.DeleteCharAt(sb.Length - 1);
            sb.Append(");");

            _builder.append(sb.ToString(), args.ToArray());

            return(compile().execute() > 0);
        }