diff --git a/src/Microsoft.Data.Sqlite.Core/SqliteCommand.cs b/src/Microsoft.Data.Sqlite.Core/SqliteCommand.cs index 0dee589f365..ab751148755 100644 --- a/src/Microsoft.Data.Sqlite.Core/SqliteCommand.cs +++ b/src/Microsoft.Data.Sqlite.Core/SqliteCommand.cs @@ -256,9 +256,7 @@ public override void Prepare() return; } - var timer = new Stopwatch(); - - using var enumerator = PrepareAndEnumerateStatements(timer).GetEnumerator(); + using var enumerator = PrepareAndEnumerateStatements(PrepareTimer()).GetEnumerator(); while (enumerator.MoveNext()) { } @@ -307,15 +305,26 @@ public override void Prepare() throw new InvalidOperationException(Resources.TransactionCompleted); } - var timer = new Stopwatch(); var closeConnection = behavior.HasFlag(CommandBehavior.CloseConnection); + Stopwatch timer = PrepareTimer(); + var dataReader = new SqliteDataReader(this, timer, GetStatements(timer), closeConnection); dataReader.NextResult(); return DataReader = dataReader; } + private Stopwatch PrepareTimer() + { + if (_connection!.HasMultipleCommands) + { + return new(); + } + _connection.Timer.Reset(); + return _connection.Timer; + } + private IEnumerable GetStatements(Stopwatch timer) { foreach ((var stmt, var expectedParams) in !_prepared @@ -477,6 +486,7 @@ public override void Cancel() int rc; sqlite3_stmt stmt; var start = 0; + timer.Reset(); do { timer.Start(); diff --git a/src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs b/src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs index b28d4602e9e..e5dfa647fcf 100644 --- a/src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs +++ b/src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs @@ -28,6 +28,8 @@ public partial class SqliteConnection : DbConnection private readonly List> _commands = new(); + internal bool HasMultipleCommands => _commands.Count > 1; + private Dictionary? _collations; private Dictionary<(string name, int arity), (int flags, object? state, delegate_function_scalar? func)>? _functions; @@ -139,6 +141,15 @@ public override string ConnectionString internal SqliteConnectionStringBuilder ConnectionOptions => PoolGroup.ConnectionOptions; + internal Stopwatch Timer + { + get + { + Debug.Assert(_innerConnection != null); + return _innerConnection!.Timer; + } + } + /// /// Gets the name of the current database. Always 'main'. /// diff --git a/src/Microsoft.Data.Sqlite.Core/SqliteConnectionInternal.cs b/src/Microsoft.Data.Sqlite.Core/SqliteConnectionInternal.cs index 60c48d4ab10..6f0e9f003b3 100644 --- a/src/Microsoft.Data.Sqlite.Core/SqliteConnectionInternal.cs +++ b/src/Microsoft.Data.Sqlite.Core/SqliteConnectionInternal.cs @@ -106,6 +106,8 @@ public bool CanBePooled public sqlite3? Handle => _db; + public Stopwatch Timer { get; } = new(); + public void DoNotPool() => _canBePooled = false;