diff --git a/Drivers/ColumnMapping.cs b/Drivers/ColumnMapping.cs index 0c29e269..7236aa22 100644 --- a/Drivers/ColumnMapping.cs +++ b/Drivers/ColumnMapping.cs @@ -11,7 +11,7 @@ public class ColumnMapping( Func? readerArrayFn = null, string? usingDirective = null, Func? writerFn = null, - string? convertFunc = null, + Func? convertFunc = null, string? sqlMapper = null, string? sqlMapperImpl = null) { @@ -20,7 +20,7 @@ public class ColumnMapping( public Func? ReaderArrayFn { get; } = readerArrayFn; public string? UsingDirective { get; } = usingDirective; public Func? WriterFn { get; } = writerFn; - public string? ConvertFunc { get; } = convertFunc; + public Func? ConvertFunc { get; } = convertFunc; public string? SqlMapper { get; } = sqlMapper; public string? SqlMapperImpl { get; } = sqlMapperImpl; } \ No newline at end of file diff --git a/Drivers/DbDriver.cs b/Drivers/DbDriver.cs index da8db470..01c0fb95 100644 --- a/Drivers/DbDriver.cs +++ b/Drivers/DbDriver.cs @@ -55,9 +55,6 @@ public abstract class DbDriver "NpgsqlCidr", ]; - protected const string IntTo32 = "Convert.ToInt32"; - protected const string IntTo64 = "Convert.ToInt64"; - public abstract Dictionary ColumnMappings { get; } @@ -339,11 +336,13 @@ public string GetIdColumnType(Query query) public virtual string[] GetLastIdStatement(Query query) { var idColumnType = GetIdColumnType(query); - var convertFunc = ColumnMappings[idColumnType].ConvertFunc ?? throw new InvalidOperationException($"ConvertFunc is missing for id column type {idColumnType}"); + var convertFunc = ColumnMappings[idColumnType].ConvertFunc ?? + throw new InvalidOperationException($"ConvertFunc is missing for id column type {idColumnType}"); + var convertFuncCall = convertFunc(Variable.Result.AsVarName()); return [ $"var {Variable.Result.AsVarName()} = await {Variable.Command.AsVarName()}.ExecuteScalarAsync();", - $"return {convertFunc}({Variable.Result.AsVarName()});" + $"return {convertFuncCall};" ]; } diff --git a/Drivers/MySqlConnectorDriver.cs b/Drivers/MySqlConnectorDriver.cs index 861b679b..c90e1f7f 100644 --- a/Drivers/MySqlConnectorDriver.cs +++ b/Drivers/MySqlConnectorDriver.cs @@ -48,7 +48,7 @@ public partial class MySqlConnectorDriver( { "bigint", new() } }, ordinal => $"reader.GetInt64({ordinal})", - convertFunc: IntTo64 + convertFunc: x => $"Convert.ToInt64{x}" ), ["byte"] = new ColumnMapping( new() @@ -99,7 +99,7 @@ public partial class MySqlConnectorDriver( { "mediumint", new() } }, ordinal => $"reader.GetInt32({ordinal})", - convertFunc: IntTo32 + convertFunc: x => $"Convert.ToInt32{x}" ), ["double"] = new( new() diff --git a/Drivers/NpgsqlDriver.cs b/Drivers/NpgsqlDriver.cs index c2120f37..cb9837b8 100644 --- a/Drivers/NpgsqlDriver.cs +++ b/Drivers/NpgsqlDriver.cs @@ -43,7 +43,7 @@ public NpgsqlDriver( }, readerFn: ordinal => $"reader.GetInt64({ordinal})", readerArrayFn: ordinal => $"reader.GetFieldValue({ordinal})", - convertFunc: IntTo64 + convertFunc: x => $"Convert.ToInt64({x})" ), ["byte[]"] = new( new() @@ -81,7 +81,8 @@ public NpgsqlDriver( { "uuid", new() } }, readerFn: ordinal => $"reader.GetFieldValue({ordinal})", - readerArrayFn: ordinal => $"reader.GetFieldValue({ordinal})" + readerArrayFn: ordinal => $"reader.GetFieldValue({ordinal})", + convertFunc: x => $"Guid.Parse({x}?.ToString())" ), ["TimeSpan"] = new( new() @@ -126,7 +127,8 @@ public NpgsqlDriver( { "int2", new() } }, readerFn: ordinal => $"reader.GetInt16({ordinal})", - readerArrayFn: ordinal => $"reader.GetFieldValue({ordinal})" + readerArrayFn: ordinal => $"reader.GetFieldValue({ordinal})", + convertFunc: x => $"Convert.ToInt16({x})" ), ["int"] = new( new() @@ -137,7 +139,8 @@ public NpgsqlDriver( { "serial", new() } }, readerFn: ordinal => $"reader.GetInt32({ordinal})", - readerArrayFn: ordinal => $"reader.GetFieldValue({ordinal})" + readerArrayFn: ordinal => $"reader.GetFieldValue({ordinal})", + convertFunc: x => $"Convert.ToInt32({x})" ), ["float"] = new( new() diff --git a/Drivers/SqliteDriver.cs b/Drivers/SqliteDriver.cs index ace613af..61d517a7 100644 --- a/Drivers/SqliteDriver.cs +++ b/Drivers/SqliteDriver.cs @@ -40,7 +40,7 @@ public partial class SqliteDriver( { "integernotnulldefaultunixepoch", new() } }, ordinal => $"reader.GetInt32({ordinal})", - convertFunc: IntTo32 + convertFunc: x => $"Convert.ToInt32({x})" ), ["decimal"] = new( new() diff --git a/docs/04_Postgres.md b/docs/04_Postgres.md index 062f55ab..24534496 100644 --- a/docs/04_Postgres.md +++ b/docs/04_Postgres.md @@ -2,8 +2,16 @@
:execlastid - Implementation -Implemented via a `RETURNING` clause, allowing the `INSERT` command to return the newly created id, which can be of any -data type that can have a unique constraint. +Implemented via a `RETURNING` clause, allowing the `INSERT` command to return the newly created id. +The data types that can be used as id data types for this annotation are: +1. uuid +2. bigint +3. integer +4. smallint (less recommended due to small id range, but possible) + +```sql +INSERT INTO tab1 (field1, field2) VALUES ('a', 1) RETURNING id_field; +```
diff --git a/docs/06_Sqlite.md b/docs/06_Sqlite.md index 1a63c783..8d027c73 100644 --- a/docs/06_Sqlite.md +++ b/docs/06_Sqlite.md @@ -3,13 +3,12 @@ :execlastid - Implementation ## :execlastid - Implementation -Implemented via a `RETURNING` clause, allowing the `INSERT` command to return the newly created id, which can be of any -data type that can have a unique constraint. - +Implemented via a `RETURNING` clause, allowing the `INSERT` command to return the newly created id. +Only integer data type is supported as id for this annotation. + ```sql INSERT INTO tab1 (field1, field2) VALUES ('a', 1) RETURNING id_field; ``` -
diff --git a/examples/NpgsqlDapperExample/Models.cs b/examples/NpgsqlDapperExample/Models.cs index c4db3bcc..2f20b05f 100644 --- a/examples/NpgsqlDapperExample/Models.cs +++ b/examples/NpgsqlDapperExample/Models.cs @@ -14,7 +14,7 @@ public class Author }; public class Book { - public required long Id { get; init; } + public required Guid Id { get; init; } public required string Name { get; init; } public required long AuthorId { get; init; } public string? Description { get; init; } diff --git a/examples/NpgsqlDapperExample/QuerySql.cs b/examples/NpgsqlDapperExample/QuerySql.cs index 911f82eb..d2d9d1a5 100644 --- a/examples/NpgsqlDapperExample/QuerySql.cs +++ b/examples/NpgsqlDapperExample/QuerySql.cs @@ -361,14 +361,14 @@ public async Task> GetAuthorsByIdsAndNames(GetA private const string CreateBookSql = "INSERT INTO books (name, author_id) VALUES (@name, @author_id) RETURNING id"; public class CreateBookRow { - public required long Id { get; init; } + public required Guid Id { get; init; } }; public class CreateBookArgs { public required string Name { get; init; } public required long AuthorId { get; init; } }; - public async Task CreateBook(CreateBookArgs args) + public async Task CreateBook(CreateBookArgs args) { var queryParams = new Dictionary(); queryParams.Add("name", args.Name); @@ -377,7 +377,7 @@ public async Task CreateBook(CreateBookArgs args) { using (var connection = new NpgsqlConnection(ConnectionString)) { - return await connection.QuerySingleAsync(CreateBookSql, queryParams); + return await connection.QuerySingleAsync(CreateBookSql, queryParams); } } @@ -386,7 +386,7 @@ public async Task CreateBook(CreateBookArgs args) throw new System.InvalidOperationException("Transaction is provided, but its connection is null."); } - return await this.Transaction.Connection.QuerySingleAsync(CreateBookSql, queryParams, transaction: this.Transaction); + return await this.Transaction.Connection.QuerySingleAsync(CreateBookSql, queryParams, transaction: this.Transaction); } 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 "; @@ -407,7 +407,7 @@ public async Task> ListAllAuthorsBooks() { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -424,7 +424,7 @@ public async Task> ListAllAuthorsBooks() { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -496,7 +496,7 @@ public async Task> GetAuthorsByBookName(GetAuthors { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -514,7 +514,7 @@ public async Task> GetAuthorsByBookName(GetAuthors { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } diff --git a/examples/NpgsqlDapperExample/request.json b/examples/NpgsqlDapperExample/request.json index f9f20192..37623228 100644 --- a/examples/NpgsqlDapperExample/request.json +++ b/examples/NpgsqlDapperExample/request.json @@ -75,7 +75,7 @@ "name": "books" }, "type": { - "name": "bigserial" + "name": "uuid" } }, { @@ -33003,7 +33003,7 @@ "name": "books" }, "type": { - "name": "bigserial" + "name": "uuid" }, "originalName": "id" } diff --git a/examples/NpgsqlDapperExample/request.message b/examples/NpgsqlDapperExample/request.message index e6125e33..7b1dad8e 100644 --- a/examples/NpgsqlDapperExample/request.message +++ b/examples/NpgsqlDapperExample/request.message @@ -3,13 +3,13 @@ 2 postgresql%examples/config/postgresql/schema.sql"$examples/config/postgresql/query.sqlb examples/NpgsqlDapperExamplecsharp{"debugRequest":true,"generateCsproj":true,"namespaceName":"NpgsqlDapperExampleGen","overrides":[{"column":"GetPostgresFunctions:max_integer","csharp_type":{"notNull":false,"type":"int"}},{"column":"GetPostgresFunctions:max_varchar","csharp_type":{"notNull":false,"type":"string"}},{"column":"GetPostgresFunctions:max_timestamp","csharp_type":{"notNull":true,"type":"DateTime"}},{"column":"*:c_json_string_override","csharp_type":{"notNull":false,"type":"string"}},{"column":"*:c_macaddr8","csharp_type":{"notNull":false,"type":"string"}}],"targetFramework":"net8.0","useDapper":true}* -./dist/LocalRunner public"public +./dist/LocalRunner public"public authors) id0R authorsb  bigserial& name0R authorsbtext# -bio0R authorsbtext -books' -id0Rbooksb  bigserial$ +bio0R authorsbtext +books" +id0Rbooksbuuid$ name0Rbooksbtext5 author_id0Rbooksb pg_catalogint8) @@ -10260,10 +10260,10 @@ WHERE id = ANY($1::BIGINT []) AND name = ANY($2::TEXT [])GetAuthorsByIdsAndNam id0R authorsb  bigserialzid", name0R authorsbtextzname"( bio0R authorsbtextzbio**& 0b -pg_catalogint8* 0btext: query.sql +pg_catalogint8* 0btext: query.sql @INSERT INTO books (name, author_id) VALUES ($1, $2) RETURNING id -CreateBook :execlastid"+ -id0Rbooksb  bigserialzid*62 +CreateBook :execlastid"& +id0Rbooksbuuidzid*62 name0Rpublicbooksbtextzname*KG author_id0Rpublicbooksbpg_catalog.int8z author_id: query.sqlBbooks SELECT diff --git a/examples/NpgsqlDapperLegacyExample/Models.cs b/examples/NpgsqlDapperLegacyExample/Models.cs index cd12384e..df908aea 100644 --- a/examples/NpgsqlDapperLegacyExample/Models.cs +++ b/examples/NpgsqlDapperLegacyExample/Models.cs @@ -15,7 +15,7 @@ public class Author }; public class Book { - public long Id { get; set; } + public Guid Id { get; set; } public string Name { get; set; } public long AuthorId { get; set; } public string Description { get; set; } diff --git a/examples/NpgsqlDapperLegacyExample/QuerySql.cs b/examples/NpgsqlDapperLegacyExample/QuerySql.cs index a85db0d7..ed78e97d 100644 --- a/examples/NpgsqlDapperLegacyExample/QuerySql.cs +++ b/examples/NpgsqlDapperLegacyExample/QuerySql.cs @@ -362,14 +362,14 @@ public async Task> GetAuthorsByIdsAndNames(GetA private const string CreateBookSql = "INSERT INTO books (name, author_id) VALUES (@name, @author_id) RETURNING id"; public class CreateBookRow { - public long Id { get; set; } + public Guid Id { get; set; } }; public class CreateBookArgs { public string Name { get; set; } public long AuthorId { get; set; } }; - public async Task CreateBook(CreateBookArgs args) + public async Task CreateBook(CreateBookArgs args) { var queryParams = new Dictionary(); queryParams.Add("name", args.Name); @@ -378,7 +378,7 @@ public async Task CreateBook(CreateBookArgs args) { using (var connection = new NpgsqlConnection(ConnectionString)) { - return await connection.QuerySingleAsync(CreateBookSql, queryParams); + return await connection.QuerySingleAsync(CreateBookSql, queryParams); } } @@ -387,7 +387,7 @@ public async Task CreateBook(CreateBookArgs args) throw new System.InvalidOperationException("Transaction is provided, but its connection is null."); } - return await this.Transaction.Connection.QuerySingleAsync(CreateBookSql, queryParams, transaction: this.Transaction); + return await this.Transaction.Connection.QuerySingleAsync(CreateBookSql, queryParams, transaction: this.Transaction); } 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 "; @@ -408,7 +408,7 @@ public async Task> ListAllAuthorsBooks() { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -425,7 +425,7 @@ public async Task> ListAllAuthorsBooks() { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -497,7 +497,7 @@ public async Task> GetAuthorsByBookName(GetAuthors { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -515,7 +515,7 @@ public async Task> GetAuthorsByBookName(GetAuthors { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } diff --git a/examples/NpgsqlDapperLegacyExample/request.json b/examples/NpgsqlDapperLegacyExample/request.json index 63645fef..ccd70c40 100644 --- a/examples/NpgsqlDapperLegacyExample/request.json +++ b/examples/NpgsqlDapperLegacyExample/request.json @@ -75,7 +75,7 @@ "name": "books" }, "type": { - "name": "bigserial" + "name": "uuid" } }, { @@ -33003,7 +33003,7 @@ "name": "books" }, "type": { - "name": "bigserial" + "name": "uuid" }, "originalName": "id" } diff --git a/examples/NpgsqlDapperLegacyExample/request.message b/examples/NpgsqlDapperLegacyExample/request.message index 0ea14029..1dc66e98 100644 --- a/examples/NpgsqlDapperLegacyExample/request.message +++ b/examples/NpgsqlDapperLegacyExample/request.message @@ -3,13 +3,13 @@ 2 postgresql%examples/config/postgresql/schema.sql"$examples/config/postgresql/query.sqlb "examples/NpgsqlDapperLegacyExamplecsharp{"debugRequest":true,"generateCsproj":true,"namespaceName":"NpgsqlDapperLegacyExampleGen","overrides":[{"column":"GetPostgresFunctions:max_integer","csharp_type":{"notNull":false,"type":"int"}},{"column":"GetPostgresFunctions:max_varchar","csharp_type":{"notNull":false,"type":"string"}},{"column":"GetPostgresFunctions:max_timestamp","csharp_type":{"notNull":true,"type":"DateTime"}},{"column":"*:c_json_string_override","csharp_type":{"notNull":false,"type":"string"}},{"column":"*:c_macaddr8","csharp_type":{"notNull":false,"type":"string"}}],"targetFramework":"netstandard2.0","useDapper":true}* -./dist/LocalRunner public"public +./dist/LocalRunner public"public authors) id0R authorsb  bigserial& name0R authorsbtext# -bio0R authorsbtext -books' -id0Rbooksb  bigserial$ +bio0R authorsbtext +books" +id0Rbooksbuuid$ name0Rbooksbtext5 author_id0Rbooksb pg_catalogint8) @@ -10260,10 +10260,10 @@ WHERE id = ANY($1::BIGINT []) AND name = ANY($2::TEXT [])GetAuthorsByIdsAndNam id0R authorsb  bigserialzid", name0R authorsbtextzname"( bio0R authorsbtextzbio**& 0b -pg_catalogint8* 0btext: query.sql +pg_catalogint8* 0btext: query.sql @INSERT INTO books (name, author_id) VALUES ($1, $2) RETURNING id -CreateBook :execlastid"+ -id0Rbooksb  bigserialzid*62 +CreateBook :execlastid"& +id0Rbooksbuuidzid*62 name0Rpublicbooksbtextzname*KG author_id0Rpublicbooksbpg_catalog.int8z author_id: query.sqlBbooks SELECT diff --git a/examples/NpgsqlExample/Models.cs b/examples/NpgsqlExample/Models.cs index 298e1948..7391c3d4 100644 --- a/examples/NpgsqlExample/Models.cs +++ b/examples/NpgsqlExample/Models.cs @@ -7,6 +7,6 @@ namespace NpgsqlExampleGen; public readonly record struct Author(long Id, string Name, string? Bio); -public readonly record struct Book(long Id, string Name, long AuthorId, string? Description); +public readonly record struct Book(Guid Id, string Name, long AuthorId, string? Description); public readonly record struct PostgresType(bool? CBoolean, byte[]? CBit, short? CSmallint, int? CInteger, long? CBigint, decimal? CDecimal, decimal? CNumeric, float? CReal, double? CDoublePrecision, decimal? CMoney, DateTime? CDate, TimeSpan? CTime, DateTime? CTimestamp, DateTime? CTimestampWithTz, TimeSpan? CInterval, string? CChar, string? CVarchar, string? CCharacterVarying, string? CBpchar, string? CText, JsonElement? CJson, JsonElement? CJsonStringOverride, JsonElement? CJsonb, string? CJsonpath, NpgsqlCidr? CCidr, IPAddress? CInet, PhysicalAddress? CMacaddr, string? CMacaddr8, Guid? CUuid, byte[]? CBytea, bool[]? CBooleanArray, string[]? CTextArray, int[]? CIntegerArray, decimal[]? CDecimalArray, DateTime[]? CDateArray, DateTime[]? CTimestampArray); public readonly record struct PostgresGeometricType(NpgsqlPoint? CPoint, NpgsqlLine? CLine, NpgsqlLSeg? CLseg, NpgsqlBox? CBox, NpgsqlPath? CPath, NpgsqlPolygon? CPolygon, NpgsqlCircle? CCircle); \ No newline at end of file diff --git a/examples/NpgsqlExample/QuerySql.cs b/examples/NpgsqlExample/QuerySql.cs index 3c6cbaf4..9da5490e 100644 --- a/examples/NpgsqlExample/QuerySql.cs +++ b/examples/NpgsqlExample/QuerySql.cs @@ -500,9 +500,9 @@ public async Task> GetAuthorsByIdsAndNames(GetA } private const string CreateBookSql = "INSERT INTO books (name, author_id) VALUES (@name, @author_id) RETURNING id"; - public readonly record struct CreateBookRow(long Id); + public readonly record struct CreateBookRow(Guid Id); public readonly record struct CreateBookArgs(string Name, long AuthorId); - public async Task CreateBook(CreateBookArgs args) + public async Task CreateBook(CreateBookArgs args) { if (this.Transaction == null) { @@ -513,7 +513,7 @@ public async Task CreateBook(CreateBookArgs args) command.Parameters.AddWithValue("@name", args.Name); command.Parameters.AddWithValue("@author_id", args.AuthorId); var result = await command.ExecuteScalarAsync(); - return Convert.ToInt64(result); + return Guid.Parse(result?.ToString()); } } } @@ -530,7 +530,7 @@ public async Task CreateBook(CreateBookArgs args) command.Parameters.AddWithValue("@name", args.Name); command.Parameters.AddWithValue("@author_id", args.AuthorId); var result = await command.ExecuteScalarAsync(); - return Convert.ToInt64(result); + return Guid.Parse(result?.ToString()); } } @@ -548,7 +548,7 @@ public async Task> ListAllAuthorsBooks() { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -565,7 +565,7 @@ public async Task> ListAllAuthorsBooks() { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -624,7 +624,7 @@ public async Task> GetAuthorsByBookName(GetAuthors { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -642,7 +642,7 @@ public async Task> GetAuthorsByBookName(GetAuthors { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } diff --git a/examples/NpgsqlExample/request.json b/examples/NpgsqlExample/request.json index 56a48480..e9dc600e 100644 --- a/examples/NpgsqlExample/request.json +++ b/examples/NpgsqlExample/request.json @@ -75,7 +75,7 @@ "name": "books" }, "type": { - "name": "bigserial" + "name": "uuid" } }, { @@ -33003,7 +33003,7 @@ "name": "books" }, "type": { - "name": "bigserial" + "name": "uuid" }, "originalName": "id" } diff --git a/examples/NpgsqlExample/request.message b/examples/NpgsqlExample/request.message index eef57db8..361842de 100644 --- a/examples/NpgsqlExample/request.message +++ b/examples/NpgsqlExample/request.message @@ -3,13 +3,13 @@ 2 postgresql%examples/config/postgresql/schema.sql"$examples/config/postgresql/query.sqlb examples/NpgsqlExamplecsharp{"debugRequest":true,"generateCsproj":true,"namespaceName":"NpgsqlExampleGen","overrides":[{"column":"GetPostgresFunctions:max_integer","csharp_type":{"notNull":false,"type":"int"}},{"column":"GetPostgresFunctions:max_varchar","csharp_type":{"notNull":false,"type":"string"}},{"column":"GetPostgresFunctions:max_timestamp","csharp_type":{"notNull":true,"type":"DateTime"}},{"column":"*:c_json_string_override","csharp_type":{"notNull":false,"type":"string"}},{"column":"*:c_macaddr8","csharp_type":{"notNull":false,"type":"string"}}],"targetFramework":"net8.0","useDapper":false}* -./dist/LocalRunner public"public +./dist/LocalRunner public"public authors) id0R authorsb  bigserial& name0R authorsbtext# -bio0R authorsbtext -books' -id0Rbooksb  bigserial$ +bio0R authorsbtext +books" +id0Rbooksbuuid$ name0Rbooksbtext5 author_id0Rbooksb pg_catalogint8) @@ -10260,10 +10260,10 @@ WHERE id = ANY($1::BIGINT []) AND name = ANY($2::TEXT [])GetAuthorsByIdsAndNam id0R authorsb  bigserialzid", name0R authorsbtextzname"( bio0R authorsbtextzbio**& 0b -pg_catalogint8* 0btext: query.sql +pg_catalogint8* 0btext: query.sql @INSERT INTO books (name, author_id) VALUES ($1, $2) RETURNING id -CreateBook :execlastid"+ -id0Rbooksb  bigserialzid*62 +CreateBook :execlastid"& +id0Rbooksbuuidzid*62 name0Rpublicbooksbtextzname*KG author_id0Rpublicbooksbpg_catalog.int8z author_id: query.sqlBbooks SELECT diff --git a/examples/NpgsqlLegacyExample/Models.cs b/examples/NpgsqlLegacyExample/Models.cs index 7a0e2c5b..3d9f5cbc 100644 --- a/examples/NpgsqlLegacyExample/Models.cs +++ b/examples/NpgsqlLegacyExample/Models.cs @@ -15,7 +15,7 @@ public class Author }; public class Book { - public long Id { get; set; } + public Guid Id { get; set; } public string Name { get; set; } public long AuthorId { get; set; } public string Description { get; set; } diff --git a/examples/NpgsqlLegacyExample/QuerySql.cs b/examples/NpgsqlLegacyExample/QuerySql.cs index bfc45978..04aa1fc3 100644 --- a/examples/NpgsqlLegacyExample/QuerySql.cs +++ b/examples/NpgsqlLegacyExample/QuerySql.cs @@ -572,14 +572,14 @@ public async Task> GetAuthorsByIdsAndNames(GetA private const string CreateBookSql = "INSERT INTO books (name, author_id) VALUES (@name, @author_id) RETURNING id"; public class CreateBookRow { - public long Id { get; set; } + public Guid Id { get; set; } }; public class CreateBookArgs { public string Name { get; set; } public long AuthorId { get; set; } }; - public async Task CreateBook(CreateBookArgs args) + public async Task CreateBook(CreateBookArgs args) { if (this.Transaction == null) { @@ -590,7 +590,7 @@ public async Task CreateBook(CreateBookArgs args) command.Parameters.AddWithValue("@name", args.Name); command.Parameters.AddWithValue("@author_id", args.AuthorId); var result = await command.ExecuteScalarAsync(); - return Convert.ToInt64(result); + return Guid.Parse(result?.ToString()); } } } @@ -607,7 +607,7 @@ public async Task CreateBook(CreateBookArgs args) command.Parameters.AddWithValue("@name", args.Name); command.Parameters.AddWithValue("@author_id", args.AuthorId); var result = await command.ExecuteScalarAsync(); - return Convert.ToInt64(result); + return Guid.Parse(result?.ToString()); } } @@ -629,7 +629,7 @@ public async Task> ListAllAuthorsBooks() { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -646,7 +646,7 @@ public async Task> ListAllAuthorsBooks() { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -718,7 +718,7 @@ public async Task> GetAuthorsByBookName(GetAuthors { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } @@ -736,7 +736,7 @@ public async Task> GetAuthorsByBookName(GetAuthors { var result = new List(); while (await reader.ReadAsync()) - 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) } }); + 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.GetFieldValue(3), Name = reader.GetString(4), AuthorId = reader.GetInt64(5), Description = reader.IsDBNull(6) ? null : reader.GetString(6) } }); return result; } } diff --git a/examples/NpgsqlLegacyExample/request.json b/examples/NpgsqlLegacyExample/request.json index bd5f98e3..f1fda4a9 100644 --- a/examples/NpgsqlLegacyExample/request.json +++ b/examples/NpgsqlLegacyExample/request.json @@ -75,7 +75,7 @@ "name": "books" }, "type": { - "name": "bigserial" + "name": "uuid" } }, { @@ -33003,7 +33003,7 @@ "name": "books" }, "type": { - "name": "bigserial" + "name": "uuid" }, "originalName": "id" } diff --git a/examples/NpgsqlLegacyExample/request.message b/examples/NpgsqlLegacyExample/request.message index 93c27b8a..a0a78cd0 100644 --- a/examples/NpgsqlLegacyExample/request.message +++ b/examples/NpgsqlLegacyExample/request.message @@ -3,13 +3,13 @@ 2 postgresql%examples/config/postgresql/schema.sql"$examples/config/postgresql/query.sqlb examples/NpgsqlLegacyExamplecsharp{"debugRequest":true,"generateCsproj":true,"namespaceName":"NpgsqlLegacyExampleGen","overrides":[{"column":"GetPostgresFunctions:max_integer","csharp_type":{"notNull":false,"type":"int"}},{"column":"GetPostgresFunctions:max_varchar","csharp_type":{"notNull":false,"type":"string"}},{"column":"GetPostgresFunctions:max_timestamp","csharp_type":{"notNull":true,"type":"DateTime"}},{"column":"*:c_json_string_override","csharp_type":{"notNull":false,"type":"string"}},{"column":"*:c_macaddr8","csharp_type":{"notNull":false,"type":"string"}}],"targetFramework":"netstandard2.0","useDapper":false}* -./dist/LocalRunner public"public +./dist/LocalRunner public"public authors) id0R authorsb  bigserial& name0R authorsbtext# -bio0R authorsbtext -books' -id0Rbooksb  bigserial$ +bio0R authorsbtext +books" +id0Rbooksbuuid$ name0Rbooksbtext5 author_id0Rbooksb pg_catalogint8) @@ -10260,10 +10260,10 @@ WHERE id = ANY($1::BIGINT []) AND name = ANY($2::TEXT [])GetAuthorsByIdsAndNam id0R authorsb  bigserialzid", name0R authorsbtextzname"( bio0R authorsbtextzbio**& 0b -pg_catalogint8* 0btext: query.sql +pg_catalogint8* 0btext: query.sql @INSERT INTO books (name, author_id) VALUES ($1, $2) RETURNING id -CreateBook :execlastid"+ -id0Rbooksb  bigserialzid*62 +CreateBook :execlastid"& +id0Rbooksbuuidzid*62 name0Rpublicbooksbtextzname*KG author_id0Rpublicbooksbpg_catalog.int8z author_id: query.sqlBbooks SELECT diff --git a/examples/config/postgresql/schema.sql b/examples/config/postgresql/schema.sql index f4564bcb..7f7c3890 100644 --- a/examples/config/postgresql/schema.sql +++ b/examples/config/postgresql/schema.sql @@ -4,8 +4,10 @@ CREATE TABLE authors ( bio TEXT ); +CREATE EXTENSION "uuid-ossp"; + CREATE TABLE books ( - id BIGSERIAL PRIMARY KEY, + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), name TEXT NOT NULL, author_id BIGINT NOT NULL, description TEXT, @@ -64,7 +66,6 @@ CREATE TABLE postgres_types ( c_timestamp_array TIMESTAMP [] ); - CREATE TABLE postgres_geometric_types ( c_point POINT, c_line LINE,