Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Drivers/MySqlConnectorDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public partial class MySqlConnectorDriver(
new()
{
{ "int", new() },
{ "integer", new() },
{ "mediumint", new() }
},
ordinal => $"reader.GetInt32({ordinal})",
Expand Down Expand Up @@ -282,9 +283,14 @@ public override string TransformQueryText(Query query)
if (query.Cmd == ":copyfrom")
return string.Empty;

var queryText = Options.UseDapper ? $"{query.Text}; SELECT LAST_INSERT_ID()" : query.Text;
var counter = 0;
return QueryParamRegex().Replace(queryText, _ => "@" + query.Params[counter++].Column.Name);
var queryText = query.Text;
queryText = QueryParamRegex().Replace(queryText, _ => "@" + query.Params[counter++].Column.Name);
queryText = Options.UseDapper && query.Cmd == ":execlastid"
? $"{queryText}; SELECT LAST_INSERT_ID()"
: queryText;

return queryText;
}

[GeneratedRegex(@"\?")]
Expand Down
20 changes: 13 additions & 7 deletions Drivers/SqliteDriver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Plugin;
using SqlcGenCsharp.Drivers.Generators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand All @@ -12,7 +13,7 @@ public partial class SqliteDriver(
Options options,
string defaultSchema,
Dictionary<string, Dictionary<string, Table>> tables,
Dictionary<string, Dictionary<string, Enum>> enums,
Dictionary<string, Dictionary<string, Plugin.Enum>> enums,
IList<Query> queries) :
DbDriver(options, defaultSchema, tables, enums, queries), IOne, IMany, IExec, IExecRows, IExecLastId, ICopyFrom
{
Expand Down Expand Up @@ -130,13 +131,18 @@ public override string CreateSqlCommand(string sqlTextConstant)

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

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

public MemberDeclarationSyntax OneDeclare(string queryTextConstant, string argInterface,
string returnInterface, Query query)
Expand Down
35 changes: 22 additions & 13 deletions end2end/EndToEndScaffold/Templates/AnnotationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,28 @@ public async Task TestMany()
Bio = {{Consts.DrSeussQuote}}
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs
{
Limit = 2,
Offset = 0
});
AssertSequenceEquals(expected, actual);
}

private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
{
return x.Id.Equals(y.Id) &&
x.Name.Equals(y.Name) &&
x.Bio.Equals(y.Bio);
Assert.That(x.Id, Is.EqualTo(y.Id));
Assert.That(x.Name, Is.EqualTo(y.Name));
Assert.That(x.Bio, Is.EqualTo(y.Bio));
}

private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
{
if (x.Count != y.Count) return false;
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
Assert.That(x.Count, Is.EqualTo(y.Count));
for (int i = 0; i < x.Count; i++)
{
AssertSingularEquals(x[i], y[i]);
}
}
"""
},
Expand Down Expand Up @@ -128,8 +133,12 @@ public async Task TestExecRows()
Bio = {{Consts.GenericQuote1}}
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs
{
Limit = 2,
Offset = 0
});
AssertSequenceEquals(expected, actual);
}
""",
},
Expand Down
26 changes: 14 additions & 12 deletions end2end/EndToEndTests/MySqlConnectorDapperTester.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,24 @@ public async Task TestMany()
Bio = "You'll miss the best things if you keep your eyes shut"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
{
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
Assert.That(x.Id, Is.EqualTo(y.Id));
Assert.That(x.Name, Is.EqualTo(y.Name));
Assert.That(x.Bio, Is.EqualTo(y.Bio));
}

private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
{
if (x.Count != y.Count)
return false;
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
Assert.That(x.Count, Is.EqualTo(y.Count));
for (int i = 0; i < x.Count; i++)
{
AssertSingularEquals(x[i], y[i]);
}
}

[Test]
Expand Down Expand Up @@ -102,8 +104,8 @@ public async Task TestExecRows()
Bio = "Quote that everyone always attribute to Einstein"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

[Test]
Expand Down
26 changes: 14 additions & 12 deletions end2end/EndToEndTests/MySqlConnectorTester.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,24 @@ public async Task TestMany()
Bio = "You'll miss the best things if you keep your eyes shut"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
{
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
Assert.That(x.Id, Is.EqualTo(y.Id));
Assert.That(x.Name, Is.EqualTo(y.Name));
Assert.That(x.Bio, Is.EqualTo(y.Bio));
}

private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
{
if (x.Count != y.Count)
return false;
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
Assert.That(x.Count, Is.EqualTo(y.Count));
for (int i = 0; i < x.Count; i++)
{
AssertSingularEquals(x[i], y[i]);
}
}

[Test]
Expand Down Expand Up @@ -102,8 +104,8 @@ public async Task TestExecRows()
Bio = "Quote that everyone always attribute to Einstein"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

[Test]
Expand Down
26 changes: 14 additions & 12 deletions end2end/EndToEndTests/NpgsqlDapperTester.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ public async Task TestMany()
Bio = "You'll miss the best things if you keep your eyes shut"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
{
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
Assert.That(x.Id, Is.EqualTo(y.Id));
Assert.That(x.Name, Is.EqualTo(y.Name));
Assert.That(x.Bio, Is.EqualTo(y.Bio));
}

private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
{
if (x.Count != y.Count)
return false;
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
Assert.That(x.Count, Is.EqualTo(y.Count));
for (int i = 0; i < x.Count; i++)
{
AssertSingularEquals(x[i], y[i]);
}
}

[Test]
Expand Down Expand Up @@ -105,8 +107,8 @@ public async Task TestExecRows()
Bio = "Quote that everyone always attribute to Einstein"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

[Test]
Expand Down
26 changes: 14 additions & 12 deletions end2end/EndToEndTests/NpgsqlTester.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ public async Task TestMany()
Bio = "You'll miss the best things if you keep your eyes shut"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
{
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
Assert.That(x.Id, Is.EqualTo(y.Id));
Assert.That(x.Name, Is.EqualTo(y.Name));
Assert.That(x.Bio, Is.EqualTo(y.Bio));
}

private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
{
if (x.Count != y.Count)
return false;
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
Assert.That(x.Count, Is.EqualTo(y.Count));
for (int i = 0; i < x.Count; i++)
{
AssertSingularEquals(x[i], y[i]);
}
}

[Test]
Expand Down Expand Up @@ -105,8 +107,8 @@ public async Task TestExecRows()
Bio = "Quote that everyone always attribute to Einstein"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

[Test]
Expand Down
26 changes: 14 additions & 12 deletions end2end/EndToEndTests/SqliteDapperTester.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,24 @@ public async Task TestMany()
Bio = "You'll miss the best things if you keep your eyes shut"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
{
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
Assert.That(x.Id, Is.EqualTo(y.Id));
Assert.That(x.Name, Is.EqualTo(y.Name));
Assert.That(x.Bio, Is.EqualTo(y.Bio));
}

private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
{
if (x.Count != y.Count)
return false;
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
Assert.That(x.Count, Is.EqualTo(y.Count));
for (int i = 0; i < x.Count; i++)
{
AssertSingularEquals(x[i], y[i]);
}
}

[Test]
Expand Down Expand Up @@ -101,8 +103,8 @@ public async Task TestExecRows()
Bio = "Quote that everyone always attribute to Einstein"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

[Test]
Expand Down
26 changes: 14 additions & 12 deletions end2end/EndToEndTests/SqliteTester.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,24 @@ public async Task TestMany()
Bio = "You'll miss the best things if you keep your eyes shut"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

private static bool SingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
private static void AssertSingularEquals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y)
{
return x.Id.Equals(y.Id) && x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio);
Assert.That(x.Id, Is.EqualTo(y.Id));
Assert.That(x.Name, Is.EqualTo(y.Name));
Assert.That(x.Bio, Is.EqualTo(y.Bio));
}

private static bool SequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
private static void AssertSequenceEquals(List<QuerySql.ListAuthorsRow> x, List<QuerySql.ListAuthorsRow> y)
{
if (x.Count != y.Count)
return false;
x = x.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
y = y.OrderBy<QuerySql.ListAuthorsRow, object>(o => o.Id).ToList();
return !x.Where((t, i) => !SingularEquals(t, y[i])).Any();
Assert.That(x.Count, Is.EqualTo(y.Count));
for (int i = 0; i < x.Count; i++)
{
AssertSingularEquals(x[i], y[i]);
}
}

[Test]
Expand Down Expand Up @@ -101,8 +103,8 @@ public async Task TestExecRows()
Bio = "Quote that everyone always attribute to Einstein"
}
};
var actual = await this.QuerySql.ListAuthors();
Assert.That(SequenceEquals(expected, actual));
var actual = await this.QuerySql.ListAuthors(new QuerySql.ListAuthorsArgs { Limit = 2, Offset = 0 });
AssertSequenceEquals(expected, actual);
}

[Test]
Expand Down
Loading
Loading