Ejemplo n.º 1
0
        public static string GetEntityMappingTableName(Type entityType)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entityType);
            string     tableName  = entityInfo.MappingTableAttribute.TableName;

            return(tableName);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 将数据库中的行数据转换成一个实体对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="dataRow">数据库中的行数据</param>
        /// <returns>指定类型的实体对象</returns>
        public static T ConvertDataRowToEntity <T>(DataRow dataRow) where T : new()
        {
            //断言传入的datarow不能为null
            PreconditionAssert.IsNotNull(dataRow, ErrorMessages.NullReferenceException);

            T          tempT      = new T();
            Type       entityType = typeof(T);
            string     fieldName;
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entityType);

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                fieldName = mfi.MappingFieldAttribute.FieldName;
                if (string.IsNullOrEmpty(fieldName))
                {
                    continue;
                }

                if (dataRow.Table.Columns.Contains(fieldName))
                {
                    if (dataRow[fieldName].Equals(DBNull.Value))
                    {
                        mfi.ProInfo.SetValue(tempT, null, null);
                    }
                    else
                    {
                        Type type = Nullable.GetUnderlyingType(mfi.ProInfo.PropertyType) ?? mfi.ProInfo.PropertyType;
                        mfi.ProInfo.SetValue(tempT, dataRow[fieldName], null);
                    }
                }
            }
            return(tempT);
        }
Ejemplo n.º 3
0
        private DbCommand GetAllCountDbCommand(Type entityType)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(entityType);
            List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>();
            string    sqlSelect = string.Format("SELECT COUNT(*) FROM {0}", this.GetQuotedName(entityInfo.MappingTableAttribute.TableName));
            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, null);

            dbCommand.CommandText = sqlSelect;
            return(dbCommand);
        }
Ejemplo n.º 4
0
        private DbCommand GetDbCommand(object entity)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(entity.GetType());
            List <PropertyInfo> fieldPropertyList = null;
            string tableName = entityInfo.MappingTableAttribute.TableName;
            string sqlSelect = string.Format("SELECT {0}", GetSelectStatement(entityInfo, out fieldPropertyList, null));

            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, entity);

            dbCommand.CommandText = sqlSelect;
            return(dbCommand);
        }
Ejemplo n.º 5
0
        public static void CheckEntityKey(object entity)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entity.GetType());

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                if (mfi.ProInfo.GetValue(entity, null) == null)//如果对象的属性为null,则把此参数设置为DBNull
                {
                    throw new ORMException(ErrorMessages.PrimaryKeyIsNull);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 删除数据库中某一对象的DbCommand,deleteCommandCreator的属性Entity不能为空
        /// </summary>
        /// <exception cref="RException(DatabaseErrorCode.CommandTextIsEmpty - 0x00010102)">
        /// 属性Entity为null时,引发此异常
        /// </exception>
        /// <returns>删除数据库中某一对象的DbCommand,DbCommand的参数为所传入的Entity的属性</returns>
        private DbCommand GetDeleteByEntityDbCommand()
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(EntityType);
            List <PropertyInfo> fieldPropertyList = null;
            string sqlDelete = string.Format("DELETE FROM {0} {1}"
                                             , GetQuotedName(entityInfo.MappingTableAttribute.TableName)
                                             , GetDeleteStatement(entityInfo, out fieldPropertyList));
            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, Entity);

            dbCommand.CommandText = sqlDelete;
            return(dbCommand);
        }
Ejemplo n.º 7
0
        public static string GetEntityInfoMessage(object entity)
        {
            TypeSchema    entityInfo  = ORMSchemaCache.GetTypeSchema(entity.GetType());
            StringBuilder infoBuilder = new StringBuilder();

            infoBuilder.AppendLine("Entity Type: " + entity.GetType().FullName);
            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                PropertyInfo property = mfi.ProInfo;
                infoBuilder.AppendLine("[" + property.Name + "]: " + property.GetValue(entity, null));
            }
            return(infoBuilder.ToString());
        }
Ejemplo n.º 8
0
        public static void EntityIsMappingDatabase(Type type, string message)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(type);

            if (entityInfo.MappingTableAttribute == null)
            {
                throw new ORMException(message);
            }
            if (entityInfo.GetKeyFieldInfos() == null || entityInfo.GetKeyFieldInfos().Count == 0)
            {
                throw new ORMException(message);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 得到新增数据库中某一对象的DbCommand,modifyCommandCreator的属性Entity不能为空
        /// </summary>
        /// <exception cref="RException(DatabaseErrorCode.CommandTextIsEmpty - 0x00010102)">
        /// 属性Entity为null时,引发此异常
        /// </exception>
        /// <returns>新增数据库中某一对象的DbCommand,DbCommand的参数为所传入的Entity的属性</returns>
        public override DbCommand GetDbCommand()
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(Entity.GetType());
            List <PropertyInfo> fieldPropertyList = null;
            string sqlInsert = string.Format("INSERT INTO {0} {1}"
                                             , GetQuotedName(entityInfo.MappingTableAttribute.TableName)
                                             , GetInsertStatement(entityInfo, out fieldPropertyList));

            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, this.Entity);

            dbCommand.CommandText = sqlInsert;
            return(dbCommand);
        }
Ejemplo n.º 10
0
        private DbCommand GetDbCommand(Type entityType, params SortInfo[] orderBy)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(entityType);
            List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>();
            string tableName = entityInfo.MappingTableAttribute.TableName;
            string sqlSelect = string.Format("SELECT {0}", GetSelectStatement(entityInfo));

            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, null);

            sqlSelect             = sqlSelect + this.ToOrderByClause(entityType, orderBy);
            dbCommand.CommandText = sqlSelect;
            return(dbCommand);
        }
Ejemplo n.º 11
0
        private DbCommand GetDbCommand(object entity, string[] filedProterties, params SortInfo[] orderBy)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(entity.GetType());
            List <PropertyInfo> fieldPropertyList = null;
            string tableName = entityInfo.MappingTableAttribute.TableName;
            string sqlSelect = string.Format("SELECT {0}",
                                             GetSelectStatement(entityInfo, out fieldPropertyList, filedProterties));

            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, entity);

            sqlSelect             = sqlSelect + this.ToOrderByClause(entity.GetType(), orderBy);
            dbCommand.CommandText = sqlSelect;
            return(dbCommand);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 得到修改数据库中某一对象的DbCommand,modifyCommandCreator的属性Entity不能为空
        /// </summary>
        /// <exception cref="RException(DatabaseErrorCode.CommandTextIsEmpty - 0x00010102)">
        /// 属性Entity为null时,引发此异常
        /// </exception>
        /// <returns>修改数据库中某一对象的DbCommand,DbCommand的参数为所传入的Entity的属性</returns>
        public override DbCommand GetDbCommand()
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(Entity.GetType());
            List <PropertyInfo> fieldPropertyList = null;

            string sqlUpdate = string.Format("UPDATE {0} SET {1}"
                                             , GetQuotedName(entityInfo.MappingTableAttribute.TableName)
                                             , GetUpdateStatement(entityInfo, out fieldPropertyList));

            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, Entity);

            dbCommand.CommandText = sqlUpdate;
            return(dbCommand);
        }
Ejemplo n.º 13
0
        private DbCommand GetDbCommand(Criteria cri)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(EntityType);
            List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>();
            DbCommand           dbCommand         = GetDbCommandByEntity(fieldPropertyList, null);

            List <DbParameter> parameters;
            string             sqlDelete = string.Format("DELETE FROM {0} WHERE 1=1 {1}"
                                                         , GetQuotedName(entityInfo.MappingTableAttribute.TableName)
                                                         , cri.GenerateExpression(EntityType, out parameters, DbAccess));

            dbCommand.Parameters.AddRange(parameters.ToArray());
            dbCommand.CommandText = sqlDelete;
            return(dbCommand);
        }
Ejemplo n.º 14
0
        private DbCommand GetDbCommand(Criteria criteria, params SortInfo[] orderBy)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(this.ObjectType);
            List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>();
            string sqlSelect = string.Format("SELECT {0}", GetSelectStatement(entityInfo));

            DbCommand          dbCommand = GetDbCommandByEntity(fieldPropertyList, null);
            List <DbParameter> parameters;

            sqlSelect = sqlSelect + " WHERE 1=1 " + criteria.GenerateExpression(this.ObjectType, out parameters, DbAccess);
            sqlSelect = sqlSelect + this.ToOrderByClause(this.ObjectType, orderBy);

            dbCommand.Parameters.AddRange(parameters.ToArray());
            dbCommand.CommandText = sqlSelect;
            return(dbCommand);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 构建select count()语句
        /// </summary>
        /// <returns></returns>
        private DbCommand GetCountDbCommand(object entity, string[] filterPerproties)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(entity.GetType());
            List <PropertyInfo> fieldPropertyList = null;
            string sqlSelect = "";

            if (filterPerproties == null)
            {
                sqlSelect         = string.Format("SELECT COUNT(*) FROM {0}", this.GetQuotedName(entityInfo.MappingTableAttribute.TableName));
                fieldPropertyList = new List <PropertyInfo>();//初始化fieldPropertyList,仅仅为了给GetDbCommandByEntity提供无元素的List
            }
            else
            {
                sqlSelect = string.Format("SELECT {0}", GetCountSelectStatement(entityInfo, out fieldPropertyList, filterPerproties));
            }
            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, entity);

            dbCommand.CommandText = sqlSelect;
            return(dbCommand);
        }
Ejemplo n.º 16
0
        internal virtual void Init(Type objectType, IDbAccess dbAccess)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(objectType);
            SchemaItem fieldInfo  = entityInfo.GetFieldInfoByPropertyName(this._propertyName);

            if (fieldInfo == null)
            {
                throw new ArgumentException(string.Format("{0} - Property \"{1}\" not define a mapping field.", this.GetType(), _propertyName));
            }
            Type type = fieldInfo.ProInfo.PropertyType;

            type = Nullable.GetUnderlyingType(type) ?? type;
            if (type.IsEnum)
            {
                this._dbType = DbType.Int32;
            }
            else
            {
                this._dbType = ORMHelper.GetDbTypeByName(type.Name);
            }
            this._fieldName = fieldInfo.MappingFieldAttribute.FieldName;
            this._dbAccess  = dbAccess;
        }
Ejemplo n.º 17
0
        private string ToOrderByClause(Type entityType, params SortInfo[] orderBy)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entityType);
            string     orderByStr = "";

            if (null != orderBy && orderBy.Length > 0)
            {
                foreach (SortInfo tempSortInfo in orderBy)
                {
                    SchemaItem fieldInfo = entityInfo.GetFieldInfoByPropertyName(tempSortInfo.PropertyName);
                    if (fieldInfo == null)
                    {
                        throw new ArgumentException("Sort property not define a mapping field. - \"" + tempSortInfo.PropertyName + "\"");
                    }
                    string tempName = GetQuotedName(fieldInfo.MappingFieldAttribute.FieldName);
                    if (tempSortInfo.Direction == SortDirection.Desc)
                    {
                        orderByStr += "," + tempName + " Desc";
                    }
                    else
                    {
                        orderByStr += "," + tempName;
                    }
                }

                if (orderByStr.Length > 0)
                {
                    orderByStr = orderByStr.Substring(1); //remove the first char ','.
                }
                return(string.Format(" ORDER BY {0}", orderByStr));
            }
            else
            {
                return(orderByStr);
            }
        }