Exemple #1
0
            /// <summary>
            /// qubo 拓展
            /// </summary>
            /// <param name="prop"></param>
            /// <param name="entityConfig"></param>
            public Column(PropertyInfo prop, EntityTypeConfigration entityConfig)
            {
                var colAttr = (ColumnAttribute)prop.GetCustomAttributes(typeof(ColumnAttribute), true).FirstOrDefault();

                _prop = prop;
                Name  = colAttr == null ? prop.Name : colAttr.Name;
                //If this type is Nullable<T> then Nullable.GetUnderlyingType returns the T, otherwise it returns null, so get the the actual type instead
                ColumnType      = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                Collation       = Orm.Collation(prop);
                IsAutoInc       = Orm.IsAutoInc(prop);
                IsPK            = Orm.IsPK(prop);
                Indices         = Orm.GetIndices(prop);
                IsNullable      = !IsPK;
                MaxStringLength = Orm.MaxStringLength(prop);


                if (!string.IsNullOrEmpty(entityConfig.PK) && entityConfig.IsAutoPK)
                {
                    IsAutoInc = true;
                }
                if (!string.IsNullOrEmpty(entityConfig.PK))
                {
                    IsPK = true;
                }
            }
Exemple #2
0
        /// <summary>
        /// Retrieves the mapping that is automatically generated for the given type.
        /// </summary>
        /// <param name="type">
        /// The type whose mapping to the database is returned.
        /// </param>
        /// <returns>
        /// The mapping represents the schema of the columns of the database and contains
        /// methods to set and get properties of objects.
        /// </returns>
        public TableMapping GetMapping(Type type)
        {
            if (_mappings == null)
            {
                _mappings = new Dictionary <string, TableMapping>();
            }
            TableMapping map;

            if (!_mappings.TryGetValue(type.FullName, out map))
            {
                EntityTypeConfigration entity = EntityConfigrationList.FirstOrDefault(t => t.FullName == type.FullName);
                if (entity != null)
                {
                    map = new TableMapping(type, entity);
                }
                else
                {
                    map = new TableMapping(type);
                }

                _mappings[type.FullName] = map;
            }
            return(map);
        }
Exemple #3
0
        /// <summary>
        /// qubo  新增的方法
        /// </summary>
        /// <param name="type"></param>
        /// <param name="entityConfig"></param>
        public TableMapping(Type type, EntityTypeConfigration entityConfig)
        {
            MappedType = type;

#if NETFX_CORE
            var tableAttr = (TableAttribute)System.Reflection.CustomAttributeExtensions
                            .GetCustomAttribute(type.GetTypeInfo(), typeof(TableAttribute), true);
#else
            var tableAttr = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault();
#endif

            TableName = tableAttr != null ? tableAttr.Name : MappedType.Name;

#if !NETFX_CORE
            var props = MappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
#else
            var props = from p in MappedType.GetRuntimeProperties()
                        where ((p.GetMethod != null && p.GetMethod.IsPublic) || (p.SetMethod != null && p.SetMethod.IsPublic) || (p.GetMethod != null && p.GetMethod.IsStatic) || (p.SetMethod != null && p.SetMethod.IsStatic))
                        select p;
#endif
            var cols = new List <Column>();
            foreach (var p in props)
            {
#if !NETFX_CORE
                var ignore = p.GetCustomAttributes(typeof(IgnoreAttribute), true).Length > 0;
#else
                var ignore = p.GetCustomAttributes(typeof(IgnoreAttribute), true).Count() > 0;
#endif
                if (entityConfig.IgnoreList.Contains(p.Name))
                {
                    ignore = true;
                }
                if (p.CanWrite && !ignore)
                {
                    if (entityConfig.PK == p.Name)
                    {
                        cols.Add(new Column(p, entityConfig));
                    }
                    else
                    {
                        cols.Add(new Column(p));
                    }
                }
            }
            Columns = cols.ToArray();
            foreach (var c in Columns)
            {
                if (c.IsAutoInc && c.IsPK)
                {
                    _autoPk = c;
                }
                if (c.IsPK)
                {
                    PK = c;
                }
            }

            HasAutoIncPK = _autoPk != null;

            if (PK != null)
            {
                GetByPrimaryKeySql = string.Format("select * from \"{0}\" where \"{1}\" = ?", TableName, PK.Name);
            }
            else
            {
                // People should not be calling Get/Find without a PK
                GetByPrimaryKeySql = string.Format("select * from \"{0}\" limit 1", TableName);
            }
        }