Esempio n. 1
0
        /// <summary>
        /// 获取单个属性的自定义特性信息
        /// </summary>
        /// <param name="proInfo"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        private TableColumnAttribute GetCustomAttributeInfo(PropertyInfo proInfo, object param)
        {
            TableColumnAttribute columnEntity = null;

            if (proInfo == null)
            {
                return(columnEntity);
            }
            object[] customAttributes = proInfo.GetCustomAttributes(typeof(TableColumnAttribute), true);
            if (customAttributes == null)
            {
                return(columnEntity);
            }
            if (customAttributes.Length <= 0)
            {
                return(columnEntity);
            }
            columnEntity = (TableColumnAttribute)customAttributes[0];
            if (columnEntity == null)
            {
                return(columnEntity);
            }
            columnEntity.ColumnName = string.IsNullOrEmpty(columnEntity.ColumnName) ? proInfo.Name : columnEntity.ColumnName;
            if (param != null)
            {
                object attrValue = proInfo.GetValue(param, null);
                if (attrValue != null)
                {
                    columnEntity.ColumnValue = attrValue;
                }
            }
            return(columnEntity);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取表主键字段信息
        /// </summary>
        /// <returns></returns>
        public TableColumnAttribute GetPkColumnInfo(Type type)
        {
            TableColumnAttribute pkColumn = null;

            PropertyInfo[] props = type.GetProperties();
            if (props == null || props.Length <= 0)
            {
                return(pkColumn);
            }
            foreach (PropertyInfo proInfo in props)
            {
                TableColumnAttribute systemColumn      = GetSystemProperInfo(proInfo, null);
                TableColumnAttribute selftDefineColumn = GetCustomAttributeInfo(proInfo, null);
                if (systemColumn == null || selftDefineColumn == null)
                {
                    return(pkColumn);
                }
                if (selftDefineColumn.IsPrimaryKey)
                {
                    pkColumn = selftDefineColumn;
                    break;
                }
            }
            return(pkColumn);
        }
        /// <summary>
        /// 根据参数获取表字段列表
        /// </summary>
        /// <param name="param">参数对象,例:new {Uid=1,Uname="joyet"}</param>
        /// <returns></returns>
        public List <TableColumnAttribute> GetColumnInfos(object param)
        {
            List <TableColumnAttribute> columnEntityList = null;

            if (param == null)
            {
                return(columnEntityList);
            }
            Type type = param.GetType();

            PropertyInfo[] props = type.GetProperties();
            if (props == null)
            {
                return(columnEntityList);
            }
            if (props.Length == 0)
            {
                return(columnEntityList);
            }
            columnEntityList = new List <TableColumnAttribute>();
            foreach (PropertyInfo proInfo in props)
            {
                TableColumnAttribute systemColumn = GetProperInfo(proInfo, param);
                columnEntityList.Add(systemColumn);
            }
            return(columnEntityList);
        }
        /// <summary>
        /// 根据实体类型、实体对象实例获取表字段列表信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public List <TableColumnAttribute> GetColumnInfos(Type type, object entity)
        {
            List <TableColumnAttribute> columnEntityList = null;

            PropertyInfo[] propertys = type.GetProperties();
            if (propertys == null)
            {
                return(columnEntityList);
            }
            if (propertys.Length <= 0)
            {
                return(columnEntityList);
            }
            columnEntityList = new List <TableColumnAttribute>();
            foreach (PropertyInfo proInfo in propertys)
            {
                TableColumnAttribute systemColumn      = GetProperInfo(proInfo, entity);
                TableColumnAttribute selftDefineColumn = GetCustomAttributeInfo(proInfo, entity);
                if (selftDefineColumn == null)
                {
                    columnEntityList.Add(systemColumn);
                    continue;
                }
                columnEntityList.Add(selftDefineColumn);
            }
            return(columnEntityList);
        }
Esempio n. 5
0
        /// <summary>
        ///根据主键查询实体对应数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual DbEntity Query <T>(object id)
        {
            DbEntity             dbEntity         = null;
            Type                 type             = typeof(T);
            var                  attributeBuilder = new AttributeBuilder();
            var                  tableEntity      = attributeBuilder.GetTableInfo(type);
            TableColumnAttribute pkColumn         = attributeBuilder.GetPkColumnInfo(type);

            if (pkColumn == null)
            {
                return(dbEntity);
            }
            pkColumn.ColumnValue = id;
            string dbOperator = DbFactory.GetDbParamOperator();
            List <IDbDataParameter> dbParams = new List <IDbDataParameter>();
            StringBuilder           sqlBuild = new StringBuilder("select * from {tableName} where {pkColumn}={dbOperator}{pkColumn}");

            sqlBuild.Replace("{tableName}", tableEntity.TableName);
            sqlBuild.Replace("{pkColumn}", pkColumn.ColumnName);
            sqlBuild.Replace("{dbOperator}", dbOperator);
            dbParams.Add(DbFactory.GetDbParam(pkColumn));
            dbEntity = new DbEntity()
            {
                TableEntity = tableEntity,
                CommandText = sqlBuild.ToString(),
                DbParams    = dbParams,
            };
            return(dbEntity);
        }
Esempio n. 6
0
        /// <summary>
        /// 创建数据库参数对象
        /// </summary>
        /// <param name="tableColumn"></param>
        /// <returns></returns>
        public virtual IDbDataParameter GetDbParam(TableColumnAttribute tableColumn)
        {
            IDbDataParameter dbParam = null;

            if (tableColumn == null)
            {
                return(dbParam);
            }
            dbParam = GetDbParam(tableColumn.ColumnName, tableColumn.ColumnValue, tableColumn.DataType, tableColumn.MaxLength);
            return(dbParam);
        }
Esempio n. 7
0
        /// <summary>
        /// 获取系统单个属性信息
        /// </summary>
        /// <param name="proInfo"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        private TableColumnAttribute GetSystemProperInfo(PropertyInfo proInfo, object entity)
        {
            TableColumnAttribute column = null;

            if (proInfo == null)
            {
                return(column);
            }
            column            = new TableColumnAttribute();
            column.ColumnName = proInfo.Name;
            if (entity != null)
            {
                object attrValue = proInfo.GetValue(entity, null);
                if (attrValue != null)
                {
                    column.ColumnValue = attrValue;
                }
            }
            return(column);
        }
Esempio n. 8
0
        /// <summary>
        ///根据主键值查询数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual DbEntity Query <T>(object id)
        {
            DbEntity             dbEntity         = null;
            Type                 type             = typeof(T);
            var                  attributeBuilder = new AttributeBuilder();
            var                  tableEntity      = attributeBuilder.GetTableInfo(type);
            TableColumnAttribute pkColumn         = attributeBuilder.GetPkColumnInfo(type);

            if (pkColumn == null)
            {
                return(dbEntity);
            }
            pkColumn.ColumnValue = id;
            string dbOperator = DbFactory.GetDbParamOperator();
            List <IDbDataParameter> dbParams   = new List <IDbDataParameter>();
            StringBuilder           sqlBuilder = new StringBuilder("select * from {tableName} {whereCriteria}");

            sqlBuilder.Replace("{tableName}", tableEntity.TableName);

            //处理过滤字段参数
            var whereItem = HandleWhereParam("", new List <TableColumnAttribute>()
            {
                pkColumn
            }, sqlBuilder, dbParams);

            sqlBuilder = whereItem.Item1;
            dbParams   = whereItem.Item2;

            dbEntity = new DbEntity()
            {
                TableEntity = tableEntity,
                CommandText = sqlBuilder.ToString(),
                DbParams    = dbParams,
            };
            return(dbEntity);
        }
Esempio n. 9
0
        /// <summary>
        /// 根据实体类型、实体对象实例获取表字段列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public List <TableColumnAttribute> GetColumnInfos(Type type, object entity)
        {
            var columnList = new List <TableColumnAttribute>();

            PropertyInfo[] propertys = type.GetProperties();
            if (propertys == null || propertys.Length <= 0)
            {
                return(columnList);
            }
            foreach (PropertyInfo proInfo in propertys)
            {
                TableColumnAttribute systemColumn      = GetSystemProperInfo(proInfo, entity);
                TableColumnAttribute selftDefineColumn = GetCustomAttributeInfo(proInfo, entity);
                if (selftDefineColumn != null)
                {
                    columnList.Add(selftDefineColumn);
                }
                else
                {
                    columnList.Add(systemColumn);
                }
            }
            return(columnList);
        }