Skip to content

Commit 2f9934e

Browse files
fix: adding LAST_INSERT_ID query only on :execlastid annotation
1 parent d989cac commit 2f9934e

15 files changed

Lines changed: 57 additions & 52 deletions

File tree

Drivers/MySqlConnectorDriver.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,14 @@ public override string TransformQueryText(Query query)
283283
if (query.Cmd == ":copyfrom")
284284
return string.Empty;
285285

286-
var queryText = Options.UseDapper ? $"{query.Text}; SELECT LAST_INSERT_ID()" : query.Text;
287286
var counter = 0;
288-
return QueryParamRegex().Replace(queryText, _ => "@" + query.Params[counter++].Column.Name);
287+
var queryText = query.Text;
288+
queryText = QueryParamRegex().Replace(queryText, _ => "@" + query.Params[counter++].Column.Name);
289+
queryText = Options.UseDapper && query.Cmd == ":execlastid"
290+
? $"{queryText}; SELECT LAST_INSERT_ID()"
291+
: queryText;
292+
293+
return queryText;
289294
}
290295

291296
[GeneratedRegex(@"\?")]

examples/MySqlConnectorDapperExample/QuerySql.cs

Lines changed: 21 additions & 21 deletions
Large diffs are not rendered by default.

examples/MySqlConnectorDapperLegacyExample/QuerySql.cs

Lines changed: 21 additions & 21 deletions
Large diffs are not rendered by default.

examples/SqliteDapperExample/QuerySql.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class GetAuthorArgs
6969
return await this.Transaction.Connection.QueryFirstOrDefaultAsync<GetAuthorRow?>(GetAuthorSql, queryParams, transaction: this.Transaction);
7070
}
7171

72-
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name LIMIT @limit OFFSET @offset ";
72+
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name LIMIT @limit OFFSET @offset ";
7373
public class ListAuthorsRow
7474
{
7575
public required int Id { get; init; }

examples/SqliteDapperExample/request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
"filename": "query.sql"
226226
},
227227
{
228-
"text": "SELECT id, name, bio \nFROM authors\nORDER BY name\nLIMIT ?2\nOFFSET ?1",
228+
"text": "SELECT id, name, bio\nFROM authors\nORDER BY name\nLIMIT ?2 OFFSET ?1",
229229
"name": "ListAuthors",
230230
"cmd": ":many",
231231
"columns": [
-1 Bytes
Binary file not shown.

examples/SqliteDapperLegacyExample/QuerySql.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public async Task<GetAuthorRow> GetAuthor(GetAuthorArgs args)
7070
return await this.Transaction.Connection.QueryFirstOrDefaultAsync<GetAuthorRow>(GetAuthorSql, queryParams, transaction: this.Transaction);
7171
}
7272

73-
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name LIMIT @limit OFFSET @offset ";
73+
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name LIMIT @limit OFFSET @offset ";
7474
public class ListAuthorsRow
7575
{
7676
public int Id { get; set; }

examples/SqliteDapperLegacyExample/request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
"filename": "query.sql"
226226
},
227227
{
228-
"text": "SELECT id, name, bio \nFROM authors\nORDER BY name\nLIMIT ?2\nOFFSET ?1",
228+
"text": "SELECT id, name, bio\nFROM authors\nORDER BY name\nLIMIT ?2 OFFSET ?1",
229229
"name": "ListAuthors",
230230
"cmd": ":many",
231231
"columns": [
-1 Bytes
Binary file not shown.

examples/SqliteExample/QuerySql.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static QuerySql WithTransaction(SqliteTransaction transaction)
9292
return null;
9393
}
9494

95-
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name LIMIT @limit OFFSET @offset ";
95+
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name LIMIT @limit OFFSET @offset ";
9696
public readonly record struct ListAuthorsRow(int Id, string Name, string? Bio);
9797
public readonly record struct ListAuthorsArgs(int Offset, int Limit);
9898
public async Task<List<ListAuthorsRow>> ListAuthors(ListAuthorsArgs args)

0 commit comments

Comments
 (0)