Пример #1
0
        /// <summary>
        /// 给select count()语句构建带where条件查询的sql语句
        /// </summary>
        private string GetCountSelectStatement(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(" COUNT(*) FROM {0} WHERE {1}", this.GetQuotedName(entityInfo.MappingTableAttribute.TableName), query));
        }
Пример #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);
        }
Пример #3
0
        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));
        }
Пример #4
0
        /// <summary>
        /// 构建无where条件查询的sql语句
        /// </summary>
        /// <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)));
        }
Пример #5
0
        internal static List <SchemaItem> GetSchemaItems(Type objectType)
        {
            TypeSchema entityInfo = GetTypeSchema(objectType);

            return(entityInfo.GetAllFieldInfos());
        }