Skip to content
15 changes: 11 additions & 4 deletions src/Microsoft.Data.Sqlite.Core/SqliteDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public override bool NextResult()
{
stmt = _stmtEnumerator.Current;

var handle = _command.Connection!.Handle;
var totalChangesBefore = sqlite3_total_changes(handle);

var timer = SharedStopwatch.StartNew();

while (IsBusy(rc = sqlite3_step(stmt)))
Expand All @@ -172,7 +175,7 @@ public override bool NextResult()

_totalElapsedTime += timer.Elapsed;

SqliteException.ThrowExceptionForRC(rc, _command.Connection!.Handle);
SqliteException.ThrowExceptionForRC(rc, handle);

// It's a SELECT statement
if (sqlite3_column_count(stmt) != 0)
Expand All @@ -185,13 +188,17 @@ public override bool NextResult()
while (rc != SQLITE_DONE)
{
rc = sqlite3_step(stmt);
SqliteException.ThrowExceptionForRC(rc, _command.Connection.Handle);
SqliteException.ThrowExceptionForRC(rc, handle);
}

sqlite3_reset(stmt);

var changes = sqlite3_changes(_command.Connection.Handle);
AddChanges(changes);
var totalChangesAfter = sqlite3_total_changes(handle);
int changes = totalChangesAfter - totalChangesBefore;
if (changes > 0)
{
AddChanges(changes);
}
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.Data.Sqlite.Tests/SqliteConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ public void CreateFunction_deterministic_param_works()
connection.ExecuteNonQuery("CREATE TABLE Data (Value); INSERT INTO Data VALUES (0);");
connection.CreateFunction("test", (double x) => x, true);

Assert.Equal(1, connection.ExecuteNonQuery("CREATE INDEX InvalidIndex ON Data (Value) WHERE test(Value) = 0;"));
Assert.Equal(-1, connection.ExecuteNonQuery("CREATE INDEX InvalidIndex ON Data (Value) WHERE test(Value) = 0;"));
}

[Fact]
Expand Down
33 changes: 33 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,39 @@ public void RecordsAffected_works_with_returning_multiple()
}
}

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

// Test with INSERT followed by DROP TABLE
var reader = connection.ExecuteReader(
@"CREATE TABLE foo(bar TEXT NOT NULL);
CREATE TABLE xyz(aaa TEXT NOT NULL);
INSERT INTO foo(bar) VALUES('baz');
INSERT INTO foo(bar) VALUES('baz2');
DROP TABLE xyz;");
((IDisposable)reader).Dispose();

Assert.Equal(2, reader.RecordsAffected);

// Test with INSERT followed by DROP TABLE and CREATE TABLE
reader = connection.ExecuteReader(
@"DROP TABLE foo;
CREATE TABLE foo(bar TEXT NOT NULL);
CREATE TABLE xyz(aaa TEXT NOT NULL);
INSERT INTO foo(bar) VALUES('baz');
INSERT INTO foo(bar) VALUES('baz2');
DROP TABLE xyz;
CREATE TABLE xyz(aaa TEXT NOT NULL);");
((IDisposable)reader).Dispose();

Assert.Equal(2, reader.RecordsAffected);
}
}

[Fact]
public void GetSchemaTable_works()
{
Expand Down
Loading