Skip to content

Commit 9458767

Browse files
authored
Merge pull request #1209 from RodolfoFerreira/issue-1208-timeout-get-fields
Updates GetCommandText() to use pg_index catalog in PostgreSqlDbHelper
2 parents 7ffd397 + b010fd0 commit 9458767

1 file changed

Lines changed: 20 additions & 33 deletions

File tree

RepoDb.PostgreSql/RepoDb.PostgreSql/DbHelpers/PostgreSqlDbHelper.cs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,39 +54,26 @@ public PostgreSqlDbHelper(IResolver<string, Type> dbTypeResolver)
5454
/// <returns></returns>
5555
private string GetCommandText()
5656
{
57-
return @"
58-
SELECT C.column_name
59-
, CAST((CASE WHEN C.column_name = TMP.column_name THEN 1 ELSE 0 END) AS BOOLEAN) AS IsPrimary
60-
, CASE WHEN C.is_identity = 'YES' OR POSITION('NEXTVAL' IN UPPER(C.column_default)) >= 1 THEN
61-
true
62-
ELSE
63-
false
64-
END AS IsIdentity
65-
, CAST(C.is_nullable AS BOOLEAN) AS IsNullable
66-
, C.data_type AS DataType
67-
, CASE WHEN C.column_default IS NOT NULL THEN
68-
true
69-
ELSE
70-
false
71-
END AS HasDefaultValue
72-
FROM information_schema.columns C
73-
LEFT JOIN
74-
(
75-
SELECT C.table_schema
76-
, C.table_name
77-
, C.column_name
78-
, C.column_default
79-
FROM information_schema.table_constraints TC
80-
JOIN information_schema.constraint_column_usage AS CCU USING (constraint_schema, constraint_name)
81-
JOIN information_schema.columns AS C ON C.table_schema = TC.constraint_schema
82-
AND TC.table_name = C.table_name
83-
AND CCU.column_name = C.column_name
84-
WHERE TC.constraint_type = 'PRIMARY KEY'
85-
) TMP ON TMP.table_schema = C.table_schema
86-
AND TMP.table_name = C.table_name
87-
AND TMP.column_name = C.column_name
88-
WHERE C.table_name = @TableName
89-
AND C.table_schema = @Schema;";
57+
return """
58+
SELECT C.column_name,
59+
COALESCE(I.indisprimary, FALSE) AS IsPrimary,
60+
CASE
61+
WHEN C.is_identity = 'YES'
62+
OR POSITION('NEXTVAL' IN UPPER(C.column_default)) >= 1 THEN TRUE
63+
ELSE FALSE
64+
END AS IsIdentity,
65+
CAST(C.is_nullable AS BOOLEAN) AS IsNullable,
66+
C.data_type AS DataType,
67+
CASE
68+
WHEN C.column_default IS NOT NULL THEN TRUE
69+
ELSE FALSE
70+
END AS HasDefaultValue
71+
FROM information_schema.columns C
72+
LEFT JOIN pg_index I ON I.indrelid = (quote_ident(C.table_schema) || '.' || quote_ident(C.table_name))::regclass
73+
AND C.ordinal_position = ANY (I.indkey)
74+
WHERE C.table_name = @TableName
75+
AND C.table_schema = @Schema;
76+
""";
9077
}
9178

9279
/// <summary>

0 commit comments

Comments
 (0)