private string GetDeleteStatement(TypeSchema entityInfo,out List<PropertyInfo> fieldPropertyList)
 {
     string query = "";
     fieldPropertyList = new List<PropertyInfo>();
     foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
     {
         if (query != "") query += " AND ";
         query += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
         fieldPropertyList.Add(mfi.ProInfo);
     }
     return string.Format("WHERE {0}", query);
 }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
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.º 4
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);
            }
        }
        /// <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.º 6
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);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 构建无where条件查询的sql语句
        /// </summary>
        /// <param name="fieldPropertyList"></param>
        /// <param name="tableName">表名</param>
        /// <returns>sql语句</returns>
        private string GetSelectStatement(TypeSchema entityInfo)
        {
            string fields = "";

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                if (fields != "")
                {
                    fields += ",";
                }
                fields += GetQuotedName(mfi.MappingFieldAttribute.FieldName);
            }

            return(string.Format("{0} FROM {1}", fields, this.GetQuotedName(entityInfo.MappingTableAttribute.TableName)));
        }
Ejemplo n.º 8
0
        private string GetDeleteStatement(TypeSchema entityInfo, out List <PropertyInfo> fieldPropertyList)
        {
            string query = "";

            fieldPropertyList = new List <PropertyInfo>();
            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                if (query != "")
                {
                    query += " AND ";
                }
                query += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
                fieldPropertyList.Add(mfi.ProInfo);
            }
            return(string.Format("WHERE {0}", query));
        }
Ejemplo 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;
        }
        private string GetInsertStatement(TypeSchema entityInfo, out List<PropertyInfo> fieldPropertyList)
        {
            string fields = "", values = "";
            fieldPropertyList = new List<PropertyInfo>();
            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                if (mfi.MappingFieldAttribute.IsAutoField) continue;

                if (fields != "") fields += ",";
                if (values != "") values += ",";
                fields += GetQuotedName(mfi.MappingFieldAttribute.FieldName);
                values += "@" + mfi.ProInfo.Name;

                fieldPropertyList.Add(mfi.ProInfo);
            }
            return string.Format("({0}) VALUES ({1})", fields, values);
        }
Ejemplo n.º 11
0
        internal static TypeSchema GetTypeSchema(Type objectType)
        {
            TypeSchema entityInfo = null;

            if (!_entityInfos.TryGetValue(objectType, out entityInfo))
            {
                lock (o)
                {
                    if (!_entityInfos.TryGetValue(objectType, out entityInfo))
                    {
                        entityInfo = new TypeSchema(objectType);
                        _entityInfos.Add(objectType, entityInfo);
                    }
                }
            }

            return(entityInfo);
        }
Ejemplo n.º 12
0
        internal static TypeSchema GetTypeSchema(Type objectType)
        {
            TypeSchema entityInfo = null;

            if (!_entityInfos.TryGetValue(objectType, out entityInfo))
            {
                lock (o)
                {
                    if (!_entityInfos.TryGetValue(objectType, out entityInfo))
                    {
                        entityInfo = new TypeSchema(objectType);
                        _entityInfos.Add(objectType, entityInfo);
                    }
                }
            }

            return entityInfo;
        }
 private string GetUpdateStatement(TypeSchema entityInfo,out List<PropertyInfo> fieldPropertyList)
 {
     fieldPropertyList = new List<PropertyInfo>();
     string sets = "";
     string query = "";
     foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
     {
         if (query != "") query += " AND ";
         query += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
         fieldPropertyList.Add(mfi.ProInfo);
     }
     foreach (SchemaItem mfi in entityInfo.GetNeedUpdateFieldInfos())
     {
         if (sets != "") sets += ",";
         sets += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
         fieldPropertyList.Add(mfi.ProInfo);
     }
     return string.Format("{0} WHERE {1}", sets, query);
 }
Ejemplo n.º 14
0
        protected DbCommand GetDbCommandByKeyValue(TypeSchema entityInfo, object keyValue)
        {
            DbCommand dbCommand = DbAccess.CreateDbCommand();

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                Type type = mfi.ProInfo.PropertyType;
                type = Nullable.GetUnderlyingType(type) ?? type;
                DbType      dbType    = ORMHelper.GetDbTypeByName(type);
                DbParameter parameter = DbAccess.CreateDbParameter();
                parameter.ParameterName = "@" + mfi.ProInfo.Name;
                parameter.DbType        = dbType;

                //parameter.Value = Convert.ChangeType(keyValue, fieldProperty.PropertyType);
                parameter.Value = Convert.ChangeType(keyValue, type);
                dbCommand.Parameters.Add(parameter);
            }
            return(dbCommand);
        }
Ejemplo n.º 15
0
        protected DbCommand GetDbCommandByKeyValue(TypeSchema entityInfo, object keyValue)
        {
            DbCommand dbCommand = DbAccess.CreateDbCommand();

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                Type type = mfi.ProInfo.PropertyType;
                type = Nullable.GetUnderlyingType(type) ?? type;
                DbType dbType = ORMHelper.GetDbTypeByName(type);
                DbParameter parameter = DbAccess.CreateDbParameter();
                parameter.ParameterName = "@" + mfi.ProInfo.Name;
                parameter.DbType = dbType;

                //parameter.Value = Convert.ChangeType(keyValue, fieldProperty.PropertyType);
                parameter.Value = Convert.ChangeType(keyValue, type);
                dbCommand.Parameters.Add(parameter);
            }
            return dbCommand;
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
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);
            }
        }
Ejemplo n.º 19
0
        internal static List <SchemaItem> GetSchemaItems(Type objectType)
        {
            TypeSchema entityInfo = GetTypeSchema(objectType);

            return(entityInfo.GetAllFieldInfos());
        }
        /// <summary>
        /// 构建带where条件查询的sql语句
        /// </summary>
        /// <param name="fieldPropertyList"></param>
        /// <param name="tableName">表名</param>
        /// <param name="filterProterties">要查询的字段</param>
        /// <returns>sql语句</returns>
        private string GetSelectStatement(TypeSchema entityInfo, out List<PropertyInfo> fieldPropertyList, string[] filterProterties)
        {
            fieldPropertyList = new List<PropertyInfo>();
            string fields = "", query = "";

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                PropertyInfo property = mfi.ProInfo;
                EntityMappingFieldAttribute fieldAttr = mfi.MappingFieldAttribute;
                if (fields != "") fields += ",";
                fields += GetQuotedName(fieldAttr.FieldName);
                //当不选择过滤条件则判定是否是主键、当选择过滤字段则判断当前属性是否包含在过滤字段内
                if ((filterProterties == null && fieldAttr.IsKey == true) || (filterProterties != null && filterProterties.Contains(property.Name)))
                {
                    if (query != "") query += " AND ";
                    query += GetQuotedName(fieldAttr.FieldName) + "=@" + property.Name;
                    fieldPropertyList.Add(property);
                }
            }

            return string.Format("{0} FROM {1} WHERE {2}", fields, this.GetQuotedName(entityInfo.MappingTableAttribute.TableName), query);
        }
        /// <summary>
        /// 构建无where条件查询的sql语句
        /// </summary>
        /// <param name="fieldPropertyList"></param>
        /// <param name="tableName">表名</param>
        /// <returns>sql语句</returns>
        private string GetSelectStatement(TypeSchema entityInfo)
        {
            string fields = "";
            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                if (fields != "") fields += ",";
                fields += GetQuotedName(mfi.MappingFieldAttribute.FieldName);
            }

            return string.Format("{0} FROM {1}", fields, this.GetQuotedName(entityInfo.MappingTableAttribute.TableName));
        }