Skip to content

Commit 6b8d862

Browse files
authored
support :execrows for sqlite (#161)
1 parent 922e4fc commit 6b8d862

File tree

10 files changed

+146
-1
lines changed

10 files changed

+146
-1
lines changed

Drivers/SqliteDriver.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace SqlcGenCsharp.Drivers;
1010

11-
public partial class SqliteDriver(Options options) : DbDriver(options)
11+
public partial class SqliteDriver(Options options) : DbDriver(options), IExecRows
1212
{
1313
protected override List<ColumnMapping> ColumnMappings { get; } = [
1414
new("byte[]", ordinal => $"Utils.GetBytes(reader, {ordinal})",
@@ -79,4 +79,9 @@ public override MemberDeclarationSyntax ManyDeclare(string queryTextConstant, st
7979

8080
[GeneratedRegex(@"\?")]
8181
private static partial Regex BindParameterRegex();
82+
83+
public MemberDeclarationSyntax ExecRowsDeclare(string queryTextConstant, string argInterface, Query query)
84+
{
85+
return new ExecRowsDeclareGen(this).Generate(queryTextConstant, argInterface, query);
86+
}
8287
}

EndToEndTests/SqliteDapperTester.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,23 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs
100100
Bio: DataGenerator.DrSeussQuote
101101
});
102102
}
103+
104+
[Test]
105+
public async Task TestExecRowsFlow()
106+
{
107+
var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs
108+
{
109+
Name = DataGenerator.GenericAuthor,
110+
Bio = DataGenerator.GenericQuote1
111+
};
112+
await QuerySql.CreateAuthor(bojackCreateAuthorArgs);
113+
await QuerySql.CreateAuthor(bojackCreateAuthorArgs);
114+
115+
var updateAuthorsArgs = new QuerySql.UpdateAuthorsArgs
116+
{
117+
Bio = DataGenerator.GenericQuote2
118+
};
119+
var affectedRows = await QuerySql.UpdateAuthors(updateAuthorsArgs);
120+
ClassicAssert.AreEqual(2, affectedRows);
121+
}
103122
}

EndToEndTests/SqliteTester.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,23 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs
100100
Bio: DataGenerator.DrSeussQuote
101101
});
102102
}
103+
104+
[Test]
105+
public async Task TestExecRowsFlow()
106+
{
107+
var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs
108+
{
109+
Name = DataGenerator.GenericAuthor,
110+
Bio = DataGenerator.GenericQuote1
111+
};
112+
await QuerySql.CreateAuthor(bojackCreateAuthorArgs);
113+
await QuerySql.CreateAuthor(bojackCreateAuthorArgs);
114+
115+
var updateAuthorsArgs = new QuerySql.UpdateAuthorsArgs
116+
{
117+
Bio = DataGenerator.GenericQuote2
118+
};
119+
var affectedRows = await QuerySql.UpdateAuthors(updateAuthorsArgs);
120+
ClassicAssert.AreEqual(2, affectedRows);
121+
}
103122
}

LegacyEndToEndTests/SqliteDapperTester.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,24 @@ private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySq
108108
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Name + o.Bio).ToList();
109109
return !x.Where((t, i) => !Equals(t, y[i])).Any();
110110
}
111+
112+
[Test]
113+
public async Task TestExecRowsFlow()
114+
{
115+
var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs
116+
{
117+
Name = DataGenerator.GenericAuthor,
118+
Bio = DataGenerator.GenericQuote1
119+
};
120+
await QuerySql.CreateAuthor(bojackCreateAuthorArgs);
121+
await QuerySql.CreateAuthor(bojackCreateAuthorArgs);
122+
123+
var updateAuthorsArgs = new QuerySql.UpdateAuthorsArgs
124+
{
125+
Bio = DataGenerator.GenericQuote2
126+
};
127+
var affectedRows = await QuerySql.UpdateAuthors(updateAuthorsArgs);
128+
ClassicAssert.AreEqual(2, affectedRows);
129+
}
111130
}
112131
}

LegacyEndToEndTests/SqliteTester.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs
9191
ClassicAssert.IsNull(actual);
9292
}
9393

94+
[Test]
95+
public async Task TestExecRowsFlow()
96+
{
97+
var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs
98+
{
99+
Name = DataGenerator.GenericAuthor,
100+
Bio = DataGenerator.GenericQuote1
101+
};
102+
await QuerySql.CreateAuthor(bojackCreateAuthorArgs);
103+
await QuerySql.CreateAuthor(bojackCreateAuthorArgs);
104+
105+
var updateAuthorsArgs = new QuerySql.UpdateAuthorsArgs
106+
{
107+
Bio = DataGenerator.GenericQuote2
108+
};
109+
var affectedRows = await QuerySql.UpdateAuthors(updateAuthorsArgs);
110+
ClassicAssert.AreEqual(2, affectedRows);
111+
}
112+
94113
private static bool Equals(QuerySql.GetAuthorRow x, QuerySql.GetAuthorRow y)
95114
{
96115
return x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);

examples/SqliteDapperExample/QuerySql.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ public async Task CreateAuthor(CreateAuthorArgs args)
6060
}
6161
}
6262

63+
private const string UpdateAuthorsSql = "UPDATE authors SET bio = @bio WHERE bio IS NOT NULL ";
64+
public class UpdateAuthorsArgs
65+
{
66+
public string? Bio { get; set; }
67+
};
68+
public async Task<long> UpdateAuthors(UpdateAuthorsArgs args)
69+
{
70+
using (var connection = new SqliteConnection(connectionString))
71+
{
72+
return await connection.ExecuteAsync(UpdateAuthorsSql, new { bio = args.Bio });
73+
}
74+
}
75+
6376
private const string DeleteAuthorSql = "DELETE FROM authors WHERE name = @name";
6477
public class DeleteAuthorArgs
6578
{

examples/SqliteDapperLegacyExample/QuerySql.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ public async Task CreateAuthor(CreateAuthorArgs args)
6868
}
6969
}
7070

71+
private const string UpdateAuthorsSql = "UPDATE authors SET bio = @bio WHERE bio IS NOT NULL ";
72+
public class UpdateAuthorsArgs
73+
{
74+
public string Bio { get; set; }
75+
};
76+
public async Task<long> UpdateAuthors(UpdateAuthorsArgs args)
77+
{
78+
using (var connection = new SqliteConnection(ConnectionString))
79+
{
80+
return await connection.ExecuteAsync(UpdateAuthorsSql, new { bio = args.Bio });
81+
}
82+
}
83+
7184
private const string DeleteAuthorSql = "DELETE FROM authors WHERE name = @name";
7285
public class DeleteAuthorArgs
7386
{

examples/SqliteExample/QuerySql.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ public async Task CreateAuthor(CreateAuthorArgs args)
6767
}
6868
}
6969

70+
private const string UpdateAuthorsSql = "UPDATE authors SET bio = @bio WHERE bio IS NOT NULL ";
71+
public readonly record struct UpdateAuthorsArgs(string? Bio);
72+
public async Task<long> UpdateAuthors(UpdateAuthorsArgs args)
73+
{
74+
{
75+
await using var connection = new SqliteConnection(connectionString);
76+
connection.Open();
77+
await using var command = new SqliteCommand(UpdateAuthorsSql, connection);
78+
command.Parameters.AddWithValue("@bio", args.Bio!);
79+
return await command.ExecuteNonQueryAsync();
80+
}
81+
}
82+
7083
private const string DeleteAuthorSql = "DELETE FROM authors WHERE name = @name";
7184
public readonly record struct DeleteAuthorArgs(string Name);
7285
public async Task DeleteAuthor(DeleteAuthorArgs args)

examples/SqliteLegacyExample/QuerySql.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ public async Task CreateAuthor(CreateAuthorArgs args)
108108
}
109109
}
110110

111+
private const string UpdateAuthorsSql = "UPDATE authors SET bio = @bio WHERE bio IS NOT NULL ";
112+
public class UpdateAuthorsArgs
113+
{
114+
public string Bio { get; set; }
115+
};
116+
public async Task<long> UpdateAuthors(UpdateAuthorsArgs args)
117+
{
118+
{
119+
using (var connection = new SqliteConnection(ConnectionString))
120+
{
121+
connection.Open();
122+
using (var command = new SqliteCommand(UpdateAuthorsSql, connection))
123+
{
124+
command.Parameters.AddWithValue("@bio", args.Bio);
125+
return await command.ExecuteNonQueryAsync();
126+
}
127+
}
128+
}
129+
}
130+
111131
private const string DeleteAuthorSql = "DELETE FROM authors WHERE name = @name";
112132
public class DeleteAuthorArgs
113133
{

examples/config/sqlite/query.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ SELECT * FROM authors ORDER BY name;
77
-- name: CreateAuthor :exec
88
INSERT INTO authors (name, bio) VALUES (?, ?);
99

10+
-- name: UpdateAuthors :execrows
11+
UPDATE authors
12+
SET bio = ?
13+
WHERE bio IS NOT NULL;
14+
1015
-- name: DeleteAuthor :exec
1116
DELETE FROM authors WHERE name = ?;
1217

0 commit comments

Comments
 (0)