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

Cache ParamCount together with a statement for SQLite #26380

Merged
Merged
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
16 changes: 9 additions & 7 deletions src/Microsoft.Data.Sqlite.Core/SqliteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SqliteCommand : DbCommand
{
private SqliteParameterCollection? _parameters;

private readonly List<sqlite3_stmt> _preparedStatements = new();
private readonly List<(sqlite3_stmt Statement, int ParamCount)> _preparedStatements = new();
private SqliteConnection? _connection;
private string _commandText = string.Empty;
private bool _prepared;
Expand Down Expand Up @@ -318,13 +318,12 @@ public override void Prepare()

private IEnumerable<sqlite3_stmt> GetStatements(Stopwatch timer)
{
foreach (var stmt in !_prepared
foreach ((var stmt, var expectedParams) in !_prepared
? PrepareAndEnumerateStatements(timer)
: _preparedStatements)
{
var boundParams = _parameters?.Bind(stmt) ?? 0;

var expectedParams = sqlite3_bind_parameter_count(stmt);
if (expectedParams != boundParams)
{
var unboundParams = new List<string>();
Expand Down Expand Up @@ -467,7 +466,7 @@ public override void Cancel()
{
}

private IEnumerable<sqlite3_stmt> PrepareAndEnumerateStatements(Stopwatch timer)
private IEnumerable<(sqlite3_stmt Statement, int ParamCount)> PrepareAndEnumerateStatements(Stopwatch timer)
{
DisposePreparedStatements(disposing: false);

Expand Down Expand Up @@ -510,9 +509,12 @@ private IEnumerable<sqlite3_stmt> PrepareAndEnumerateStatements(Stopwatch timer)
break;
}

_preparedStatements.Add(stmt);
var paramsCount = sqlite3_bind_parameter_count(stmt);
var statementWithParams = (stmt, paramsCount);

yield return stmt;
_preparedStatements.Add(statementWithParams);

yield return statementWithParams;
}
while (start < byteCount);

Expand All @@ -530,7 +532,7 @@ private void DisposePreparedStatements(bool disposing = true)

if (_preparedStatements != null)
{
foreach (var stmt in _preparedStatements)
foreach ((var stmt, _) in _preparedStatements)
{
stmt.Dispose();
}
Expand Down