Пример #1
0
        /// <summary>
        /// 获取要执行SQL时的列,添加和修改数据时
        /// </summary>
        /// <param name="des"></param>
        /// <param name="add">是否是添加</param>
        /// <returns></returns>
        internal static IList <ParamColumnModel> GetExecColumns(ModelDes des, bool add = true)
        {
            var columns = new List <ParamColumnModel>();

            if (des != null && des.Properties != null)
            {
                foreach (var item in des.Properties)
                {
                    if ((!add) && item.CusAttribute is IdAttribute)
                    {
                        continue;
                    }
                    else if ((item.CusAttribute is IdAttribute) && ((item.CusAttribute as IdAttribute).CheckAutoId))
                    {
                        continue;
                    }
                    else if ((item.CusAttribute is ColumnAttribute) && ((item.CusAttribute as ColumnAttribute).AutoIncrement))
                    {
                        continue;
                    }
                    columns.Add(new ParamColumnModel()
                    {
                        ColumnName = item.Column, FieldName = item.Field
                    });
                }
            }
            return(columns);
        }
Пример #2
0
 /// <summary>
 /// 添加缓存
 /// </summary>
 /// <param name="key"></param>
 /// <param name="des"></param>
 private static void Add(string key, ModelDes des)
 {
     lock (objLock)
     {
         if ((!_ModelDesCache.ContainsKey(key)) && des != null)
         {
             _ModelDesCache.Add(key, des);
         }
     }
 }
Пример #3
0
        /// <summary>
        /// 获取对象的主键标识列和属性
        /// </summary>
        /// <param name="des"></param>
        /// <returns></returns>
        internal static PropertyDes GetPrimary(ModelDes des)
        {
            var p = des.Properties.Where(m => m.CusAttribute is IdAttribute).FirstOrDefault();

            if (p == null)
            {
                throw new Exception("没有任何列标记为主键特性");
            }
            return(p as PropertyDes);
        }
Пример #4
0
        /// <summary>
        /// 更新缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        private static ModelDes UpdateModelDesCache <T>()
        {
            var type       = typeof(T);
            var cacheValue = ExistModelDesCache(type.FullName);

            if (cacheValue == null)
            {
                var model = new ModelDes();
                #region 表描述
                model.ClassType = type;
                model.ClassName = type.Name;
                var tbAttrObj = type.GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault();
                if (tbAttrObj != null)
                {
                    var tbAttr = tbAttrObj as TableAttribute;
                    if (!string.IsNullOrEmpty(tbAttr.Name))
                    {
                        model.TableName = tbAttr.Name;
                    }
                    else
                    {
                        model.TableName = model.ClassName;
                    }
                }
                else
                {
                    model.TableName = model.ClassName;
                }
                #endregion
                #region 属性描述
                foreach (var propertyInfo in type.GetProperties())
                {
                    var pty = new PropertyDes();
                    pty.Field = propertyInfo.Name;
                    var arri = propertyInfo.GetCustomAttributes(typeof(BaseAttribute), true).FirstOrDefault();
                    if (arri is IgnoreAttribute)
                    {
                        continue;
                    }
                    else if (arri is IdAttribute)
                    {
                        pty.CusAttribute = arri as IdAttribute;
                    }
                    else if (arri is ColumnAttribute)
                    {
                        pty.CusAttribute = arri as ColumnAttribute;
                    }
                    model.Properties.Add(pty);
                }
                #endregion
                Add(type.FullName, model);
                cacheValue = model;
            }
            return(cacheValue);
        }
Пример #5
0
        /// <summary>
        /// 通过表达式树获取属性名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expr"></param>
        /// <returns></returns>
        internal static PropertyDes GetPropertyByExpress <T>(ModelDes des, Expression <Func <T, object> > expr) where T : class
        {
            var pname = "";

            if (expr.Body is UnaryExpression)
            {
                var uy = expr.Body as UnaryExpression;
                pname = (uy.Operand as MemberExpression).Member.Name;
            }
            else
            {
                pname = (expr.Body as MemberExpression).Member.Name;
            }
            var p = des.Properties.Where(m => m.Column == pname).FirstOrDefault();

            if (p == null)
            {
                throw new Exception(string.Format("{0}不是映射列,不能进行SQL处理", pname));
            }
            return(p);
        }
Пример #6
0
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbs"></param>
        /// <param name="t"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        public static bool Insert <T>(this DbBase dbs, T t, IDbTransaction transaction = null, int?commandTimeout = null) where T : class
        {
            int flag = 0;

            Stopwatch watch = new Stopwatch();

            try
            {
                var db = dbs.DbConnecttion;
                watch.Start();
                var sql = SqlQuery <T> .Builder(dbs);

                bool   isAutoId  = false;
                string insertSql = sql.InsertSql;

                ModelDes modelDes = Common.GetModelDes <T>();
                if (modelDes != null && modelDes.Properties != null)
                {
                    foreach (var item in modelDes.Properties)
                    {
                        if ((item.CusAttribute is IdAttribute) && ((item.CusAttribute as IdAttribute).CheckAutoId))
                        {
                            isAutoId = true;
                            break;
                        }
                    }
                }

                if (isAutoId)
                {
                    insertSql += ";SELECT @@IDENTITY;";
                    var recordId = db.ExecuteScalar(insertSql, t, transaction, commandTimeout);
                    flag = Convert.ToInt32(recordId);
                }
                else
                {
                    flag = db.Execute(insertSql, t, transaction, commandTimeout, null, watch);
                }

                if (isAutoId)
                {
                    #region 如果主键是自增Id,则将生成的id赋值到实体类中

                    if (modelDes != null && modelDes.Properties != null)
                    {
                        foreach (var item in modelDes.Properties)
                        {
                            if ((item.CusAttribute is IdAttribute) && ((item.CusAttribute as IdAttribute).CheckAutoId))
                            {
                                var filed = t.GetType().GetProperty(item.Field);
                                filed.SetValue(t, Convert.ChangeType(flag, filed.PropertyType), null);
                                break;
                            }
                        }
                    }

                    #endregion

                    return(true);
                }
                return(flag > 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (watch.IsRunning)
                {
                    watch.Stop();
                }
            }
        }