Skip to content

Commit bdb9f51

Browse files
authored
support multi-line sql code generation (#352)
* working multi-line sql generation * remove unneeded single double convertor
1 parent acd05d4 commit bdb9f51

File tree

27 files changed

+2282
-272
lines changed

27 files changed

+2282
-272
lines changed

CodeGenerator/Generators/QueriesGen.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,31 @@ private IEnumerable<MemberDeclarationSyntax> GetMembersForSingleQuery(Query quer
137137
if (transformedQueryText == string.Empty)
138138
return null;
139139

140+
var memberName = ClassMember.Sql.Name(query.Name);
141+
if (transformedQueryText.IndexOfAny(['\r', '\n']) >= 0)
142+
{
143+
var queryText = transformedQueryText.Replace("\"", "\"\"");
144+
var lines = queryText.TrimEnd().Split(["\r\n", "\r", "\n"], StringSplitOptions.None);
145+
146+
// The indentation of the constant declaration itself. for legacy generation it 8 indents and for modern it 4.
147+
string memberIndentPrefix = dbDriver.Options.DotnetFramework.IsDotnetLegacy() ? " " : " ";
148+
var declaration = $"private const string {memberName} = @\"";
149+
150+
// We need to calculate the indentation for subsequent lines so they align with the first.
151+
// This is based on the member indent, the declaration length, and any leading space on the first SQL line.
152+
var firstLineLeadingSpaceCount = lines[0].TakeWhile(char.IsWhiteSpace).Count();
153+
var subsequentLineIndent = new string(' ', memberIndentPrefix.Length + declaration.Length + firstLineLeadingSpaceCount);
154+
155+
var indentedLines = lines.Skip(1).Select(line => subsequentLineIndent + line);
156+
var fullQueryString = string.Join(Environment.NewLine, [lines[0], .. indentedLines]);
157+
158+
return ParseMemberDeclaration(
159+
$"{memberIndentPrefix}{declaration}{fullQueryString}\";")!;
160+
}
161+
140162
var singleLineQueryText = LongWhitespaceRegex().Replace(transformedQueryText, " ");
141163
return ParseMemberDeclaration(
142-
$"""
143-
private const string {ClassMember.Sql.Name(query.Name)} = "{singleLineQueryText}";
144-
""")!
164+
$"""private const string {memberName} = "{singleLineQueryText}";""")!
145165
.AppendNewLine();
146166
}
147167

examples/MySqlConnectorDapperExample/QuerySql.cs

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

examples/MySqlConnectorDapperLegacyExample/QuerySql.cs

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

examples/MySqlConnectorExample/QuerySql.cs

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

examples/MySqlConnectorLegacyExample/QuerySql.cs

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

examples/NpgsqlDapperExample/QuerySql.cs

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

examples/NpgsqlDapperLegacyExample/QuerySql.cs

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

examples/NpgsqlExample/QuerySql.cs

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

examples/NpgsqlLegacyExample/QuerySql.cs

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

examples/QuickStartMySqlDalGen/QuerySql.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public static QuerySql WithTransaction(MySqlTransaction 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
96+
FROM authors
97+
ORDER BY name
98+
LIMIT @limit OFFSET @offset";
9699
public readonly record struct ListAuthorsRow(long Id, string Name, string? Bio);
97100
public readonly record struct ListAuthorsArgs(int Limit, int Offset);
98101
public async Task<List<ListAuthorsRow>> ListAuthors(ListAuthorsArgs args)
@@ -256,7 +259,8 @@ public async Task<long> CreateAuthorReturnId(CreateAuthorReturnIdArgs args)
256259
return null;
257260
}
258261

259-
private const string GetAuthorByNamePatternSql = "SELECT id, name, bio FROM authors WHERE name LIKE COALESCE(@name_pattern, '%')";
262+
private const string GetAuthorByNamePatternSql = @"SELECT id, name, bio FROM authors
263+
WHERE name LIKE COALESCE(@name_pattern, '%')";
260264
public readonly record struct GetAuthorByNamePatternRow(long Id, string Name, string? Bio);
261265
public readonly record struct GetAuthorByNamePatternArgs(string? NamePattern);
262266
public async Task<List<GetAuthorByNamePatternRow>> GetAuthorByNamePattern(GetAuthorByNamePatternArgs args)
@@ -297,7 +301,8 @@ public async Task<List<GetAuthorByNamePatternRow>> GetAuthorByNamePattern(GetAut
297301
}
298302
}
299303

300-
private const string DeleteAuthorSql = "DELETE FROM authors WHERE name = @name";
304+
private const string DeleteAuthorSql = @"DELETE FROM authors
305+
WHERE name = @name";
301306
public readonly record struct DeleteAuthorArgs(string Name);
302307
public async Task DeleteAuthor(DeleteAuthorArgs args)
303308
{
@@ -354,7 +359,9 @@ public async Task DeleteAllAuthors()
354359
}
355360
}
356361

357-
private const string UpdateAuthorsSql = "UPDATE authors SET bio = @bio WHERE bio IS NOT NULL";
362+
private const string UpdateAuthorsSql = @"UPDATE authors
363+
SET bio = @bio
364+
WHERE bio IS NOT NULL";
358365
public readonly record struct UpdateAuthorsArgs(string? Bio);
359366
public async Task<long> UpdateAuthors(UpdateAuthorsArgs args)
360367
{
@@ -509,7 +516,9 @@ public async Task<long> CreateBook(CreateBookArgs args)
509516
}
510517
}
511518

512-
private const string ListAllAuthorsBooksSql = "SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description FROM authors JOIN books ON authors.id = books.author_id ORDER BY authors.name";
519+
private const string ListAllAuthorsBooksSql = @"SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description
520+
FROM authors JOIN books ON authors.id = books.author_id
521+
ORDER BY authors.name";
513522
public readonly record struct ListAllAuthorsBooksRow(Author? Author, Book? Book);
514523
public async Task<List<ListAllAuthorsBooksRow>> ListAllAuthorsBooks()
515524
{
@@ -547,7 +556,9 @@ public async Task<List<ListAllAuthorsBooksRow>> ListAllAuthorsBooks()
547556
}
548557
}
549558

550-
private const string GetDuplicateAuthorsSql = "SELECT authors1.id, authors1.name, authors1.bio, authors2.id, authors2.name, authors2.bio FROM authors authors1 JOIN authors authors2 ON authors1.name = authors2.name WHERE authors1.id < authors2.id";
559+
private const string GetDuplicateAuthorsSql = @"SELECT authors1.id, authors1.name, authors1.bio, authors2.id, authors2.name, authors2.bio
560+
FROM authors authors1 JOIN authors authors2 ON authors1.name = authors2.name
561+
WHERE authors1.id < authors2.id";
551562
public readonly record struct GetDuplicateAuthorsRow(Author? Author, Author? Author2);
552563
public async Task<List<GetDuplicateAuthorsRow>> GetDuplicateAuthors()
553564
{
@@ -585,7 +596,9 @@ public async Task<List<GetDuplicateAuthorsRow>> GetDuplicateAuthors()
585596
}
586597
}
587598

588-
private const string GetAuthorsByBookNameSql = "SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description FROM authors JOIN books ON authors.id = books.author_id WHERE books.name = @name";
599+
private const string GetAuthorsByBookNameSql = @"SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description
600+
FROM authors JOIN books ON authors.id = books.author_id
601+
WHERE books.name = @name";
589602
public readonly record struct GetAuthorsByBookNameRow(long Id, string Name, string? Bio, Book? Book);
590603
public readonly record struct GetAuthorsByBookNameArgs(string Name);
591604
public async Task<List<GetAuthorsByBookNameRow>> GetAuthorsByBookName(GetAuthorsByBookNameArgs args)

0 commit comments

Comments
 (0)