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

Add Caching to SqliteDataRecord for Ordinal and Name #24141

Merged
merged 2 commits into from
Mar 12, 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
20 changes: 16 additions & 4 deletions src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand All @@ -17,6 +18,8 @@ internal class SqliteDataRecord : SqliteValueReader, IDisposable
private readonly SqliteConnection _connection;
private byte[][]? _blobCache;
private int?[]? _typeCache;
private Dictionary<string, int>? _columnNameOrdinalCache;
private string[]? _columnNameCache;
private bool _stepped;
private int? _rowidOrdinal;

Expand Down Expand Up @@ -100,26 +103,35 @@ protected override T GetNull<T>(int ordinal)

public virtual string GetName(int ordinal)
{
var name = sqlite3_column_name(Handle, ordinal).utf8_to_string();
var name = _columnNameCache?[ordinal] ?? sqlite3_column_name(Handle, ordinal).utf8_to_string();
if (name == null
&& (ordinal < 0 || ordinal >= FieldCount))
{
// NB: Message is provided by the framework
throw new ArgumentOutOfRangeException(nameof(ordinal), ordinal, message: null);
}

_columnNameCache ??= new string[FieldCount];
_columnNameCache[ordinal] = name!;

return name!;
}

public virtual int GetOrdinal(string name)
{
for (var i = 0; i < FieldCount; i++)
if (_columnNameOrdinalCache == null)
{
if (GetName(i) == name)
_columnNameOrdinalCache = new Dictionary<string, int>();
for (var i = 0; i < FieldCount; i++)
{
return i;
_columnNameOrdinalCache[GetName(i)] = i;
}
}

bricelam marked this conversation as resolved.
Show resolved Hide resolved
if (_columnNameOrdinalCache.TryGetValue(name, out var ordinal))
{
return ordinal;
}

// NB: Message is provided by framework
throw new ArgumentOutOfRangeException(nameof(name), name, message: null);
Expand Down
6 changes: 6 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,9 @@ public void GetName_works()
using (var reader = connection.ExecuteReader("SELECT 1 AS Id;"))
{
Assert.Equal("Id", reader.GetName(0));

// NB: Repeated to use caching
Assert.Equal("Id", reader.GetName(0));
}
}
}
Expand Down Expand Up @@ -1266,6 +1269,9 @@ public void GetOrdinal_works()
using (var reader = connection.ExecuteReader("SELECT 1 AS Id;"))
{
Assert.Equal(0, reader.GetOrdinal("Id"));

// NB: Repeated to use caching
Assert.Equal(0, reader.GetOrdinal("Id"));
}
}
}
Expand Down