Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9b0f9a3

Browse filesBrowse files
dvas-hashdvas-veeam
authored andcommitted
Add support for postgresql type names with dots (#5971)
Fixes #5972 --------- Co-authored-by: Dmitry Vasliyev <dmitry.vasilyev@veeam.com> (cherry picked from commit 3505dc3)
1 parent c4c9d0d commit 9b0f9a3
Copy full SHA for 9b0f9a3

File tree

Expand file treeCollapse file tree

3 files changed

+34
-4
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+34
-4
lines changed

‎src/Npgsql/Internal/Postgres/DataTypeName.cs

Copy file name to clipboardExpand all lines: src/Npgsql/Internal/Postgres/DataTypeName.cs
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,14 @@ public static DataTypeName FromDisplayName(string displayName, string? schema =
127127
// There is one exception and that's array syntax, which is always resolvable in both ways, while we want the canonical name.
128128
var schemaEndIndex = displayNameSpan.IndexOf('.');
129129
if (schemaEndIndex is not -1 &&
130+
string.IsNullOrEmpty(schema) &&
130131
!displayNameSpan.Slice(schemaEndIndex).StartsWith("_".AsSpan(), StringComparison.Ordinal) &&
131132
!displayNameSpan.EndsWith("[]".AsSpan(), StringComparison.Ordinal))
132133
return new(displayName);
133134

134135
// First we strip the schema to get the type name.
135-
if (schemaEndIndex is not -1)
136+
if (schemaEndIndex is not -1 &&
137+
string.IsNullOrEmpty(schema))
136138
{
137139
schema = displayNameSpan.Slice(0, schemaEndIndex).ToString();
138140
displayNameSpan = displayNameSpan.Slice(schemaEndIndex + 1);

‎test/Npgsql.Tests/Support/TestBase.cs

Copy file name to clipboardExpand all lines: test/Npgsql.Tests/Support/TestBase.cs
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ internal static async Task<T> AssertTypeReadCore<T>(
201201
if (dotIndex > -1 && dataTypeName.Substring(0, dotIndex) is "pg_catalog" or "public")
202202
dataTypeName = dataTypeName.Substring(dotIndex + 1);
203203

204-
Assert.That(dataTypeName, Is.EqualTo(pgTypeName),
204+
// For composite type with dots, postgres works only with quoted name - scheme."My.type.name"
205+
// but npgsql converts it to name without quotes
206+
var pgTypeNameWithoutQuotes = dataTypeName.Replace("\"", string.Empty);
207+
Assert.That(dataTypeName, Is.EqualTo(pgTypeNameWithoutQuotes),
205208
$"Got wrong result from GetDataTypeName when reading '{truncatedSqlLiteral}'");
206209

207210
if (isDefault)
@@ -300,9 +303,12 @@ internal static async Task AssertTypeWriteCore<T>(
300303
}
301304

302305
// With data type name
303-
p = new NpgsqlParameter { Value = valueFactory(), DataTypeName = pgTypeNameWithoutFacets };
306+
// For composite type with dots in name, Postgresql returns name with quotes - scheme."My.type.name"
307+
// but for npgsql mapping we should use names without quotes - scheme.My.type.name
308+
var pgTypeNameWithoutFacetsAndDots = pgTypeNameWithoutFacets.Replace("\"", string.Empty);
309+
p = new NpgsqlParameter { Value = valueFactory(), DataTypeName = pgTypeNameWithoutFacetsAndDots };
304310
cmd.Parameters.Add(p);
305-
errorIdentifier[++errorIdentifierIndex] = $"DataTypeName={pgTypeNameWithoutFacets}";
311+
errorIdentifier[++errorIdentifierIndex] = $"DataTypeName={pgTypeNameWithoutFacetsAndDots}";
306312
CheckInference();
307313

308314
// With DbType

‎test/Npgsql.Tests/Types/CompositeTests.cs

Copy file name to clipboardExpand all lines: test/Npgsql.Tests/Types/CompositeTests.cs
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@ await AssertType(
201201
isDefaultForWriting: true);
202202
}
203203

204+
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/5972")]
205+
public async Task With_schema_and_dots_in_type_name()
206+
{
207+
await using var adminConnection = await OpenConnectionAsync();
208+
var schema = await CreateTempSchema(adminConnection);
209+
var typename = "Some.Composite.with.dots";
210+
211+
await adminConnection.ExecuteNonQueryAsync($"CREATE TYPE {schema}.\"{typename}\" AS (x int, some_text text)");
212+
213+
var dataSourceBuilder = CreateDataSourceBuilder();
214+
dataSourceBuilder.MapComposite<SomeComposite>($"{schema}.{typename}");
215+
await using var dataSource = dataSourceBuilder.Build();
216+
await using var connection = await dataSource.OpenConnectionAsync();
217+
218+
await AssertType(
219+
connection,
220+
new SomeComposite { SomeText = "foobar", X = 10 },
221+
"(10,foobar)",
222+
$"{schema}.\"{typename}\"",
223+
npgsqlDbType: null);
224+
}
225+
204226
[Test]
205227
public async Task Struct()
206228
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.