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
26 changes: 23 additions & 3 deletions CodeGenerator/Generators/QueriesGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,31 @@ private IEnumerable<MemberDeclarationSyntax> GetMembersForSingleQuery(Query quer
if (transformedQueryText == string.Empty)
return null;

var memberName = ClassMember.Sql.Name(query.Name);
if (transformedQueryText.IndexOfAny(['\r', '\n']) >= 0)
{
var queryText = transformedQueryText.Replace("\"", "\"\"");
var lines = queryText.TrimEnd().Split(["\r\n", "\r", "\n"], StringSplitOptions.None);

// The indentation of the constant declaration itself. for legacy generation it 8 indents and for modern it 4.
string memberIndentPrefix = dbDriver.Options.DotnetFramework.IsDotnetLegacy() ? " " : " ";
var declaration = $"private const string {memberName} = @\"";

// We need to calculate the indentation for subsequent lines so they align with the first.
// This is based on the member indent, the declaration length, and any leading space on the first SQL line.
var firstLineLeadingSpaceCount = lines[0].TakeWhile(char.IsWhiteSpace).Count();
var subsequentLineIndent = new string(' ', memberIndentPrefix.Length + declaration.Length + firstLineLeadingSpaceCount);

var indentedLines = lines.Skip(1).Select(line => subsequentLineIndent + line);
var fullQueryString = string.Join(Environment.NewLine, [lines[0], .. indentedLines]);

return ParseMemberDeclaration(
$"{memberIndentPrefix}{declaration}{fullQueryString}\";")!;
}

var singleLineQueryText = LongWhitespaceRegex().Replace(transformedQueryText, " ");
return ParseMemberDeclaration(
$"""
private const string {ClassMember.Sql.Name(query.Name)} = "{singleLineQueryText}";
""")!
$"""private const string {memberName} = "{singleLineQueryText}";""")!
.AppendNewLine();
}

Expand Down
202 changes: 186 additions & 16 deletions examples/MySqlConnectorDapperExample/QuerySql.cs

Large diffs are not rendered by default.

202 changes: 186 additions & 16 deletions examples/MySqlConnectorDapperLegacyExample/QuerySql.cs

Large diffs are not rendered by default.

202 changes: 186 additions & 16 deletions examples/MySqlConnectorExample/QuerySql.cs

Large diffs are not rendered by default.

202 changes: 186 additions & 16 deletions examples/MySqlConnectorLegacyExample/QuerySql.cs

Large diffs are not rendered by default.

313 changes: 283 additions & 30 deletions examples/NpgsqlDapperExample/QuerySql.cs

Large diffs are not rendered by default.

313 changes: 283 additions & 30 deletions examples/NpgsqlDapperLegacyExample/QuerySql.cs

Large diffs are not rendered by default.

313 changes: 283 additions & 30 deletions examples/NpgsqlExample/QuerySql.cs

Large diffs are not rendered by default.

313 changes: 283 additions & 30 deletions examples/NpgsqlLegacyExample/QuerySql.cs

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions examples/QuickStartMySqlDalGen/QuerySql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public static QuerySql WithTransaction(MySqlTransaction transaction)
return null;
}

private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name LIMIT @limit OFFSET @offset";
private const string ListAuthorsSql = @"SELECT id, name, bio
FROM authors
ORDER BY name
LIMIT @limit OFFSET @offset";
public readonly record struct ListAuthorsRow(long Id, string Name, string? Bio);
public readonly record struct ListAuthorsArgs(int Limit, int Offset);
public async Task<List<ListAuthorsRow>> ListAuthors(ListAuthorsArgs args)
Expand Down Expand Up @@ -256,7 +259,8 @@ public async Task<long> CreateAuthorReturnId(CreateAuthorReturnIdArgs args)
return null;
}

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

private const string DeleteAuthorSql = "DELETE FROM authors WHERE name = @name";
private const string DeleteAuthorSql = @"DELETE FROM authors
WHERE name = @name";
public readonly record struct DeleteAuthorArgs(string Name);
public async Task DeleteAuthor(DeleteAuthorArgs args)
{
Expand Down Expand Up @@ -354,7 +359,9 @@ public async Task DeleteAllAuthors()
}
}

private const string UpdateAuthorsSql = "UPDATE authors SET bio = @bio WHERE bio IS NOT NULL";
private const string UpdateAuthorsSql = @"UPDATE authors
SET bio = @bio
WHERE bio IS NOT NULL";
public readonly record struct UpdateAuthorsArgs(string? Bio);
public async Task<long> UpdateAuthors(UpdateAuthorsArgs args)
{
Expand Down Expand Up @@ -509,7 +516,9 @@ public async Task<long> CreateBook(CreateBookArgs args)
}
}

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";
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";
public readonly record struct ListAllAuthorsBooksRow(Author? Author, Book? Book);
public async Task<List<ListAllAuthorsBooksRow>> ListAllAuthorsBooks()
{
Expand Down Expand Up @@ -547,7 +556,9 @@ public async Task<List<ListAllAuthorsBooksRow>> ListAllAuthorsBooks()
}
}

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";
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";
public readonly record struct GetDuplicateAuthorsRow(Author? Author, Author? Author2);
public async Task<List<GetDuplicateAuthorsRow>> GetDuplicateAuthors()
{
Expand Down Expand Up @@ -585,7 +596,9 @@ public async Task<List<GetDuplicateAuthorsRow>> GetDuplicateAuthors()
}
}

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";
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";
public readonly record struct GetAuthorsByBookNameRow(long Id, string Name, string? Bio, Book? Book);
public readonly record struct GetAuthorsByBookNameArgs(string Name);
public async Task<List<GetAuthorsByBookNameRow>> GetAuthorsByBookName(GetAuthorsByBookNameArgs args)
Expand Down
49 changes: 38 additions & 11 deletions examples/QuickStartPostgresDalGen/QuerySql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static QuerySql WithTransaction(NpgsqlTransaction transaction)
private NpgsqlTransaction? Transaction { get; }
private string? ConnectionString { get; }

private const string GetAuthorSql = "SELECT id, name, bio FROM authors WHERE name = @name LIMIT 1";
private const string GetAuthorSql = @"SELECT id, name, bio FROM authors
WHERE name = @name LIMIT 1";
public readonly record struct GetAuthorRow(long Id, string Name, string? Bio);
public readonly record struct GetAuthorArgs(string Name);
public async Task<GetAuthorRow?> GetAuthor(GetAuthorArgs args)
Expand Down Expand Up @@ -89,7 +90,11 @@ public static QuerySql WithTransaction(NpgsqlTransaction transaction)
return null;
}

private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name LIMIT @limit OFFSET @offset";
private const string ListAuthorsSql = @"SELECT id, name, bio
FROM authors
ORDER BY name
LIMIT @limit
OFFSET @offset";
public readonly record struct ListAuthorsRow(long Id, string Name, string? Bio);
public readonly record struct ListAuthorsArgs(int Offset, int Limit);
public async Task<List<ListAuthorsRow>> ListAuthors(ListAuthorsArgs args)
Expand Down Expand Up @@ -221,7 +226,8 @@ public async Task<long> CreateAuthorReturnId(CreateAuthorReturnIdArgs args)
}
}

private const string GetAuthorByIdSql = "SELECT id, name, bio FROM authors WHERE id = @id LIMIT 1";
private const string GetAuthorByIdSql = @"SELECT id, name, bio FROM authors
WHERE id = @id LIMIT 1";
public readonly record struct GetAuthorByIdRow(long Id, string Name, string? Bio);
public readonly record struct GetAuthorByIdArgs(long Id);
public async Task<GetAuthorByIdRow?> GetAuthorById(GetAuthorByIdArgs args)
Expand Down Expand Up @@ -275,7 +281,8 @@ public async Task<long> CreateAuthorReturnId(CreateAuthorReturnIdArgs args)
return null;
}

private const string GetAuthorByNamePatternSql = "SELECT id, name, bio FROM authors WHERE name LIKE COALESCE(@name_pattern, '%')";
private const string GetAuthorByNamePatternSql = @"SELECT id, name, bio FROM authors
WHERE name LIKE COALESCE(@name_pattern, '%')";
public readonly record struct GetAuthorByNamePatternRow(long Id, string Name, string? Bio);
public readonly record struct GetAuthorByNamePatternArgs(string? NamePattern);
public async Task<List<GetAuthorByNamePatternRow>> GetAuthorByNamePattern(GetAuthorByNamePatternArgs args)
Expand Down Expand Up @@ -315,7 +322,8 @@ public async Task<List<GetAuthorByNamePatternRow>> GetAuthorByNamePattern(GetAut
}
}

private const string DeleteAuthorSql = "DELETE FROM authors WHERE name = @name";
private const string DeleteAuthorSql = @"DELETE FROM authors
WHERE name = @name";
public readonly record struct DeleteAuthorArgs(string Name);
public async Task DeleteAuthor(DeleteAuthorArgs args)
{
Expand Down Expand Up @@ -370,7 +378,9 @@ public async Task TruncateAuthors()
}
}

private const string UpdateAuthorsSql = "UPDATE authors SET bio = @bio WHERE bio IS NOT NULL";
private const string UpdateAuthorsSql = @"UPDATE authors
SET bio = @bio
WHERE bio IS NOT NULL";
public readonly record struct UpdateAuthorsArgs(string? Bio);
public async Task<long> UpdateAuthors(UpdateAuthorsArgs args)
{
Expand All @@ -397,7 +407,8 @@ public async Task<long> UpdateAuthors(UpdateAuthorsArgs args)
}
}

private const string GetAuthorsByIdsSql = "SELECT id, name, bio FROM authors WHERE id = ANY(@longArr_1::BIGINT [])";
private const string GetAuthorsByIdsSql = @"SELECT id, name, bio FROM authors
WHERE id = ANY(@longArr_1::BIGINT [])";
public readonly record struct GetAuthorsByIdsRow(long Id, string Name, string? Bio);
public readonly record struct GetAuthorsByIdsArgs(long[] LongArr1);
public async Task<List<GetAuthorsByIdsRow>> GetAuthorsByIds(GetAuthorsByIdsArgs args)
Expand Down Expand Up @@ -437,7 +448,9 @@ public async Task<List<GetAuthorsByIdsRow>> GetAuthorsByIds(GetAuthorsByIdsArgs
}
}

private const string GetAuthorsByIdsAndNamesSql = "SELECT id, name, bio FROM authors WHERE id = ANY(@longArr_1::BIGINT []) AND name = ANY(@stringArr_2::TEXT [])";
private const string GetAuthorsByIdsAndNamesSql = @"SELECT id, name, bio
FROM authors
WHERE id = ANY(@longArr_1::BIGINT []) AND name = ANY(@stringArr_2::TEXT [])";
public readonly record struct GetAuthorsByIdsAndNamesRow(long Id, string Name, string? Bio);
public readonly record struct GetAuthorsByIdsAndNamesArgs(long[] LongArr1, string[] StringArr2);
public async Task<List<GetAuthorsByIdsAndNamesRow>> GetAuthorsByIdsAndNames(GetAuthorsByIdsAndNamesArgs args)
Expand Down Expand Up @@ -511,7 +524,12 @@ public async Task<Guid> CreateBook(CreateBookArgs args)
}
}

private const string ListAllAuthorsBooksSql = "SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description FROM authors INNER JOIN books ON authors.id = books.author_id ORDER BY authors.name";
private const string ListAllAuthorsBooksSql = @"SELECT
authors.id, authors.name, authors.bio,
books.id, books.name, books.author_id, books.description
FROM authors
INNER JOIN books ON authors.id = books.author_id
ORDER BY authors.name";
public readonly record struct ListAllAuthorsBooksRow(Author? Author, Book? Book);
public async Task<List<ListAllAuthorsBooksRow>> ListAllAuthorsBooks()
{
Expand Down Expand Up @@ -548,7 +566,12 @@ public async Task<List<ListAllAuthorsBooksRow>> ListAllAuthorsBooks()
}
}

private const string GetDuplicateAuthorsSql = "SELECT authors1.id, authors1.name, authors1.bio, authors2.id, authors2.name, authors2.bio FROM authors AS authors1 INNER JOIN authors AS authors2 ON authors1.name = authors2.name WHERE authors1.id < authors2.id";
private const string GetDuplicateAuthorsSql = @"SELECT
authors1.id, authors1.name, authors1.bio,
authors2.id, authors2.name, authors2.bio
FROM authors AS authors1
INNER JOIN authors AS authors2 ON authors1.name = authors2.name
WHERE authors1.id < authors2.id";
public readonly record struct GetDuplicateAuthorsRow(Author? Author, Author? Author2);
public async Task<List<GetDuplicateAuthorsRow>> GetDuplicateAuthors()
{
Expand Down Expand Up @@ -585,7 +608,11 @@ public async Task<List<GetDuplicateAuthorsRow>> GetDuplicateAuthors()
}
}

private const string GetAuthorsByBookNameSql = "SELECT authors.id, authors.name, authors.bio, books.id, books.name, books.author_id, books.description FROM authors INNER JOIN books ON authors.id = books.author_id WHERE books.name = @name";
private const string GetAuthorsByBookNameSql = @"SELECT
authors.id, authors.name, authors.bio,
books.id, books.name, books.author_id, books.description
FROM authors INNER JOIN books ON authors.id = books.author_id
WHERE books.name = @name";
public readonly record struct GetAuthorsByBookNameRow(long Id, string Name, string? Bio, Book? Book);
public readonly record struct GetAuthorsByBookNameArgs(string Name);
public async Task<List<GetAuthorsByBookNameRow>> GetAuthorsByBookName(GetAuthorsByBookNameArgs args)
Expand Down
Loading