Skip to content

Commit

Permalink
Implement statement reset
Browse files Browse the repository at this point in the history
  • Loading branch information
tvandinther committed Aug 31, 2024
1 parent 6e5052a commit 85e5e7b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Libsql.Client.Tests/StatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,33 @@ public async Task Database_CanExecute_Statements()

Assert.Equal(3ul, rowsAffected);
}

[Fact]
public async Task Statement_CanBeReset()
{
await _db.Execute("CREATE TABLE `test` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT)");
await _db.Execute("INSERT INTO `test` (`name`) VALUES ('a'), ('b'), ('c')");

using var statement = await _db.Prepare("SELECT `name` FROM `test` WHERE `id` = ?");
var firstExpected = "a";
var secondExpected = "b";

statement.Bind(new Integer(1));
var rs = await statement.Query();
var row = rs.Rows.First();
var value = row.First();
var text = Assert.IsType<Text>(value);

Assert.Equal(firstExpected, text);

statement.Reset();

statement.Bind(new Integer(2));
var rs2 = await statement.Query();
var row2 = rs2.Rows.First();
var value2 = row2.First();
var text2 = Assert.IsType<Text>(value2);

Assert.Equal(secondExpected, text2);
}
}
1 change: 1 addition & 0 deletions Libsql.Client/IStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public interface IStatement : IDisposable
void BindNull();
Task<ulong> Execute();
Task<IResultSet> Query();
void Reset();
}
}
2 changes: 1 addition & 1 deletion Libsql.Client/Rows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private unsafe Value[] ParseRow(libsql_row_t row)
_enumeratorData.ColumnTypes[i] == ValueType.Text ? row.GetText(i) :
_enumeratorData.ColumnTypes[i] == ValueType.Blob ? row.GetBlob(i) :
_enumeratorData.ColumnTypes[i] == ValueType.Null ? (Value)new Null() :
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException($"Non-exhaustive check. Could not find a case to match value of {_enumeratorData.ColumnTypes[i]}");

parsedRow[i] = value;
}
Expand Down
15 changes: 15 additions & 0 deletions Libsql.Client/StatementWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ public Task<IResultSet> Query()
return _database.Query(this);
}

public void Reset()
{
var error = new Error();
int exitCode;

unsafe {
exitCode = Bindings.libsql_reset_stmt(Stmt, &error.Ptr);
}

_bindIndex = 1;

error.ThrowIfNonZero(exitCode, "Failed to reset statement");
}


private void ReleaseUnmanagedResources()
{
Bindings.libsql_free_stmt(Stmt);
Expand Down

0 comments on commit 85e5e7b

Please sign in to comment.