Beispiel #1
0
 protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
 {
     this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
     if (!item.IsPrimarykey)
     {
         this.Context.DbMaintenance.DropConstraint(tableName, null);
     }
     if (item.IsPrimarykey)
     {
         this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
     }
 }
Beispiel #2
0
 protected virtual void GetDbType(EntityColumnInfo item, Type propertyType, DbColumnInfo result)
 {
     if (!string.IsNullOrEmpty(item.DataType))
     {
         result.DataType = item.DataType;
     }
     else if (propertyType.IsEnum())
     {
         result.DataType = this.Context.Ado.DbBind.GetDbTypeName(item.Length > 9 ? UtilConstants.LongType.Name : UtilConstants.IntType.Name);
     }
     else
     {
         result.DataType = this.Context.Ado.DbBind.GetDbTypeName(propertyType.Name);
     }
 }
Beispiel #3
0
        protected virtual void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
        {
            string constraintName = string.Format("PK_{0}_{1}", tableName, item.DbColumnName);

            if (this.Context.DbMaintenance.IsAnyConstraint(constraintName))
            {
                this.Context.DbMaintenance.DropConstraint(tableName, constraintName);
            }
            this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName);
            this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
            if (item.IsPrimarykey)
            {
                this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
            }
        }
        private void BindField(ILGenerator generator, LocalBuilder result, EntityColumnInfo columnInfo, string fieldName)
        {
            int   i          = DataRecord.GetOrdinal(fieldName);
            Label endIfLabel = generator.DefineLabel();

            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Ldc_I4, i);
            generator.Emit(OpCodes.Callvirt, isDBNullMethod);
            generator.Emit(OpCodes.Brtrue, endIfLabel);
            generator.Emit(OpCodes.Ldloc, result);
            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Ldc_I4, i);
            BindMethod(generator, columnInfo, i);
            generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod());
            generator.MarkLabel(endIfLabel);
        }
 private void BindClass(ILGenerator generator, LocalBuilder result, EntityColumnInfo columnInfo, string fieldName)
 {
     if (columnInfo.IsJson)
     {
         MethodInfo jsonMethod = getJson.MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
         int        i          = DataRecord.GetOrdinal(fieldName);
         Label      endIfLabel = generator.DefineLabel();
         generator.Emit(OpCodes.Ldarg_0);
         generator.Emit(OpCodes.Ldc_I4, i);
         generator.Emit(OpCodes.Callvirt, isDBNullMethod);
         generator.Emit(OpCodes.Brtrue, endIfLabel);
         generator.Emit(OpCodes.Ldloc, result);
         generator.Emit(OpCodes.Ldarg_0);
         generator.Emit(OpCodes.Ldc_I4, i);
         generator.Emit(OpCodes.Call, jsonMethod);
         generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod());
         generator.MarkLabel(endIfLabel);
     }
 }
Beispiel #6
0
        protected virtual bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc)
        {
            if (!string.IsNullOrEmpty(ec.DataType))
            {
                return(ec.DataType != dc.DataType);
            }
            var propertyType    = UtilMethods.GetUnderType(ec.PropertyInfo);
            var properyTypeName = string.Empty;

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

            return(properyTypeName != dataType);
        }
        private EntityInfo CopyEntityInfo(EntityInfo entityInfo)
        {
            EntityInfo result = new EntityInfo()
            {
                DbTableName = entityInfo.DbTableName,
                EntityName  = entityInfo.EntityName,
                Type        = entityInfo.Type
            };
            List <EntityColumnInfo> columns = new List <EntityColumnInfo>();

            if (entityInfo.Columns.HasValue())
            {
                foreach (var item in entityInfo.Columns)
                {
                    EntityColumnInfo column = new EntityColumnInfo()
                    {
                        ColumnDescription  = item.ColumnDescription,
                        DataType           = item.DataType,
                        DbColumnName       = item.DbColumnName,
                        DbTableName        = item.DbTableName,
                        DecimalDigits      = item.DecimalDigits,
                        DefaultValue       = item.DefaultValue,
                        EntityName         = item.EntityName,
                        IsIdentity         = item.IsIdentity,
                        IsIgnore           = item.IsIgnore,
                        IsNullable         = item.IsNullable,
                        IsOnlyIgnoreInsert = item.IsOnlyIgnoreInsert,
                        IsPrimarykey       = item.IsPrimarykey,
                        Length             = item.Length,
                        OldDbColumnName    = item.OldDbColumnName,
                        OracleSequenceName = item.OracleSequenceName,
                        PropertyInfo       = item.PropertyInfo,
                        PropertyName       = item.PropertyName
                    };
                    columns.Add(item);
                }
            }
            result.Columns = columns;
            return(result);
        }
 protected override void GetDbType(EntityColumnInfo item, Type propertyType, DbColumnInfo result)
 {
     if (!string.IsNullOrEmpty(item.DataType))
     {
         result.DataType = item.DataType;
     }
     else if (propertyType.IsEnum())
     {
         result.DataType = this.Context.Ado.DbBind.GetDbTypeName(item.Length > 9 ? UtilConstants.LongType.Name : UtilConstants.IntType.Name);
     }
     else
     {
         if (propertyType.Name.Equals("Guid", StringComparison.CurrentCultureIgnoreCase))
         {
             result.DataType = this.Context.Ado.DbBind.GetDbTypeName(UtilConstants.StringType.Name);
         }
         else
         {
             result.DataType = this.Context.Ado.DbBind.GetDbTypeName(propertyType.Name);
         }
     }
 }
 protected override void KeyAction(EntityColumnInfo item, DbColumnInfo dbColumn, out bool pkDiff, out bool idEntityDiff)
 {
     pkDiff       = item.IsPrimarykey != dbColumn.IsPrimarykey;
     idEntityDiff = false;
 }
Beispiel #10
0
        protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
        {
            var propertyType = UtilMethods.GetUnderType(item.PropertyInfo);
            var result       = new DbColumnInfo()
            {
                TableId           = entityInfo.Columns.IndexOf(item),
                DbColumnName      = item.DbColumnName.HasValue() ? item.DbColumnName : item.PropertyName,
                IsPrimarykey      = item.IsPrimarykey,
                IsIdentity        = item.IsIdentity,
                TableName         = tableName,
                IsNullable        = item.IsNullable,
                DefaultValue      = item.DefaultValue,
                ColumnDescription = item.ColumnDescription,
                Length            = item.Length
            };

            GetDbType(item, propertyType, result);
            if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
            {
                result.Length = 1;
            }
            return(result);
        }
        private void BindMethod(ILGenerator generator, EntityColumnInfo columnInfo, int ordinal)
        {
            IDbBind    bind             = Context.Ado.DbBind;
            bool       isNullableType   = false;
            MethodInfo method           = null;
            Type       bindPropertyType = UtilMethods.GetUnderType(columnInfo.PropertyInfo, ref isNullableType);
            string     dbTypeName       = UtilMethods.GetParenthesesValue(DataRecord.GetDataTypeName(ordinal));

            if (dbTypeName.IsNullOrEmpty())
            {
                dbTypeName = bindPropertyType.Name;
            }
            string propertyName      = columnInfo.PropertyName;
            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 == UtilConstants.IntType)
                {
                    method = isNullableType ? getConvertInt32 : getInt32;
                }
                else if (bindPropertyType == UtilConstants.ByteType)
                {
                    method = isNullableType ? getConvertByte : getByte;
                }
                else if (bindPropertyType == UtilConstants.StringType)
                {
                    method = getString;
                }
                else
                {
                    method = getConvertValueMethod.MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
                }

                if (method.IsVirtual)
                {
                    generator.Emit(OpCodes.Callvirt, method);
                }
                else
                {
                    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;
                }
                if (bindProperyTypeName == "datetime" && dbTypeName.ToLower() == "time")
                {
                    method = isNullableType ? getConvertTime : getTime;
                }
                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.IsIn("double", "single") && dbTypeName != "real")
                {
                    method = isNullableType ? getConvertDouble : getDouble;
                }
                else
                {
                    method = isNullableType ? getConvertFloat : getFloat;
                }
                if (dbTypeName.Equals("float", StringComparison.CurrentCultureIgnoreCase) && isNullableType && bindProperyTypeName.Equals("single", StringComparison.CurrentCultureIgnoreCase))
                {
                    method = getConvertDoubleToFloat;
                }
                if (bindPropertyType == UtilConstants.DecType)
                {
                    method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
                }
                if (bindPropertyType == UtilConstants.IntType)
                {
                    method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
                }
                break;

            case CSharpDataType.Guid:
                CheckType(bind.GuidThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "guid")
                {
                    method = isNullableType ? getConvertGuid : getGuid;
                }
                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;

            case CSharpDataType.DateTimeOffset:
                method = isNullableType ? getConvertdatetimeoffset : getdatetimeoffset;
                if (bindProperyTypeName == "datetime")
                {
                    method = isNullableType ? getConvertdatetimeoffsetDate : getdatetimeoffsetDate;
                }
                break;

            default:
                method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
                break;
            }
            if (method == null && bindPropertyType == UtilConstants.StringType)
            {
                method = getConvertString;
            }
            if (bindPropertyType == UtilConstants.ObjType)
            {
                method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
            }
            if (method == null)
            {
                method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
            }

            if (method.IsVirtual)
            {
                generator.Emit(OpCodes.Callvirt, method);
            }
            else
            {
                generator.Emit(OpCodes.Call, method);
            }
            #endregion
        }
Beispiel #12
0
        protected virtual DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
        {
            var propertyType = UtilMethods.GetUnderType(item.PropertyInfo);
            var result       = new DbColumnInfo()
            {
                TableId           = entityInfo.Columns.IndexOf(item),
                DbColumnName      = item.DbColumnName.HasValue() ? item.DbColumnName : item.PropertyName,
                IsPrimarykey      = item.IsPrimarykey,
                IsIdentity        = item.IsIdentity,
                TableName         = tableName,
                IsNullable        = item.IsNullable,
                DefaultValue      = item.DefaultValue,
                ColumnDescription = item.ColumnDescription,
                Length            = item.Length,
                DecimalDigits     = item.DecimalDigits
            };

            GetDbType(item, propertyType, result);
            return(result);
        }
Beispiel #13
0
 protected virtual void KeyAction(EntityColumnInfo item, DbColumnInfo dbColumn, out bool pkDiff, out bool idEntityDiff)
 {
     pkDiff       = item.IsPrimarykey != dbColumn.IsPrimarykey;
     idEntityDiff = item.IsIdentity != dbColumn.IsIdentity;
 }
 private void SetColumns(EntityInfo result)
 {
     foreach (var property in result.Type.GetProperties())
     {
         EntityColumnInfo column = new EntityColumnInfo();
         //var isVirtual = property.GetGetMethod().IsVirtual;
         //if (isVirtual) continue;
         var sugarColumn = property.GetCustomAttributes(typeof(SugarColumn), true)
                           .Where(it => it is SugarColumn)
                           .Select(it => (SugarColumn)it)
                           .FirstOrDefault();
         column.DbTableName  = result.DbTableName;
         column.EntityName   = result.EntityName;
         column.PropertyName = property.Name;
         column.PropertyInfo = property;
         if (sugarColumn.IsNullOrEmpty())
         {
             column.DbColumnName = property.Name;
         }
         else
         {
             if (sugarColumn.IsIgnore == false)
             {
                 column.DbColumnName       = sugarColumn.ColumnName.IsNullOrEmpty() ? property.Name : sugarColumn.ColumnName;
                 column.IsPrimarykey       = sugarColumn.IsPrimaryKey;
                 column.IsIdentity         = sugarColumn.IsIdentity;
                 column.ColumnDescription  = sugarColumn.ColumnDescription;
                 column.IsNullable         = sugarColumn.IsNullable;
                 column.Length             = sugarColumn.Length;
                 column.OldDbColumnName    = sugarColumn.OldColumnName;
                 column.DataType           = sugarColumn.ColumnDataType;
                 column.DecimalDigits      = sugarColumn.DecimalDigits;
                 column.OracleSequenceName = sugarColumn.OracleSequenceName;
                 column.IsOnlyIgnoreInsert = sugarColumn.IsOnlyIgnoreInsert;
                 column.IsEnableUpdateVersionValidation = sugarColumn.IsEnableUpdateVersionValidation;
                 column.IsTranscoding           = sugarColumn.IsTranscoding;
                 column.SerializeDateTimeFormat = sugarColumn.SerializeDateTimeFormat;
                 column.IsJson             = sugarColumn.IsJson;
                 column.NoSerialize        = sugarColumn.NoSerialize;
                 column.DefaultValue       = sugarColumn.DefaultValue;
                 column.IndexGroupNameList = sugarColumn.IndexGroupNameList;
                 column.IsOnlyIgnoreUpdate = sugarColumn.IsOnlyIgnoreUpdate;
             }
             else
             {
                 column.IsIgnore    = true;
                 column.NoSerialize = sugarColumn.NoSerialize;
             }
         }
         if (this.Context.MappingColumns.HasValue())
         {
             var golbalMappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName.Equals(result.EntityName, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName == column.PropertyName);
             if (golbalMappingInfo != null)
             {
                 column.DbColumnName = golbalMappingInfo.DbColumnName;
             }
         }
         if (this.Context.IgnoreColumns.HasValue())
         {
             var golbalMappingInfo = this.Context.IgnoreColumns.FirstOrDefault(it => it.EntityName.Equals(result.EntityName, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName == column.PropertyName);
             if (golbalMappingInfo != null)
             {
                 column.IsIgnore = true;
             }
         }
         if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityService != null)
         {
             this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityService(property, column);
         }
         result.Columns.Add(column);
     }
 }
 internal DbColumnInfo GetEntityColumnToDbColumn(EntityInfo entity, string dbTableName, EntityColumnInfo item)
 {
     return(EntityColumnToDbColumn(entity, dbTableName, item));
 }