-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathNpgsqlSchema.cs
More file actions
1149 lines (1045 loc) · 44.5 KB
/
NpgsqlSchema.cs
File metadata and controls
1149 lines (1045 loc) · 44.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Npgsql.Internal;
using Npgsql.PostgresTypes;
using NpgsqlTypes;
namespace Npgsql;
/// <summary>
/// Provides the underlying mechanism for reading schema information.
/// </summary>
static class NpgsqlSchema
{
public static Task<DataTable> GetSchema(bool async, NpgsqlConnection conn, string? collectionName, string?[]? restrictions, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(collectionName);
if (collectionName.Length == 0)
throw new ArgumentException("Collection name cannot be empty.", nameof(collectionName));
return collectionName.ToUpperInvariant() switch
{
"METADATACOLLECTIONS" => Task.FromResult(GetMetaDataCollections()),
"RESTRICTIONS" => Task.FromResult(GetRestrictions()),
"DATASOURCEINFORMATION" => Task.FromResult(GetDataSourceInformation(conn)),
"DATATYPES" => Task.FromResult(GetDataTypes(conn)),
"RESERVEDWORDS" => Task.FromResult(GetReservedWords()),
// custom collections for npgsql
"DATABASES" => GetDatabases(conn, restrictions, async, cancellationToken),
"SCHEMATA" => GetSchemata(conn, restrictions, async, cancellationToken),
"TABLES" => GetTables(conn, restrictions, async, cancellationToken),
"COLUMNS" => GetColumns(conn, restrictions, async, cancellationToken),
"VIEWS" => GetViews(conn, restrictions, async, cancellationToken),
"MATERIALIZEDVIEWS" => GetMaterializedViews(conn, restrictions, async, cancellationToken),
"USERS" => GetUsers(conn, restrictions, async, cancellationToken),
"INDEXES" => GetIndexes(conn, restrictions, async, cancellationToken),
"INDEXCOLUMNS" => GetIndexColumns(conn, restrictions, async, cancellationToken),
"CONSTRAINTS" => GetConstraints(conn, restrictions, collectionName, async, cancellationToken),
"PRIMARYKEY" => GetConstraints(conn, restrictions, collectionName, async, cancellationToken),
"UNIQUEKEYS" => GetConstraints(conn, restrictions, collectionName, async, cancellationToken),
"FOREIGNKEYS" => GetConstraints(conn, restrictions, collectionName, async, cancellationToken),
"CONSTRAINTCOLUMNS" => GetConstraintColumns(conn, restrictions, async, cancellationToken),
_ => throw new ArgumentOutOfRangeException(nameof(collectionName), collectionName, "Invalid collection name.")
};
}
/// <summary>
/// Returns the MetaDataCollections that lists all possible collections.
/// </summary>
/// <returns>The MetaDataCollections</returns>
static DataTable GetMetaDataCollections()
{
var table = new DataTable("MetaDataCollections");
table.Columns.Add("CollectionName", typeof(string));
table.Columns.Add("NumberOfRestrictions", typeof(int));
table.Columns.Add("NumberOfIdentifierParts", typeof(int));
table.Rows.Add("MetaDataCollections", 0, 0);
table.Rows.Add("DataSourceInformation", 0, 0);
table.Rows.Add("Restrictions", 0, 0);
table.Rows.Add("DataTypes", 0, 0); // TODO: Support type name restriction
table.Rows.Add("Databases", 1, 1);
table.Rows.Add("Tables", 4, 3);
table.Rows.Add("Columns", 4, 4);
table.Rows.Add("Views", 3, 3);
table.Rows.Add("Users", 1, 1);
table.Rows.Add("Indexes", 4, 4);
table.Rows.Add("IndexColumns", 5, 5);
return table;
}
/// <summary>
/// Returns the Restrictions that contains the meaning and position of the values in the restrictions array.
/// </summary>
/// <returns>The Restrictions</returns>
static DataTable GetRestrictions()
{
var table = new DataTable("Restrictions");
table.Columns.Add("CollectionName", typeof(string));
table.Columns.Add("RestrictionName", typeof(string));
table.Columns.Add("RestrictionDefault", typeof(string));
table.Columns.Add("RestrictionNumber", typeof(int));
table.Rows.Add("Database", "Name", "Name", 1);
table.Rows.Add("Tables", "Catalog", "table_catalog", 1);
table.Rows.Add("Tables", "Schema", "schema_catalog", 2);
table.Rows.Add("Tables", "Table", "table_name", 3);
table.Rows.Add("Tables", "TableType", "table_type", 4);
table.Rows.Add("Columns", "Catalog", "table_catalog", 1);
table.Rows.Add("Columns", "Schema", "table_schema", 2);
table.Rows.Add("Columns", "TableName", "table_name", 3);
table.Rows.Add("Columns", "Column", "column_name", 4);
table.Rows.Add("Views", "Catalog", "table_catalog", 1);
table.Rows.Add("Views", "Schema", "table_schema", 2);
table.Rows.Add("Views", "Table", "table_name", 3);
table.Rows.Add("Users", "User", "user_name", 1);
table.Rows.Add("Indexes", "Catalog", "table_catalog", 1);
table.Rows.Add("Indexes", "Schema", "table_schema", 2);
table.Rows.Add("Indexes", "Table", "table_name", 3);
table.Rows.Add("Indexes", "Index", "index_name", 4);
table.Rows.Add("IndexColumns", "Catalog", "table_catalog", 1);
table.Rows.Add("IndexColumns", "Schema", "table_schema", 2);
table.Rows.Add("IndexColumns", "Table", "table_name", 3);
table.Rows.Add("IndexColumns", "Index", "index_name", 4);
table.Rows.Add("IndexColumns", "Column", "column_name", 5);
return table;
}
static NpgsqlCommand BuildCommand(NpgsqlConnection conn, StringBuilder query, string?[]? restrictions, params string[]? names)
=> BuildCommand(conn, query, restrictions, true, names);
static NpgsqlCommand BuildCommand(NpgsqlConnection conn, StringBuilder query, string?[]? restrictions, bool addWhere, params string[]? names)
{
var command = new NpgsqlCommand();
if (restrictions != null && names != null)
{
for (var i = 0; i < restrictions.Length && i < names.Length; ++i)
{
if (restrictions[i] is { Length: > 0 } restriction)
{
if (addWhere)
{
query.Append(" WHERE ");
addWhere = false;
}
else
{
query.Append(" AND ");
}
var paramName = RemoveSpecialChars(names[i]);
query.AppendFormat("{0} = :{1}", names[i], paramName);
command.Parameters.Add(new NpgsqlParameter(paramName, restriction));
}
}
}
command.CommandText = query.ToString();
command.Connection = conn;
return command;
}
static string RemoveSpecialChars(string paramName)
=> paramName.Replace("(", "").Replace(")", "").Replace(".", "");
static Task<DataTable> GetDatabases(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("Databases")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("database_name"),
new DataColumn("owner"),
new DataColumn("encoding")
}
};
var sql = new StringBuilder();
sql.Append(
"""
SELECT d.datname, u.usename, pg_catalog.pg_encoding_to_char(d.encoding)
FROM pg_catalog.pg_database d
LEFT JOIN pg_catalog.pg_user u ON d.datdba = u.usesysid
""");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, "datname"),
dataTable,
(reader, row) =>
{
row["database_name"] = GetFieldValueOrDBNull<string>(reader, 0);
row["owner"] = GetFieldValueOrDBNull<string>(reader, 1);
row["encoding"] = GetFieldValueOrDBNull<string>(reader, 2);
}, cancellationToken);
}
static Task<DataTable> GetSchemata(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("Schemata")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("catalog_name"),
new DataColumn("schema_name"),
new DataColumn("schema_owner")
}
};
var sql = new StringBuilder(
"""
SELECT * FROM (
SELECT current_database(), nspname, r.rolname
FROM pg_catalog.pg_namespace
LEFT JOIN pg_catalog.pg_roles r ON r.oid = nspowner
) tmp
""");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, "catalog_name", "schema_name", "schema_owner"),
dataTable,
(reader, row) =>
{
row["catalog_name"] = GetFieldValueOrDBNull<string>(reader, 0);
row["schema_name"] = GetFieldValueOrDBNull<string>(reader, 1);
row["schema_owner"] = GetFieldValueOrDBNull<string>(reader, 2);
}, cancellationToken);
}
static Task<DataTable> GetTables(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("Tables")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("table_catalog"),
new DataColumn("table_schema"),
new DataColumn("table_name"),
new DataColumn("table_type")
}
};
var sql = new StringBuilder();
sql.Append(
"""
SELECT table_catalog, table_schema, table_name, table_type
FROM information_schema.tables
WHERE
table_type IN ('BASE TABLE', 'FOREIGN', 'FOREIGN TABLE') AND
table_schema NOT IN ('pg_catalog', 'information_schema')
""");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, false, "table_catalog", "table_schema", "table_name", "table_type"),
dataTable,
(reader, row) =>
{
row["table_catalog"] = GetFieldValueOrDBNull<string>(reader, 0);
row["table_schema"] = GetFieldValueOrDBNull<string>(reader, 1);
row["table_name"] = GetFieldValueOrDBNull<string>(reader, 2);
row["table_type"] = GetFieldValueOrDBNull<string>(reader, 3);
}, cancellationToken);
}
static Task<DataTable> GetColumns(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("Columns")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("table_catalog"),
new DataColumn("table_schema"),
new DataColumn("table_name"),
new DataColumn("column_name"),
new DataColumn("ordinal_position", typeof(int)),
new DataColumn("column_default"),
new DataColumn("is_nullable"),
new DataColumn("data_type"),
new DataColumn("character_maximum_length", typeof(int)),
new DataColumn("character_octet_length", typeof(int)),
new DataColumn("numeric_precision", typeof(int)),
new DataColumn("numeric_precision_radix", typeof(int)),
new DataColumn("numeric_scale", typeof(int)),
new DataColumn("datetime_precision", typeof(int)),
new DataColumn("character_set_catalog"),
new DataColumn("character_set_schema"),
new DataColumn("character_set_name"),
new DataColumn("collation_catalog")
}
};
var sql = new StringBuilder(
"""
SELECT
table_catalog,
table_schema,
table_name,
column_name,
ordinal_position,
column_default,
is_nullable,
CASE WHEN udt_schema is NULL THEN udt_name ELSE format_type(typ.oid, NULL) END,
character_maximum_length,
character_octet_length,
numeric_precision,
numeric_precision_radix,
numeric_scale,
datetime_precision,
character_set_catalog,
character_set_schema,
character_set_name,
collation_catalog
FROM information_schema.columns
JOIN pg_namespace AS ns ON ns.nspname = udt_schema
JOIN pg_type AS typ ON typnamespace = ns.oid AND typname = udt_name
""");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, "table_catalog", "table_schema", "table_name", "column_name"),
dataTable,
(reader, row) =>
{
row["table_catalog"] = GetFieldValueOrDBNull<string>(reader, 0);
row["table_schema"] = GetFieldValueOrDBNull<string>(reader, 1);
row["table_name"] = GetFieldValueOrDBNull<string>(reader, 2);
row["column_name"] = GetFieldValueOrDBNull<string>(reader, 3);
row["ordinal_position"] = GetFieldValueOrDBNull<int>(reader, 4);
row["column_default"] = GetFieldValueOrDBNull<string>(reader, 5);
row["is_nullable"] = GetFieldValueOrDBNull<string>(reader, 6);
row["data_type"] = GetFieldValueOrDBNull<string>(reader, 7);
row["character_maximum_length"] = GetFieldValueOrDBNull<int>(reader, 8);
row["character_octet_length"] = GetFieldValueOrDBNull<int>(reader, 9);
row["numeric_precision"] = GetFieldValueOrDBNull<int>(reader, 10);
row["numeric_precision_radix"] = GetFieldValueOrDBNull<int>(reader, 11);
row["numeric_scale"] = GetFieldValueOrDBNull<int>(reader, 12);
row["datetime_precision"] = GetFieldValueOrDBNull<int>(reader, 13);
row["character_set_catalog"] = GetFieldValueOrDBNull<string>(reader, 14);
row["character_set_schema"] = GetFieldValueOrDBNull<string>(reader, 15);
row["character_set_name"] = GetFieldValueOrDBNull<string>(reader, 16);
row["collation_catalog"] = GetFieldValueOrDBNull<string>(reader, 17);
}, cancellationToken);
}
static Task<DataTable> GetViews(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("Views")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("table_catalog"),
new DataColumn("table_schema"),
new DataColumn("table_name"),
new DataColumn("check_option"),
new DataColumn("is_updatable")
}
};
var sql = new StringBuilder(
"""
SELECT table_catalog, table_schema, table_name, check_option, is_updatable
FROM information_schema.views
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
""");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, false, "table_catalog", "table_schema", "table_name"),
dataTable,
(reader, row) =>
{
row["table_catalog"] = GetFieldValueOrDBNull<string>(reader, 0);
row["table_schema"] = GetFieldValueOrDBNull<string>(reader, 1);
row["table_name"] = GetFieldValueOrDBNull<string>(reader, 2);
row["check_option"] = GetFieldValueOrDBNull<string>(reader, 3);
row["is_updatable"] = GetFieldValueOrDBNull<string>(reader, 3);
}, cancellationToken);
}
static Task<DataTable> GetMaterializedViews(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("MaterializedViews")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("table_catalog"),
new DataColumn("table_schema"),
new DataColumn("table_name"),
new DataColumn("table_owner"),
new DataColumn("has_indexes", typeof(bool)),
new DataColumn("is_populated", typeof(bool))
}
};
var sql = new StringBuilder();
sql.Append("""SELECT current_database(), schemaname, matviewname, matviewowner, hasindexes, ispopulated FROM pg_catalog.pg_matviews""");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, "current_database()", "schemaname", "matviewname", "matviewowner"),
dataTable,
(reader, row) =>
{
row["table_catalog"] = GetFieldValueOrDBNull<string>(reader, 0);
row["table_schema"] = GetFieldValueOrDBNull<string>(reader, 1);
row["table_name"] = GetFieldValueOrDBNull<string>(reader, 2);
row["table_owner"] = GetFieldValueOrDBNull<string>(reader, 3);
row["has_indexes"] = GetFieldValueOrDBNull<bool>(reader, 4);
row["is_populated"] = GetFieldValueOrDBNull<bool>(reader, 5);
}, cancellationToken);
}
static Task<DataTable> GetUsers(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("Users")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("user_name"),
new DataColumn("user_sysid", typeof(uint))
}
};
var sql = new StringBuilder();
sql.Append("SELECT usename, usesysid FROM pg_catalog.pg_user");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, "usename"),
dataTable,
(reader, row) =>
{
row["user_name"] = GetFieldValueOrDBNull<string>(reader, 0);
row["user_sysid"] = GetFieldValueOrDBNull<uint>(reader, 1);
}, cancellationToken);
}
static Task<DataTable> GetIndexes(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("Indexes")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("table_catalog"),
new DataColumn("table_schema"),
new DataColumn("table_name"),
new DataColumn("index_name"),
new DataColumn("type_desc")
}
};
var sql = new StringBuilder(
"""
SELECT current_database(),
n.nspname,
t.relname,
i.relname,
''
FROM
pg_catalog.pg_class i
JOIN pg_catalog.pg_index ix ON ix.indexrelid = i.oid
JOIN pg_catalog.pg_class t ON ix.indrelid = t.oid
LEFT JOIN pg_catalog.pg_user u ON u.usesysid = i.relowner
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = i.relnamespace
WHERE
i.relkind = 'i' AND
n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
t.relkind = 'r'
""");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, false, "current_database()", "n.nspname", "t.relname", "i.relname"),
dataTable,
(reader, row) =>
{
row["table_catalog"] = GetFieldValueOrDBNull<string>(reader, 0);
row["table_schema"] = GetFieldValueOrDBNull<string>(reader, 1);
row["table_name"] = GetFieldValueOrDBNull<string>(reader, 2);
row["index_name"] = GetFieldValueOrDBNull<string>(reader, 3);
row["type_desc"] = GetFieldValueOrDBNull<string>(reader, 4);
}, cancellationToken);
}
static Task<DataTable> GetIndexColumns(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var dataTable = new DataTable("IndexColumns")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("constraint_catalog"),
new DataColumn("constraint_schema"),
new DataColumn("constraint_name"),
new DataColumn("table_catalog"),
new DataColumn("table_schema"),
new DataColumn("table_name"),
new DataColumn("column_name"),
new DataColumn("index_name")
}
};
var sql = new StringBuilder(
"""
SELECT
current_database(),
t_ns.nspname,
ix_cls.relname,
current_database(),
ix_ns.nspname,
t.relname,
a.attname,
ix_cls.relname
FROM
pg_class t
JOIN pg_index ix ON t.oid = ix.indrelid
JOIN pg_class ix_cls ON ix.indexrelid = ix_cls.oid
JOIN pg_attribute a ON t.oid = a.attrelid
LEFT JOIN pg_namespace t_ns ON t.relnamespace = t_ns.oid
LEFT JOIN pg_namespace ix_ns ON ix_cls.relnamespace = ix_ns.oid
WHERE
ix_cls.relkind = 'i' AND
t_ns.nspname NOT IN ('pg_catalog', 'pg_toast') AND
a.attnum = ANY(ix.indkey) AND
t.relkind = 'r'
""");
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, false, "current_database()", "t_ns.nspname", "t.relname", "ix_cls.relname", "a.attname"),
dataTable,
(reader, row) =>
{
row["constraint_catalog"] = GetFieldValueOrDBNull<string>(reader, 0);
row["constraint_schema"] = GetFieldValueOrDBNull<string>(reader, 1);
row["constraint_name"] = GetFieldValueOrDBNull<string>(reader, 2);
row["table_catalog"] = GetFieldValueOrDBNull<string>(reader, 3);
row["table_schema"] = GetFieldValueOrDBNull<string>(reader, 4);
row["table_name"] = GetFieldValueOrDBNull<string>(reader, 5);
row["column_name"] = GetFieldValueOrDBNull<string>(reader, 6);
row["index_name"] = GetFieldValueOrDBNull<string>(reader, 7);
}, cancellationToken);
}
static Task<DataTable> GetConstraints(NpgsqlConnection conn, string?[]? restrictions, string? constraintType, bool async, CancellationToken cancellationToken = default)
{
var sql = new StringBuilder(
"""
SELECT
current_database(),
pgn.nspname,
pgc.conname,
current_database(),
pgtn.nspname,
pgt.relname,
constraint_type,
pgc.condeferrable,
pgc.condeferred
FROM
pg_catalog.pg_constraint pgc
JOIN pg_catalog.pg_namespace pgn ON pgc.connamespace = pgn.oid
JOIN pg_catalog.pg_class pgt ON pgc.conrelid = pgt.oid
JOIN pg_catalog.pg_namespace pgtn ON pgt.relnamespace = pgtn.oid
JOIN (
SELECT 'PRIMARY KEY' AS constraint_type, 'p' AS contype
UNION ALL
SELECT 'FOREIGN KEY' AS constraint_type, 'f' AS contype
UNION ALL
SELECT 'UNIQUE KEY' AS constraint_type, 'u' AS contype
) mapping_table ON mapping_table.contype = pgc.contype
""");
switch (constraintType)
{
case "ForeignKeys":
sql.Append(" and pgc.contype='f'");
break;
case "PrimaryKey":
sql.Append(" and pgc.contype='p'");
break;
case "UniqueKeys":
sql.Append(" and pgc.contype='u'");
break;
default:
constraintType = "Constraints";
break;
}
var dataTable = new DataTable(constraintType)
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("CONSTRAINT_CATALOG"),
new DataColumn("CONSTRAINT_SCHEMA"),
new DataColumn("CONSTRAINT_NAME"),
new DataColumn("TABLE_CATALOG"),
new DataColumn("TABLE_SCHEMA"),
new DataColumn("TABLE_NAME"),
new DataColumn("CONSTRAINT_TYPE"),
new DataColumn("IS_DEFERRABLE", typeof(bool)),
new DataColumn("INITIALLY_DEFERRED", typeof(bool))
}
};
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, false, "current_database()", "pgtn.nspname", "pgt.relname", "pgc.conname"),
dataTable,
(reader, row) =>
{
row["CONSTRAINT_CATALOG"] = GetFieldValueOrDBNull<string>(reader, 0);
row["CONSTRAINT_SCHEMA"] = GetFieldValueOrDBNull<string>(reader, 1);
row["CONSTRAINT_NAME"] = GetFieldValueOrDBNull<string>(reader, 2);
row["TABLE_CATALOG"] = GetFieldValueOrDBNull<string>(reader, 3);
row["TABLE_SCHEMA"] = GetFieldValueOrDBNull<string>(reader, 4);
row["TABLE_NAME"] = GetFieldValueOrDBNull<string>(reader, 5);
row["CONSTRAINT_TYPE"] = GetFieldValueOrDBNull<string>(reader, 6);
row["IS_DEFERRABLE"] = GetFieldValueOrDBNull<bool>(reader, 7);
row["INITIALLY_DEFERRED"] = GetFieldValueOrDBNull<bool>(reader, 8);
}, cancellationToken);
}
static Task<DataTable> GetConstraintColumns(NpgsqlConnection conn, string?[]? restrictions, bool async, CancellationToken cancellationToken = default)
{
var sql = new StringBuilder(
"""
SELECT current_database(),
n.nspname,
c.conname,
current_database(),
n.nspname,
t.relname,
a.attname,
a.attnum,
mapping_table.constraint_type
FROM pg_constraint c
JOIN pg_namespace n on n.oid = c.connamespace
JOIN pg_class t on t.oid = c.conrelid AND t.relkind = 'r'
JOIN pg_attribute a on t.oid = a.attrelid AND a.attnum = ANY(c.conkey)
JOIN (
SELECT 'PRIMARY KEY' AS constraint_type, 'p' AS contype
UNION ALL
SELECT 'FOREIGN KEY' AS constraint_type, 'f' AS contype
UNION ALL
SELECT 'UNIQUE KEY' AS constraint_type, 'u' AS contype
) mapping_table ON
mapping_table.contype = c.contype
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
""");
var dataTable = new DataTable("ConstraintColumns")
{
Locale = CultureInfo.InvariantCulture,
Columns =
{
new DataColumn("constraint_catalog"),
new DataColumn("constraint_schema"),
new DataColumn("constraint_name"),
new DataColumn("table_catalog"),
new DataColumn("table_schema"),
new DataColumn("table_name"),
new DataColumn("column_name"),
new DataColumn("ordinal_number", typeof(int)),
new DataColumn("constraint_type")
}
};
return ParseResults(
async,
BuildCommand(conn, sql, restrictions, false, "current_database()", "n.nspname", "t.relname", "c.conname", "a.attname"),
dataTable,
(reader, row) =>
{
row["constraint_catalog"] = GetFieldValueOrDBNull<string>(reader, 0);
row["constraint_schema"] = GetFieldValueOrDBNull<string>(reader, 1);
row["constraint_name"] = GetFieldValueOrDBNull<string>(reader, 2);
row["table_catalog"] = GetFieldValueOrDBNull<string>(reader, 3);
row["table_schema"] = GetFieldValueOrDBNull<string>(reader, 4);
row["table_name"] = GetFieldValueOrDBNull<string>(reader, 5);
row["column_name"] = GetFieldValueOrDBNull<string>(reader, 6);
row["ordinal_number"] = GetFieldValueOrDBNull<int>(reader, 7);
row["constraint_type"] = GetFieldValueOrDBNull<string>(reader, 8);
}, cancellationToken);
}
static DataTable GetDataSourceInformation(NpgsqlConnection conn)
{
var table = new DataTable("DataSourceInformation");
var row = table.Rows.Add();
table.Columns.Add("CompositeIdentifierSeparatorPattern", typeof(string));
// TODO: DefaultCatalog? Was in XML (unfilled) but isn't in docs
table.Columns.Add("DataSourceProductName", typeof(string));
table.Columns.Add("DataSourceProductVersion", typeof(string));
table.Columns.Add("DataSourceProductVersionNormalized", typeof(string));
table.Columns.Add("GroupByBehavior", typeof(GroupByBehavior));
table.Columns.Add("IdentifierPattern", typeof(string));
table.Columns.Add("IdentifierCase", typeof(IdentifierCase));
table.Columns.Add("OrderByColumnsInSelect", typeof(bool));
table.Columns.Add("ParameterMarkerFormat", typeof(string));
table.Columns.Add("ParameterMarkerPattern", typeof(string));
table.Columns.Add("ParameterNameMaxLength", typeof(int));
table.Columns.Add("QuotedIdentifierPattern", typeof(string));
table.Columns.Add("QuotedIdentifierCase", typeof(IdentifierCase));
table.Columns.Add("ParameterNamePattern", typeof(string));
table.Columns.Add("StatementSeparatorPattern", typeof(string));
table.Columns.Add("StringLiteralPattern", typeof(string));
table.Columns.Add("SupportedJoinOperators", typeof(SupportedJoinOperators));
var version = conn.PostgreSqlVersion;
var normalizedVersion = $"{version.Major:00}.{version.Minor:00}";
if (version.Build >= 0)
normalizedVersion += $".{version.Build:00}";
row["CompositeIdentifierSeparatorPattern"] = @"\.";
row["DataSourceProductName"] = "Npgsql";
row["DataSourceProductVersion"] = version.ToString();
row["DataSourceProductVersionNormalized"] = normalizedVersion;
row["GroupByBehavior"] = GroupByBehavior.Unrelated;
row["IdentifierPattern"] = @"(^\[\p{Lo}\p{Lu}\p{Ll}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Nd}@$#_]*$)|(^\[[^\]\0]|\]\]+\]$)|(^\""[^\""\0]|\""\""+\""$)";
row["IdentifierCase"] = IdentifierCase.Insensitive;
row["OrderByColumnsInSelect"] = false;
row["QuotedIdentifierPattern"] = @"""(([^\""]|\""\"")*)""";
row["QuotedIdentifierCase"] = IdentifierCase.Sensitive;
row["StatementSeparatorPattern"] = ";";
row["StringLiteralPattern"] = @"'(([^']|'')*)'";
row["SupportedJoinOperators"] =
SupportedJoinOperators.FullOuter |
SupportedJoinOperators.Inner |
SupportedJoinOperators.LeftOuter |
SupportedJoinOperators.RightOuter;
row["ParameterNameMaxLength"] = 63; // For function out parameters
row["ParameterMarkerFormat"] = @"{0}"; // TODO: Not sure
if (NpgsqlCommand.EnableSqlRewriting)
{
row["ParameterMarkerPattern"] = @"@[\p{Lo}\p{Lu}\p{Ll}\p{Lm}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Lm}\p{Nd}\uff3f_@#\$]*(?=\s+|$)";
row["ParameterNamePattern"] = @"^[\p{Lo}\p{Lu}\p{Ll}\p{Lm}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Lm}\p{Nd}\uff3f_@#\$]*(?=\s+|$)";
}
else
{
row["ParameterMarkerPattern"] = @"$\d+";
row["ParameterNamePattern"] = @"\d+";
}
return table;
}
#region DataTypes
static DataTable GetDataTypes(NpgsqlConnection conn)
{
conn.CheckReady();
var connector = conn.Connector!;
var table = new DataTable("DataTypes");
table.Columns.Add("TypeName", typeof(string));
table.Columns.Add("ColumnSize", typeof(long));
table.Columns.Add("CreateFormat", typeof(string));
table.Columns.Add("CreateParameters", typeof(string));
table.Columns.Add("DataType", typeof(string));
table.Columns.Add("IsAutoIncrementable", typeof(bool));
table.Columns.Add("IsBestMatch", typeof(bool));
table.Columns.Add("IsCaseSensitive", typeof(bool));
table.Columns.Add("IsConcurrencyType", typeof(bool));
table.Columns.Add("IsFixedLength", typeof(bool));
table.Columns.Add("IsFixedPrecisionAndScale", typeof(bool));
table.Columns.Add("IsLiteralSupported", typeof(bool));
table.Columns.Add("IsLong", typeof(bool));
table.Columns.Add("IsNullable", typeof(bool));
table.Columns.Add("IsSearchable", typeof(bool));
table.Columns.Add("IsSearchableWithLike", typeof(bool));
table.Columns.Add("IsUnsigned", typeof(bool));
table.Columns.Add("LiteralPrefix", typeof(string));
table.Columns.Add("LiteralSuffix", typeof(string));
table.Columns.Add("MaximumScale", typeof(short));
table.Columns.Add("MinimumScale", typeof(short));
table.Columns.Add("NativeDataType", typeof(string));
table.Columns.Add("ProviderDbType", typeof(int));
// Npgsql-specific
table.Columns.Add("OID", typeof(uint));
// TODO: Support type name restriction
try
{
var serializerOptions = connector.SerializerOptions;
PgSerializerOptions.IntrospectionCaller = true;
var types = new List<PostgresType>();
types.AddRange(connector.DatabaseInfo.BaseTypes);
types.AddRange(connector.DatabaseInfo.EnumTypes);
types.AddRange(connector.DatabaseInfo.CompositeTypes);
foreach (var baseType in types)
{
if (serializerOptions.GetTypeInfoInternal(null, serializerOptions.ToCanonicalTypeId(baseType)) is not { } info)
continue;
var row = table.Rows.Add();
PopulateDefaultDataTypeInfo(row, baseType);
PopulateHardcodedDataTypeInfo(row, baseType);
row["DataType"] = info.Type.FullName;
if (baseType.DataTypeName.ToNpgsqlDbType() is { } npgsqlDbType)
row["ProviderDbType"] = (int)npgsqlDbType;
}
foreach (var arrayType in connector.DatabaseInfo.ArrayTypes)
{
if (serializerOptions.GetTypeInfoInternal(null, serializerOptions.ToCanonicalTypeId(arrayType)) is not { } info)
continue;
var row = table.Rows.Add();
PopulateDefaultDataTypeInfo(row, arrayType.Element);
// Populate hardcoded values based on the element type (e.g. citext[] is case-insensitive).
PopulateHardcodedDataTypeInfo(row, arrayType.Element);
row["TypeName"] = arrayType.DisplayName;
row["OID"] = arrayType.OID;
row["CreateFormat"] += "[]";
row["DataType"] = info.Type.FullName;
if (arrayType.DataTypeName.ToNpgsqlDbType() is { } npgsqlDbType)
row["ProviderDbType"] = (int)npgsqlDbType;
}
foreach (var rangeType in connector.DatabaseInfo.RangeTypes)
{
if (serializerOptions.GetTypeInfoInternal(null, serializerOptions.ToCanonicalTypeId(rangeType)) is not { } info)
continue;
var row = table.Rows.Add();
PopulateDefaultDataTypeInfo(row, rangeType.Subtype);
// Populate hardcoded values based on the subtype type (e.g. citext[] is case-insensitive).
PopulateHardcodedDataTypeInfo(row, rangeType.Subtype);
row["TypeName"] = rangeType.DisplayName;
row["OID"] = rangeType.OID;
row["CreateFormat"] = rangeType.DisplayName.ToUpperInvariant();
row["DataType"] = info.Type.FullName;
if (rangeType.DataTypeName.ToNpgsqlDbType() is { } npgsqlDbType)
row["ProviderDbType"] = (int)npgsqlDbType;
}
foreach (var multirangeType in connector.DatabaseInfo.MultirangeTypes)
{
var subtypeType = multirangeType.Subrange.Subtype;
if (serializerOptions.GetTypeInfoInternal(null, serializerOptions.ToCanonicalTypeId(multirangeType)) is not { } info)
continue;
var row = table.Rows.Add();
PopulateDefaultDataTypeInfo(row, subtypeType);
// Populate hardcoded values based on the subtype type (e.g. citext[] is case-insensitive).
PopulateHardcodedDataTypeInfo(row, subtypeType);
row["TypeName"] = multirangeType.DisplayName;
row["OID"] = multirangeType.OID;
row["CreateFormat"] = multirangeType.DisplayName.ToUpperInvariant();
row["DataType"] = info.Type.FullName;
if (multirangeType.DataTypeName.ToNpgsqlDbType() is { } npgsqlDbType)
row["ProviderDbType"] = (int)npgsqlDbType;
}
foreach (var domainType in connector.DatabaseInfo.DomainTypes)
{
var representationalType = domainType.GetRepresentationalType();
if (serializerOptions.GetTypeInfoInternal(null, serializerOptions.ToCanonicalTypeId(representationalType)) is not { } info)
continue;
var row = table.Rows.Add();
PopulateDefaultDataTypeInfo(row, representationalType);
// Populate hardcoded values based on the element type (e.g. citext[] is case-insensitive).
PopulateHardcodedDataTypeInfo(row, representationalType);
row["TypeName"] = domainType.DisplayName;
row["OID"] = domainType.OID;
// A domain is never the best match, since its underlying base type is
row["IsBestMatch"] = false;
row["DataType"] = info.Type.FullName;
if (representationalType.DataTypeName.ToNpgsqlDbType() is { } npgsqlDbType)
row["ProviderDbType"] = (int)npgsqlDbType;
}
}
finally
{
PgSerializerOptions.IntrospectionCaller = false;
}
return table;
}
/// <summary>
/// Populates some generic type information that is common for base types, arrays, enums, etc. Some will
/// be overridden later.
/// </summary>
static void PopulateDefaultDataTypeInfo(DataRow row, PostgresType type)
{
row["TypeName"] = type.DisplayName;
// Skipping ColumnSize at least for now, not very meaningful
row["CreateFormat"] = type.DisplayName.ToUpperInvariant();
row["CreateParameters"] = "";
row["IsAutoIncrementable"] = false;
// We populate the DataType above from mapping.ClrTypes, which means we take the .NET type from
// which we *infer* the PostgreSQL type. Since only a single PostgreSQL type gets inferred from a given
// .NET type, we never have the same DataType in more than one row - so the mapping is always the
// best match. See the hardcoding override below for some exceptions.
row["IsBestMatch"] = true;
row["IsCaseSensitive"] = true;
row["IsConcurrencyType"] = false;
row["IsFixedLength"] = false;
row["IsFixedPrecisionAndScale"] = false;
row["IsLiteralSupported"] = false; // See hardcoding override below
row["IsLong"] = false;
row["IsNullable"] = true;
row["IsSearchable"] = true;
row["IsSearchableWithLike"] = false;
row["IsUnsigned"] = DBNull.Value; // See hardcoding override below
// LiteralPrefix/Suffix: no literal for now except for strings, see hardcoding override below
row["MaximumScale"] = DBNull.Value;
row["MinimumScale"] = DBNull.Value;
// NativeDataType is unset
row["OID"] = type.OID;
}
/// <summary>
/// Sets some custom, hardcoded info on a DataType row that cannot be loaded/inferred from PostgreSQL
/// </summary>
static void PopulateHardcodedDataTypeInfo(DataRow row, PostgresType type)
{
switch (type.Name)
{
case "varchar":
case "char":
row["DataType"] = "String";
row["IsBestMatch"] = false;
goto case "text";
case "text":
row["CreateFormat"] += "({0})";
row["CreateParameters"] = "size";
row["IsSearchableWithLike"] = true;
row["IsLiteralSupported"] = true;
row["LiteralPrefix"] = "'";
row["LiteralSuffix"] = "'";
return;
case "numeric":
row["CreateFormat"] += "({0},{1})";
row["CreateParameters"] = "precision, scale";
row["MaximumScale"] = 16383;
row["MinimumScale"] = 16383;
row["IsUnsigned"] = false;
return;
case "bytea":
row["IsLong"] = true;
return;
case "citext":
row["IsCaseSensitive"] = false;
return;
case "integer":
case "smallint":
case "bigint":
case "double precision":
case "real":
case "money":
row["IsUnsigned"] = false;
return;
case "oid":
case "cid":
case "regtype":
case "regconfig":
row["IsUnsigned"] = true;
return;
case "xid":
row["IsUnsigned"] = true;
row["IsConcurrencyType"] = true;
return;
}
}
#endregion DataTypes
#region Reserved Keywords
static DataTable GetReservedWords()
{
var table = new DataTable("ReservedWords") { Locale = CultureInfo.InvariantCulture };
table.Columns.Add("ReservedWord", typeof(string));
foreach (var keyword in ReservedKeywords)