Esempio n. 1
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);
        }
Esempio n. 2
0
        internal static void CheckEntityKey(object entity)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entity.GetType());

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                if (mfi.ProInfo.GetValue(entity, null) == null)
                {
                    throw new ORMException(ErrorMessages.PrimaryKeyIsNull);
                }
            }
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /// <summary>
        /// 删除数据库中某一对象的DbCommand,deleteCommandCreator的属性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 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);
        }
Esempio n. 5
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.GetAllFieldInfos())
            {
                PropertyInfo property = mfi.ProInfo;
                infoBuilder.AppendLine("[" + property.Name + "]: " + property.GetValue(entity, null));
            }
            return(infoBuilder.ToString());
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        internal 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);
            }
        }
Esempio n. 8
0
        private DbCommand GetDbCommand(Criteria criteria, params SortInfo[] orderBy)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(this.ObjectType);
            List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>();
            string             sqlFields          = GetSelectStatement(entityInfo);
            List <DbParameter> parameters;
            string             sqlWhere   = criteria.GenerateExpression(this.ObjectType, out parameters, DbAccess);
            string             sqlOrder   = ToOrderByClause(this.ObjectType, orderBy);
            StringBuilder      sqlBuilder = new StringBuilder();

            if (NeedPaged)
            {
                if (sqlOrder == "")
                {
                    foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
                    {
                        if (sqlOrder != "")
                        {
                            sqlOrder += ",";
                        }
                        else
                        {
                            sqlOrder += "ORDER BY ";
                        }
                        sqlOrder += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + " ASC";
                    }
                }
                sqlBuilder.AppendLine("WITH [TMP] AS(");
                sqlBuilder.AppendLine("SELECT *, ROW_NUMBER() OVER (" + sqlOrder + ") AS [ROW]");
                sqlBuilder.AppendLine("FROM(");
                sqlBuilder.AppendLine(string.Format("SELECT {0}", sqlFields));
                sqlBuilder.AppendLine(string.Format("WHERE 1=1 {0}", sqlWhere));
                sqlBuilder.AppendLine(") AS [A] )");
                sqlBuilder.AppendLine("SELECT * FROM [TMP]");
                sqlBuilder.AppendLine("INNER JOIN (SELECT COUNT([ROW]) AS [TotalCount] FROM [TMP]) AS [B] ON 1 = 1");
                sqlBuilder.AppendLine(string.Format(" AND ROW > {0} AND ROW <= {1}", PageSize * (PageIndex - 1), PageSize * PageIndex));
            }
            else
            {
                sqlBuilder.AppendLine(string.Format("SELECT {0}", sqlFields));
                sqlBuilder.AppendLine(string.Format("WHERE 1=1 {0}", sqlWhere));
                sqlBuilder.AppendLine(sqlOrder);
            }
            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, null);

            dbCommand.Parameters.AddRange(parameters.ToArray());
            dbCommand.CommandText = sqlBuilder.ToString();
            return(dbCommand);
        }
Esempio n. 9
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;
            this._dbType    = ORMHelper.GetDbTypeByName(type);
            this._fieldName = fieldInfo.MappingFieldAttribute.FieldName;
            this._dbAccess  = dbAccess;
        }
Esempio n. 10
0
        /// <summary>
        /// 构建select count()语句
        /// </summary>
        /// <param name="entityType">指定实体对象</param>
        /// <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);
        }
Esempio n. 11
0
        public static T ConvertDataRowToEntity <T>(DataRow dataRow) where T : new()
        {
            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))
                {
                    object value = null;
                    if (!dataRow[fieldName].Equals(DBNull.Value))
                    {
                        Type type = Nullable.GetUnderlyingType(mfi.ProInfo.PropertyType) ?? mfi.ProInfo.PropertyType;
                        if (type.IsEnum)
                        {
                            value = Enum.Parse(type, dataRow[fieldName].ToString());
                        }
                        else
                        {
                            value = Convert.ChangeType(dataRow[fieldName], type);
                        }
                    }
                    mfi.ProInfo.SetValue(tempT, value, null);
                }
            }
            return(tempT);
        }
Esempio n. 12
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);
            }
        }