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
5 changes: 2 additions & 3 deletions CodeGenerator/Generators/CsprojGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public File GenerateFile()

private string GetFileContents()
{
var nullableProperty = options.DotnetFramework == DotnetFramework.DotnetStandard21 ? "enable" : "disable";
var nullableProperty = options.DotnetFramework.LatestDotnetSupported() ? "\n<Nullable>enable</Nullable>" : "";
return $"""
<!--{Consts.AutoGeneratedComment}-->
<!--Run the following to add the project to the solution:
Expand All @@ -28,8 +28,7 @@ dotnet sln add {projectName}/{projectName}.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>{options.DotnetFramework.ToName()}</TargetFramework>
<Nullable>{nullableProperty}</Nullable>
<TargetFramework>{options.DotnetFramework.ToName()}</TargetFramework>{nullableProperty}
<OutputType>Library</OutputType>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion CodeGenerator/Generators/RootGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class RootGen(Options options)
public CompilationUnitSyntax CompilationRootGen(IdentifierNameSyntax namespaceName,
UsingDirectiveSyntax[] usingDirectives, MemberDeclarationSyntax classDeclaration)
{
return options.DotnetFramework == DotnetFramework.DotnetStandard21 ? GetFileScoped() : GetBLockScoped();
return options.DotnetFramework.LatestDotnetSupported() ? GetFileScoped() : GetBLockScoped();

CompilationUnitSyntax GetFileScoped()
{
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ update-wasm-plugin:

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

test-wasm-plugin: sqlc-generate-wasm dockerfile-generate run-tests
4 changes: 2 additions & 2 deletions MySqlConnectorExample/MySqlConnectorExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
</PropertyGroup>

Expand Down
274 changes: 136 additions & 138 deletions MySqlConnectorExample/QuerySql.cs

Large diffs are not rendered by default.

44 changes: 21 additions & 23 deletions MySqlConnectorExample/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
// auto-generated by sqlc - do not edit
namespace MySqlConnectorExample
{
using System;
using System.Data;
using System;
using System.Data;

public static class Utils
namespace MySqlConnectorExample;
public static class Utils
{
public static byte[] GetBytes(IDataRecord reader, int ordinal)
{
public static byte[] GetBytes(IDataRecord reader, int ordinal)
const int bufferSize = 100000;
if (reader is null)
throw new ArgumentNullException(nameof(reader));
var buffer = new byte[bufferSize];
var(bytesRead, offset) = (0, 0);
while (bytesRead < bufferSize)
{
const int bufferSize = 100000;
if (reader is null)
throw new ArgumentNullException(nameof(reader));
var buffer = new byte[bufferSize];
var(bytesRead, offset) = (0, 0);
while (bytesRead < bufferSize)
{
var read = (int)reader.GetBytes(ordinal, bufferSize + bytesRead, buffer, offset, bufferSize - bytesRead);
if (read == 0)
break;
bytesRead += read;
offset += read;
}

if (bytesRead < bufferSize)
Array.Resize(ref buffer, bytesRead);
return buffer;
var read = (int)reader.GetBytes(ordinal, bufferSize + bytesRead, buffer, offset, bufferSize - bytesRead);
if (read == 0)
break;
bytesRead += read;
offset += read;
}

if (bytesRead < bufferSize)
Array.Resize(ref buffer, bytesRead);
return buffer;
}
}
4 changes: 2 additions & 2 deletions NpgsqlExample/NpgsqlExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
</PropertyGroup>

Expand Down
200 changes: 99 additions & 101 deletions NpgsqlExample/QuerySql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,133 +2,131 @@
// ReSharper disable NotAccessedPositionalProperty.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable InconsistentNaming
namespace NpgsqlExample
{
using System.Collections.Generic;
using System.Threading.Tasks;
using Npgsql;
using System.Collections.Generic;
using System.Threading.Tasks;
using Npgsql;

public class QuerySql(string connectionString)
namespace NpgsqlExample;
public class QuerySql(string connectionString)
{
private const string GetAuthorSql = "SELECT id, name, bio FROM authors WHERE id = @id LIMIT 1 ";
public readonly record struct GetAuthorRow(long Id, string Name, string? Bio);
public readonly record struct GetAuthorArgs(long Id);
public async Task<GetAuthorRow?> GetAuthor(GetAuthorArgs args)
{
private const string GetAuthorSql = "SELECT id, name, bio FROM authors WHERE id = @id LIMIT 1 ";
public readonly record struct GetAuthorRow(long Id, string Name, string? Bio);
public readonly record struct GetAuthorArgs(long Id);
public async Task<GetAuthorRow?> GetAuthor(GetAuthorArgs args)
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(GetAuthorSql);
command.Parameters.AddWithValue("@id", args.Id);
var reader = await command.ExecuteReaderAsync();
if (await reader.ReadAsync())
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(GetAuthorSql);
command.Parameters.AddWithValue("@id", args.Id);
var reader = await command.ExecuteReaderAsync();
if (await reader.ReadAsync())
return new GetAuthorRow
{
return new GetAuthorRow
{
Id = reader.GetInt64(0),
Name = reader.GetString(1),
Bio = reader.IsDBNull(2) ? null : reader.GetString(2)
};
}

return null;
Id = reader.GetInt64(0),
Name = reader.GetString(1),
Bio = reader.IsDBNull(2) ? null : reader.GetString(2)
};
}

return null;
}
}

private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name ";
public readonly record struct ListAuthorsRow(long Id, string Name, string? Bio);
public async Task<List<ListAuthorsRow>> ListAuthors()
private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name ";
public readonly record struct ListAuthorsRow(long Id, string Name, string? Bio);
public async Task<List<ListAuthorsRow>> ListAuthors()
{
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(ListAuthorsSql);
var reader = await command.ExecuteReaderAsync();
var result = new List<ListAuthorsRow>();
while (await reader.ReadAsync())
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(ListAuthorsSql);
var reader = await command.ExecuteReaderAsync();
var result = new List<ListAuthorsRow>();
while (await reader.ReadAsync())
{
result.Add(new ListAuthorsRow { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2) });
}

return result;
result.Add(new ListAuthorsRow { Id = reader.GetInt64(0), Name = reader.GetString(1), Bio = reader.IsDBNull(2) ? null : reader.GetString(2) });
}

return result;
}
}

private const string CreateAuthorSql = "INSERT INTO authors ( name , bio ) VALUES ( @name, @bio ) RETURNING id, name, bio ";
public readonly record struct CreateAuthorRow(long Id, string Name, string? Bio);
public readonly record struct CreateAuthorArgs(string Name, string? Bio);
public async Task<CreateAuthorRow?> CreateAuthor(CreateAuthorArgs args)
private const string CreateAuthorSql = "INSERT INTO authors ( name , bio ) VALUES ( @name, @bio ) RETURNING id, name, bio ";
public readonly record struct CreateAuthorRow(long Id, string Name, string? Bio);
public readonly record struct CreateAuthorArgs(string Name, string? Bio);
public async Task<CreateAuthorRow?> CreateAuthor(CreateAuthorArgs args)
{
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(CreateAuthorSql);
command.Parameters.AddWithValue("@name", args.Name);
command.Parameters.AddWithValue("@bio", args.Bio);
var reader = await command.ExecuteReaderAsync();
if (await reader.ReadAsync())
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(CreateAuthorSql);
command.Parameters.AddWithValue("@name", args.Name);
command.Parameters.AddWithValue("@bio", args.Bio);
var reader = await command.ExecuteReaderAsync();
if (await reader.ReadAsync())
return new CreateAuthorRow
{
return new CreateAuthorRow
{
Id = reader.GetInt64(0),
Name = reader.GetString(1),
Bio = reader.IsDBNull(2) ? null : reader.GetString(2)
};
}

return null;
Id = reader.GetInt64(0),
Name = reader.GetString(1),
Bio = reader.IsDBNull(2) ? null : reader.GetString(2)
};
}

return null;
}
}

private const string DeleteAuthorSql = "DELETE FROM authors WHERE id = @id ";
public readonly record struct DeleteAuthorArgs(long Id);
public async Task DeleteAuthor(DeleteAuthorArgs args)
private const string DeleteAuthorSql = "DELETE FROM authors WHERE id = @id ";
public readonly record struct DeleteAuthorArgs(long Id);
public async Task DeleteAuthor(DeleteAuthorArgs args)
{
{
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(DeleteAuthorSql);
command.Parameters.AddWithValue("@id", args.Id);
await command.ExecuteScalarAsync();
}
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(DeleteAuthorSql);
command.Parameters.AddWithValue("@id", args.Id);
await command.ExecuteScalarAsync();
}
}

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 ";
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);
public async Task<TestRow?> Test()
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 ";
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);
public async Task<TestRow?> Test()
{
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(TestSql);
var reader = await command.ExecuteReaderAsync();
if (await reader.ReadAsync())
{
await using var connection = NpgsqlDataSource.Create(connectionString);
;
await using var command = connection.CreateCommand(TestSql);
var reader = await command.ExecuteReaderAsync();
if (await reader.ReadAsync())
return new TestRow
{
return new TestRow
{
C_bit = reader.IsDBNull(0) ? null : Utils.GetBytes(reader, 0),
C_smallint = reader.IsDBNull(1) ? null : reader.GetInt32(1),
C_boolean = reader.IsDBNull(2) ? null : reader.GetBoolean(2),
C_integer = reader.IsDBNull(3) ? null : reader.GetInt32(3),
C_bigint = reader.IsDBNull(4) ? null : reader.GetInt32(4),
C_serial = reader.IsDBNull(5) ? null : reader.GetInt64(5),
C_decimal = reader.IsDBNull(6) ? null : reader.GetFloat(6),
C_numeric = reader.IsDBNull(7) ? null : reader.GetFloat(7),
C_real = reader.IsDBNull(8) ? null : reader.GetFloat(8),
C_double_precision = reader.IsDBNull(9) ? null : reader.GetFloat(9),
C_date = reader.IsDBNull(10) ? null : reader.GetString(10),
C_time = reader.IsDBNull(11) ? null : reader.GetString(11),
C_timestamp = reader.IsDBNull(12) ? null : reader.GetString(12),
C_char = reader.IsDBNull(13) ? null : reader.GetString(13),
C_varchar = reader.IsDBNull(14) ? null : reader.GetString(14),
C_bytea = reader.IsDBNull(15) ? null : Utils.GetBytes(reader, 15),
C_text = reader.IsDBNull(16) ? null : reader.GetString(16),
C_json = reader.IsDBNull(17) ? null : reader.GetString(17)
};
}

return null;
C_bit = reader.IsDBNull(0) ? null : Utils.GetBytes(reader, 0),
C_smallint = reader.IsDBNull(1) ? null : reader.GetInt32(1),
C_boolean = reader.IsDBNull(2) ? null : reader.GetBoolean(2),
C_integer = reader.IsDBNull(3) ? null : reader.GetInt32(3),
C_bigint = reader.IsDBNull(4) ? null : reader.GetInt32(4),
C_serial = reader.IsDBNull(5) ? null : reader.GetInt64(5),
C_decimal = reader.IsDBNull(6) ? null : reader.GetFloat(6),
C_numeric = reader.IsDBNull(7) ? null : reader.GetFloat(7),
C_real = reader.IsDBNull(8) ? null : reader.GetFloat(8),
C_double_precision = reader.IsDBNull(9) ? null : reader.GetFloat(9),
C_date = reader.IsDBNull(10) ? null : reader.GetString(10),
C_time = reader.IsDBNull(11) ? null : reader.GetString(11),
C_timestamp = reader.IsDBNull(12) ? null : reader.GetString(12),
C_char = reader.IsDBNull(13) ? null : reader.GetString(13),
C_varchar = reader.IsDBNull(14) ? null : reader.GetString(14),
C_bytea = reader.IsDBNull(15) ? null : Utils.GetBytes(reader, 15),
C_text = reader.IsDBNull(16) ? null : reader.GetString(16),
C_json = reader.IsDBNull(17) ? null : reader.GetString(17)
};
}

return null;
}
}
}
44 changes: 21 additions & 23 deletions NpgsqlExample/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
// auto-generated by sqlc - do not edit
namespace NpgsqlExample
{
using System;
using System.Data;
using System;
using System.Data;

public static class Utils
namespace NpgsqlExample;
public static class Utils
{
public static byte[] GetBytes(IDataRecord reader, int ordinal)
{
public static byte[] GetBytes(IDataRecord reader, int ordinal)
const int bufferSize = 100000;
if (reader is null)
throw new ArgumentNullException(nameof(reader));
var buffer = new byte[bufferSize];
var(bytesRead, offset) = (0, 0);
while (bytesRead < bufferSize)
{
const int bufferSize = 100000;
if (reader is null)
throw new ArgumentNullException(nameof(reader));
var buffer = new byte[bufferSize];
var(bytesRead, offset) = (0, 0);
while (bytesRead < bufferSize)
{
var read = (int)reader.GetBytes(ordinal, bufferSize + bytesRead, buffer, offset, bufferSize - bytesRead);
if (read == 0)
break;
bytesRead += read;
offset += read;
}

if (bytesRead < bufferSize)
Array.Resize(ref buffer, bytesRead);
return buffer;
var read = (int)reader.GetBytes(ordinal, bufferSize + bytesRead, buffer, offset, bufferSize - bytesRead);
if (read == 0)
break;
bytesRead += read;
offset += read;
}

if (bytesRead < bufferSize)
Array.Resize(ref buffer, bytesRead);
return buffer;
}
}
Loading