Exemplo n.º 1
0
        internal DbCommand CreateUpdate <T>(Table table, List <FieldValue> fvlist, WhereClip where)
            where T : Entity
        {
            T entity = DataUtils.CreateInstance <T>();

            if (entity.GetReadOnly())
            {
                throw new MySoftException("只读实体" + typeof(T).Name + "只能用于查询!");
            }

            if ((object)where == null)
            {
                throw new MySoftException("更新条件不能为null!");
            }

            //移除缓存
            RemoveCache(typeof(T).Name);

            string tableName = table == null?entity.GetTable().Name : table.Name;

            List <SQLParameter> plist = new List <SQLParameter>();
            StringBuilder       sb    = new StringBuilder();

            sb.Append("update " + tableName + " set ");

            fvlist.ForEach(fv =>
            {
                if (fv.IsPrimaryKey || fv.IsIdentity)
                {
                    return;
                }

                if (fv.IsChanged)
                {
                    if (CheckValue(fv.Value))
                    {
                        sb.Append(fv.Field.At((string)null).Name + " = " + DataUtils.FormatValue(fv.Value));
                    }
                    else
                    {
                        SQLParameter p = null;
                        if (CheckStruct(fv.Value))
                        {
                            p = CreateOrmParameter(DataUtils.FormatValue(fv.Value));
                        }
                        else
                        {
                            p = CreateOrmParameter(fv.Value);
                        }

                        sb.Append(fv.Field.At((string)null).Name + " = " + p.Name);
                        plist.Add(p);
                    }

                    sb.Append(",");
                }
            });

            sb.Remove(sb.Length - 1, 1);

            if (!DataUtils.IsNullOrEmpty(where))
            {
                sb.Append(" where " + where.ToString());
                plist.AddRange(where.Parameters);
            }

            return(CreateSqlCommand(sb.ToString(), plist.ToArray()));
        }
Exemplo n.º 2
0
        internal DbCommand CreateInsert <T>(Table table, List <FieldValue> fvlist, Field identityfield, string autoIncrementName)
            where T : Entity
        {
            T entity = DataUtils.CreateInstance <T>();

            if (entity.GetReadOnly())
            {
                throw new MySoftException("只读实体" + typeof(T).Name + "只能用于查询!");
            }

            //移除缓存
            RemoveCache(typeof(T).Name);

            string tableName = table == null?entity.GetTable().Name : table.Name;

            List <SQLParameter> plist   = new List <SQLParameter>();
            StringBuilder       sbsql   = new StringBuilder();
            StringBuilder       sbparam = new StringBuilder();

            if (UseAutoIncrement)
            {
                //如果标识列和标识名称都不为null
                if ((IField)identityfield != null && !string.IsNullOrEmpty(autoIncrementName))
                {
                    string identityName = FormatIdentityName(autoIncrementName);
                    bool   exist        = false;
                    fvlist.ForEach(fv =>
                    {
                        if (fv.IsIdentity)
                        {
                            fv.Value      = new SysValue(identityName);
                            fv.IsIdentity = false;
                            exist         = true;
                        }
                    });

                    if (!exist)
                    {
                        object     value = new SysValue(identityName);
                        FieldValue fv    = new FieldValue(identityfield, value);
                        fvlist.Insert(0, fv);
                    }
                }
            }

            sbsql.Append("insert into " + tableName + "(");
            sbparam.Append(" values (");

            fvlist.ForEach(fv =>
            {
                if (fv.IsIdentity)
                {
                    return;
                }

                sbsql.Append(fv.Field.At((string)null).Name);
                if (CheckValue(fv.Value))
                {
                    sbparam.Append(DataUtils.FormatValue(fv.Value));
                }
                else
                {
                    SQLParameter p = null;
                    if (CheckStruct(fv.Value))
                    {
                        p = CreateOrmParameter(DataUtils.FormatValue(fv.Value));
                    }
                    else
                    {
                        p = CreateOrmParameter(fv.Value);
                    }

                    sbparam.Append(p.Name);
                    plist.Add(p);
                }

                sbsql.Append(",");
                sbparam.Append(",");
            });

            sbsql.Remove(sbsql.Length - 1, 1).Append(")");
            sbparam.Remove(sbparam.Length - 1, 1).Append(")");

            string cmdText = string.Format("{0}{1}", sbsql, sbparam);

            return(CreateSqlCommand(cmdText, plist.ToArray()));
        }