Skip to content

Commit

Permalink
SQLite: Change fallback type to BLOB
Browse files Browse the repository at this point in the history
It was previously a mixture of INTEGER and TEXT.

Resolves #13253, resolves #13841
  • Loading branch information
bricelam authored Mar 19, 2019
1 parent 049edb0 commit f763c6e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ protected override RelationalTypeMapping FindMapping(in RelationalTypeMappingInf
? mapping
: storeTypeName != null
? storeTypeName.Length != 0
? _typeRules.Select(r => r(storeTypeName)).FirstOrDefault(r => r != null) ?? _text
: _text // This may seem odd, but it's okay because we are matching SQLite's loose typing.
? _typeRules.Select(r => r(storeTypeName)).FirstOrDefault(r => r != null) ?? _blob
: _blob // This may seem odd, but it's okay because we are matching SQLite's loose typing.
: null;
}

Expand Down
34 changes: 10 additions & 24 deletions src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,11 @@ public virtual string GetDataTypeName(int ordinal)
case SQLITE_TEXT:
return "TEXT";

case SQLITE_BLOB:
return "BLOB";

case SQLITE_NULL:
return "INTEGER";

default:
Debug.Assert(false, "Unexpected column type: " + sqliteType);
return "INTEGER";
Debug.Assert(
sqliteType == SQLITE_BLOB || sqliteType == SQLITE_NULL,
"Unexpected column type: " + sqliteType);
return "BLOB";
}
}

Expand All @@ -155,15 +151,11 @@ public virtual Type GetFieldType(int ordinal)
case SQLITE_TEXT:
return typeof(string);

case SQLITE_BLOB:
return typeof(byte[]);

case SQLITE_NULL:
return typeof(int);

default:
Debug.Assert(false, "Unexpected column type: " + sqliteType);
return typeof(int);
Debug.Assert(
sqliteType == SQLITE_BLOB || sqliteType == SQLITE_NULL,
"Unexpected column type: " + sqliteType);
return typeof(byte[]);
}
}

Expand All @@ -180,15 +172,9 @@ public static Type GetFieldType(string type)
case "text":
return typeof(string);

case "blob":
return typeof(byte[]);

case null:
return typeof(int);

default:
Debug.Assert(false, "Unexpected column type: " + type);
return typeof(int);
Debug.Assert(type == "blob" || type == null, "Unexpected column type: " + type);
return typeof(byte[]);
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/Microsoft.Data.Sqlite.Core/SqliteValueReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,12 @@ public virtual object GetValue(int ordinal)
case SQLITE_TEXT:
return GetString(ordinal);

case SQLITE_BLOB:
return GetBlob(ordinal);

case SQLITE_NULL:
return GetNull<object>(ordinal);

default:
Debug.Assert(false, "Unexpected column type: " + sqliteType);
return GetInt32(ordinal);
Debug.Assert(sqliteType == SQLITE_BLOB, "Unexpected column type: " + sqliteType);
return GetBlob(ordinal);
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/EFCore.Sqlite.Tests/Storage/SqliteTypeMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public override void Create_and_clone_with_converter(Type mappingType, Type clrT
[InlineData("TEXT", typeof(string))]
[InlineData("Integer", typeof(long))]
[InlineData("Blob", typeof(byte[]))]
[InlineData("numeric", typeof(string))]
[InlineData("numeric", typeof(byte[]))]
[InlineData("real", typeof(double))]
[InlineData("doub", typeof(double))]
[InlineData("int", typeof(long))]
[InlineData("SMALLINT", typeof(long))]
[InlineData("UNSIGNED BIG INT", typeof(long))]
[InlineData("VARCHAR(255)", typeof(string))]
[InlineData("nchar(55)", typeof(string))]
[InlineData("datetime", typeof(string))]
[InlineData("decimal(10,4)", typeof(string))]
[InlineData("boolean", typeof(string))]
[InlineData("unknown_type", typeof(string))]
[InlineData("", typeof(string))]
[InlineData("datetime", typeof(byte[]))]
[InlineData("decimal(10,4)", typeof(byte[]))]
[InlineData("boolean", typeof(byte[]))]
[InlineData("unknown_type", typeof(byte[]))]
[InlineData("", typeof(byte[]))]
public void It_maps_strings_to_not_null_types(string typeName, Type clrType)
{
Assert.Equal(clrType, CreateTypeMapper().FindMapping(typeName).ClrType);
Expand Down
6 changes: 3 additions & 3 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public void GetDateTimeOffset_throws_when_null()
[InlineData("SELECT 3.14;", "REAL")]
[InlineData("SELECT 'test';", "TEXT")]
[InlineData("SELECT X'7E57';", "BLOB")]
[InlineData("SELECT NULL;", "INTEGER")]
[InlineData("SELECT NULL;", "BLOB")]
public void GetDataTypeName_works(string sql, string expected)
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
Expand Down Expand Up @@ -701,7 +701,7 @@ public void GetFieldValue_throws_on_overflow<T>(T minValue)
[InlineData("SELECT 3.14;", typeof(double))]
[InlineData("SELECT 'test';", typeof(string))]
[InlineData("SELECT X'7E57';", typeof(byte[]))]
[InlineData("SELECT NULL;", typeof(int))]
[InlineData("SELECT NULL;", typeof(byte[]))]
public void GetFieldType_works(string sql, Type expected)
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
Expand Down Expand Up @@ -1480,7 +1480,7 @@ public void GetSchemaTable_works()
[InlineData("('Z'), (1), ('A')", typeof(string))]
[InlineData("(0.1), (0.01), ('A')", typeof(double))]
[InlineData("(X'7E57'), (X'577E'), ('A')", typeof(byte[]))]
[InlineData("(NULL), (NULL), (NULL)", typeof(int))]
[InlineData("(NULL), (NULL), (NULL)", typeof(byte[]))]
[InlineData("(NULL), ('A'), ('B')", typeof(string))]
public void GetSchemaTable_DataType_works(string values, Type expectedType)
{
Expand Down

0 comments on commit f763c6e

Please sign in to comment.