示例#1
0
        public void ToObject <T>(IDataReader reader, T item) where T : DatabaseEntity, new()
        {
            if (reader == null)
            {
                return;
            }

            int len = reader.FieldCount;

            string[] propertyNames = new string[len];

            DatabaseEntityDef definition = _modelDefFactory.GetDef <T>();

            for (int i = 0; i < len; ++i)
            {
                propertyNames[i] = reader.GetName(i);
            }

            if (reader.Read())
            {
                for (int i = 0; i < len; ++i)
                {
                    DatabaseEntityPropertyDef property = definition.GetProperty(propertyNames[i]);

                    object value = property.TypeConverter == null?
                                   ValueConverter.DbValueToTypeValue(property.PropertyType, reader[i]) :
                                       property.TypeConverter.DbValueToTypeValue(reader[i]);

                    property.SetValue(item, value);
                }
            }
        }
示例#2
0
        public IList <T> ToList <T>(IDataReader reader)
            where T : DatabaseEntity, new()
        {
            IList <T> lst = new List <T>();

            if (reader == null)
            {
                return(lst);
            }

            int len = reader.FieldCount;

            string[] propertyNames = new string[len];


            DatabaseEntityDef definition = _modelDefFactory.GetDef <T>();

            for (int i = 0; i < len; ++i)
            {
                propertyNames[i] = reader.GetName(i);
            }

            while (reader.Read())
            {
                T item = new T();

                for (int i = 0; i < len; ++i)
                {
                    DatabaseEntityPropertyDef property = definition.GetProperty(propertyNames[i]);
                    object fieldValue = reader[i];

                    if (property.PropertyName == "Id" && fieldValue == DBNull.Value)
                    {
                        item = null;
                        break;
                    }

                    object value = property.TypeConverter == null?
                                   ValueConverter.DbValueToTypeValue(property.PropertyType, fieldValue) :
                                       property.TypeConverter.DbValueToTypeValue(fieldValue);

                    property.SetValue(item, value);
                }

                if (item != null && !item.Deleted)
                {
                    lst.Add(item);
                }
            }

            return(lst);
        }
        private DatabaseEntityDef CreateModelDef(Type modelType)
        {
            DatabaseEntityDef modelDef = new DatabaseEntityDef();

            #region 自身

            modelDef.EntityType     = modelType;
            modelDef.EntityFullName = modelType.FullName;
            //modelDef.PropertyDict = new Dictionary<string, DatabaseEntityPropertyDef>();

            #endregion

            #region 数据库

            if (_entitySchemaDict.TryGetValue(modelType.FullName, out EntitySchema dbSchema))
            {
                modelDef.IsTableModel        = true;
                modelDef.DatabaseName        = dbSchema.DatabaseName;
                modelDef.TableName           = dbSchema.TableName;
                modelDef.DbTableDescription  = dbSchema.Description;
                modelDef.DbTableReservedName = _databaseEngine.GetReservedStatement(modelDef.TableName);
                modelDef.DatabaseWriteable   = !dbSchema.ReadOnly;
            }
            else
            {
                modelDef.IsTableModel = false;
            }

            #endregion

            #region 属性

            foreach (PropertyInfo info in modelType.GetTypeInfo().GetProperties())
            {
                IEnumerable <Attribute> atts2 = info.GetCustomAttributes(typeof(EntityPropertyIgnoreAttribute), false).Select <object, Attribute>(o => (Attribute)o);

                if (atts2 == null || atts2.Count() == 0)
                {
                    DatabaseEntityPropertyDef propertyDef = CreatePropertyDef(modelDef, info);

                    modelDef.PropertyDict.Add(propertyDef.PropertyName, propertyDef);

                    modelDef.FieldCount++;
                }
            }

            #endregion

            return(modelDef);
        }
        private DatabaseEntityPropertyDef CreatePropertyDef(DatabaseEntityDef modelDef, PropertyInfo info)
        {
            DatabaseEntityPropertyDef propertyDef = new DatabaseEntityPropertyDef();

            #region 自身

            propertyDef.EntityDef    = modelDef;
            propertyDef.PropertyName = info.Name;
            propertyDef.PropertyType = info.PropertyType;
            propertyDef.GetMethod    = info.GetGetMethod();
            propertyDef.SetMethod    = info.GetSetMethod();

            #endregion

            #region 数据库

            IEnumerable <Attribute> propertyAttrs = info.GetCustomAttributes(typeof(EntityPropertyAttribute), false).Select(o => (Attribute)o);
            if (propertyAttrs != null && propertyAttrs.Count() > 0)
            {
                EntityPropertyAttribute propertyAttr = propertyAttrs.ElementAt(0) as EntityPropertyAttribute;

                propertyDef.IsTableProperty = true;
                propertyDef.IsNullable      = !propertyAttr.NotNull;
                propertyDef.IsUnique        = propertyAttr.Unique;
                propertyDef.DbLength        = propertyAttr.Length > 0 ? (int?)propertyAttr.Length : null;
                propertyDef.IsLengthFixed   = propertyAttr.FixedLength;
                propertyDef.DbDefaultValue  = ValueConverter.TypeValueToDbValue(propertyAttr.DefaultValue);
                propertyDef.DbDescription   = propertyAttr.Description;

                if (propertyAttr.ConverterType != null)
                {
                    propertyDef.TypeConverter = _typeConverterFactory.GetTypeConverter(propertyAttr.ConverterType);
                }
            }

            //判断是否是主键
            IEnumerable <Attribute> atts1 = info.GetCustomAttributes(typeof(AutoIncrementPrimaryKeyAttribute), false).Select <object, Attribute>(o => (Attribute)o);
            if (atts1 != null && atts1.Count() > 0)
            {
                propertyDef.IsTableProperty           = true;
                propertyDef.IsAutoIncrementPrimaryKey = true;
                propertyDef.IsNullable   = false;
                propertyDef.IsForeignKey = false;
                propertyDef.IsUnique     = true;
            }
            else
            {
                //判断是否外键
                IEnumerable <Attribute> atts2 = info.GetCustomAttributes(typeof(ForeignKeyAttribute), false).Select <object, Attribute>(o => (Attribute)o);
                if (atts2 != null && atts2.Count() > 0)
                {
                    propertyDef.IsTableProperty           = true;
                    propertyDef.IsAutoIncrementPrimaryKey = false;
                    propertyDef.IsForeignKey = true;
                    //propertyDef.IsNullable = false;
                    propertyDef.IsUnique = false;
                }
            }

            if (propertyDef.IsTableProperty)
            {
                propertyDef.DbReservedName      = _databaseEngine.GetReservedStatement(propertyDef.PropertyName);
                propertyDef.DbParameterizedName = _databaseEngine.GetParameterizedStatement(propertyDef.PropertyName);

                if (propertyDef.TypeConverter != null)
                {
                    propertyDef.DbFieldType = propertyDef.TypeConverter.TypeToDbType(propertyDef.PropertyType);
                }
                else
                {
                    propertyDef.DbFieldType = _databaseEngine.GetDbType(propertyDef.PropertyType);
                }
            }

            #endregion

            return(propertyDef);
        }
示例#5
0
        public IList <Tuple <TSource, TTarget> > ToList <TSource, TTarget>(IDataReader reader)
            where TSource : DatabaseEntity, new()
            where TTarget : DatabaseEntity, new()
        {
            IList <Tuple <TSource, TTarget> > lst = new List <Tuple <TSource, TTarget> >();

            if (reader == null)
            {
                return(lst);
            }

            DatabaseEntityDef definition1 = _modelDefFactory.GetDef <TSource>();
            DatabaseEntityDef definition2 = _modelDefFactory.GetDef <TTarget>();

            string[] propertyNames1 = new string[definition1.FieldCount];
            string[] propertyNames2 = new string[definition2.FieldCount];

            int j = 0;

            for (int i = 0; i < definition1.FieldCount; ++j, ++i)
            {
                propertyNames1[i] = reader.GetName(j);
            }

            for (int i = 0; i < definition2.FieldCount; ++j, ++i)
            {
                propertyNames2[i] = reader.GetName(j);
            }

            while (reader.Read())
            {
                TSource t1 = new TSource();
                TTarget t2 = new TTarget();

                j = 0;

                for (int i = 0; i < definition1.FieldCount; ++i, ++j)
                {
                    DatabaseEntityPropertyDef pDef = definition1.GetProperty(propertyNames1[i]);
                    object fieldValue = reader[j];

                    if (pDef.PropertyName == "Id" && fieldValue == DBNull.Value)
                    {
                        t1 = null;
                        break;
                    }

                    if (pDef != null)
                    {
                        object value = pDef.TypeConverter == null?
                                       ValueConverter.DbValueToTypeValue(pDef.PropertyType, fieldValue) :
                                           pDef.TypeConverter.DbValueToTypeValue(fieldValue);

                        pDef.SetValue(t1, value);
                    }
                }

                for (int i = 0; i < definition2.FieldCount; ++i, ++j)
                {
                    DatabaseEntityPropertyDef pDef = definition2.GetProperty(propertyNames2[i]);
                    object fieldValue = reader[j];

                    if (pDef.PropertyName == "Id" && fieldValue == DBNull.Value)
                    {
                        t2 = null;
                        break;
                    }

                    if (pDef != null)
                    {
                        object value = pDef.TypeConverter == null?
                                       ValueConverter.DbValueToTypeValue(pDef.PropertyType, fieldValue) :
                                           pDef.TypeConverter.DbValueToTypeValue(fieldValue);

                        pDef.SetValue(t2, value);
                    }
                }

                if (t1 != null && t1.Deleted)
                {
                    t1 = null;
                }
                if (t2 != null && t2.Deleted)
                {
                    t2 = null;
                }

                //删除全为空
                if (t1 != null || t2 != null)
                {
                    lst.Add(new Tuple <TSource, TTarget>(t1, t2));
                }
            }

            return(lst);
        }