예제 #1
0
        public SQLiteTypeRegistry(SQLiteDbDriver driver) : base(driver)
        {
            var realTypes   = new Type[] { typeof(Single), typeof(double), typeof(decimal) };
            var stringTypes = new Type[] { typeof(string), typeof(char), /*typeof(DateTime), typeof(TimeSpan)*/ };
            var blobTypes   = new Type[] { typeof(Guid), typeof(Vita.Common.Binary), typeof(byte[]) };
            var intTypes    = new Type[] {
                typeof(byte), typeof(sbyte), typeof(Int16), typeof(UInt16), typeof(Int32), typeof(UInt32),
            };
            var int64Types = new Type[] { typeof(Int64), typeof(UInt64) };

            // Note: we associate multiple int types with single storage class, but intercept in particular cases
            // (in GetDbTypeInfo below), and provide specific DbType values and converters
            // We have to put Int32 into a separate class for enum types to work
            AddType("INT", DbType.Int32, typeof(Int32), clrTypes: intTypes, dbFirstClrType: typeof(Int32), aliases: "integer");
            AddType("INT64", DbType.Int64, typeof(Int64), clrTypes: int64Types, dbFirstClrType: typeof(Int64));
            AddType("REAL", DbType.Double, typeof(double), clrTypes: realTypes, dbFirstClrType: typeof(double));
            AddType("TEXT", DbType.String, typeof(string), supportsMemo: true, clrTypes: stringTypes, dbFirstClrType: typeof(string));
            AddType("BLOB", DbType.Binary, typeof(byte[]), supportsMemo: true, clrTypes: blobTypes,
                    dbFirstClrType: typeof(Vita.Common.Binary), valueToLiteral: ConvertBinaryToLiteral);
            AddType("Date", DbType.DateTime, typeof(DateTime));
            AddType("Time", DbType.DateTimeOffset, typeof(TimeSpan));
            AddType("Bool", DbType.Boolean, typeof(bool));

            Converters.AddConverter <string, TimeSpan>(x => TimeSpan.Parse((string)x), x => ((TimeSpan)x).ToString("G"));
            Converters.AddConverter <Int64, bool>(x => (Int64)x == 1, x => (bool)x ? 1L : 0L);
            RegisterAutoMemoTypes("TEXT", "BLOB");
        }
예제 #2
0
        public SqlCeTypeRegistry(SqlCeDbDriver driver) : base(driver)
        {
            //numerics
            AddType("real", DbType.Single, typeof(Single), SqlDbType.Real);
            AddType("float", DbType.Double, typeof(Double), SqlDbType.Float);
            AddType("decimal", DbType.Decimal, typeof(Decimal), SqlDbType.Decimal, args: "({precision},{scale})", aliases: "numeric");

            //integers
            // Note: for int types that do not have direct match in database type set, we assign bigger int type in database
            // For example, signed byte (CLR sbyte) does not have direct match, so we use 'smallint' to handle it in the database.
            // For ulong (UInt64) CLR type there's no bigger type, so we handle it using 'bigint' which is signed 64-bit int; as a result there might be overflows
            // in some cases.
            AddType("int", DbType.Int32, typeof(int), SqlDbType.Int, clrTypes: new Type[] { typeof(UInt16) });
            AddType("smallint", DbType.Int16, typeof(Int16), SqlDbType.SmallInt, clrTypes: new Type[] { typeof(sbyte) });
            AddType("tinyint", DbType.Byte, typeof(byte), SqlDbType.TinyInt);
            AddType("bigint", DbType.Int64, typeof(Int64), SqlDbType.BigInt, clrTypes: new Type[] { typeof(UInt32), typeof(UInt64) });

            // Bool
            AddType("bit", DbType.Boolean, typeof(bool), SqlDbType.Bit);
            // Strings
            AddType("nvarchar", DbType.String, typeof(string), SqlDbType.NVarChar, supportsMemo: false, args: "({size})");
            AddType("nchar", DbType.StringFixedLength, typeof(string), SqlDbType.NChar, args: "({size})", isDefault: false, clrTypes: new Type[] { typeof(char) });
            AddType("varchar", DbType.AnsiString, typeof(string), SqlDbType.VarChar, args: "({size})", isDefault: false);
            AddType("char", DbType.AnsiStringFixedLength, typeof(string), SqlDbType.Char, args: "({size})", isDefault: false);
            AddType("ntext", DbType.String, typeof(string), SqlDbType.NText, supportsMemo: true, isDefault: false);
            AddType("text", DbType.AnsiString, typeof(string), SqlDbType.Text, supportsMemo: true, isDefault: false);


            // Datetime
            AddType("datetime", DbType.DateTime, typeof(DateTime), SqlDbType.DateTime);
            //AddType("date", DbType.Date, typeof(DateTime), SqlDbType.Date, isDefault: false);
            //AddType("time", DbType.Time, typeof(TimeSpan), SqlDbType.Time);
            AddType("smalldatetime", DbType.DateTime, typeof(DateTime), SqlDbType.SmallDateTime, isDefault: false);

            // Binaries
            var binTypes = new Type[] { typeof(Vita.Common.Binary) };

            AddType("varbinary", DbType.Binary, typeof(byte[]), SqlDbType.VarBinary, supportsMemo: false, args: "({size})", clrTypes: binTypes);
            AddType("binary", DbType.Binary, typeof(byte[]), SqlDbType.Binary, isDefault: false, args: "({size})", clrTypes: binTypes);
            AddType("image", DbType.Binary, typeof(byte[]), SqlDbType.Image, supportsMemo: true, isDefault: false, clrTypes: binTypes);

            AddType("uniqueidentifier", DbType.Guid, typeof(Guid), SqlDbType.UniqueIdentifier);
            AddType("money", DbType.Currency, typeof(Decimal), SqlDbType.Money, isDefault: false);

            // DateTimeOffset - store it as string and provide converters
            AddType("nvarchar(40)", DbType.String, typeof(string), SqlDbType.NVarChar, isDefault: false, isSubType: true, clrTypes: new Type[] { typeof(DateTimeOffset) });
            Converters.AddConverter <string, DateTimeOffset>(StringToDateTimeOffset, DateTimeOffsetToString);
            AddType("nvarchar(20)", DbType.String, typeof(string), SqlDbType.NVarChar, isDefault: false, isSubType: true, clrTypes: new Type[] { typeof(TimeSpan) });
            Converters.AddConverter <string, TimeSpan>(x => TimeSpan.Parse((string)x), x => ((TimeSpan)x).ToString("G"));


            // the following types should be treated as auto-memo - whenevery property specifies this type explicitly through [Column(TypeName=?)] spec,
            // the column is set to Memo (unlimited size) - unless size is specified explicitly.
            RegisterAutoMemoTypes("image", "ntext", "text");
        }
예제 #3
0
        public SQLiteTypeRegistry(SQLiteDbDriver driver) : base(driver)
        {
            // For all int types SQLite provider returns Int64, not sure about actual storage; so we set ColOutType to Int64 here for Int32 type
            var tdInt = AddDbTypeDef("int", typeof(Int32), mapColumnType: false, aliases: "integer");

            MapMany(new[] { typeof(byte), typeof(sbyte), typeof(Int16), typeof(UInt16), typeof(Int32), typeof(UInt32) }, tdInt);
            IntTypeDef = tdInt;

            var tdInt64 = AddDbTypeDef("int64", typeof(Int64));

            MapMany(new[] { typeof(Int64), typeof(UInt64) }, tdInt64);
            Int64TypeDef = tdInt64;

            var tdReal = AddDbTypeDef("real", typeof(double));

            MapMany(new[] { typeof(Single), typeof(double), typeof(decimal) }, tdReal);

            var tdText = AddDbTypeDef("text", typeof(string));

            MapMany(new[] { typeof(string), typeof(char) }, tdText);
            SpecialTypeDefs[DbSpecialType.String]              = tdText;
            SpecialTypeDefs[DbSpecialType.StringAnsi]          = tdText;
            SpecialTypeDefs[DbSpecialType.StringUnlimited]     = tdText;
            SpecialTypeDefs[DbSpecialType.StringAnsiUnlimited] = tdText;

            var tdBlob = AddDbTypeDef("blob", typeof(byte[]), toLiteral: BytesToLiteral);

            MapMany(new Type[] { typeof(Guid), typeof(Vita.Entities.Binary), typeof(byte[]) }, tdBlob);
            SpecialTypeDefs[DbSpecialType.Binary]          = tdBlob;
            SpecialTypeDefs[DbSpecialType.BinaryUnlimited] = tdBlob;

            // SQLite data provider can automatically handle 'date' values, if there's 'date' column association
            // it returns DateTime values from these columns. But in this case it cuts off milliseconds.
            // So we store dates/time as strings and handle conversion in code; DbReader still sometimes returns value as DateTime -
            //  we handle it in code.
            // Important - we need to to set type name (affinity) to smth special, not just 'date' - otherwise provider recognizes
            // it and starts converting dates in DbReader and this blows up. Did not find any reliable way to disable this behavior
            var tdDate = AddDbTypeDef("date", typeof(string), mapColumnType: false, toLiteral: DateTimeToLiteral);

            Map(typeof(DateTime), tdDate);
            var tdTime = AddDbTypeDef("time", typeof(string), mapColumnType: false, toLiteral: TimeSpanToLiteral);

            Map(typeof(TimeSpan), tdTime);
            var tdBool = AddDbTypeDef("bool", typeof(Int32), mapColumnType: false, toLiteral: BoolToBitLiteral);

            Map(typeof(bool), tdBool);

            Converters.AddConverter <string, TimeSpan>(ParseTimeSpan, TimeSpanToString);
            Converters.AddConverter <Int32, bool>(IntToBool, x => (bool)x ? 1 : 0);
            Converters.AddConverter <string, DateTime>(ParseDateTime, DateTimeToString);
        }
예제 #4
0
        public MySqlTypeRegistry(MySqlDbDriver driver)  : base(driver)
        {
            //numerics
            AddType("float", DbType.Single, typeof(Single), MySqlDbType.Float, aliases: "float unsigned");
            AddType("double", DbType.Double, typeof(Double), MySqlDbType.Double, aliases: "real;double unsigned");
            AddType("decimal", DbType.Decimal, typeof(Decimal), MySqlDbType.Decimal, args: "({precision},{scale})", aliases: "numeric,fixed,dec,decimal unsigned");
            //Just to handle props marked with DbType=DbType.Currency
            AddType("decimal", DbType.Currency, typeof(Decimal), MySqlDbType.Decimal, args: "({precision},{scale})", isDefault: false, isSubType: true);

            //integers
            AddType("int", DbType.Int32, typeof(int), MySqlDbType.Int32, aliases: "integer");
            AddType("int unsigned", DbType.UInt32, typeof(uint), MySqlDbType.UInt32);
            AddType("tinyint", DbType.SByte, typeof(sbyte), MySqlDbType.Byte); //tinyint in MySql is signed byte - unlike in MSSQL
            AddType("tinyint unsigned", DbType.Byte, typeof(byte), MySqlDbType.UByte);
            AddType("smallint", DbType.Int16, typeof(Int16), MySqlDbType.Int16);
            AddType("smallint unsigned", DbType.UInt16, typeof(UInt16), MySqlDbType.UInt16);
            AddType("mediumint", DbType.Int16, typeof(Int32), MySqlDbType.Int24, isDefault: false);
            AddType("bigint", DbType.Int64, typeof(Int64), MySqlDbType.Int64);
            AddType("bigint unsigned", DbType.UInt64, typeof(UInt64), MySqlDbType.UInt64);
            AddType("enum", DbType.Int16, typeof(Int16), MySqlDbType.Enum, isDefault: false);
            AddType("set", DbType.UInt64, typeof(UInt64), MySqlDbType.Set, isDefault: false);

            // Bool
            AddType("bit", DbType.Boolean, typeof(ulong), MySqlDbType.Bit, isDefault: false, clrTypes: new Type[] { typeof(bool) });

            // Strings
            AddType("varchar", DbType.String, typeof(string), MySqlDbType.VarChar, args: "({size})", valueToLiteral: MySqlStringToLiteral);
            AddType("char", DbType.StringFixedLength, typeof(string), MySqlDbType.String, args: "({size})",
                    isDefault: false, clrTypes: new Type[] { typeof(char) }, valueToLiteral: MySqlStringToLiteral);
            AddType("tinytext", DbType.String, typeof(string), MySqlDbType.TinyText, isDefault: false, valueToLiteral: MySqlStringToLiteral);
            AddType("mediumtext", DbType.String, typeof(string), MySqlDbType.MediumText, supportsMemo: true, isDefault: false, valueToLiteral: MySqlStringToLiteral);
            AddType("text", DbType.String, typeof(string), MySqlDbType.Text, supportsMemo: true, isDefault: false, valueToLiteral: MySqlStringToLiteral);
            AddType("longtext", DbType.String, typeof(string), MySqlDbType.LongText, supportsMemo: true, isDefault: false, valueToLiteral: MySqlStringToLiteral);


            // Datetime
            AddType("datetime", DbType.DateTime, typeof(DateTime), MySqlDbType.DateTime, valueToLiteral: DbValueToLiteralConverters.DateTimeToLiteralNoMs);
            AddType("date", DbType.Date, typeof(DateTime), MySqlDbType.Date, isDefault: false, valueToLiteral: DbValueToLiteralConverters.DateTimeToLiteralNoMs);
            AddType("time", DbType.Time, typeof(TimeSpan), MySqlDbType.Time, valueToLiteral: DbValueToLiteralConverters.TimeSpanToLiteralNoMs);

            AddType("timestamp", DbType.Object, typeof(DateTime), MySqlDbType.Timestamp, isDefault: false);
            AddType("year", DbType.Int16, typeof(Int16), MySqlDbType.Year, isDefault: false);

            // Binaries
            var binTypes = new Type[] { typeof(Vita.Common.Binary) };

            AddType("varbinary", DbType.Binary, typeof(byte[]), MySqlDbType.VarBinary, args: "({size})", clrTypes: binTypes);
            AddType("binary", DbType.Binary, typeof(byte[]), MySqlDbType.Binary, isDefault: false, args: "({size})", clrTypes: binTypes);
            AddType("tinyblob", DbType.Binary, typeof(byte[]), MySqlDbType.TinyBlob, isDefault: false, clrTypes: binTypes);
            AddType("blob", DbType.Binary, typeof(byte[]), MySqlDbType.Blob, supportsMemo: true, isDefault: false, clrTypes: binTypes);
            AddType("mediumblob", DbType.Binary, typeof(byte[]), MySqlDbType.MediumBlob, supportsMemo: true, isDefault: false, clrTypes: binTypes);
            AddType("longblob", DbType.Binary, typeof(byte[]), MySqlDbType.LongBlob, supportsMemo: true, isDefault: false, clrTypes: binTypes);

            // Guid - specialized subtype binary(16)
            AddType("binary", DbType.Binary, typeof(byte[]), MySqlDbType.Binary, args: "(16)", isDefault: true, isSubType: true,
                    clrTypes: new Type[] { typeof(Guid) }, dbFirstClrType: typeof(Guid), columnInit: "0");

            // the following types should be treated as auto-memo - whenevery property specifies this type explicitly through [Column(TypeName=?)] spec,
            // the column is set to Memo (unlimited size) - unless size is specified explicitly.
            RegisterAutoMemoTypes("mediumtext", "text", "longtext", "blob", "longblob");

            // bool is stored as UInt64
            Converters.AddConverter <ulong, bool>(LongToBool, x => x); //this is for MySql bit type
        }
예제 #5
0
        public MySqlTypeRegistry(MySqlDbDriver driver)  : base(driver)
        {
            AddDbTypeDef("float", typeof(Single), aliases: "float unsigned");
            AddDbTypeDef("double", typeof(Double), aliases: "real;double unsigned");
            AddDbTypeDef("decimal", typeof(Decimal), DbTypeFlags.PrecisionScale, aliases: "numeric,fixed,dec,decimal unsigned");

            //integers
            AddDbTypeDef("int", typeof(int), aliases: "integer");
            AddDbTypeDef("int unsigned", typeof(uint));
            AddDbTypeDef("tinyint", typeof(sbyte)); //tinyint in MySql is signed byte - unlike in MSSQL
            AddDbTypeDef("tinyint unsigned", typeof(byte));
            AddDbTypeDef("smallint", typeof(Int16));
            AddDbTypeDef("smallint unsigned", typeof(UInt16));
            AddDbTypeDef("mediumint", typeof(Int32), mapColumnType: false);
            AddDbTypeDef("bigint", typeof(Int64));
            AddDbTypeDef("bigint unsigned", typeof(UInt64));
            AddDbTypeDef("enum", typeof(Int16), mapColumnType: false);
            AddDbTypeDef("set", typeof(UInt64), mapColumnType: false);

            // Bool
            var bitTd = AddDbTypeDef("bit", typeof(ulong), mapColumnType: false);

            Map(typeof(bool), bitTd);

            // Strings
            var tdVarChar = AddDbTypeDef("varchar", typeof(string), DbTypeFlags.Size, toLiteral: MySqlStringToLiteral);

            base.SpecialTypeDefs[DbSpecialType.String] = base.SpecialTypeDefs[DbSpecialType.StringAnsi] = tdVarChar;

            var tdChar = AddDbTypeDef("char", typeof(string), DbTypeFlags.Size, mapColumnType: false, toLiteral: MySqlStringToLiteral);

            Map(typeof(char), tdChar, size: 1);

            AddDbTypeDef("tinytext", typeof(string), mapColumnType: false, toLiteral: MySqlStringToLiteral);
            AddDbTypeDef("mediumtext", typeof(string), flags: DbTypeFlags.Unlimited, mapColumnType: false,
                         toLiteral: MySqlStringToLiteral);
            AddDbTypeDef("text", typeof(string), flags: DbTypeFlags.Unlimited, mapColumnType: false, toLiteral: MySqlStringToLiteral);
            // this maps to unlimited string
            var tdLText = AddDbTypeDef("longtext", typeof(string), flags: DbTypeFlags.Unlimited, toLiteral: MySqlStringToLiteral);

            base.SpecialTypeDefs[DbSpecialType.StringUnlimited] = base.SpecialTypeDefs[DbSpecialType.StringAnsiUnlimited] = tdLText;


            // Datetime
            AddDbTypeDef("datetime", typeof(DateTime), toLiteral: DbValueToLiteralConverters.DateTimeToLiteralNoMs);
            AddDbTypeDef("date", typeof(DateTime), mapColumnType: false, toLiteral: DbValueToLiteralConverters.DateTimeToLiteralNoMs);
            AddDbTypeDef("time", typeof(TimeSpan), toLiteral: DbValueToLiteralConverters.TimeSpanToLiteralNoMs);

            AddDbTypeDef("timestamp", typeof(DateTime), mapColumnType: false);
            AddDbTypeDef("year", typeof(Int16), mapColumnType: false);

            // Binaries
            AddDbTypeDef("varbinary", typeof(byte[]), DbTypeFlags.Size, specialType: DbSpecialType.Binary);
            var tdBin = AddDbTypeDef("binary", typeof(byte[]), DbTypeFlags.Size, mapColumnType: false);

            AddDbTypeDef("tinyblob", typeof(byte[]), mapColumnType: false);
            AddDbTypeDef("blob", typeof(byte[]), flags: DbTypeFlags.Unlimited, mapColumnType: false);
            AddDbTypeDef("mediumblob", typeof(byte[]), flags: DbTypeFlags.Unlimited, mapColumnType: false);
            // serves as unlimited binary
            AddDbTypeDef("longblob", typeof(byte[]), flags: DbTypeFlags.Unlimited, columnInit: "0",
                         specialType: DbSpecialType.BinaryUnlimited);

            // Guid - specialized subtype binary(16)
            Map(typeof(Guid), tdBin, size: 16);

            // bool is stored as UInt64
            Converters.AddConverter <ulong, bool>(LongToBool, x => x); //this is for MySql bit type
        }