Skip to content

Commit c64b31d

Browse files
fix: more TODOs
1 parent 0238e11 commit c64b31d

18 files changed

Lines changed: 127 additions & 747 deletions

File tree

CodegenTests/CodegenSchemaTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CodegenSchemaTests
1111
[Test]
1212
public void TestDefaultSchemaEnum()
1313
{
14-
const string filename = "DefaultSchemaEnum/request.json";
14+
const string filename = "DefaultSchemaEnum/request.message";
1515
var request = TestRequestHelper.ParseRequestFile(filename);
1616
var response = CodeGenerator.Generate(request);
1717

@@ -34,7 +34,7 @@ public void TestDefaultSchemaEnum()
3434
[Test]
3535
public void TestSchemaScopedEnum()
3636
{
37-
const string filename = "SchemaScopedEnum/request.json";
37+
const string filename = "SchemaScopedEnum/request.message";
3838
var request = TestRequestHelper.ParseRequestFile(filename);
3939
var response = CodeGenerator.Generate(request);
4040

CodegenTests/TestRequestHelper.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
using Google.Protobuf;
2-
using Plugin;
3-
using System;
4-
using System.IO;
51
using File = System.IO.File;
62

73
namespace CodegenTests;
84

95
public static class TestRequestHelper
106
{
11-
public static GenerateRequest ParseRequestFile(string filename)
7+
public static Plugin.GenerateRequest ParseRequestFile(string filename)
128
{
13-
149
var baseDirectory = AppContext.BaseDirectory;
1510
var filePath = Path.Combine(baseDirectory, "test-requests", filename);
1611

1712
if (!File.Exists(filePath))
1813
throw new FileNotFoundException("File not found", filePath);
19-
var contents = File.ReadAllText(filePath);
20-
var jsonParserSettings = JsonParser.Settings.Default.WithIgnoreUnknownFields(true);
21-
return new JsonParser(jsonParserSettings).Parse<GenerateRequest>(contents);
14+
return Plugin.GenerateRequest.Parser.ParseFrom(File.ReadAllBytes(filePath));
2215
}
2316
}

Drivers/Generators/ManyDeclareGen.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ private string GetDapperWithTxBody(string sqlVar, string returnInterface, Query
6969

7070
return $$"""
7171
if (this.{{transactionProperty}}?.Connection == null || this.{{transactionProperty}}?.Connection.State != System.Data.ConnectionState.Open)
72-
{
73-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
74-
}
72+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
7573
7674
return (await this.{{transactionProperty}}.Connection.QueryAsync<{{returnType}}>(
7775
{{sqlVar}}{{dapperArgs}},
@@ -89,11 +87,9 @@ private string GetDriverNoTxBody(string sqlVar, string returnInterface, Query qu
8987
var dataclassInit = CommonGen.InstantiateDataclass(query.Columns.ToArray(), returnInterface, query);
9088
var resultVar = Variable.Result.AsVarName();
9189
var readWhileExists = $$"""
92-
while ({{awaitReaderRow}})
93-
{
94-
{{resultVar}}.Add({{dataclassInit}});
95-
}
96-
""";
90+
while ({{awaitReaderRow}})
91+
{{resultVar}}.Add({{dataclassInit}});
92+
""";
9793
// TODO: add return null at end of code run so transaction code will not run?
9894
return $$"""
9995
using ({{establishConnection}})
@@ -123,17 +119,13 @@ private string GetDriverWithTxBody(string sqlVar, string returnInterface, Query
123119
var dataclassInit = CommonGen.InstantiateDataclass(query.Columns.ToArray(), returnInterface, query);
124120
var resultVar = Variable.Result.AsVarName();
125121
var readWhileExists = $$"""
126-
while ({{awaitReaderRow}})
127-
{
128-
{{resultVar}}.Add({{dataclassInit}});
129-
}
130-
""";
122+
while ({{awaitReaderRow}})
123+
{{resultVar}}.Add({{dataclassInit}});
124+
""";
131125

132126
return $$"""
133127
if (this.{{transactionProperty}}?.Connection == null || this.{{transactionProperty}}?.Connection.State != System.Data.ConnectionState.Open)
134-
{
135-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
136-
}
128+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
137129
138130
using (var {{commandVar}} = this.{{transactionProperty}}.Connection.CreateCommand())
139131
{

Extensions/ListExtensions.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ private static IEnumerable<T> AddIfNotNull<T>(this IEnumerable<T> me, T? item)
66
{
77
return item is not null ? me.Append(item) : me;
88
}
9-
private static ISet<T> AddIfNotNull<T>(this ISet<T> me, T? item)
10-
{
11-
if (item is not null)
12-
me.Add(item);
13-
return me;
14-
}
159

1610
public static IEnumerable<T> AddRangeExcludeNulls<T>(this IEnumerable<T> me, IEnumerable<T?> items)
1711
{
@@ -20,27 +14,13 @@ public static IEnumerable<T> AddRangeExcludeNulls<T>(this IEnumerable<T> me, IEn
2014
return me;
2115
}
2216

23-
public static ISet<T> AddRangeExcludeNulls<T>(this ISet<T> me, IEnumerable<T?> items)
24-
{
25-
foreach (var item in items)
26-
me.AddIfNotNull(item);
27-
return me;
28-
}
29-
3017
public static IEnumerable<T> AddRangeIf<T>(this IEnumerable<T> me, IEnumerable<T?> items, bool condition)
3118
{
3219
if (condition)
3320
return me.AddRangeExcludeNulls(items);
3421
return me;
3522
}
3623

37-
public static ISet<T> AddRangeIf<T>(this ISet<T> me, IEnumerable<T?> items, bool condition)
38-
{
39-
if (condition)
40-
return me.AddRangeExcludeNulls(items);
41-
return me;
42-
}
43-
4424
public static string JoinByNewLine(this IEnumerable<string> me)
4525
{
4626
return string.Join("\n", me);

Extensions/SetExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace SqlcGenCsharp;
2+
3+
public static class SetExtensions
4+
{
5+
private static ISet<T> AddIfNotNull<T>(this ISet<T> me, T? item)
6+
{
7+
if (item is not null)
8+
me.Add(item);
9+
return me;
10+
}
11+
12+
public static ISet<T> AddRangeExcludeNulls<T>(this ISet<T> me, IEnumerable<T?> items)
13+
{
14+
foreach (var item in items)
15+
me.AddIfNotNull(item);
16+
return me;
17+
}
18+
19+
public static ISet<T> AddRangeIf<T>(this ISet<T> me, IEnumerable<T?> items, bool condition)
20+
{
21+
if (condition)
22+
return me.AddRangeExcludeNulls(items);
23+
return me;
24+
}
25+
}

RequestRunner/App.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Threading.Tasks;
45

56
namespace SqlcGenCsharp;
67

78
public static class App
89
{
9-
public static void Main(string[] requestFiles)
10+
public static async Task Main(string[] requestFiles)
1011
{
11-
for (int i = 0; i < requestFiles.Length; i++)
12-
ProcessRequestFile(requestFiles[i]);
12+
foreach (var requestFile in requestFiles)
13+
await ProcessRequestFile(requestFile);
1314
}
1415

1516
private static async Task ProcessRequestFile(string requestFile)
@@ -19,6 +20,7 @@ private static async Task ProcessRequestFile(string requestFile)
1920
Console.WriteLine($"Processing request file {requestFile}");
2021
var request = Plugin.GenerateRequest.Parser.ParseFrom(File.ReadAllBytes(requestFile));
2122
var response = await new CodeGenerator().Generate(request);
22-
Console.WriteLine($"Response: {response.Files.Count} output files");
23+
Console.WriteLine($"Response files: {response.Files.Select(f => f.Name).JoinByComma()}");
24+
Console.WriteLine("--------------------------------");
2325
}
2426
}

examples/MySqlConnectorDapperExample/QuerySql.cs

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ public async Task<List<ListAuthorsRow>> ListAuthors()
9595
}
9696

9797
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != System.Data.ConnectionState.Open)
98-
{
99-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
100-
}
101-
98+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
10299
return (await this.Transaction.Connection.QueryAsync<ListAuthorsRow>(ListAuthorsSql, transaction: this.Transaction)).AsList();
103100
}
104101

@@ -217,10 +214,7 @@ public async Task<List<GetAuthorByNamePatternRow>> GetAuthorByNamePattern(GetAut
217214
}
218215

219216
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != System.Data.ConnectionState.Open)
220-
{
221-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
222-
}
223-
217+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
224218
return (await this.Transaction.Connection.QueryAsync<GetAuthorByNamePatternRow>(GetAuthorByNamePatternSql, queryParams, transaction: this.Transaction)).AsList();
225219
}
226220

@@ -325,10 +319,7 @@ public async Task<List<GetAuthorsByIdsRow>> GetAuthorsByIds(GetAuthorsByIdsArgs
325319
}
326320

327321
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != System.Data.ConnectionState.Open)
328-
{
329-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
330-
}
331-
322+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
332323
return (await this.Transaction.Connection.QueryAsync<GetAuthorsByIdsRow>(transformedSql, queryParams, transaction: this.Transaction)).AsList();
333324
}
334325

@@ -364,10 +355,7 @@ public async Task<List<GetAuthorsByIdsAndNamesRow>> GetAuthorsByIdsAndNames(GetA
364355
}
365356

366357
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != System.Data.ConnectionState.Open)
367-
{
368-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
369-
}
370-
358+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
371359
return (await this.Transaction.Connection.QueryAsync<GetAuthorsByIdsAndNamesRow>(transformedSql, queryParams, transaction: this.Transaction)).AsList();
372360
}
373361

@@ -417,21 +405,15 @@ public async Task<List<ListAllAuthorsBooksRow>> ListAllAuthorsBooks()
417405
{
418406
var result = new List<ListAllAuthorsBooksRow>();
419407
while (await reader.ReadAsync())
420-
{
421408
result.Add(new ListAllAuthorsBooksRow { Author = new Author { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2) }, Book = new Book { Id = reader.GetInt64(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } });
422-
}
423-
424409
return result;
425410
}
426411
}
427412
}
428413
}
429414

430415
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != System.Data.ConnectionState.Open)
431-
{
432-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
433-
}
434-
416+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
435417
using (var command = this.Transaction.Connection.CreateCommand())
436418
{
437419
command.CommandText = ListAllAuthorsBooksSql;
@@ -440,10 +422,7 @@ public async Task<List<ListAllAuthorsBooksRow>> ListAllAuthorsBooks()
440422
{
441423
var result = new List<ListAllAuthorsBooksRow>();
442424
while (await reader.ReadAsync())
443-
{
444425
result.Add(new ListAllAuthorsBooksRow { Author = new Author { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2) }, Book = new Book { Id = reader.GetInt64(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } });
445-
}
446-
447426
return result;
448427
}
449428
}
@@ -468,21 +447,15 @@ public async Task<List<GetDuplicateAuthorsRow>> GetDuplicateAuthors()
468447
{
469448
var result = new List<GetDuplicateAuthorsRow>();
470449
while (await reader.ReadAsync())
471-
{
472450
result.Add(new GetDuplicateAuthorsRow { Author = new Author { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2) }, Author2 = new Author { Id = reader.GetInt64(3), Name = reader.GetString(4), Bio = reader.IsDBNull(5) ? null : reader.GetString(5) } });
473-
}
474-
475451
return result;
476452
}
477453
}
478454
}
479455
}
480456

481457
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != System.Data.ConnectionState.Open)
482-
{
483-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
484-
}
485-
458+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
486459
using (var command = this.Transaction.Connection.CreateCommand())
487460
{
488461
command.CommandText = GetDuplicateAuthorsSql;
@@ -491,10 +464,7 @@ public async Task<List<GetDuplicateAuthorsRow>> GetDuplicateAuthors()
491464
{
492465
var result = new List<GetDuplicateAuthorsRow>();
493466
while (await reader.ReadAsync())
494-
{
495467
result.Add(new GetDuplicateAuthorsRow { Author = new Author { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2) }, Author2 = new Author { Id = reader.GetInt64(3), Name = reader.GetString(4), Bio = reader.IsDBNull(5) ? null : reader.GetString(5) } });
496-
}
497-
498468
return result;
499469
}
500470
}
@@ -526,21 +496,15 @@ public async Task<List<GetAuthorsByBookNameRow>> GetAuthorsByBookName(GetAuthors
526496
{
527497
var result = new List<GetAuthorsByBookNameRow>();
528498
while (await reader.ReadAsync())
529-
{
530499
result.Add(new GetAuthorsByBookNameRow { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2), Book = new Book { Id = reader.GetInt64(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } });
531-
}
532-
533500
return result;
534501
}
535502
}
536503
}
537504
}
538505

539506
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != System.Data.ConnectionState.Open)
540-
{
541-
throw new System.InvalidOperationException("Transaction is provided, but its connection is null.");
542-
}
543-
507+
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
544508
using (var command = this.Transaction.Connection.CreateCommand())
545509
{
546510
command.CommandText = GetAuthorsByBookNameSql;
@@ -550,10 +514,7 @@ public async Task<List<GetAuthorsByBookNameRow>> GetAuthorsByBookName(GetAuthors
550514
{
551515
var result = new List<GetAuthorsByBookNameRow>();
552516
while (await reader.ReadAsync())
553-
{
554517
result.Add(new GetAuthorsByBookNameRow { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2), Book = new Book { Id = reader.GetInt64(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } });
555-
}
556-
557518
return result;
558519
}
559520
}

0 commit comments

Comments
 (0)