Exemple #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);
     }
 }
        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);
            }
        }
 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);
     }
 }
Exemple #4
0
        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(true));
            generator.MarkLabel(endIfLabel);
        }
Exemple #5
0
 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(true));
         generator.MarkLabel(endIfLabel);
     }
 }
        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);
        }
 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);
         }
     }
 }
        public string DeleteByColumns(Type type, string tableName, ICollection <string> columnNames)
        {
            var key = $"{nameof(DeleteByColumns)}_{type.FullName}_{tableName}_{string.Join(",", columnNames)}";

            return(_sqlsCache.GetOrAdd(key, () =>
            {
                var tableInfo = _mapper.GetEntityTableInfo(type);
                EntityColumnInfo pkInfo = null;
                var paramPrefix = _sqlAdapter.GetParameterPrefix();

                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = _sqlAdapter.EscapeTableName(tableInfo.TableName);
                }
                var where = string.Join(" and ", columnNames.Select(p =>
                {
                    var colName = p ?? (pkInfo ?? (pkInfo = GetPrimaryKey(tableInfo))).ColumnName;
                    return $"{_sqlAdapter.EscapeSqlIdentifier(colName)} = {paramPrefix}{colName}";
                }));

                return $"delete from {tableName} where {where}";
            }));
        }
 protected override void KeyAction(EntityColumnInfo item, DbColumnInfo dbColumn, out bool pkDiff, out bool idEntityDiff)
 {
     pkDiff       = item.IsPrimarykey != dbColumn.IsPrimarykey;
     idEntityDiff = false;
 }
Exemple #10
0
 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 agileColumn = property.GetCustomAttributes(typeof(AgileColumn), true)
                           .Where(it => it is AgileColumn)
                           .Select(it => (AgileColumn)it)
                           .FirstOrDefault();
         column.DbTableName  = result.DbTableName;
         column.EntityName   = result.EntityName;
         column.PropertyName = property.Name;
         column.PropertyInfo = property;
         if (agileColumn.IsNullOrEmpty())
         {
             column.DbColumnName = property.Name;
         }
         else
         {
             if (agileColumn.IsIgnore == false)
             {
                 column.DbColumnName       = agileColumn.ColumnName.IsNullOrEmpty() ? property.Name : agileColumn.ColumnName;
                 column.IsPrimarykey       = agileColumn.IsPrimaryKey;
                 column.IsIdentity         = agileColumn.IsIdentity;
                 column.ColumnDescription  = agileColumn.ColumnDescription;
                 column.IsNullable         = agileColumn.IsNullable;
                 column.Length             = agileColumn.Length;
                 column.OldDbColumnName    = agileColumn.OldColumnName;
                 column.DataType           = agileColumn.ColumnDataType;
                 column.DecimalDigits      = agileColumn.DecimalDigits;
                 column.OracleSequenceName = agileColumn.OracleSequenceName;
                 column.IsOnlyIgnoreInsert = agileColumn.IsOnlyIgnoreInsert;
                 column.IsEnableUpdateVersionValidation = agileColumn.IsEnableUpdateVersionValidation;
                 column.IsTranscoding           = agileColumn.IsTranscoding;
                 column.SerializeDateTimeFormat = agileColumn.SerializeDateTimeFormat;
                 column.IsJson             = agileColumn.IsJson;
                 column.NoSerialize        = agileColumn.NoSerialize;
                 column.DefaultValue       = agileColumn.DefaultValue;
                 column.IndexGroupNameList = agileColumn.IndexGroupNameList;
                 column.IsOnlyIgnoreUpdate = agileColumn.IsOnlyIgnoreUpdate;
             }
             else
             {
                 column.IsIgnore    = true;
                 column.NoSerialize = agileColumn.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);
     }
 }
Exemple #11
0
 internal DbColumnInfo GetEntityColumnToDbColumn(EntityInfo entity, string dbTableName, EntityColumnInfo item)
 {
     return(EntityColumnToDbColumn(entity, dbTableName, item));
 }
Exemple #12
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,
                DecimalDigits     = item.DecimalDigits
            };

            GetDbType(item, propertyType, result);
            if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
            {
                result.Length = 1;
            }
            return(result);
        }
        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);
        }
 protected virtual void KeyAction(EntityColumnInfo item, DbColumnInfo dbColumn, out bool pkDiff, out bool idEntityDiff)
 {
     pkDiff       = item.IsPrimarykey != dbColumn.IsPrimarykey;
     idEntityDiff = item.IsIdentity != dbColumn.IsIdentity;
 }
Exemple #15
0
        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 == DatabaseType.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
        }