Пример #1
0
        public VendorDbTypeInfo AddType(string typeName, DbType dbType, Type columnOutType,
                                        bool isDefault   = true, bool supportsMemo   = false, bool isSubType = false,
                                        string args      = null, string memoArgs     = null,
                                        Type[] clrTypes  = null, Type dbFirstClrType = null, string aliases = null, string columnInit = null,
                                        int vendorDbType = -1, DbValueToLiteralConvertFunc valueToLiteral = null)
        {
            //validate that we have only one default type for clr type
            if (isDefault && clrTypes != null)
            {
                foreach (var clrType in clrTypes)
                {
                    var oldType = Types.FirstOrDefault(td => td.ClrTypes.Contains(clrType) && td.Flags.IsSet(VendorDbTypeFlags.IsDefaultForClrType));
                    Util.Check(oldType == null, "There is already Db type definition registered as default for CLR type {0}.", columnOutType);
                }
            }
            typeName = typeName.ToLowerInvariant();
            var flags = VendorDbTypeFlags.None;

            if (isDefault)
            {
                flags |= VendorDbTypeFlags.IsDefaultForClrType;
            }
            if (supportsMemo)
            {
                flags |= VendorDbTypeFlags.SupportsUnlimitedSize;
            }
            if (isSubType)
            {
                flags |= VendorDbTypeFlags.IsSubType;
            }

            columnInit = columnInit ?? GetDefaultColumnInitExpression(columnOutType);
            var typeDef = new VendorDbTypeInfo(typeName, dbType, columnOutType, args, memoArgs, clrTypes, dbFirstClrType,
                                               flags, aliases, valueToLiteral, vendorDbType, columnInit);

            Types.Add(typeDef);
            return(typeDef);
        }
Пример #2
0
        public virtual DbTypeInfo GetDbTypeInfo(EntityMemberInfo member, SystemLog log)
        {
            bool isUnlimited = member.Flags.IsSet(EntityMemberFlags.UnlimitedSize);
            //Storage data type is derived from member data type. For Nullable<T>, it is underlying type T; for enums, it is underlying int type
            var columnType = GetUnderlyingClrType(member.DataType);
            //Find DbTypeDefinition
            VendorDbTypeInfo vendorType = null;
            //Check if DbTypeSpec is provided explicitly through attribute
            string typeSpec = null;

            if (!string.IsNullOrWhiteSpace(member.ExplicitDbTypeSpec))
            {
                typeSpec = member.ExplicitDbTypeSpec;
                string typeName;
                string typeArgs = null;
                UnpackTypeSpec(member.ExplicitDbTypeSpec, out typeName, out typeArgs);
                if (AutoMemoTypes.Contains(typeName) && member.Size == 0)
                {
                    //if size was not specified explicitly, mark member/column as memo
                    isUnlimited   = true;
                    member.Flags |= EntityMemberFlags.UnlimitedSize;
                    member.Size   = -1;
                }
                vendorType = FindVendorDbTypeInfo(typeName);
            }
            else
            //Otherwise check if DbType is provided explicitly
            if (member.ExplicitDbType != null)
            {
                vendorType = FindVendorDbTypeInfo(member.ExplicitDbType.Value, columnType, isUnlimited);
            }
            else
            {
                // Otherwise find default for Clr type.
                vendorType = FindVendorDbTypeInfo(columnType, isUnlimited);
            }
            if (vendorType == null)
            {
                return(null);
            }
            //create db typeinfo
            //figure out type arguments and format them
            typeSpec = typeSpec ?? vendorType.FormatTypeSpec(member.Size, member.Precision, member.Scale, isUnlimited);
            var isNullable = member.Flags.IsSet(EntityMemberFlags.Nullable);
            var colDefault = member.ColumnDefault ?? vendorType.DefaultColumnInit;
            var typeInfo   = new DbTypeInfo(vendorType, typeSpec, isNullable, member.Size, member.Precision, member.Scale, colDefault);

            // Assign value converters
            if (vendorType.ColumnOutType != member.DataType)
            {
                var conv = Converters.GetConverter(typeInfo.VendorDbType.ColumnOutType, member.DataType);
                if (conv == null)
                {
                    log.Error("Converter from type {0} to type {1} not found.", typeInfo.VendorDbType.ColumnOutType, member.DataType);
                }
                else
                {
                    typeInfo.ColumnToPropertyConverter = conv.ColumnToProperty;
                    typeInfo.PropertyToColumnConverter = conv.PropertyToColumn;
                }
            }
            return(typeInfo);
        }