Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case insensitive SqliteDataReader. Fixes dotnet/efcore#24011 #24588

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,7 @@
<data name="UnknownCollection" xml:space="preserve">
<value>The requested collection '{collectionName}' is not defined.</value>
</data>
<data name="AmbiguousColumnName" xml:space="preserve">
<value>The name '{name}' is ambiguous between columns '{column1}' and '{column2}'. Specify one using its exact case.</value>
</data>
</root>
24 changes: 23 additions & 1 deletion src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,34 @@ public virtual int GetOrdinal(string name)
_columnNameOrdinalCache[GetName(i)] = i;
}
}

if (_columnNameOrdinalCache.TryGetValue(name, out var ordinal))
{
return ordinal;
}

KeyValuePair<string, int>? match = null;
foreach (var item in _columnNameOrdinalCache)
{
if (string.Equals(name, item.Key, StringComparison.OrdinalIgnoreCase))
{
if (match != null)
{
throw new InvalidOperationException(
Resources.AmbiguousColumnName(name, match.Value.Key, item.Key));
}

match = item;
}
}

if (match != null)
{
_columnNameOrdinalCache.Add(name, match.Value.Value);

return match.Value.Value;
}

// NB: Message is provided by framework
throw new ArgumentOutOfRangeException(nameof(name), name, message: null);
}
Expand Down
23 changes: 19 additions & 4 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,18 @@ public void GetOrdinal_throws_when_out_of_range()
}
}

[Fact]
public void GetOrdinal_throws_when_ambiguous()
{
using var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();

using var reader = connection.ExecuteReader("SELECT 1 AS Id, 2 AS ID");
var ex = Assert.Throws<InvalidOperationException>(() => reader.GetOrdinal("id"));

Assert.Contains(Resources.AmbiguousColumnName("id", "Id", "ID"), ex.Message);
}

[Fact]
public void GetOrdinal_throws_when_closed()
{
Expand Down Expand Up @@ -1583,19 +1595,22 @@ public void Item_by_ordinal_throws_when_non_query()
}
}

[Fact]
public void Item_by_name_works()
[Theory]
[InlineData("SELECT 1 AS Id;", "Id", 1L)]
[InlineData("SELECT 1 AS Id;", "id", 1L)]
[InlineData("SELECT 1 AS Id, 2 AS id;", "id", 2L)]
public void Item_by_name_works(string query, string column, long expected)
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.Open();

using (var reader = connection.ExecuteReader("SELECT 1 AS Id;"))
using (var reader = connection.ExecuteReader(query))
{
var hasData = reader.Read();
Assert.True(hasData);

Assert.Equal(1L, reader["Id"]);
Assert.Equal(expected, reader[column]);
}
}
}
Expand Down