예제 #1
0
        protected virtual DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
        {
            var propertyType = PubMethod.GetUnderType(item.PropertyInfo);
            var result       = new DbColumnInfo()
            {
                TableId           = entityInfo.Columns.IndexOf(item),
                DbColumnName      = item.DbColumnName.IsValuable() ? item.DbColumnName : item.PropertyName,
                IsPrimarykey      = item.IsPrimarykey,
                IsIdentity        = item.IsIdentity,
                TableName         = tableName,
                IsNullable        = item.IsNullable,
                DefaultValue      = item.DefaultValue,
                ColumnDescription = item.ColumnDescription,
                Length            = item.Length
            };

            if (!string.IsNullOrEmpty(item.DataType))
            {
                result.DataType = item.DataType;
            }
            else if (propertyType.IsEnum())
            {
                result.DataType = this.Context.Ado.DbBind.GetDbTypeName(item.Length > 9 ? PubConst.LongType.Name : PubConst.IntType.Name);
            }
            else
            {
                result.DataType = this.Context.Ado.DbBind.GetDbTypeName(propertyType.Name);
            }
            return(result);
        }
예제 #2
0
        internal void Init()
        {
            InsertBuilder.EntityInfo = this.EntityInfo;
            Check.Exception(InsertObjs == null || InsertObjs.Count() == 0, "InsertObjs is null");
            int i = 0;

            foreach (var item in InsertObjs)
            {
                List <DbColumnInfo> insertItem = new List <DbColumnInfo>();
                foreach (var column in EntityInfo.Columns)
                {
                    var columnInfo = new DbColumnInfo()
                    {
                        Value        = column.PropertyInfo.GetValue(item, null),
                        DbColumnName = GetDbColumnName(column.PropertyName),
                        PropertyName = column.PropertyName,
                        PropertyType = PubMethod.GetUnderType(column.PropertyInfo),
                        TableId      = i
                    };
                    if (columnInfo.PropertyType.IsEnum())
                    {
                        columnInfo.Value = Convert.ToInt64(columnInfo.Value);
                    }
                    insertItem.Add(columnInfo);
                }
                this.InsertBuilder.DbColumnInfoList.AddRange(insertItem);
                ++i;
            }
        }
예제 #3
0
        public static Nullable <T> GetSqliteTypeNull <T>(this IDataReader dr, int i) where T : struct
        {
            var type = PubMethod.GetUnderType(typeof(T));

            if (dr.IsDBNull(i))
            {
                return(null);
            }
            return(SqliteTypeConvert <T>(dr, i, type));
        }
예제 #4
0
        protected List <T> GetValueTypeList <T>(Type type, IDataReader dataReader)
        {
            List <T> reval = new List <T>();

            using (IDataReader re = dataReader)
            {
                while (re.Read())
                {
                    var value = re.GetValue(0);
                    if (value == DBNull.Value)
                    {
                        reval.Add(default(T));
                    }
                    else
                    {
                        reval.Add((T)Convert.ChangeType(re.GetValue(0), PubMethod.GetUnderType(type)));
                    }
                }
            }
            return(reval);
        }
예제 #5
0
        protected virtual bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc)
        {
            if (!string.IsNullOrEmpty(ec.DataType))
            {
                return(ec.DataType != dc.DataType);
            }
            var propertyType    = PubMethod.GetUnderType(ec.PropertyInfo);
            var properyTypeName = string.Empty;

            if (propertyType.IsEnum())
            {
                properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(ec.Length > 9 ? PubConst.LongType.Name : PubConst.IntType.Name);
            }
            else
            {
                properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(propertyType.Name);
            }
            var dataType = dc.DataType;

            return(properyTypeName != dataType);
        }
예제 #6
0
        protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
        {
            var result = new DbColumnInfo()
            {
                DataType          = this.Context.Ado.DbBind.GetDbTypeName(PubMethod.GetUnderType(item.PropertyInfo).Name),
                TableId           = entityInfo.Columns.IndexOf(item),
                DbColumnName      = item.DbColumnName.IsValuable() ? item.DbColumnName : item.PropertyName,
                IsPrimarykey      = item.IsPrimarykey,
                IsIdentity        = item.IsIdentity,
                TableName         = tableName,
                IsNullable        = item.IsNullable,
                DefaultValue      = item.DefaultValue,
                ColumnDescription = item.ColumnDescription,
                Length            = item.Length
            };

            if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
            {
                result.Length = 1;
            }
            return(result);
        }
예제 #7
0
        internal void Init()
        {
            this.UpdateBuilder.TableName = EntityInfo.EntityName;
            if (IsMappingTable)
            {
                var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == EntityInfo.EntityName);
                if (mappingInfo != null)
                {
                    this.UpdateBuilder.TableName = mappingInfo.DbTableName;
                }
            }
            Check.Exception(UpdateObjs == null || UpdateObjs.Count() == 0, "UpdateObjs is null");
            int i = 0;

            foreach (var item in UpdateObjs)
            {
                List <DbColumnInfo> updateItem = new List <DbColumnInfo>();
                foreach (var column in EntityInfo.Columns)
                {
                    var columnInfo = new DbColumnInfo()
                    {
                        Value        = column.PropertyInfo.GetValue(item, null),
                        DbColumnName = GetDbColumnName(column.PropertyName),
                        PropertyName = column.PropertyName,
                        PropertyType = PubMethod.GetUnderType(column.PropertyInfo),
                        TableId      = i
                    };
                    if (columnInfo.PropertyType.IsEnum())
                    {
                        columnInfo.Value = Convert.ToInt64(columnInfo.Value);
                    }
                    updateItem.Add(columnInfo);
                }
                this.UpdateBuilder.DbColumnInfoList.AddRange(updateItem);
                ++i;
            }
        }
예제 #8
0
        private void BindMethod(ILGenerator generator, PropertyInfo bindProperty, int ordinal)
        {
            IDbBind    bind             = Context.Ado.DbBind;
            bool       isNullableType   = false;
            MethodInfo method           = null;
            Type       bindPropertyType = PubMethod.GetUnderType(bindProperty, ref isNullableType);
            string     dbTypeName       = DataRecord.GetDataTypeName(ordinal);

            if (Regex.IsMatch(dbTypeName, @"\(.+\)"))
            {
                dbTypeName = Regex.Replace(dbTypeName, @"\(.+\)", "");
            }
            string propertyName      = bindProperty.Name;
            string validPropertyName = bind.GetPropertyTypeName(dbTypeName);

            validPropertyName = validPropertyName == "byte[]" ? "byteArray" : validPropertyName;
            CSharpDataType validPropertyType = (CSharpDataType)Enum.Parse(typeof(CSharpDataType), validPropertyName);

            #region Sqlite Logic
            if (this.Context.CurrentConnectionConfig.DbType == DbType.Sqlite)
            {
                if (bindPropertyType.IsEnum())
                {
                    method = isNullableType ? getConvertEnum_Null.MakeGenericMethod(bindPropertyType) : getEnum.MakeGenericMethod(bindPropertyType);
                }
                else if (bindPropertyType == PubConst.IntType)
                {
                    method = isNullableType ? getConvertInt32 : getInt32;
                }
                else if (bindPropertyType == PubConst.StringType)
                {
                    method = getString;
                }
                else if (bindPropertyType == PubConst.ByteArrayType)
                {
                    method = getValueMethod;
                    generator.Emit(OpCodes.Call, method);
                    generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType);
                    return;
                }
                else
                {
                    method = isNullableType ? getSqliteTypeNull.MakeGenericMethod(bindPropertyType) : getSqliteType.MakeGenericMethod(bindPropertyType);
                }
                generator.Emit(OpCodes.Call, method);
                return;
            }
            ;
            #endregion

            #region Common Database Logic
            string bindProperyTypeName = bindPropertyType.Name.ToLower();
            bool   isEnum = bindPropertyType.IsEnum();
            if (isEnum)
            {
                validPropertyType = CSharpDataType.@enum;
            }
            switch (validPropertyType)
            {
            case CSharpDataType.@int:
                CheckType(bind.IntThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName.IsContainsIn("int", "int32"))
                {
                    method = isNullableType ? getConvertInt32 : getInt32;
                }
                if (bindProperyTypeName.IsContainsIn("int64"))
                {
                    method = isNullableType ? getConvertInt64 : getInt64;
                }
                if (bindProperyTypeName.IsContainsIn("byte"))
                {
                    method = isNullableType ? getConvertByte : getByte;
                }
                if (bindProperyTypeName.IsContainsIn("int16"))
                {
                    method = isNullableType ? getConvertInt16 : getInt16;
                }
                break;

            case CSharpDataType.@bool:
                if (bindProperyTypeName == "bool" || bindProperyTypeName == "boolean")
                {
                    method = isNullableType ? getConvertBoolean : getBoolean;
                }
                break;

            case CSharpDataType.@string:
                CheckType(bind.StringThrow, bindProperyTypeName, validPropertyName, propertyName);
                method = getString;
                if (bindProperyTypeName == "guid")
                {
                    method = isNullableType? getConvertStringGuid : getStringGuid;
                }
                break;

            case CSharpDataType.DateTime:
                CheckType(bind.DateThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "datetime")
                {
                    method = isNullableType ? getConvertDateTime : getDateTime;
                }
                break;

            case CSharpDataType.@decimal:
                CheckType(bind.DecimalThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "decimal")
                {
                    method = isNullableType ? getConvertDecimal : getDecimal;
                }
                break;

            case CSharpDataType.@float:
            case CSharpDataType.@double:
                CheckType(bind.DoubleThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "double")
                {
                    method = isNullableType ? getConvertDouble : getDouble;
                }
                break;

            case CSharpDataType.Guid:
                CheckType(bind.GuidThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "guid")
                {
                    method = isNullableType ? getConvertStringGuid : getStringGuid;
                }
                break;

            case CSharpDataType.@byte:
                if (bindProperyTypeName == "byte")
                {
                    method = isNullableType ? getConvertByte : getByte;
                }
                break;

            case CSharpDataType.@enum:
                method = isNullableType ? getConvertEnum_Null.MakeGenericMethod(bindPropertyType) : getEnum.MakeGenericMethod(bindPropertyType);
                break;

            case CSharpDataType.@short:
                CheckType(bind.ShortThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "int16" || bindProperyTypeName == "short")
                {
                    method = isNullableType ? getConvertInt16 : getInt16;
                }
                break;

            case CSharpDataType.@long:
                if (bindProperyTypeName == "int64" || bindProperyTypeName == "long")
                {
                    method = isNullableType ? getConvertInt64 : getInt64;
                }
                break;

            default:
                method = getValueMethod;
                break;
            }
            if (method == null && bindPropertyType == PubConst.StringType)
            {
                method = getConvertString;
            }
            if (method == null)
            {
                method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
            }
            generator.Emit(OpCodes.Call, method);
            if (method == getValueMethod)
            {
                generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType);
            }
            #endregion
        }
예제 #9
0
        public virtual void ExistLogic(EntityInfo entityInfo)
        {
            if (entityInfo.Columns.IsValuable())
            {
                Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");

                var tableName = GetTableName(entityInfo);
                var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName);
                ConvertColumns(dbColumns);
                var entityColumns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList();
                var dropColumns   = dbColumns
                                    .Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
                                    .Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))
                                    .ToList();
                var addColumns = entityColumns
                                 .Where(ec => ec.OldDbColumnName.IsNullOrEmpty() || !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
                                 .Where(ec => !dbColumns.Any(dc => ec.DbColumnName.Equals(dc.DbColumnName, StringComparison.CurrentCultureIgnoreCase))).ToList();
                var alterColumns = entityColumns
                                   .Where(ec => !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
                                   .Where(ec =>
                                          dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName) &&
                                                        ((ec.Length != dc.Length && !PubMethod.GetUnderType(ec.PropertyInfo).IsEnum() && PubMethod.GetUnderType(ec.PropertyInfo).IsIn(PubConst.StringType)) ||
                                                         ec.IsNullable != dc.IsNullable ||
                                                         IsSamgeType(ec, dc)))).ToList();
                var renameColumns = entityColumns
                                    .Where(it => !string.IsNullOrEmpty(it.OldDbColumnName))
                                    .Where(entityColumn => dbColumns.Any(dbColumn => entityColumn.OldDbColumnName.Equals(dbColumn.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))
                                    .ToList();


                var isChange = false;
                foreach (var item in addColumns)
                {
                    this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
                    isChange = true;
                }
                foreach (var item in dropColumns)
                {
                    this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName);
                    isChange = true;
                }
                foreach (var item in alterColumns)
                {
                    this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
                    isChange = true;
                }
                foreach (var item in renameColumns)
                {
                    this.Context.DbMaintenance.RenameColumn(tableName, item.OldDbColumnName, item.DbColumnName);
                    isChange = true;
                }

                foreach (var item in entityColumns)
                {
                    var dbColumn = dbColumns.FirstOrDefault(dc => dc.DbColumnName.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase));
                    if (dbColumn == null)
                    {
                        continue;
                    }
                    var pkDiff       = item.IsPrimarykey != dbColumn.IsPrimarykey;
                    var idEntityDiff = item.IsIdentity != dbColumn.IsIdentity;
                    if (dbColumn != null && pkDiff && !idEntityDiff)
                    {
                        var isAdd = item.IsPrimarykey;
                        if (isAdd)
                        {
                            this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
                        }
                        else
                        {
                            this.Context.DbMaintenance.DropConstraint(tableName, string.Format("PK_{0}_{1}", tableName, item.DbColumnName));
                        }
                    }
                    else if (pkDiff || idEntityDiff)
                    {
                        ChangeKey(entityInfo, tableName, item);
                    }
                }
                if (isChange && IsBackupTable)
                {
                    this.Context.DbMaintenance.BackupTable(tableName, tableName + DateTime.Now.ToString("yyyyMMddHHmmss"), MaxBackupDataRows);
                }
            }
        }