예제 #1
0
        public virtual void GenerateSqlLiteral_returns_Guid_literal()
        {
            var value   = new Guid("c6f43a9e-91e1-45ef-a320-832ea23b7292");
            var literal = new GuidTypeMapping("guid").GenerateSqlLiteral(value);

            Assert.Equal("'c6f43a9e-91e1-45ef-a320-832ea23b7292'", literal);
        }
예제 #2
0
        void AddCustomizedMappings()
        {
            // Mappings where we need literal string generation
            _baseClrMappings[typeof(string)] = _storeTypeMappings["text"] = new NpgsqlStringTypeMapping("text", NpgsqlDbType.Text);
            _storeTypeMappings["varchar"]    = new NpgsqlStringTypeMapping("varchar", NpgsqlDbType.Varchar);
            _storeTypeMappings["char"]       = new NpgsqlStringTypeMapping("char", NpgsqlDbType.Char);
            _storeTypeMappings["citext"]     = new NpgsqlStringTypeMapping("citext", NpgsqlDbType.Citext);
            _storeTypeMappings["json"]       = new NpgsqlStringTypeMapping("json", NpgsqlDbType.Json);
            _storeTypeMappings["jsonb"]      = new NpgsqlStringTypeMapping("jsonb", NpgsqlDbType.Jsonb);

            _baseClrMappings[typeof(char)]           = new CharTypeMapping("text", DbType.String);
            _baseClrMappings[typeof(DateTime)]       = _storeTypeMappings["timestamp"] = new DateTimeTypeMapping("timestamp", DbType.DateTime);
            _baseClrMappings[typeof(DateTimeOffset)] = _storeTypeMappings["timestamptz"] = new NpgsqlDateTimeOffsetTypeMapping("timestamptz", DbType.DateTimeOffset);
            _baseClrMappings[typeof(TimeSpan)]       = _storeTypeMappings["interval"] = new NpgsqlTimeSpanTypeMapping();
            _baseClrMappings[typeof(bool)]           = _storeTypeMappings["bool"] = new NpgsqlBoolTypeMapping();

            _baseClrMappings[typeof(decimal)] = new DecimalTypeMapping("numeric", DbType.Decimal);
            // Note that "decimal" in PostgreSQL is just an alias for numeric, PostgreSQL itself always reports numeric for column types.

            _baseClrMappings[typeof(Guid)]   = _storeTypeMappings["uuid"] = new GuidTypeMapping("uuid", DbType.Guid);
            _baseClrMappings[typeof(byte[])] = _storeTypeMappings["bytea"] = new NpgsqlByteArrayTypeMapping();

            // The following isn't necessary for int itself - a simple ToString() (the default) produces the right
            // literal representation. However, with an explicit int mapping the standard mapping would be returned
            // for enums, and there a simple ToString produces the enum *name*.
            // Example for test for enum literal: InheritanceNpgsqlTest.Can_query_just_roses
            _baseClrMappings[typeof(short)] = _storeTypeMappings["int2"] = new ShortTypeMapping("int2", DbType.Int16);
            _baseClrMappings[typeof(int)]   = _storeTypeMappings["int4"] = new IntTypeMapping("int4", DbType.Int32);
            _baseClrMappings[typeof(long)]  = _storeTypeMappings["int8"] = new LongTypeMapping("int8", DbType.Int64);

            // uint is special: there are three internal system uint types: oid, xid, cid. None are supposed to
            // be truly user-facing, so we don't want to automatically map uint properties to any of them.
            // However, if the user explicitly sets the properties store type to oid/xid/cid, we want to allow
            // that (especially since the xmin system column is important for optimistic concurrency).
            // EFCore doesn't allow a situation where a CLR type has no default store type, so we arbitrarily
            // choose oid.
            _baseClrMappings[typeof(uint)] = new NpgsqlBaseTypeMapping("oid", typeof(uint), NpgsqlDbType.Oid);
        }
        private void Initialize()
        {
            //
            // String mappings depend on the MySqlOptions.NoBackslashEscapes setting:
            //

            _charUnicode       = new MySqlStringTypeMapping("char", DbType.StringFixedLength, _options, fixedLength: true);
            _varcharUnicode    = new MySqlStringTypeMapping("varchar", DbType.String, _options);
            _tinytextUnicode   = new MySqlStringTypeMapping("tinytext", DbType.String, _options);
            _textUnicode       = new MySqlStringTypeMapping("text", DbType.String, _options);
            _mediumtextUnicode = new MySqlStringTypeMapping("mediumtext", DbType.String, _options);
            _longtextUnicode   = new MySqlStringTypeMapping("longtext", DbType.String, _options);

            _nchar    = new MySqlStringTypeMapping("nchar", DbType.StringFixedLength, _options, fixedLength: true);
            _nvarchar = new MySqlStringTypeMapping("nvarchar", DbType.String, _options);

            _enum = new MySqlStringTypeMapping("enum", DbType.String, _options);

            _guid = MySqlGuidTypeMapping.IsValidGuidFormat(_options.ConnectionSettings.GuidFormat)
                ? new MySqlGuidTypeMapping(_options.ConnectionSettings.GuidFormat)
                : null;

            _storeTypeMappings
                = new Dictionary <string, RelationalTypeMapping[]>(StringComparer.OrdinalIgnoreCase)
                {
                // bit
                { "bit", new[] { _bit } },

                // integers
                { "tinyint", new[] { _tinyint } },
                { "tinyint unsigned", new[] { _utinyint } },
                { "smallint", new[] { _smallint } },
                { "smallint unsigned", new[] { _usmallint } },
                { "mediumint", new[] { _int } },
                { "mediumint unsigned", new[] { _uint } },
                { "int", new[] { _int } },
                { "int unsigned", new[] { _uint } },
                { "integer", new[] { _int } },
                { "integer unsigned", new[] { _uint } },
                { "bigint", new[] { _bigint } },
                { "bigint unsigned", new[] { _ubigint } },

                // decimals
                { "decimal", new[] { _decimal } },
                { "decimal unsigned", new[] { _decimal } },            // deprecated since 8.0.17-mysql
                { "numeric", new[] { _decimal } },
                { "numeric unsigned", new[] { _decimal } },            // deprecated since 8.0.17-mysql
                { "dec", new[] { _decimal } },
                { "dec unsigned", new[] { _decimal } },                // deprecated since 8.0.17-mysql
                { "fixed", new[] { _decimal } },
                { "fixed unsigned", new[] { _decimal } },              // deprecated since 8.0.17-mysql
                { "double", new[] { _double } },
                { "double unsigned", new[] { _double } },              // deprecated since 8.0.17-mysql
                { "double precision", new[] { _double } },
                { "double precision unsigned", new[] { _double } },    // deprecated since 8.0.17-mysql
                { "real", new[] { _double } },
                { "real unsigned", new[] { _double } },                // deprecated since 8.0.17-mysql
                { "float", new[] { _float } },
                { "float unsigned", new[] { _float } },                // deprecated since 8.0.17-mysql

                // binary
                { "binary", new[] { _binary } },
                { "varbinary", new[] { _varbinary } },
                { "tinyblob", new[] { _varbinary } },
                { "blob", new[] { _varbinary } },
                { "mediumblob", new[] { _varbinary } },
                { "longblob", new[] { _varbinary } },

                // string
                { "char", new[] { _charUnicode } },
                { "varchar", new[] { _varcharUnicode } },
                { "tinytext", new[] { _tinytextUnicode } },
                { "text", new[] { _textUnicode } },
                { "mediumtext", new[] { _mediumtextUnicode } },
                { "longtext", new[] { _longtextUnicode } },

                { "enum", new[] { _enum } },

                { "nchar", new[] { _nchar } },
                { "nvarchar", new[] { _nvarchar } },

                // DateTime
                { "year", new[] { _year } },
                { "date", new[] { _date } },
                { "time", new[] { _time } },
                { "datetime", new RelationalTypeMapping[] { _dateTime, _dateTimeOffset } },
                { "timestamp", new RelationalTypeMapping[] { _timeStamp, _timeStampOffset } },
                };

            _clrTypeMappings
                = new Dictionary <Type, RelationalTypeMapping>
                {
                // integers
                { typeof(short), _smallint },
        protected void Initialize()
        {
            //
            // String mappings depend on the MySqlOptions.NoBackslashEscapes setting:
            //

            _charUnicode       = new MySqlStringTypeMapping("char", DbType.StringFixedLength, _options, unicode: true, fixedLength: true);
            _varcharUnicode    = new MySqlStringTypeMapping("varchar", DbType.String, _options, unicode: true);
            _varcharmaxUnicode = new MySqlStringTypeMapping("longtext", DbType.String, _options, unicode: true);

            _nchar    = new MySqlStringTypeMapping("nchar", DbType.StringFixedLength, _options, unicode: true, fixedLength: true);
            _nvarchar = new MySqlStringTypeMapping("nvarchar", DbType.String, _options, unicode: true);

            _enum = new MySqlStringTypeMapping("enum", DbType.String, _options, unicode: true);

            _guid = null;

            _storeTypeMappings
                = new Dictionary <string, RelationalTypeMapping>(StringComparer.OrdinalIgnoreCase)
                {
                // bit
                { "bit", _bit },

                // integers
                { "tinyint", _tinyint },
                { "tinyint unsigned", _utinyint },
                { "smallint", _smallint },
                { "smallint unsigned", _usmallint },
                { "mediumint", _int },
                { "mediumint unsigned", _uint },
                { "int", _int },
                { "int unsigned", _uint },
                { "integer", _int },
                { "integer unsigned", _uint },
                { "bigint", _bigint },
                { "bigint unsigned", _ubigint },

                // decimals
                { "decimal", _decimal },
                { "decimal unsigned", _decimal },         // deprecated since 8.0.17-mysql
                { "numeric", _decimal },
                { "numeric unsigned", _decimal },         // deprecated since 8.0.17-mysql
                { "dec", _decimal },
                { "dec unsigned", _decimal },             // deprecated since 8.0.17-mysql
                { "fixed", _decimal },
                { "fixed unsigned", _decimal },           // deprecated since 8.0.17-mysql
                { "double", _double },
                { "double unsigned", _double },           // deprecated since 8.0.17-mysql
                { "double precision", _double },
                { "double precision unsigned", _double }, // deprecated since 8.0.17-mysql
                { "real", _double },
                { "real unsigned", _double },             // deprecated since 8.0.17-mysql
                { "float", _float },
                { "float unsigned", _float },             // deprecated since 8.0.17-mysql

                // binary
                { "binary", _binary },
                { "varbinary", _varbinary },
                { "tinyblob", _varbinary },
                { "blob", _varbinary },
                { "mediumblob", _varbinary },
                { "longblob", _varbinary },

                // string
                { "char", _charUnicode },
                { "varchar", _varcharUnicode },
                { "tinytext", _varcharmaxUnicode },
                { "text", _varcharmaxUnicode },
                { "mediumtext", _varcharmaxUnicode },
                { "longtext", _varcharmaxUnicode },

                { "enum", _enum },

                { "nchar", _nchar },
                { "nvarchar", _nvarchar },

                // DateTime
                { "date", _date }
                };

            _clrTypeMappings
                = new Dictionary <Type, RelationalTypeMapping>
                {
                // integers
                { typeof(short), _smallint },
                { typeof(ushort), _usmallint },
                { typeof(int), _int },
                { typeof(uint), _uint },
                { typeof(long), _bigint },
                { typeof(ulong), _ubigint },

                // decimals
                { typeof(decimal), _decimal },
                { typeof(float), _float },
                { typeof(double), _double },

                // byte / char
                { typeof(sbyte), _tinyint },
                { typeof(byte), _utinyint },
                };

            // Boolean
            if (_options.DefaultDataTypeMappings.ClrBoolean != MySqlBooleanType.None)
            {
                _clrTypeMappings[typeof(bool)] = _options.DefaultDataTypeMappings.ClrBoolean == MySqlBooleanType.Bit1
                    ? _bit1
                    : _tinyint1;
            }

            // DateTime
            _storeTypeMappings["time"] = !_options.ServerVersion.SupportsDateTime6 ||
                                         _options.DefaultDataTypeMappings.ClrTimeSpan == MySqlTimeSpanType.Time
                ? _time
                : _time6;

            _clrTypeMappings[typeof(TimeSpan)] = !_options.ServerVersion.SupportsDateTime6 ||
                                                 _options.DefaultDataTypeMappings.ClrTimeSpan == MySqlTimeSpanType.Time
                ? _time
                : _time6;

            _clrTypeMappings[typeof(DateTime)] = !_options.ServerVersion.SupportsDateTime6 ||
                                                 _options.DefaultDataTypeMappings.ClrDateTime == MySqlDateTimeType.DateTime
                ? _dateTime
                : _options.DefaultDataTypeMappings.ClrDateTime == MySqlDateTimeType.Timestamp6
                    ? _timeStamp6
                    : _dateTime6;

            _clrTypeMappings[typeof(DateTimeOffset)] = !_options.ServerVersion.SupportsDateTime6 ||
                                                       _options.DefaultDataTypeMappings.ClrDateTime == MySqlDateTimeType.DateTime
                ? _dateTimeOffset
                : _options.DefaultDataTypeMappings.ClrDateTimeOffset == MySqlDateTimeType.Timestamp6
                    ? _timeStampOffset6
                    : _dateTimeOffset6;

            // Guid
            if (_guid != null)
            {
                _clrTypeMappings[typeof(Guid)] = _guid;
            }

            // Type mappings that only exist to work around the limited code generation capabilites when scaffolding:
            _scaffoldingClrTypeMappings = new Dictionary <Type, RelationalTypeMapping>
            {
                { typeof(MySqlCodeGenerationMemberAccess), _codeGenerationMemberAccess }
            };
        }