Skip to content

Commit

Permalink
ResultSet Improvements (#12)
Browse files Browse the repository at this point in the history
* Add tests

* Implement

* Use ulong literals in test

* Update cargo.toml

* Update demo with new functionality

* Update the readme with new progress

* Update target libsql commit to latest

* Bump minor version number
  • Loading branch information
tvandinther authored Nov 17, 2023
1 parent 69f7fd3 commit 27eb76a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
var rs = await dbClient.Execute("CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT NOT NULL, `height` REAL, `data` BLOB)");

var rs1 = await dbClient.Execute("INSERT INTO `users` (`name`, `height`, `data`) VALUES ('John Doe', 182.6, X'a4c7b8e21d3f50a6b9d2e8f7c1349a0b5c6d7e218349b6d012c71e8f9a093fed'), ('Jane Doe', 0.5, X'00')");
Console.WriteLine($"Inserted {rs1.RowsAffected} rows");
Console.WriteLine($"Last inserted id: {rs1.LastInsertRowId}");
var rs2 = await dbClient.Execute("SELECT `id`, `name`, `height`, `data` FROM `users`");

Console.WriteLine();
Console.WriteLine(string.Join(", ", rs2.Columns));
Console.WriteLine("------------------------");
Console.WriteLine(string.Join("\n", rs2.Rows.Select(row => string.Join(", ", row.Select(x => x.ToString())))));
Console.WriteLine(string.Join("\n", rs2.Rows.Select(row => string.Join(", ", row.Select(x => x.ToString())))));

var user = ToUser(rs2.Rows.First());
Console.WriteLine(user);

var users = rs2.Rows.Select(ToUser);

Expand Down
48 changes: 48 additions & 0 deletions Libsql.Client.Tests/ResultSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,52 @@ public async Task Columns_NamesAreMarshalled_WhenSeveral()
Assert.Equal("column_one", columns.First());
Assert.Equal("column_two", columns.Last());
}

[Fact]
public async Task LastInsertRowId_ReturnsExpectedValue()
{
await _db.Execute("CREATE TABLE `test` (`id` INTEGER PRIMARY KEY AUTOINCREMENT)");

var rs = await _db.Execute("INSERT INTO `test` DEFAULT VALUES");

Assert.Equal(1, rs.LastInsertRowId);
}

[Fact]
public async Task LastInsertRowId_ReturnsExpectedValue_WhenMultipleInserts()
{
await _db.Execute("CREATE TABLE `test` (`id` INTEGER PRIMARY KEY AUTOINCREMENT)");

IResultSet rs;
for (int i = 0; i < 10; i++)
{
rs = await _db.Execute("INSERT INTO `test` DEFAULT VALUES");
Assert.Equal(i + 1, rs.LastInsertRowId);
}
}

[Fact]
public async Task Changes_ReturnsExpectedValue()
{
await _db.Execute("CREATE TABLE `test` (`id` INTEGER PRIMARY KEY AUTOINCREMENT)");

var rs = await _db.Execute("INSERT INTO `test` DEFAULT VALUES");

Assert.Equal(1ul, rs.RowsAffected);
}

[Fact]
public async Task Changes_ReturnsExectedValue_WhenMultipleUpdates()
{
await _db.Execute("CREATE TABLE `test` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `value` INTEGER)");

for (int i = 0; i < 10; i++)
{
var rs = await _db.Execute("INSERT INTO `test` DEFAULT VALUES");
}

var rs2 = await _db.Execute("UPDATE `test` SET `value` = 1");

Assert.Equal(10ul, rs2.RowsAffected);
}
}
4 changes: 2 additions & 2 deletions Libsql.Client/DatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public async Task<IResultSet> Execute(string sql)
error.ThrowIfNonZero(exitCode, "Failed to execute query");

return new ResultSet(
0,
0,
Bindings.libsql_last_insert_rowid(_connection),
Bindings.libsql_changes(_connection),
rows.GetColumnNames(),
new Rows(rows)
);
Expand Down
2 changes: 1 addition & 1 deletion Libsql.Client/Libsql.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Title>Libsql.Client</Title>
<Authors>Tom van Dinther</Authors>
<Description>A client library for Libsql.</Description>
<PackageVersion>0.2.3</PackageVersion>
<PackageVersion>0.3.0</PackageVersion>
<Copyright>Copyright (c) Tom van Dinther 2023</Copyright>
<PackageProjectUrl>https://github.com/tvandinther/libsql-client-dotnet</PackageProjectUrl>
<PackageLicense>https://raw.githubusercontent.com/tvandinther/libsql-client-dotnet/master/LICENSE</PackageLicense>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ using (var dbClient = DatabaseClient.Create(opts => {
- [x] A result set is returned from an execution.
- [x] With the column names.
- [x] With an enumerable of enumerable (rows) of typed boxed values.
- [ ] With the number of affected rows.
- [ ] With the last inserted row id.
- [x] With the number of affected rows.
- [x] With the last inserted row id.
2 changes: 1 addition & 1 deletion generate-bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fi
# git sparse-checkout set bindings/c bindings/wasm libsql libsql-sys
# -----
git fetch --quiet origin
git checkout 8e321cf0
git checkout 7daffe6
# -----
# git fetch --depth 1 origin tag v${LIBSQL_VERSION}
# git reset --hard tags/v${LIBSQL_VERSION}
Expand Down
8 changes: 8 additions & 0 deletions rust-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ members = [
"libsql/bindings/c"
]

[workspace.dependencies]
rusqlite = { path = "libsql/vendored/rusqlite", version = "0.29", default-features = false, features = [
"libsql-experimental",
"column_decltype",
"load_extension",
"modern_sqlite"
] }

[patch.crates-io]
sqlite3-parser = { git = "https://github.com/LucioFranco/lemon-rs" }

0 comments on commit 27eb76a

Please sign in to comment.