mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
bindings/dotnet: resolve declared column types without per-cell PRAGMA queries
SqliteDataReader.GetValue() resolved each column's declared type through GetDeclaredTypeName -> GetTableColumns, which ran PRAGMA table_info, PRAGMA index_list and PRAGMA index_info (plus regexes over the SQL) for every column of every row, with no caching. Reading a result set with GetValues() or loading a DataTable therefore ran dozens of nested statements per row. Loading 10,000 rows of 11 columns took seconds and allocated gigabytes through this path. Ask the engine first, through turso_statement_column_decltype, as Microsoft.Data.Sqlite does with sqlite3_column_decltype. This costs no query. The existing SQL/PRAGMA heuristics remain as the fallback for columns the engine leaves unresolved. Cache the resolved declared types and the table-column maps for each result set. The cache is keyed on the current statement handle, so NextResult() invalidates it. A new test checks that declared types are resolved separately for each result set of a multi-statement batch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -31,6 +31,12 @@ public class SqliteDataReader : DbDataReader
|
||||
private int _managedResultIndex;
|
||||
private int _managedRowIndex = -1;
|
||||
|
||||
// Declared types and table schemas resolved for the current result set. The fallback
|
||||
// heuristics run PRAGMA queries, so they are computed once per statement, not once per cell.
|
||||
private TursoStatementHandle? _schemaCacheStatement;
|
||||
private string?[] _declaredTypeNames = [];
|
||||
private Dictionary<string, Dictionary<string, SchemaColumnInfo>>? _tableColumnsCache;
|
||||
|
||||
internal SqliteDataReader(SqliteCommand command, TursoStatementHandle statement, string currentSql, List<string> remainingSql, int recordsAffected, CommandBehavior behavior, Action closeCallback)
|
||||
{
|
||||
_command = command;
|
||||
@@ -1090,6 +1096,31 @@ public class SqliteDataReader : DbDataReader
|
||||
return CurrentManagedResult.Columns[ordinal].DataTypeName;
|
||||
}
|
||||
|
||||
EnsureSchemaCache();
|
||||
if ((uint)ordinal >= (uint)_declaredTypeNames.Length)
|
||||
return ResolveDeclaredTypeName(ordinal);
|
||||
|
||||
return _declaredTypeNames[ordinal] ??= ResolveDeclaredTypeName(ordinal);
|
||||
}
|
||||
|
||||
private void EnsureSchemaCache()
|
||||
{
|
||||
if (_schemaCacheStatement is not null && ReferenceEquals(_schemaCacheStatement, _statement))
|
||||
return;
|
||||
|
||||
_schemaCacheStatement = _statement;
|
||||
_declaredTypeNames = _statement is null ? [] : new string?[TursoBindings.GetFieldCount(_statement)];
|
||||
_tableColumnsCache = null;
|
||||
}
|
||||
|
||||
private string ResolveDeclaredTypeName(int ordinal)
|
||||
{
|
||||
// The engine knows the declared type of direct column references; the SQL heuristics
|
||||
// below only cover what it leaves unresolved.
|
||||
var nativeDeclaredType = TursoBindings.GetDeclaredTypeName(GetStatement(), ordinal);
|
||||
if (!string.IsNullOrEmpty(nativeDeclaredType))
|
||||
return StripTypeLength(nativeDeclaredType);
|
||||
|
||||
if (TryGetSelectSource(out var tableName, out var selections))
|
||||
{
|
||||
var tableColumns = GetTableColumns(tableName);
|
||||
@@ -1201,6 +1232,19 @@ public class SqliteDataReader : DbDataReader
|
||||
}
|
||||
|
||||
private Dictionary<string, SchemaColumnInfo> GetTableColumns(string tableName)
|
||||
{
|
||||
EnsureSchemaCache();
|
||||
_tableColumnsCache ??= new Dictionary<string, Dictionary<string, SchemaColumnInfo>>(StringComparer.OrdinalIgnoreCase);
|
||||
if (!_tableColumnsCache.TryGetValue(tableName, out var columns))
|
||||
{
|
||||
columns = QueryTableColumns(tableName);
|
||||
_tableColumnsCache[tableName] = columns;
|
||||
}
|
||||
|
||||
return columns;
|
||||
}
|
||||
|
||||
private Dictionary<string, SchemaColumnInfo> QueryTableColumns(string tableName)
|
||||
{
|
||||
var columns = new Dictionary<string, SchemaColumnInfo>(StringComparer.OrdinalIgnoreCase);
|
||||
if (_command.Connection is null)
|
||||
|
||||
@@ -1046,6 +1046,30 @@ public class SqliteFacadeTests
|
||||
schema.Rows[2][SchemaTableColumn.DataType].Should().Be(typeof(byte[]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetValueResolvesDeclaredTypesForEachResultSet()
|
||||
{
|
||||
using var connection = new SqliteConnection("Data Source=:memory:");
|
||||
connection.Open();
|
||||
connection.ExecuteNonQuery(
|
||||
"""
|
||||
CREATE TABLE GuidIds (Id GUID);
|
||||
CREATE TABLE TextIds (Id TEXT);
|
||||
INSERT INTO GuidIds VALUES ('dc0d7e0e-365d-4948-ab9b-8ca8056bf93a'), ('0e7e0ddc-5d36-4849-ab9b-8ca8056bf93a');
|
||||
INSERT INTO TextIds VALUES ('dc0d7e0e-365d-4948-ab9b-8ca8056bf93a');
|
||||
""");
|
||||
|
||||
using var reader = connection.ExecuteReader("SELECT Id FROM GuidIds; SELECT Id FROM TextIds;");
|
||||
reader.Read().Should().BeTrue();
|
||||
reader.GetValue(0).Should().BeOfType<Guid>();
|
||||
reader.Read().Should().BeTrue();
|
||||
reader.GetValue(0).Should().Be(new Guid("0e7e0ddc-5d36-4849-ab9b-8ca8056bf93a"));
|
||||
|
||||
reader.NextResult().Should().BeTrue();
|
||||
reader.Read().Should().BeTrue();
|
||||
reader.GetValue(0).Should().Be("dc0d7e0e-365d-4948-ab9b-8ca8056bf93a");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetFieldValueThrowsForNullTypedValues()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user