Ejemplo n.º 1
0
        public long insert(IDataItem data)
        {
            if (data == null || data.count() == 0)
            {
                return(0);
            }

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

            sb.Append(" INSERT INTO ").Append(_table).Append(" (");

            data.forEach((key, value) => {
                if (value == null)
                {
                    return;
                }

                sb.Append(_context.field(key)).Append(",");
            });

            sb.DeleteCharAt(sb.Length - 1);

            sb.Append(") VALUES (");
            data.forEach((key, value) => {
                if (value == null)
                {
                    return;
                }

                if (value is String)
                {
                    String val2 = (String)value;
                    if (val2.Length > 0 && val2[0] == '$') //说明是SQL函数
                    {
                        sb.Append(val2.Substring(1)).Append(",");
                    }
                    else
                    {
                        sb.Append("?,");
                        args.Add(value);
                    }
                }
                else
                {
                    sb.Append("?,");
                    args.Add(value);
                }
            });

            sb.DeleteCharAt(sb.Length - 1);
            sb.Append("); SELECT @@IDENTITY;");

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

            return(compile().insert());
        }
Ejemplo n.º 2
0
        public int update(IDataItem data)
        {
            if (data == null || data.count() == 0)
            {
                return(0);
            }

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

            sb.Append("UPDATE ").Append(_table).Append(" SET ");

            data.forEach((key, value) => {
                if (value == null)
                {
                    return;
                }

                if (value is String)
                {
                    String val2 = (String)value;
                    if (val2.Length > 0 && val2[0] == DataFlagConfig.sqlFunctionFlag[0])
                    {
                        sb.Append(_context.field(key)).Append("=").Append(val2.Substring(1)).Append(",");
                    }
                    else
                    {
                        sb.Append(_context.field(key)).Append("=").Append(DataFlagConfig.paramReplaceFlag).Append(",");
                        args.Add(value);
                    }
                }
                else
                {
                    sb.Append(_context.field(key)).Append("=").Append(DataFlagConfig.paramReplaceFlag).Append(",");
                    args.Add(value);
                }
            });

            sb.DeleteCharAt(sb.Length - 1);

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

            return(compile().execute());
        }
Ejemplo n.º 3
0
        public long insert(IDataItem data)
        {
            if (data == null || data.count() == 0)
            {
                return(0);
            }

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

            sb.Append(" INSERT INTO ").Append(_table).Append(" (");

            data.forEach((key, value) => {
                if (value == null)
                {
                    return;
                }

                sb.Append(_context.field(key)).Append(",");
            });

            sb.DeleteCharAt(sb.Length - 1);

            sb.Append(") VALUES (");
            data.forEach((key, value) => {
                if (value == null)
                {
                    return;
                }

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

            sb.DeleteCharAt(sb.Length - 1);

            //检查当前数据库是否支持使用Select @@Identity
            if (_context.IsSupportSelectIdentityAfterInsert)
            {
                sb.Append("); SELECT @@IDENTITY;");
            }
            else
            {
                sb.Append("); ");
            }

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

            return(compile().insert());
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }