Skip to content

Commit 5c676bb

Browse files
[release] [minor] fix: sqlite arguments regex (#264)
* fix: sqlite arguments regex * fix: lint * fix: adding LAST_INSERT_ID query only on :execlastid annotation
1 parent 4dee8d2 commit 5c676bb

54 files changed

Lines changed: 742 additions & 276 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Drivers/MySqlConnectorDriver.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public partial class MySqlConnectorDriver(
9696
new()
9797
{
9898
{ "int", new() },
99+
{ "integer", new() },
99100
{ "mediumint", new() }
100101
},
101102
ordinal => $"reader.GetInt32({ordinal})",
@@ -282,9 +283,14 @@ public override string TransformQueryText(Query query)
282283
if (query.Cmd == ":copyfrom")
283284
return string.Empty;
284285

285-
var queryText = Options.UseDapper ? $"{query.Text}; SELECT LAST_INSERT_ID()" : query.Text;
286286
var counter = 0;
287-
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;
288294
}
289295

290296
[GeneratedRegex(@"\?")]

Drivers/SqliteDriver.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
22
using Plugin;
33
using SqlcGenCsharp.Drivers.Generators;
4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Text.RegularExpressions;
@@ -12,7 +13,7 @@ public partial class SqliteDriver(
1213
Options options,
1314
string defaultSchema,
1415
Dictionary<string, Dictionary<string, Table>> tables,
15-
Dictionary<string, Dictionary<string, Enum>> enums,
16+
Dictionary<string, Dictionary<string, Plugin.Enum>> enums,
1617
IList<Query> queries) :
1718
DbDriver(options, defaultSchema, tables, enums, queries), IOne, IMany, IExec, IExecRows, IExecLastId, ICopyFrom
1819
{
@@ -130,13 +131,18 @@ public override string CreateSqlCommand(string sqlTextConstant)
130131

131132
public override string TransformQueryText(Query query)
132133
{
133-
var counter = 0;
134-
var transformedQueryText = QueryParamRegex().Replace(query.Text, _ => "@" + query.Params[counter++].Column.Name);
135-
return transformedQueryText;
136-
}
134+
var areArgumentsNumbered = new Regex($@"\?\d\b*").IsMatch(query.Text);
135+
var queryText = query.Text;
137136

138-
[GeneratedRegex(@"\?\d*")]
139-
private static partial Regex QueryParamRegex();
137+
for (var i = 0; i < query.Params.Count; i++)
138+
{
139+
var currentParameter = query.Params[i];
140+
var column = GetColumnFromParam(currentParameter, query);
141+
var regexToUse = areArgumentsNumbered ? $@"\?{i + 1}\b*" : $@"\?\b*";
142+
queryText = new Regex(regexToUse).Replace(queryText, $"@{column.Name}", 1);
143+
}
144+
return queryText;
145+
}
140146

141147
public MemberDeclarationSyntax OneDeclare(string queryTextConstant, string argInterface,
142148
string returnInterface, Query query)

end2end/EndToEndScaffold/Templates/AnnotationTests.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,28 @@ public async Task TestMany()
6060
Bio = {{Consts.DrSeussQuote}}
6161
}
6262
};
63-
var actual = await this.QuerySql.ListAuthors();
64-
Assert.That(SequenceEquals(expected, actual));
63+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs
64+
{
65+
Limit = 2,
66+
Offset = 0
67+
});
68+
AssertSequenceEquals(expected, actual);
6569
}
6670
67-
private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
71+
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
6872
{
69-
return x.Id.Equals(y.Id) &&
70-
x.Name.Equals(y.Name) &&
71-
x.Bio.Equals(y.Bio);
73+
Assert.That(x.Id, Is.EqualTo(y.Id));
74+
Assert.That(x.Name, Is.EqualTo(y.Name));
75+
Assert.That(x.Bio, Is.EqualTo(y.Bio));
7276
}
7377
74-
private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
78+
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
7579
{
76-
if (x.Count != y.Count) return false;
77-
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
78-
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
79-
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
80+
Assert.That(x.Count, Is.EqualTo(y.Count));
81+
for (int i = 0; i < x.Count; i++)
82+
{
83+
AssertSingularEquals(x[i], y[i]);
84+
}
8085
}
8186
"""
8287
},
@@ -128,8 +133,12 @@ public async Task TestExecRows()
128133
Bio = {{Consts.GenericQuote1}}
129134
}
130135
};
131-
var actual = await this.QuerySql.ListAuthors();
132-
Assert.That(SequenceEquals(expected, actual));
136+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs
137+
{
138+
Limit = 2,
139+
Offset = 0
140+
});
141+
AssertSequenceEquals(expected, actual);
133142
}
134143
""",
135144
},

end2end/EndToEndTests/MySqlConnectorDapperTester.generated.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,24 @@ public async Task TestMany()
5252
Bio = "You'll miss the best things if you keep your eyes shut"
5353
}
5454
};
55-
var actual = await this.QuerySql.ListAuthors();
56-
Assert.That(SequenceEquals(expected, actual));
55+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
56+
AssertSequenceEquals(expected, actual);
5757
}
5858

59-
private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
59+
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
6060
{
61-
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
61+
Assert.That(x.Id, Is.EqualTo(y.Id));
62+
Assert.That(x.Name, Is.EqualTo(y.Name));
63+
Assert.That(x.Bio, Is.EqualTo(y.Bio));
6264
}
6365

64-
private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
66+
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
6567
{
66-
if (x.Count != y.Count)
67-
return false;
68-
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
69-
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
70-
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
68+
Assert.That(x.Count, Is.EqualTo(y.Count));
69+
for (int i = 0; i < x.Count; i++)
70+
{
71+
AssertSingularEquals(x[i], y[i]);
72+
}
7173
}
7274

7375
[Test]
@@ -102,8 +104,8 @@ public async Task TestExecRows()
102104
Bio = "Quote that everyone always attribute to Einstein"
103105
}
104106
};
105-
var actual = await this.QuerySql.ListAuthors();
106-
Assert.That(SequenceEquals(expected, actual));
107+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
108+
AssertSequenceEquals(expected, actual);
107109
}
108110

109111
[Test]

end2end/EndToEndTests/MySqlConnectorTester.generated.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,24 @@ public async Task TestMany()
5252
Bio = "You'll miss the best things if you keep your eyes shut"
5353
}
5454
};
55-
var actual = await this.QuerySql.ListAuthors();
56-
Assert.That(SequenceEquals(expected, actual));
55+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
56+
AssertSequenceEquals(expected, actual);
5757
}
5858

59-
private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
59+
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
6060
{
61-
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
61+
Assert.That(x.Id, Is.EqualTo(y.Id));
62+
Assert.That(x.Name, Is.EqualTo(y.Name));
63+
Assert.That(x.Bio, Is.EqualTo(y.Bio));
6264
}
6365

64-
private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
66+
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
6567
{
66-
if (x.Count != y.Count)
67-
return false;
68-
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
69-
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
70-
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
68+
Assert.That(x.Count, Is.EqualTo(y.Count));
69+
for (int i = 0; i < x.Count; i++)
70+
{
71+
AssertSingularEquals(x[i], y[i]);
72+
}
7173
}
7274

7375
[Test]
@@ -102,8 +104,8 @@ public async Task TestExecRows()
102104
Bio = "Quote that everyone always attribute to Einstein"
103105
}
104106
};
105-
var actual = await this.QuerySql.ListAuthors();
106-
Assert.That(SequenceEquals(expected, actual));
107+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
108+
AssertSequenceEquals(expected, actual);
107109
}
108110

109111
[Test]

end2end/EndToEndTests/NpgsqlDapperTester.generated.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,24 @@ public async Task TestMany()
5555
Bio = "You'll miss the best things if you keep your eyes shut"
5656
}
5757
};
58-
var actual = await this.QuerySql.ListAuthors();
59-
Assert.That(SequenceEquals(expected, actual));
58+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
59+
AssertSequenceEquals(expected, actual);
6060
}
6161

62-
private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
62+
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
6363
{
64-
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
64+
Assert.That(x.Id, Is.EqualTo(y.Id));
65+
Assert.That(x.Name, Is.EqualTo(y.Name));
66+
Assert.That(x.Bio, Is.EqualTo(y.Bio));
6567
}
6668

67-
private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
69+
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
6870
{
69-
if (x.Count != y.Count)
70-
return false;
71-
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
72-
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
73-
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
71+
Assert.That(x.Count, Is.EqualTo(y.Count));
72+
for (int i = 0; i < x.Count; i++)
73+
{
74+
AssertSingularEquals(x[i], y[i]);
75+
}
7476
}
7577

7678
[Test]
@@ -105,8 +107,8 @@ public async Task TestExecRows()
105107
Bio = "Quote that everyone always attribute to Einstein"
106108
}
107109
};
108-
var actual = await this.QuerySql.ListAuthors();
109-
Assert.That(SequenceEquals(expected, actual));
110+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
111+
AssertSequenceEquals(expected, actual);
110112
}
111113

112114
[Test]

end2end/EndToEndTests/NpgsqlTester.generated.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,24 @@ public async Task TestMany()
5555
Bio = "You'll miss the best things if you keep your eyes shut"
5656
}
5757
};
58-
var actual = await this.QuerySql.ListAuthors();
59-
Assert.That(SequenceEquals(expected, actual));
58+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
59+
AssertSequenceEquals(expected, actual);
6060
}
6161

62-
private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
62+
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
6363
{
64-
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
64+
Assert.That(x.Id, Is.EqualTo(y.Id));
65+
Assert.That(x.Name, Is.EqualTo(y.Name));
66+
Assert.That(x.Bio, Is.EqualTo(y.Bio));
6567
}
6668

67-
private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
69+
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
6870
{
69-
if (x.Count != y.Count)
70-
return false;
71-
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
72-
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
73-
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
71+
Assert.That(x.Count, Is.EqualTo(y.Count));
72+
for (int i = 0; i < x.Count; i++)
73+
{
74+
AssertSingularEquals(x[i], y[i]);
75+
}
7476
}
7577

7678
[Test]
@@ -105,8 +107,8 @@ public async Task TestExecRows()
105107
Bio = "Quote that everyone always attribute to Einstein"
106108
}
107109
};
108-
var actual = await this.QuerySql.ListAuthors();
109-
Assert.That(SequenceEquals(expected, actual));
110+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
111+
AssertSequenceEquals(expected, actual);
110112
}
111113

112114
[Test]

end2end/EndToEndTests/SqliteDapperTester.generated.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,24 @@ public async Task TestMany()
5151
Bio = "You'll miss the best things if you keep your eyes shut"
5252
}
5353
};
54-
var actual = await this.QuerySql.ListAuthors();
55-
Assert.That(SequenceEquals(expected, actual));
54+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
55+
AssertSequenceEquals(expected, actual);
5656
}
5757

58-
private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
58+
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
5959
{
60-
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
60+
Assert.That(x.Id, Is.EqualTo(y.Id));
61+
Assert.That(x.Name, Is.EqualTo(y.Name));
62+
Assert.That(x.Bio, Is.EqualTo(y.Bio));
6163
}
6264

63-
private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
65+
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
6466
{
65-
if (x.Count != y.Count)
66-
return false;
67-
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
68-
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
69-
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
67+
Assert.That(x.Count, Is.EqualTo(y.Count));
68+
for (int i = 0; i < x.Count; i++)
69+
{
70+
AssertSingularEquals(x[i], y[i]);
71+
}
7072
}
7173

7274
[Test]
@@ -101,8 +103,8 @@ public async Task TestExecRows()
101103
Bio = "Quote that everyone always attribute to Einstein"
102104
}
103105
};
104-
var actual = await this.QuerySql.ListAuthors();
105-
Assert.That(SequenceEquals(expected, actual));
106+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
107+
AssertSequenceEquals(expected, actual);
106108
}
107109

108110
[Test]

end2end/EndToEndTests/SqliteTester.generated.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,24 @@ public async Task TestMany()
5151
Bio = "You'll miss the best things if you keep your eyes shut"
5252
}
5353
};
54-
var actual = await this.QuerySql.ListAuthors();
55-
Assert.That(SequenceEquals(expected, actual));
54+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
55+
AssertSequenceEquals(expected, actual);
5656
}
5757

58-
private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
58+
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
5959
{
60-
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
60+
Assert.That(x.Id, Is.EqualTo(y.Id));
61+
Assert.That(x.Name, Is.EqualTo(y.Name));
62+
Assert.That(x.Bio, Is.EqualTo(y.Bio));
6163
}
6264

63-
private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
65+
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
6466
{
65-
if (x.Count != y.Count)
66-
return false;
67-
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
68-
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
69-
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
67+
Assert.That(x.Count, Is.EqualTo(y.Count));
68+
for (int i = 0; i < x.Count; i++)
69+
{
70+
AssertSingularEquals(x[i], y[i]);
71+
}
7072
}
7173

7274
[Test]
@@ -101,8 +103,8 @@ public async Task TestExecRows()
101103
Bio = "Quote that everyone always attribute to Einstein"
102104
}
103105
};
104-
var actual = await this.QuerySql.ListAuthors();
105-
Assert.That(SequenceEquals(expected, actual));
106+
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
107+
AssertSequenceEquals(expected, actual);
106108
}
107109

108110
[Test]

0 commit comments

Comments
 (0)