Beispiel #1
0
        /// <summary>
        /// 获取数据库实体的表名
        /// </summary>
        /// <returns></returns>
        public string GetTableName <T>()
        {
            Type   type = typeof(T);
            string key  = type.FullName + "_tableName";

            //检查缓存是否存在
            if (StringCacheUntil.ExitsKey(key))
            {
                return(StringCacheUntil.GetCache(key));
            }
            else
            {
                //获取特性
                TableName tableNameAttr = type.GetCustomAttribute <TableName>();
                string    tableName;
                if (tableNameAttr != null)
                {
                    tableName = tableNameAttr._TableName;
                }
                else
                {
                    tableName = type.Name;
                }
                //加入缓存
                StringCacheUntil.AddCache(key, tableName);
                return(tableName);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 构造SQL参数化
        /// </summary>
        /// <param name="propertyInfos"></param>
        /// <returns></returns>
        public string BuildSqlParms <T>(PropertyInfo[] propertyInfos)
        {
            Type   type = typeof(T);
            string key  = type.FullName + "sqlParms";

            #region 缓存校验
            if (StringCacheUntil.ExitsKey(key))
            {
                return(StringCacheUntil.GetCache(key));
            }
            #endregion

            #region 拼接参数化
            StringBuilder paramsSql = new StringBuilder();
            paramsSql.Append("VALUES( ");
            int size = propertyInfos.Length - 1;
            for (int i = 0; i <= size; i++)
            {
                PropertyInfo property = propertyInfos[i];
                //判断字段是否是自增主键,如果是自增主键直接跳过
                TableId tableId = property.GetCustomAttribute <TableId>();
                if (tableId != null)
                {
                    if (tableId._idType == Enum.IdTypeEnum.AUTO)
                    {
                        continue;
                    }
                }
                //校验是否是表字段
                TableFiled tableFiled = property.GetCustomAttribute <TableFiled>();
                if (tableFiled != null)
                {
                    if (!tableFiled.Exits)
                    {
                        continue;
                    }
                }
                if (i < size)
                {
                    paramsSql.Append("@");
                    paramsSql.Append(property.Name);
                    paramsSql.Append(",");
                }
                else
                {
                    paramsSql.Append("@");
                    paramsSql.Append(property.Name);
                }
            }
            #endregion
            //加入缓存
            string res = paramsSql.ToString();
            StringCacheUntil.AddCache(key, res);
            return(res);
        }
Beispiel #3
0
        /// <summary>
        /// 构造SQL字段
        /// </summary>
        /// <param name="propertyInfos">实体属性</param>
        /// <returns></returns>
        public string BuildSqlFiled <T>(PropertyInfo[] propertyInfos)
        {
            Type   type     = typeof(T);
            string cacheKey = type.FullName + "_sqlFiled";
            string idKey    = type.FullName + "idType";

            #region 缓存校验
            if (StringCacheUntil.ExitsKey(cacheKey))
            {
                return(StringCacheUntil.GetCache(cacheKey));
            }
            #endregion

            StringBuilder sqlFiled = new StringBuilder();
            sqlFiled.Append("(");
            int length = propertyInfos.Length - 1;
            #region 拼接Sql
            for (int i = 0; i <= length; i++)
            {
                PropertyInfo property = propertyInfos[i];
                //判断字段是否是自增主键,如果是自增主键直接跳过
                TableId tableId = property.GetCustomAttribute <TableId>();
                if (tableId != null)
                {
                    StringCacheUntil.AddCache(idKey, JsonConvert.SerializeObject(new KeyValuePair <string, IdTypeEnum>(property.Name, tableId._idType)));
                    if (tableId._idType == Enum.IdTypeEnum.AUTO)
                    {
                        continue;
                    }
                }
                TableFiled tableFiled = property.GetCustomAttribute <TableFiled>();
                //最后一个字段不加,
                if (i < length)
                {
                    //如果不是数据库字段直接跳过
                    if (tableFiled != null)
                    {
                        if (!tableFiled.Exits)
                        {
                            continue;
                        }
                        sqlFiled.Append("`");
                        sqlFiled.Append(tableFiled.FiledName);
                        sqlFiled.Append("`");
                        sqlFiled.Append(",");
                    }
                    else
                    {
                        sqlFiled.Append("`");
                        sqlFiled.Append(property.Name);
                        sqlFiled.Append("`");
                        sqlFiled.Append(",");
                    }
                }
                else
                {
                    //如果不是数据库字段直接跳过
                    if (tableFiled != null)
                    {
                        if (!tableFiled.Exits)
                        {
                            continue;
                        }
                        sqlFiled.Append("`");
                        sqlFiled.Append(tableFiled.FiledName);
                        sqlFiled.Append("`");
                    }
                    else
                    {
                        sqlFiled.Append("`");
                        sqlFiled.Append(property.Name);
                        sqlFiled.Append("`");
                    }
                }
            }
            #endregion
            sqlFiled.Append(")");
            //加入缓存
            string res = sqlFiled.ToString();
            StringCacheUntil.AddCache(cacheKey, res);
            return(res);
        }