Skip to content

Commit cb575f9

Browse files
committed
refactor: merge qualified enum column into existing postgres_special_types table
Move c_qualified_enum (public.c_enum) into the existing postgres_special_types table instead of a separate postgres_qualified_enum_types table, per PR feedback. The schema-qualified enum type is tested alongside the existing unqualified c_enum column in the same table and its queries. This avoids adding a new table to the schema.
1 parent 4728286 commit cb575f9

20 files changed

Lines changed: 277 additions & 818 deletions

File tree

end2end/EndToEndTests/NpgsqlDapperTester.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ public async Task EmptyTestsTable()
2121
await QuerySql.TruncatePostgresNetworkTypesAsync();
2222
await QuerySql.TruncatePostgresArrayTypesAsync();
2323
await QuerySql.TruncatePostgresSpecialTypesAsync();
24-
await QuerySql.TruncatePostgresQualifiedEnumTypesAsync();
2524
}
2625
}

end2end/EndToEndTests/NpgsqlTester.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ public async Task EmptyTestsTables()
2222
await QuerySql.TruncatePostgresArrayTypesAsync();
2323
await QuerySql.TruncatePostgresSpecialTypesAsync();
2424
await QuerySql.TruncatePostgresNotNullTypesAsync();
25-
await QuerySql.TruncatePostgresQualifiedEnumTypesAsync();
2625
}
2726
}

examples/NpgsqlDapperExample/Models.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class PostgresSpecialType
8585
{
8686
public Guid? CUuid { get; init; }
8787
public CEnum? CEnum { get; init; }
88+
public CEnum? CQualifiedEnum { get; init; }
8889
public JsonElement? CJson { get; init; }
8990
public JsonElement? CJsonStringOverride { get; init; }
9091
public JsonElement? CJsonb { get; init; }
@@ -96,10 +97,6 @@ public class PostgresNotNullType
9697
{
9798
public required CEnum CEnumNotNull { get; init; }
9899
};
99-
public class PostgresQualifiedEnumType
100-
{
101-
public CEnum? CQualifiedEnum { get; init; }
102-
};
103100
public class ExtendedBio
104101
{
105102
public required string AuthorName { get; init; }

examples/NpgsqlDapperExample/QuerySql.cs

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,8 @@ INSERT INTO postgres_special_types
13581358
c_xml,
13591359
c_xml_string_override,
13601360
c_uuid,
1361-
c_enum
1361+
c_enum,
1362+
c_qualified_enum
13621363
)
13631364
VALUES (
13641365
@c_json,
@@ -1368,7 +1369,8 @@ INSERT INTO postgres_special_types
13681369
@c_xml::xml,
13691370
@c_xml_string_override::xml,
13701371
@c_uuid,
1371-
@c_enum::c_enum
1372+
@c_enum::c_enum,
1373+
@c_qualified_enum::c_enum
13721374
)";
13731375
public class InsertPostgresSpecialTypesArgs
13741376
{
@@ -1380,6 +1382,7 @@ public class InsertPostgresSpecialTypesArgs
13801382
public string? CXmlStringOverride { get; init; }
13811383
public Guid? CUuid { get; init; }
13821384
public CEnum? CEnum { get; init; }
1385+
public CEnum? CQualifiedEnum { get; init; }
13831386
};
13841387
public async Task InsertPostgresSpecialTypesAsync(InsertPostgresSpecialTypesArgs args)
13851388
{
@@ -1392,6 +1395,7 @@ public async Task InsertPostgresSpecialTypesAsync(InsertPostgresSpecialTypesArgs
13921395
queryParams.Add("c_xml_string_override", args.CXmlStringOverride);
13931396
queryParams.Add("c_uuid", args.CUuid);
13941397
queryParams.Add("c_enum", args.CEnum != null ? args.CEnum.Value.Stringify() : null);
1398+
queryParams.Add("c_qualified_enum", args.CQualifiedEnum != null ? args.CQualifiedEnum.Value.Stringify() : null);
13951399
if (this.Transaction == null)
13961400
{
13971401
using (var connection = await GetDataSource().OpenConnectionAsync())
@@ -1484,7 +1488,8 @@ public async Task TruncatePostgresNotNullTypesAsync()
14841488
c_xml,
14851489
c_xml_string_override,
14861490
c_uuid,
1487-
c_enum
1491+
c_enum,
1492+
c_qualified_enum
14881493
FROM postgres_special_types
14891494
LIMIT 1";
14901495
public class GetPostgresSpecialTypesRow
@@ -1497,6 +1502,7 @@ public class GetPostgresSpecialTypesRow
14971502
public string? CXmlStringOverride { get; init; }
14981503
public Guid? CUuid { get; init; }
14991504
public CEnum? CEnum { get; init; }
1505+
public CEnum? CQualifiedEnum { get; init; }
15001506
};
15011507
public async Task<GetPostgresSpecialTypesRow?> GetPostgresSpecialTypesAsync()
15021508
{
@@ -1887,74 +1893,4 @@ public async Task TruncatePostgresGeoTypesAsync()
18871893
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
18881894
await this.Transaction.Connection.ExecuteAsync(TruncatePostgresGeoTypesSql, transaction: this.Transaction);
18891895
}
1890-
1891-
private const string InsertPostgresQualifiedEnumTypesSql = @"INSERT INTO postgres_qualified_enum_types
1892-
(
1893-
c_qualified_enum
1894-
)
1895-
VALUES (
1896-
@c_qualified_enum::c_enum
1897-
)";
1898-
public class InsertPostgresQualifiedEnumTypesArgs
1899-
{
1900-
public CEnum? CQualifiedEnum { get; init; }
1901-
};
1902-
public async Task InsertPostgresQualifiedEnumTypesAsync(InsertPostgresQualifiedEnumTypesArgs args)
1903-
{
1904-
var queryParams = new Dictionary<string, object?>();
1905-
queryParams.Add("c_qualified_enum", args.CQualifiedEnum != null ? args.CQualifiedEnum.Value.Stringify() : null);
1906-
if (this.Transaction == null)
1907-
{
1908-
using (var connection = await GetDataSource().OpenConnectionAsync())
1909-
{
1910-
await connection.ExecuteAsync(InsertPostgresQualifiedEnumTypesSql, queryParams);
1911-
return;
1912-
}
1913-
}
1914-
1915-
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != ConnectionState.Open)
1916-
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
1917-
await this.Transaction.Connection.ExecuteAsync(InsertPostgresQualifiedEnumTypesSql, queryParams, transaction: this.Transaction);
1918-
}
1919-
1920-
private const string GetPostgresQualifiedEnumTypesSql = @"SELECT
1921-
c_qualified_enum
1922-
FROM postgres_qualified_enum_types
1923-
LIMIT 1";
1924-
public class GetPostgresQualifiedEnumTypesRow
1925-
{
1926-
public CEnum? CQualifiedEnum { get; init; }
1927-
};
1928-
public async Task<GetPostgresQualifiedEnumTypesRow?> GetPostgresQualifiedEnumTypesAsync()
1929-
{
1930-
if (this.Transaction == null)
1931-
{
1932-
using (var connection = await GetDataSource().OpenConnectionAsync())
1933-
{
1934-
var result = await connection.QueryFirstOrDefaultAsync<GetPostgresQualifiedEnumTypesRow?>(GetPostgresQualifiedEnumTypesSql);
1935-
return result;
1936-
}
1937-
}
1938-
1939-
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != ConnectionState.Open)
1940-
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
1941-
return await this.Transaction.Connection.QueryFirstOrDefaultAsync<GetPostgresQualifiedEnumTypesRow?>(GetPostgresQualifiedEnumTypesSql, transaction: this.Transaction);
1942-
}
1943-
1944-
private const string TruncatePostgresQualifiedEnumTypesSql = "TRUNCATE TABLE postgres_qualified_enum_types";
1945-
public async Task TruncatePostgresQualifiedEnumTypesAsync()
1946-
{
1947-
if (this.Transaction == null)
1948-
{
1949-
using (var connection = await GetDataSource().OpenConnectionAsync())
1950-
{
1951-
await connection.ExecuteAsync(TruncatePostgresQualifiedEnumTypesSql);
1952-
return;
1953-
}
1954-
}
1955-
1956-
if (this.Transaction?.Connection == null || this.Transaction?.Connection.State != ConnectionState.Open)
1957-
throw new InvalidOperationException("Transaction is provided, but its connection is null.");
1958-
await this.Transaction.Connection.ExecuteAsync(TruncatePostgresQualifiedEnumTypesSql, transaction: this.Transaction);
1959-
}
19601896
}

examples/NpgsqlDapperExample/request.json

Lines changed: 35 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,17 @@
605605
"name": "c_enum"
606606
}
607607
},
608+
{
609+
"name": "c_qualified_enum",
610+
"length": -1,
611+
"table": {
612+
"name": "postgres_special_types"
613+
},
614+
"type": {
615+
"schema": "public",
616+
"name": "c_enum"
617+
}
618+
},
608619
{
609620
"name": "c_json",
610621
"length": -1,
@@ -686,24 +697,6 @@
686697
}
687698
}
688699
]
689-
},
690-
{
691-
"rel": {
692-
"name": "postgres_qualified_enum_types"
693-
},
694-
"columns": [
695-
{
696-
"name": "c_qualified_enum",
697-
"length": -1,
698-
"table": {
699-
"name": "postgres_qualified_enum_types"
700-
},
701-
"type": {
702-
"schema": "public",
703-
"name": "c_enum"
704-
}
705-
}
706-
]
707700
}
708701
],
709702
"enums": [
@@ -35108,7 +35101,7 @@
3510835101
}
3510935102
},
3511035103
{
35111-
"text": "\nINSERT INTO postgres_special_types\n(\n c_json,\n c_json_string_override,\n c_jsonb,\n c_jsonpath,\n c_xml,\n c_xml_string_override,\n c_uuid,\n c_enum\n)\nVALUES (\n $1, \n $2::json, \n $3,\n $4::jsonpath,\n $5::xml,\n $6::xml,\n $7,\n $8::c_enum\n)",
35104+
"text": "\nINSERT INTO postgres_special_types\n(\n c_json,\n c_json_string_override,\n c_jsonb,\n c_jsonpath,\n c_xml,\n c_xml_string_override,\n c_uuid,\n c_enum,\n c_qualified_enum\n)\nVALUES (\n $1, \n $2::json, \n $3,\n $4::jsonpath,\n $5::xml,\n $6::xml,\n $7,\n $8::c_enum,\n $9::c_enum\n)",
3511235105
"name": "InsertPostgresSpecialTypes",
3511335106
"cmd": ":exec",
3511435107
"parameters": [
@@ -35210,6 +35203,16 @@
3521035203
"name": "c_enum"
3521135204
}
3521235205
}
35206+
},
35207+
{
35208+
"number": 9,
35209+
"column": {
35210+
"name": "c_qualified_enum",
35211+
"length": -1,
35212+
"type": {
35213+
"name": "c_enum"
35214+
}
35215+
}
3521335216
}
3521435217
],
3521535218
"comments": [
@@ -35269,7 +35272,7 @@
3526935272
"filename": "query.sql"
3527035273
},
3527135274
{
35272-
"text": "SELECT\n c_json,\n c_json_string_override,\n c_jsonb,\n c_jsonpath,\n c_xml,\n c_xml_string_override,\n c_uuid,\n c_enum\nFROM postgres_special_types \nLIMIT 1",
35275+
"text": "SELECT\n c_json,\n c_json_string_override,\n c_jsonb,\n c_jsonpath,\n c_xml,\n c_xml_string_override,\n c_uuid,\n c_enum,\n c_qualified_enum\nFROM postgres_special_types \nLIMIT 1",
3527335276
"name": "GetPostgresSpecialTypes",
3527435277
"cmd": ":one",
3527535278
"columns": [
@@ -35362,6 +35365,18 @@
3536235365
"name": "c_enum"
3536335366
},
3536435367
"originalName": "c_enum"
35368+
},
35369+
{
35370+
"name": "c_qualified_enum",
35371+
"length": -1,
35372+
"table": {
35373+
"name": "postgres_special_types"
35374+
},
35375+
"type": {
35376+
"schema": "public",
35377+
"name": "c_enum"
35378+
},
35379+
"originalName": "c_qualified_enum"
3536535380
}
3536635381
],
3536735382
"filename": "query.sql"
@@ -36247,53 +36262,6 @@
3624736262
"name": "TruncatePostgresGeoTypes",
3624836263
"cmd": ":exec",
3624936264
"filename": "query.sql"
36250-
},
36251-
{
36252-
"text": "INSERT INTO postgres_qualified_enum_types\n(\n c_qualified_enum\n)\nVALUES (\n $1::c_enum\n)",
36253-
"name": "InsertPostgresQualifiedEnumTypes",
36254-
"cmd": ":exec",
36255-
"parameters": [
36256-
{
36257-
"number": 1,
36258-
"column": {
36259-
"name": "c_qualified_enum",
36260-
"length": -1,
36261-
"type": {
36262-
"name": "c_enum"
36263-
}
36264-
}
36265-
}
36266-
],
36267-
"filename": "query.sql",
36268-
"insert_into_table": {
36269-
"name": "postgres_qualified_enum_types"
36270-
}
36271-
},
36272-
{
36273-
"text": "SELECT\n c_qualified_enum\nFROM postgres_qualified_enum_types\nLIMIT 1",
36274-
"name": "GetPostgresQualifiedEnumTypes",
36275-
"cmd": ":one",
36276-
"columns": [
36277-
{
36278-
"name": "c_qualified_enum",
36279-
"length": -1,
36280-
"table": {
36281-
"name": "postgres_qualified_enum_types"
36282-
},
36283-
"type": {
36284-
"schema": "public",
36285-
"name": "c_enum"
36286-
},
36287-
"originalName": "c_qualified_enum"
36288-
}
36289-
],
36290-
"filename": "query.sql"
36291-
},
36292-
{
36293-
"text": "TRUNCATE TABLE postgres_qualified_enum_types",
36294-
"name": "TruncatePostgresQualifiedEnumTypes",
36295-
"cmd": ":exec",
36296-
"filename": "query.sql"
3629736265
}
3629836266
],
3629936267
"sqlc_version": "v1.30.0",

0 commit comments

Comments
 (0)