Skip to content

Commit e614884

Browse files
bugfix around DotnetFramwork option value check (#44)
* bugfix around DotnetFramwork option value check * bugfix in sha to replace - reverting the sha back to placeholder after generating code
1 parent 70053b1 commit e614884

File tree

10 files changed

+286
-294
lines changed

10 files changed

+286
-294
lines changed

CodeGenerator/Generators/CsprojGen.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public File GenerateFile()
1919

2020
private string GetFileContents()
2121
{
22-
var nullableProperty = options.DotnetFramework == DotnetFramework.DotnetStandard21 ? "enable" : "disable";
22+
var nullableProperty = options.DotnetFramework.LatestDotnetSupported() ? "\n<Nullable>enable</Nullable>" : "";
2323
return $"""
2424
<!--{Consts.AutoGeneratedComment}-->
2525
<!--Run the following to add the project to the solution:
@@ -28,8 +28,7 @@ dotnet sln add {projectName}/{projectName}.csproj
2828
<Project Sdk="Microsoft.NET.Sdk">
2929
3030
<PropertyGroup>
31-
<TargetFramework>{options.DotnetFramework.ToName()}</TargetFramework>
32-
<Nullable>{nullableProperty}</Nullable>
31+
<TargetFramework>{options.DotnetFramework.ToName()}</TargetFramework>{nullableProperty}
3332
<OutputType>Library</OutputType>
3433
</PropertyGroup>
3534

CodeGenerator/Generators/RootGen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal class RootGen(Options options)
99
public CompilationUnitSyntax CompilationRootGen(IdentifierNameSyntax namespaceName,
1010
UsingDirectiveSyntax[] usingDirectives, MemberDeclarationSyntax classDeclaration)
1111
{
12-
return options.DotnetFramework == DotnetFramework.DotnetStandard21 ? GetFileScoped() : GetBLockScoped();
12+
return options.DotnetFramework.LatestDotnetSupported() ? GetFileScoped() : GetBLockScoped();
1313

1414
CompilationUnitSyntax GetFileScoped()
1515
{

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ update-wasm-plugin:
3939

4040
sqlc-generate-wasm: dotnet-publish-wasm update-wasm-plugin
4141
SQLCCACHE=./; sqlc -f sqlc.wasm.yaml generate
42+
yq -i ".plugins[0].wasm.sha256 = \"SHA_TO_REPLACE\"" sqlc.wasm.yaml
4243

4344
test-wasm-plugin: sqlc-generate-wasm dockerfile-generate run-tests

MySqlConnectorExample/MySqlConnectorExample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<Project Sdk="Microsoft.NET.Sdk">
66

77
<PropertyGroup>
8-
<TargetFramework>net8.0</TargetFramework>
9-
<Nullable>disable</Nullable>
8+
<TargetFramework>net8.0</TargetFramework>
9+
<Nullable>enable</Nullable>
1010
<OutputType>Library</OutputType>
1111
</PropertyGroup>
1212

MySqlConnectorExample/QuerySql.cs

Lines changed: 136 additions & 138 deletions
Large diffs are not rendered by default.

MySqlConnectorExample/Utils.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
// auto-generated by sqlc - do not edit
2-
namespace MySqlConnectorExample
3-
{
4-
using System;
5-
using System.Data;
2+
using System;
3+
using System.Data;
64

7-
public static class Utils
5+
namespace MySqlConnectorExample;
6+
public static class Utils
7+
{
8+
public static byte[] GetBytes(IDataRecord reader, int ordinal)
89
{
9-
public static byte[] GetBytes(IDataRecord reader, int ordinal)
10+
const int bufferSize = 100000;
11+
if (reader is null)
12+
throw new ArgumentNullException(nameof(reader));
13+
var buffer = new byte[bufferSize];
14+
var(bytesRead, offset) = (0, 0);
15+
while (bytesRead < bufferSize)
1016
{
11-
const int bufferSize = 100000;
12-
if (reader is null)
13-
throw new ArgumentNullException(nameof(reader));
14-
var buffer = new byte[bufferSize];
15-
var(bytesRead, offset) = (0, 0);
16-
while (bytesRead < bufferSize)
17-
{
18-
var read = (int)reader.GetBytes(ordinal, bufferSize + bytesRead, buffer, offset, bufferSize - bytesRead);
19-
if (read == 0)
20-
break;
21-
bytesRead += read;
22-
offset += read;
23-
}
24-
25-
if (bytesRead < bufferSize)
26-
Array.Resize(ref buffer, bytesRead);
27-
return buffer;
17+
var read = (int)reader.GetBytes(ordinal, bufferSize + bytesRead, buffer, offset, bufferSize - bytesRead);
18+
if (read == 0)
19+
break;
20+
bytesRead += read;
21+
offset += read;
2822
}
23+
24+
if (bytesRead < bufferSize)
25+
Array.Resize(ref buffer, bytesRead);
26+
return buffer;
2927
}
3028
}

NpgsqlExample/NpgsqlExample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<Project Sdk="Microsoft.NET.Sdk">
66

77
<PropertyGroup>
8-
<TargetFramework>net8.0</TargetFramework>
9-
<Nullable>disable</Nullable>
8+
<TargetFramework>net8.0</TargetFramework>
9+
<Nullable>enable</Nullable>
1010
<OutputType>Library</OutputType>
1111
</PropertyGroup>
1212

NpgsqlExample/QuerySql.cs

Lines changed: 99 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,133 +2,131 @@
22
// ReSharper disable NotAccessedPositionalProperty.Global
33
// ReSharper disable UnusedAutoPropertyAccessor.Global
44
// ReSharper disable InconsistentNaming
5-
namespace NpgsqlExample
6-
{
7-
using System.Collections.Generic;
8-
using System.Threading.Tasks;
9-
using Npgsql;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
7+
using Npgsql;
108

11-
public class QuerySql(string connectionString)
9+
namespace NpgsqlExample;
10+
public class QuerySql(string connectionString)
11+
{
12+
private const string GetAuthorSql = "SELECT id, name, bio FROM authors WHERE id = @id LIMIT 1 ";
13+
public readonly record struct GetAuthorRow(long Id, string Name, string? Bio);
14+
public readonly record struct GetAuthorArgs(long Id);
15+
public async Task<GetAuthorRow?> GetAuthor(GetAuthorArgs args)
1216
{
13-
private const string GetAuthorSql = "SELECT id, name, bio FROM authors WHERE id = @id LIMIT 1 ";
14-
public readonly record struct GetAuthorRow(long Id, string Name, string? Bio);
15-
public readonly record struct GetAuthorArgs(long Id);
16-
public async Task<GetAuthorRow?> GetAuthor(GetAuthorArgs args)
1717
{
18+
await using var connection = NpgsqlDataSource.Create(connectionString);
19+
;
20+
await using var command = connection.CreateCommand(GetAuthorSql);
21+
command.Parameters.AddWithValue("@id", args.Id);
22+
var reader = await command.ExecuteReaderAsync();
23+
if (await reader.ReadAsync())
1824
{
19-
await using var connection = NpgsqlDataSource.Create(connectionString);
20-
;
21-
await using var command = connection.CreateCommand(GetAuthorSql);
22-
command.Parameters.AddWithValue("@id", args.Id);
23-
var reader = await command.ExecuteReaderAsync();
24-
if (await reader.ReadAsync())
25+
return new GetAuthorRow
2526
{
26-
return new GetAuthorRow
27-
{
28-
Id = reader.GetInt64(0),
29-
Name = reader.GetString(1),
30-
Bio = reader.IsDBNull(2) ? null : reader.GetString(2)
31-
};
32-
}
33-
34-
return null;
27+
Id = reader.GetInt64(0),
28+
Name = reader.GetString(1),
29+
Bio = reader.IsDBNull(2) ? null : reader.GetString(2)
30+
};
3531
}
32+
33+
return null;
3634
}
35+
}
3736

38-
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name ";
39-
public readonly record struct ListAuthorsRow(long Id, string Name, string? Bio);
40-
public async Task<List<ListAuthorsRow>> ListAuthors()
37+
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name ";
38+
public readonly record struct ListAuthorsRow(long Id, string Name, string? Bio);
39+
public async Task<List<ListAuthorsRow>> ListAuthors()
40+
{
4141
{
42+
await using var connection = NpgsqlDataSource.Create(connectionString);
43+
;
44+
await using var command = connection.CreateCommand(ListAuthorsSql);
45+
var reader = await command.ExecuteReaderAsync();
46+
var result = new List<ListAuthorsRow>();
47+
while (await reader.ReadAsync())
4248
{
43-
await using var connection = NpgsqlDataSource.Create(connectionString);
44-
;
45-
await using var command = connection.CreateCommand(ListAuthorsSql);
46-
var reader = await command.ExecuteReaderAsync();
47-
var result = new List<ListAuthorsRow>();
48-
while (await reader.ReadAsync())
49-
{
50-
result.Add(new ListAuthorsRow { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2) });
51-
}
52-
53-
return result;
49+
result.Add(new ListAuthorsRow { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2) });
5450
}
51+
52+
return result;
5553
}
54+
}
5655

57-
private const string CreateAuthorSql = "INSERT INTO authors ( name , bio ) VALUES ( @name, @bio ) RETURNING id, name, bio ";
58-
public readonly record struct CreateAuthorRow(long Id, string Name, string? Bio);
59-
public readonly record struct CreateAuthorArgs(string Name, string? Bio);
60-
public async Task<CreateAuthorRow?> CreateAuthor(CreateAuthorArgs args)
56+
private const string CreateAuthorSql = "INSERT INTO authors ( name , bio ) VALUES ( @name, @bio ) RETURNING id, name, bio ";
57+
public readonly record struct CreateAuthorRow(long Id, string Name, string? Bio);
58+
public readonly record struct CreateAuthorArgs(string Name, string? Bio);
59+
public async Task<CreateAuthorRow?> CreateAuthor(CreateAuthorArgs args)
60+
{
6161
{
62+
await using var connection = NpgsqlDataSource.Create(connectionString);
63+
;
64+
await using var command = connection.CreateCommand(CreateAuthorSql);
65+
command.Parameters.AddWithValue("@name", args.Name);
66+
command.Parameters.AddWithValue("@bio", args.Bio);
67+
var reader = await command.ExecuteReaderAsync();
68+
if (await reader.ReadAsync())
6269
{
63-
await using var connection = NpgsqlDataSource.Create(connectionString);
64-
;
65-
await using var command = connection.CreateCommand(CreateAuthorSql);
66-
command.Parameters.AddWithValue("@name", args.Name);
67-
command.Parameters.AddWithValue("@bio", args.Bio);
68-
var reader = await command.ExecuteReaderAsync();
69-
if (await reader.ReadAsync())
70+
return new CreateAuthorRow
7071
{
71-
return new CreateAuthorRow
72-
{
73-
Id = reader.GetInt64(0),
74-
Name = reader.GetString(1),
75-
Bio = reader.IsDBNull(2) ? null : reader.GetString(2)
76-
};
77-
}
78-
79-
return null;
72+
Id = reader.GetInt64(0),
73+
Name = reader.GetString(1),
74+
Bio = reader.IsDBNull(2) ? null : reader.GetString(2)
75+
};
8076
}
77+
78+
return null;
8179
}
80+
}
8281

83-
private const string DeleteAuthorSql = "DELETE FROM authors WHERE id = @id ";
84-
public readonly record struct DeleteAuthorArgs(long Id);
85-
public async Task DeleteAuthor(DeleteAuthorArgs args)
82+
private const string DeleteAuthorSql = "DELETE FROM authors WHERE id = @id ";
83+
public readonly record struct DeleteAuthorArgs(long Id);
84+
public async Task DeleteAuthor(DeleteAuthorArgs args)
85+
{
8686
{
87-
{
88-
await using var connection = NpgsqlDataSource.Create(connectionString);
89-
;
90-
await using var command = connection.CreateCommand(DeleteAuthorSql);
91-
command.Parameters.AddWithValue("@id", args.Id);
92-
await command.ExecuteScalarAsync();
93-
}
87+
await using var connection = NpgsqlDataSource.Create(connectionString);
88+
;
89+
await using var command = connection.CreateCommand(DeleteAuthorSql);
90+
command.Parameters.AddWithValue("@id", args.Id);
91+
await command.ExecuteScalarAsync();
9492
}
93+
}
9594

96-
private const string TestSql = "SELECT c_bit, c_smallint, c_boolean, c_integer, c_bigint, c_serial, c_decimal, c_numeric, c_real, c_double_precision, c_date, c_time, c_timestamp, c_char, c_varchar, c_bytea, c_text, c_json FROM node_postgres_types LIMIT 1 ";
97-
public readonly record struct TestRow(byte[]? C_bit, int? C_smallint, bool? C_boolean, int? C_integer, int? C_bigint, long? C_serial, float? C_decimal, float? C_numeric, float? C_real, float? C_double_precision, string? C_date, string? C_time, string? C_timestamp, string? C_char, string? C_varchar, byte[]? C_bytea, string? C_text, object? C_json);
98-
public async Task<TestRow?> Test()
95+
private const string TestSql = "SELECT c_bit, c_smallint, c_boolean, c_integer, c_bigint, c_serial, c_decimal, c_numeric, c_real, c_double_precision, c_date, c_time, c_timestamp, c_char, c_varchar, c_bytea, c_text, c_json FROM node_postgres_types LIMIT 1 ";
96+
public readonly record struct TestRow(byte[]? C_bit, int? C_smallint, bool? C_boolean, int? C_integer, int? C_bigint, long? C_serial, float? C_decimal, float? C_numeric, float? C_real, float? C_double_precision, string? C_date, string? C_time, string? C_timestamp, string? C_char, string? C_varchar, byte[]? C_bytea, string? C_text, object? C_json);
97+
public async Task<TestRow?> Test()
98+
{
9999
{
100+
await using var connection = NpgsqlDataSource.Create(connectionString);
101+
;
102+
await using var command = connection.CreateCommand(TestSql);
103+
var reader = await command.ExecuteReaderAsync();
104+
if (await reader.ReadAsync())
100105
{
101-
await using var connection = NpgsqlDataSource.Create(connectionString);
102-
;
103-
await using var command = connection.CreateCommand(TestSql);
104-
var reader = await command.ExecuteReaderAsync();
105-
if (await reader.ReadAsync())
106+
return new TestRow
106107
{
107-
return new TestRow
108-
{
109-
C_bit = reader.IsDBNull(0) ? null : Utils.GetBytes(reader, 0),
110-
C_smallint = reader.IsDBNull(1) ? null : reader.GetInt32(1),
111-
C_boolean = reader.IsDBNull(2) ? null : reader.GetBoolean(2),
112-
C_integer = reader.IsDBNull(3) ? null : reader.GetInt32(3),
113-
C_bigint = reader.IsDBNull(4) ? null : reader.GetInt32(4),
114-
C_serial = reader.IsDBNull(5) ? null : reader.GetInt64(5),
115-
C_decimal = reader.IsDBNull(6) ? null : reader.GetFloat(6),
116-
C_numeric = reader.IsDBNull(7) ? null : reader.GetFloat(7),
117-
C_real = reader.IsDBNull(8) ? null : reader.GetFloat(8),
118-
C_double_precision = reader.IsDBNull(9) ? null : reader.GetFloat(9),
119-
C_date = reader.IsDBNull(10) ? null : reader.GetString(10),
120-
C_time = reader.IsDBNull(11) ? null : reader.GetString(11),
121-
C_timestamp = reader.IsDBNull(12) ? null : reader.GetString(12),
122-
C_char = reader.IsDBNull(13) ? null : reader.GetString(13),
123-
C_varchar = reader.IsDBNull(14) ? null : reader.GetString(14),
124-
C_bytea = reader.IsDBNull(15) ? null : Utils.GetBytes(reader, 15),
125-
C_text = reader.IsDBNull(16) ? null : reader.GetString(16),
126-
C_json = reader.IsDBNull(17) ? null : reader.GetString(17)
127-
};
128-
}
129-
130-
return null;
108+
C_bit = reader.IsDBNull(0) ? null : Utils.GetBytes(reader, 0),
109+
C_smallint = reader.IsDBNull(1) ? null : reader.GetInt32(1),
110+
C_boolean = reader.IsDBNull(2) ? null : reader.GetBoolean(2),
111+
C_integer = reader.IsDBNull(3) ? null : reader.GetInt32(3),
112+
C_bigint = reader.IsDBNull(4) ? null : reader.GetInt32(4),
113+
C_serial = reader.IsDBNull(5) ? null : reader.GetInt64(5),
114+
C_decimal = reader.IsDBNull(6) ? null : reader.GetFloat(6),
115+
C_numeric = reader.IsDBNull(7) ? null : reader.GetFloat(7),
116+
C_real = reader.IsDBNull(8) ? null : reader.GetFloat(8),
117+
C_double_precision = reader.IsDBNull(9) ? null : reader.GetFloat(9),
118+
C_date = reader.IsDBNull(10) ? null : reader.GetString(10),
119+
C_time = reader.IsDBNull(11) ? null : reader.GetString(11),
120+
C_timestamp = reader.IsDBNull(12) ? null : reader.GetString(12),
121+
C_char = reader.IsDBNull(13) ? null : reader.GetString(13),
122+
C_varchar = reader.IsDBNull(14) ? null : reader.GetString(14),
123+
C_bytea = reader.IsDBNull(15) ? null : Utils.GetBytes(reader, 15),
124+
C_text = reader.IsDBNull(16) ? null : reader.GetString(16),
125+
C_json = reader.IsDBNull(17) ? null : reader.GetString(17)
126+
};
131127
}
128+
129+
return null;
132130
}
133131
}
134132
}

NpgsqlExample/Utils.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
// auto-generated by sqlc - do not edit
2-
namespace NpgsqlExample
3-
{
4-
using System;
5-
using System.Data;
2+
using System;
3+
using System.Data;
64

7-
public static class Utils
5+
namespace NpgsqlExample;
6+
public static class Utils
7+
{
8+
public static byte[] GetBytes(IDataRecord reader, int ordinal)
89
{
9-
public static byte[] GetBytes(IDataRecord reader, int ordinal)
10+
const int bufferSize = 100000;
11+
if (reader is null)
12+
throw new ArgumentNullException(nameof(reader));
13+
var buffer = new byte[bufferSize];
14+
var(bytesRead, offset) = (0, 0);
15+
while (bytesRead < bufferSize)
1016
{
11-
const int bufferSize = 100000;
12-
if (reader is null)
13-
throw new ArgumentNullException(nameof(reader));
14-
var buffer = new byte[bufferSize];
15-
var(bytesRead, offset) = (0, 0);
16-
while (bytesRead < bufferSize)
17-
{
18-
var read = (int)reader.GetBytes(ordinal, bufferSize + bytesRead, buffer, offset, bufferSize - bytesRead);
19-
if (read == 0)
20-
break;
21-
bytesRead += read;
22-
offset += read;
23-
}
24-
25-
if (bytesRead < bufferSize)
26-
Array.Resize(ref buffer, bytesRead);
27-
return buffer;
17+
var read = (int)reader.GetBytes(ordinal, bufferSize + bytesRead, buffer, offset, bufferSize - bytesRead);
18+
if (read == 0)
19+
break;
20+
bytesRead += read;
21+
offset += read;
2822
}
23+
24+
if (bytesRead < bufferSize)
25+
Array.Resize(ref buffer, bytesRead);
26+
return buffer;
2927
}
3028
}

0 commit comments

Comments
 (0)