Example #1
0
 private string GetSelectFieldExpress(string n)
 {
     if (MsSqlServerHelper.IsFeildNameInSqlExpress(n))
     {
         return(n);
     }
     if (MsSqlServerHelper.IsValidFieldName(n))
     {
         return(MsSqlServerHelper.GetFeildNameInSqlExpress(n));
     }
     return(n);
 }
 /// <summary>
 /// 新增数据发生变化
 /// <para>重新生成Sql表达式</para>
 /// </summary>
 private void OnUpdateItemChanged()
 {
     if (UpdateItem == null)
     {
         Express = null;
         Paramet = null;
     }
     else
     {
         IEnumerable <IDataParamet> p;
         Express = MsSqlServerHelper.GetUpdateSql <T>(UpdateItem, UpdateFieldRange, UpdateFieldNames, out p);
         Paramet = p;
     }
 }
Example #3
0
 private void OnDeleteItemChanged()
 {
     if (p_DeleteItem == null)
     {
         Express = null;
         Paramet = null;
     }
     else
     {
         IEnumerable <IDataParamet> p;
         Express = MsSqlServerHelper.GetDeleteSql <T>(p_DeleteItem, out p);
         Paramet = p;
     }
 }
Example #4
0
        /// <summary>
        /// 生成删除数据的Sql表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="deleteItem"></param>
        /// <param name="paramet"></param>
        /// <returns></returns>
        internal static string GetDeleteSql <T>(
            T deleteItem,
            out IEnumerable <IDataParamet> paramet)
        {
            paramet = null;
            if (deleteItem == null)
            {
                throw new ObjectIsNullException(LocalResource.DeleteItem);
            }

            StringBuilder       wheresql = new StringBuilder();
            List <IDataParamet> paramets = new List <IDataParamet>();
            IDataModel          model    = typeof(T).GetDataModel();
            string namehz = GetIdKeyStringValues(deleteItem, model);
            int    c      = 0;

            foreach (var k in model.PrimaryKeys)
            {
                IDataFieldModel f = model.Fields[k.Name];
                DataParamet     p = AppendParamet(paramets, namehz, f, deleteItem);
                if (c == 0)
                {
                    wheresql.Append(string.Format("[{0}]={1}", f.Name, p.Name));
                }
                else
                {
                    wheresql.Append(string.Format(" AND [{0}]={1}", f.Name, p.Name));
                }
                c++;
            }
            if (wheresql.Length == 0) //没有任何主键信息
            {
                throw new NotFoundPrimaryKeyException(typeof(T));
            }
            paramet = paramets.ToArray();
            string sdf = model.GetSoftDeleteFieldName();

            if (!string.IsNullOrEmpty(sdf))
            {
                return(string.Format("UPDATE {0} SET {1}=1 WHERE {2}",
                                     MsSqlServerHelper.GetFeildNameInSqlExpress(model.Name),
                                     MsSqlServerHelper.GetFeildNameInSqlExpress(sdf),
                                     wheresql.ToString()));
            }
            else
            {
                return(string.Format("DELETE {0} WHERE {1}", GetFeildNameInSqlExpress(model.Name), wheresql.ToString()));
            }
        }
        protected override object OnSuccessCommandForEffectCount(int effect)
        {
            IEnumerable <IDataParamet> p = Paramet;

            if (p != null && p_NewItem != null && p_Par != null && p_Par.OutputGenerateFieldNames != null)
            {
                IDataModel model = typeof(T).GetDataModel();
                foreach (string fn in p_Par.OutputGenerateFieldNames)
                {
                    object v     = MsSqlServerHelper.GetParametValue(fn, p_Par.NameHz, p);
                    var    field = model.Fields[fn];
                    v = MsSqlServerHelper.ConvertFieldValueFromDbValue(field, v);
                    field.SetModelFieldValue(p_NewItem, v);
                }
            }
            return(base.OnSuccessCommandForEffectCount(effect));
        }
 /// <summary>
 /// 新增数据发生变化
 /// <para>重新生成Sql表达式</para>
 /// </summary>
 private void OnNewItemChanged()
 {
     if (p_NewItem == null)
     {
         Express = null;
         Paramet = null;
     }
     else
     {
         Express = MsSqlServerHelper.GetAddNewSql <T>(p_NewItem, out p_Par);
         if (p_Par != null)
         {
             Paramet = p_Par.Paramets;
         }
         else
         {
             Paramet = null;
         }
     }
 }
 /// <summary>
 /// 生成参数名称
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 protected override string CreateParametName(string name)
 {
     return(MsSqlServerHelper.GetParametName(name));
 }
Example #8
0
        /// <summary>
        /// 创建字段定义的SQL
        /// </summary>
        /// <param name="field">字段模型</param>
        /// <param name="tablesourcename">表名称</param>
        /// <param name="stringcollatename">字符排序</param>
        /// <returns></returns>
        private static string CreateTableFieldSql(
            IDataFieldModel field,
            string tablesourcename,
            string stringcollatename)
        {
            StringBuilder xx = new StringBuilder();

            //添加 "[fieldname]"
            xx.Append(MsSqlServerHelper.GetFeildNameInSqlExpress(field.Name));
            if (field.IsRowVersion())
            {
                xx.Append(" [timestamp] NULL");
            }
            else
            {
                //添加 " [dbtype]" -> "[fieldname] [dbtype]"
                xx.AppendFormat(" [{0}]", DbTypeToSqlDbType(field.FieldDbType).ToString().ToLower());
                switch (field.FieldDbType)
                {
                case System.Data.DbType.AnsiString:
                case System.Data.DbType.String:
                {
                    int size = field.GetFieldSize();
                    if (size <= 0)
                    {
                        size = 50;
                    }
                    if (size == int.MaxValue)
                    {
                        xx.AppendFormat("(max) COLLATE {0}", stringcollatename);
                    }
                    else
                    {
                        xx.AppendFormat("({0}) COLLATE {1}", size, stringcollatename);
                    }
                    //添加 "(size) COLLATE ....." -> "[fieldname] [dbtype](size) COLLATE...."
                    break;
                }

                case System.Data.DbType.Decimal:
                {
                    int l   = 18;
                    int dot = 4;
                    var d   = field.GetDecimalDefine();
                    if (d != null)
                    {
                        l   = d.DecimalLength;
                        dot = d.DotLength;
                        if (l <= 0)
                        {
                            l = 18;
                        }
                        if (dot < 0)
                        {
                            dot = 0;
                        }
                    }
                    //添加 "(length,dotlength)" -> "[fieldname] [dbtype](length,dotlength)"
                    xx.AppendFormat("({0},{1})", l, dot);
                    break;
                }

                case System.Data.DbType.Int16:
                case System.Data.DbType.Int32:
                case System.Data.DbType.Int64:
                {
                    var g = field.GetDbGeneratedDefine();
                    if (g != null)
                    {
                        //添加 " IDENTITY(start,step)" -> "[fieldname] [dbtype]IDENTITY(start,step)"
                        xx.AppendFormat(" IDENTITY({0},{1})", g.StartValue, g.Step);
                    }
                    break;
                }
                }
                if (field.CanbeNull())
                {
                    xx.Append(" NULL");
                }
                else
                {
                    xx.Append(" NOT NULL");
                }

                string defaultvalue = field.GetDefaultValue();
                if (!string.IsNullOrEmpty(defaultvalue))
                {
                    xx.AppendFormat(" CONSTRAINT [DF_{0}_{1}_defaultvalue] DEFAULT ({2})", tablesourcename, field.Name, defaultvalue);
                }
            }
            return(xx.ToString());
        }
Example #9
0
 /// <summary>
 /// 将字段值从数据库格式转换为本机格式
 /// </summary>
 /// <param name="field">字段信息</param>
 /// <param name="value">字段值</param>
 /// <returns></returns>
 protected object ConvertFieldValueFromDbValue(IDataModel model, string name, object value)
 {
     return(MsSqlServerHelper.ConvertFieldValueFromDbValue(model.Fields[name], value));
 }