-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathNpgsqlDbType.cs
More file actions
988 lines (816 loc) · 36.2 KB
/
NpgsqlDbType.cs
File metadata and controls
988 lines (816 loc) · 36.2 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
using System;
using System.Data;
using Npgsql;
using Npgsql.Internal.Postgres;
using static Npgsql.Util.Statics;
#pragma warning disable CA1720
// ReSharper disable once CheckNamespace
namespace NpgsqlTypes;
/// <summary>
/// Represents a PostgreSQL data type that can be written or read to the database.
/// Used in places such as <see cref="NpgsqlParameter.NpgsqlDbType"/> to unambiguously specify
/// how to encode or decode values.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/static/datatype.html.
/// </remarks>
// Source for PG OIDs: <see href="https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat" />
public enum NpgsqlDbType
{
// Note that it's important to never change the numeric values of this enum, since user applications
// compile them in.
#region Numeric Types
/// <summary>
/// Corresponds to the PostgreSQL 8-byte "bigint" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-numeric.html</remarks>
Bigint = 1,
/// <summary>
/// Corresponds to the PostgreSQL 8-byte floating-point "double" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-numeric.html</remarks>
Double = 8,
/// <summary>
/// Corresponds to the PostgreSQL 4-byte "integer" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-numeric.html</remarks>
Integer = 9,
/// <summary>
/// Corresponds to the PostgreSQL arbitrary-precision "numeric" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-numeric.html</remarks>
Numeric = 13,
/// <summary>
/// Corresponds to the PostgreSQL floating-point "real" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-numeric.html</remarks>
Real = 17,
/// <summary>
/// Corresponds to the PostgreSQL 2-byte "smallint" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-numeric.html</remarks>
Smallint = 18,
/// <summary>
/// Corresponds to the PostgreSQL "money" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-money.html</remarks>
Money = 12,
#endregion
#region Boolean Type
/// <summary>
/// Corresponds to the PostgreSQL "boolean" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-boolean.html</remarks>
Boolean = 2,
#endregion
#region Geometric types
/// <summary>
/// Corresponds to the PostgreSQL geometric "box" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-geometric.html</remarks>
Box = 3,
/// <summary>
/// Corresponds to the PostgreSQL geometric "circle" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-geometric.html</remarks>
Circle = 5,
/// <summary>
/// Corresponds to the PostgreSQL geometric "line" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-geometric.html</remarks>
Line = 10,
/// <summary>
/// Corresponds to the PostgreSQL geometric "lseg" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-geometric.html</remarks>
LSeg = 11,
/// <summary>
/// Corresponds to the PostgreSQL geometric "path" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-geometric.html</remarks>
Path = 14,
/// <summary>
/// Corresponds to the PostgreSQL geometric "point" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-geometric.html</remarks>
Point = 15,
/// <summary>
/// Corresponds to the PostgreSQL geometric "polygon" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-geometric.html</remarks>
Polygon = 16,
/// <summary>
/// Corresponds to the PostgreSQL "cube" type, a geometric type representing multi-dimensional cubes.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/cube.html</remarks>
Cube = 63, // Extension type
#endregion
#region Character Types
/// <summary>
/// Corresponds to the PostgreSQL "char(n)" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-character.html</remarks>
Char = 6,
/// <summary>
/// Corresponds to the PostgreSQL "text" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-character.html</remarks>
Text = 19,
/// <summary>
/// Corresponds to the PostgreSQL "varchar" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-character.html</remarks>
Varchar = 22,
/// <summary>
/// Corresponds to the PostgreSQL internal "name" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-character.html</remarks>
Name = 32,
/// <summary>
/// Corresponds to the PostgreSQL "citext" type for the citext module.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/citext.html</remarks>
Citext = 51, // Extension type
/// <summary>
/// Corresponds to the PostgreSQL "char" type.
/// </summary>
/// <remarks>
/// This is an internal field and should normally not be used for regular applications.
///
/// See https://www.postgresql.org/docs/current/static/datatype-text.html
/// </remarks>
InternalChar = 38,
#endregion
#region Binary Data Types
/// <summary>
/// Corresponds to the PostgreSQL "bytea" type, holding a raw byte string.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-binary.html</remarks>
Bytea = 4,
#endregion
#region Date/Time Types
/// <summary>
/// Corresponds to the PostgreSQL "date" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-datetime.html</remarks>
Date = 7,
/// <summary>
/// Corresponds to the PostgreSQL "time" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-datetime.html</remarks>
Time = 20,
/// <summary>
/// Corresponds to the PostgreSQL "timestamp" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-datetime.html</remarks>
Timestamp = 21,
/// <summary>
/// Corresponds to the PostgreSQL "timestamp with time zone" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-datetime.html</remarks>
TimestampTz = 26,
/// <summary>
/// Corresponds to the PostgreSQL "interval" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-datetime.html</remarks>
Interval = 30,
/// <summary>
/// Corresponds to the PostgreSQL "time with time zone" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-datetime.html</remarks>
TimeTz = 31,
/// <summary>
/// Corresponds to the obsolete PostgreSQL "abstime" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-datetime.html</remarks>
[Obsolete("The PostgreSQL abstime time is obsolete.")]
Abstime = 33,
#endregion
#region Network Address Types
/// <summary>
/// Corresponds to the PostgreSQL "inet" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-net-types.html</remarks>
Inet = 24,
/// <summary>
/// Corresponds to the PostgreSQL "cidr" type, a field storing an IPv4 or IPv6 network.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-net-types.html</remarks>
Cidr = 44,
/// <summary>
/// Corresponds to the PostgreSQL "macaddr" type, a field storing a 6-byte physical address.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-net-types.html</remarks>
MacAddr = 34,
/// <summary>
/// Corresponds to the PostgreSQL "macaddr8" type, a field storing a 6-byte or 8-byte physical address.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-net-types.html</remarks>
MacAddr8 = 54,
#endregion
#region Bit String Types
/// <summary>
/// Corresponds to the PostgreSQL "bit" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-bit.html</remarks>
Bit = 25,
/// <summary>
/// Corresponds to the PostgreSQL "varbit" type, a field storing a variable-length string of bits.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-boolean.html</remarks>
Varbit = 39,
#endregion
#region Text Search Types
/// <summary>
/// Corresponds to the PostgreSQL "tsvector" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-textsearch.html</remarks>
TsVector = 45,
/// <summary>
/// Corresponds to the PostgreSQL "tsquery" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-textsearch.html</remarks>
TsQuery = 46,
/// <summary>
/// Corresponds to the PostgreSQL "regconfig" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-textsearch.html</remarks>
Regconfig = 56,
#endregion
#region UUID Type
/// <summary>
/// Corresponds to the PostgreSQL "uuid" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-uuid.html</remarks>
Uuid = 27,
#endregion
#region XML Type
/// <summary>
/// Corresponds to the PostgreSQL "xml" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-xml.html</remarks>
Xml = 28,
#endregion
#region JSON Types
/// <summary>
/// Corresponds to the PostgreSQL "json" type, a field storing JSON in text format.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-json.html</remarks>
/// <seealso cref="Jsonb"/>
Json = 35,
/// <summary>
/// Corresponds to the PostgreSQL "jsonb" type, a field storing JSON in an optimized binary.
/// format.
/// </summary>
/// <remarks>
/// Supported since PostgreSQL 9.4.
/// See https://www.postgresql.org/docs/current/static/datatype-json.html
/// </remarks>
Jsonb = 36,
/// <summary>
/// Corresponds to the PostgreSQL "jsonpath" type, a field storing JSON path in text format.
/// format.
/// </summary>
/// <remarks>
/// Supported since PostgreSQL 12.
/// See https://www.postgresql.org/docs/current/datatype-json.html#DATATYPE-JSONPATH
/// </remarks>
JsonPath = 57,
#endregion
#region HSTORE Type
/// <summary>
/// Corresponds to the PostgreSQL "hstore" type, a dictionary of string key-value pairs.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/hstore.html</remarks>
Hstore = 37, // Extension type
#endregion
#region Internal Types
/// <summary>
/// Corresponds to the PostgreSQL "refcursor" type.
/// </summary>
Refcursor = 23,
/// <summary>
/// Corresponds to the PostgreSQL internal "oidvector" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-oid.html</remarks>
Oidvector = 29,
/// <summary>
/// Corresponds to the PostgreSQL internal "int2vector" type.
/// </summary>
Int2Vector = 52,
/// <summary>
/// Corresponds to the PostgreSQL "oid" type.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-oid.html</remarks>
Oid = 41,
/// <summary>
/// Corresponds to the PostgreSQL "xid" type, an internal transaction identifier.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-oid.html</remarks>
Xid = 42,
/// <summary>
/// Corresponds to the PostgreSQL "xid8" type, an internal transaction identifier.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-oid.html</remarks>
Xid8 = 64,
/// <summary>
/// Corresponds to the PostgreSQL "cid" type, an internal command identifier.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/datatype-oid.html</remarks>
Cid = 43,
/// <summary>
/// Corresponds to the PostgreSQL "regtype" type, a numeric (OID) ID of a type in the pg_type table.
/// </summary>
Regtype = 49,
/// <summary>
/// Corresponds to the PostgreSQL "tid" type, a tuple id identifying the physical location of a row within its table.
/// </summary>
Tid = 53,
/// <summary>
/// Corresponds to the PostgreSQL "pg_lsn" type, which can be used to store LSN (Log Sequence Number) data which
/// is a pointer to a location in the WAL.
/// </summary>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/datatype-pg-lsn.html and
/// https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=7d03a83f4d0736ba869fa6f93973f7623a27038a
/// </remarks>
PgLsn = 59,
#endregion
#region Special
/// <summary>
/// A special value that can be used to send parameter values to the database without
/// specifying their type, allowing the database to cast them to another value based on context.
/// The value will be converted to a string and send as text.
/// </summary>
/// <remarks>
/// This value shouldn't ordinarily be used, and makes sense only when sending a data type
/// unsupported by Npgsql.
/// </remarks>
Unknown = 40,
#endregion
#region PostGIS
/// <summary>
/// The geometry type for PostgreSQL spatial extension PostGIS.
/// </summary>
Geometry = 50, // Extension type
/// <summary>
/// The geography (geodetic) type for PostgreSQL spatial extension PostGIS.
/// </summary>
Geography = 55, // Extension type
#endregion
#region Label tree types
/// <summary>
/// The PostgreSQL ltree type, each value is a label path "a.label.tree.value", forming a tree in a set.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/ltree.html</remarks>
LTree = 60, // Extension type
/// <summary>
/// The PostgreSQL lquery type for PostgreSQL extension ltree
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/ltree.html</remarks>
LQuery = 61, // Extension type
/// <summary>
/// The PostgreSQL ltxtquery type for PostgreSQL extension ltree
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/ltree.html</remarks>
LTxtQuery = 62, // Extension type
#endregion
#region Range types
/// <summary>
/// Corresponds to the PostgreSQL "int4range" type.
/// </summary>
IntegerRange = Range | Integer,
/// <summary>
/// Corresponds to the PostgreSQL "int8range" type.
/// </summary>
BigIntRange = Range | Bigint,
/// <summary>
/// Corresponds to the PostgreSQL "numrange" type.
/// </summary>
NumericRange = Range | Numeric,
/// <summary>
/// Corresponds to the PostgreSQL "tsrange" type.
/// </summary>
TimestampRange = Range | Timestamp,
/// <summary>
/// Corresponds to the PostgreSQL "tstzrange" type.
/// </summary>
TimestampTzRange = Range | TimestampTz,
/// <summary>
/// Corresponds to the PostgreSQL "daterange" type.
/// </summary>
DateRange = Range | Date,
#endregion Range types
#region Multirange types
/// <summary>
/// Corresponds to the PostgreSQL "int4multirange" type.
/// </summary>
IntegerMultirange = Multirange | Integer,
/// <summary>
/// Corresponds to the PostgreSQL "int8multirange" type.
/// </summary>
BigIntMultirange = Multirange | Bigint,
/// <summary>
/// Corresponds to the PostgreSQL "nummultirange" type.
/// </summary>
NumericMultirange = Multirange | Numeric,
/// <summary>
/// Corresponds to the PostgreSQL "tsmultirange" type.
/// </summary>
TimestampMultirange = Multirange | Timestamp,
/// <summary>
/// Corresponds to the PostgreSQL "tstzmultirange" type.
/// </summary>
TimestampTzMultirange = Multirange | TimestampTz,
/// <summary>
/// Corresponds to the PostgreSQL "datemultirange" type.
/// </summary>
DateMultirange = Multirange | Date,
#endregion Multirange types
#region Composables
/// <summary>
/// Corresponds to the PostgreSQL "array" type, a variable-length multidimensional array of
/// another type. This value must be combined with another value from <see cref="NpgsqlDbType"/>
/// via a bit OR (e.g. NpgsqlDbType.Array | NpgsqlDbType.Integer)
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/static/arrays.html</remarks>
Array = int.MinValue,
/// <summary>
/// Corresponds to the PostgreSQL "range" type, continuous range of values of specific type.
/// This value must be combined with another value from <see cref="NpgsqlDbType"/>
/// via a bit OR (e.g. NpgsqlDbType.Range | NpgsqlDbType.Integer)
/// </summary>
/// <remarks>
/// Supported since PostgreSQL 9.2.
/// See https://www.postgresql.org/docs/current/static/rangetypes.html
/// </remarks>
Range = 0x40000000,
/// <summary>
/// Corresponds to the PostgreSQL "multirange" type, continuous range of values of specific type.
/// This value must be combined with another value from <see cref="NpgsqlDbType"/>
/// via a bit OR (e.g. NpgsqlDbType.Multirange | NpgsqlDbType.Integer)
/// </summary>
/// <remarks>
/// Supported since PostgreSQL 14.
/// See https://www.postgresql.org/docs/current/static/rangetypes.html
/// </remarks>
Multirange = 0x20000000,
#endregion
}
static class NpgsqlDbTypeExtensions
{
internal static NpgsqlDbType? ToNpgsqlDbType(this DbType dbType)
=> dbType switch
{
DbType.AnsiString => NpgsqlDbType.Text,
DbType.Binary => NpgsqlDbType.Bytea,
DbType.Byte => NpgsqlDbType.Smallint,
DbType.Boolean => NpgsqlDbType.Boolean,
DbType.Currency => NpgsqlDbType.Money,
DbType.Date => NpgsqlDbType.Date,
DbType.DateTime => LegacyTimestampBehavior ? NpgsqlDbType.Timestamp : NpgsqlDbType.TimestampTz,
DbType.Decimal => NpgsqlDbType.Numeric,
DbType.VarNumeric => NpgsqlDbType.Numeric,
DbType.Double => NpgsqlDbType.Double,
DbType.Guid => NpgsqlDbType.Uuid,
DbType.Int16 => NpgsqlDbType.Smallint,
DbType.Int32 => NpgsqlDbType.Integer,
DbType.Int64 => NpgsqlDbType.Bigint,
DbType.Single => NpgsqlDbType.Real,
DbType.String => NpgsqlDbType.Text,
DbType.Time => NpgsqlDbType.Time,
DbType.AnsiStringFixedLength => NpgsqlDbType.Text,
DbType.StringFixedLength => NpgsqlDbType.Text,
DbType.Xml => NpgsqlDbType.Xml,
DbType.DateTime2 => NpgsqlDbType.Timestamp,
DbType.DateTimeOffset => NpgsqlDbType.TimestampTz,
DbType.Object => null,
DbType.SByte => null,
DbType.UInt16 => null,
DbType.UInt32 => null,
DbType.UInt64 => null,
_ => throw new ArgumentOutOfRangeException(nameof(dbType), dbType, null)
};
public static DbType ToDbType(this NpgsqlDbType npgsqlDbType)
=> npgsqlDbType switch
{
// Numeric types
NpgsqlDbType.Smallint => DbType.Int16,
NpgsqlDbType.Integer => DbType.Int32,
NpgsqlDbType.Bigint => DbType.Int64,
NpgsqlDbType.Real => DbType.Single,
NpgsqlDbType.Double => DbType.Double,
NpgsqlDbType.Numeric => DbType.Decimal,
NpgsqlDbType.Money => DbType.Currency,
// Text types
NpgsqlDbType.Text => DbType.String,
NpgsqlDbType.Xml => DbType.Xml,
NpgsqlDbType.Varchar => DbType.String,
NpgsqlDbType.Char => DbType.String,
NpgsqlDbType.Name => DbType.String,
NpgsqlDbType.Citext => DbType.String,
// Date/time types
NpgsqlDbType.Timestamp => LegacyTimestampBehavior ? DbType.DateTime : DbType.DateTime2,
NpgsqlDbType.TimestampTz => LegacyTimestampBehavior ? DbType.DateTimeOffset : DbType.DateTime,
NpgsqlDbType.Date => DbType.Date,
NpgsqlDbType.Time => DbType.Time,
// Misc data types
NpgsqlDbType.Bytea => DbType.Binary,
NpgsqlDbType.Boolean => DbType.Boolean,
NpgsqlDbType.Uuid => DbType.Guid,
_ => DbType.Object
};
/// Can return null when a custom range type is used.
internal static string? ToUnqualifiedDataTypeName(this NpgsqlDbType npgsqlDbType)
=> npgsqlDbType switch
{
// Numeric types
NpgsqlDbType.Smallint => "int2",
NpgsqlDbType.Integer => "int4",
NpgsqlDbType.Bigint => "int8",
NpgsqlDbType.Real => "float4",
NpgsqlDbType.Double => "float8",
NpgsqlDbType.Numeric => "numeric",
NpgsqlDbType.Money => "money",
// Text types
NpgsqlDbType.Text => "text",
NpgsqlDbType.Xml => "xml",
NpgsqlDbType.Varchar => "varchar",
NpgsqlDbType.Char => "bpchar",
NpgsqlDbType.Name => "name",
NpgsqlDbType.Refcursor => "refcursor",
NpgsqlDbType.Jsonb => "jsonb",
NpgsqlDbType.Json => "json",
NpgsqlDbType.JsonPath => "jsonpath",
// Date/time types
NpgsqlDbType.Timestamp => "timestamp",
NpgsqlDbType.TimestampTz => "timestamptz",
NpgsqlDbType.Date => "date",
NpgsqlDbType.Time => "time",
NpgsqlDbType.TimeTz => "timetz",
NpgsqlDbType.Interval => "interval",
// Network types
NpgsqlDbType.Cidr => "cidr",
NpgsqlDbType.Inet => "inet",
NpgsqlDbType.MacAddr => "macaddr",
NpgsqlDbType.MacAddr8 => "macaddr8",
// Full-text search types
NpgsqlDbType.TsQuery => "tsquery",
NpgsqlDbType.TsVector => "tsvector",
// Geometry types
NpgsqlDbType.Box => "box",
NpgsqlDbType.Circle => "circle",
NpgsqlDbType.Line => "line",
NpgsqlDbType.LSeg => "lseg",
NpgsqlDbType.Path => "path",
NpgsqlDbType.Point => "point",
NpgsqlDbType.Polygon => "polygon",
// UInt types
NpgsqlDbType.Oid => "oid",
NpgsqlDbType.Xid => "xid",
NpgsqlDbType.Xid8 => "xid8",
NpgsqlDbType.Cid => "cid",
NpgsqlDbType.Regtype => "regtype",
NpgsqlDbType.Regconfig => "regconfig",
// Misc types
NpgsqlDbType.Boolean => "bool",
NpgsqlDbType.Bytea => "bytea",
NpgsqlDbType.Uuid => "uuid",
NpgsqlDbType.Varbit => "varbit",
NpgsqlDbType.Bit => "bit",
// Built-in range types
NpgsqlDbType.IntegerRange => "int4range",
NpgsqlDbType.BigIntRange => "int8range",
NpgsqlDbType.NumericRange => "numrange",
NpgsqlDbType.TimestampRange => "tsrange",
NpgsqlDbType.TimestampTzRange => "tstzrange",
NpgsqlDbType.DateRange => "daterange",
// Built-in multirange types
NpgsqlDbType.IntegerMultirange => "int4multirange",
NpgsqlDbType.BigIntMultirange => "int8multirange",
NpgsqlDbType.NumericMultirange => "nummultirange",
NpgsqlDbType.TimestampMultirange => "tsmultirange",
NpgsqlDbType.TimestampTzMultirange => "tstzmultirange",
NpgsqlDbType.DateMultirange => "datemultirange",
// Internal types
NpgsqlDbType.Int2Vector => "int2vector",
NpgsqlDbType.Oidvector => "oidvector",
NpgsqlDbType.PgLsn => "pg_lsn",
NpgsqlDbType.Tid => "tid",
NpgsqlDbType.InternalChar => "char",
// Plugin types
NpgsqlDbType.Citext => "citext",
NpgsqlDbType.Cube => "cube",
NpgsqlDbType.LQuery => "lquery",
NpgsqlDbType.LTree => "ltree",
NpgsqlDbType.LTxtQuery => "ltxtquery",
NpgsqlDbType.Hstore => "hstore",
NpgsqlDbType.Geometry => "geometry",
NpgsqlDbType.Geography => "geography",
NpgsqlDbType.Unknown => "unknown",
// Unknown cannot be composed
_ when npgsqlDbType.HasFlag(NpgsqlDbType.Array) && (npgsqlDbType & ~NpgsqlDbType.Array) == NpgsqlDbType.Unknown
=> "unknown",
_ when npgsqlDbType.HasFlag(NpgsqlDbType.Range) && (npgsqlDbType & ~NpgsqlDbType.Range) == NpgsqlDbType.Unknown
=> "unknown",
_ when npgsqlDbType.HasFlag(NpgsqlDbType.Multirange) && (npgsqlDbType & ~NpgsqlDbType.Multirange) == NpgsqlDbType.Unknown
=> "unknown",
_ => npgsqlDbType.HasFlag(NpgsqlDbType.Array)
? ToUnqualifiedDataTypeName(npgsqlDbType & ~NpgsqlDbType.Array) is { } name ? "_" + name : null
: null // e.g. ranges
};
internal static string ToUnqualifiedDataTypeNameOrThrow(this NpgsqlDbType npgsqlDbType)
=> npgsqlDbType.ToUnqualifiedDataTypeName() ?? throw new ArgumentOutOfRangeException(nameof(npgsqlDbType), npgsqlDbType, "Cannot convert NpgsqlDbType to DataTypeName");
/// Can return null when a plugin type or custom range type is used.
internal static DataTypeName? ToDataTypeName(this NpgsqlDbType npgsqlDbType)
=> npgsqlDbType switch
{
// Numeric types
NpgsqlDbType.Smallint => DataTypeNames.Int2,
NpgsqlDbType.Integer => DataTypeNames.Int4,
NpgsqlDbType.Bigint => DataTypeNames.Int8,
NpgsqlDbType.Real => DataTypeNames.Float4,
NpgsqlDbType.Double => DataTypeNames.Float8,
NpgsqlDbType.Numeric => DataTypeNames.Numeric,
NpgsqlDbType.Money => DataTypeNames.Money,
// Text types
NpgsqlDbType.Text => DataTypeNames.Text,
NpgsqlDbType.Xml => DataTypeNames.Xml,
NpgsqlDbType.Varchar => DataTypeNames.Varchar,
NpgsqlDbType.Char => DataTypeNames.Bpchar,
NpgsqlDbType.Name => DataTypeNames.Name,
NpgsqlDbType.Refcursor => DataTypeNames.RefCursor,
NpgsqlDbType.Jsonb => DataTypeNames.Jsonb,
NpgsqlDbType.Json => DataTypeNames.Json,
NpgsqlDbType.JsonPath => DataTypeNames.Jsonpath,
// Date/time types
NpgsqlDbType.Timestamp => DataTypeNames.Timestamp,
NpgsqlDbType.TimestampTz => DataTypeNames.TimestampTz,
NpgsqlDbType.Date => DataTypeNames.Date,
NpgsqlDbType.Time => DataTypeNames.Time,
NpgsqlDbType.TimeTz => DataTypeNames.TimeTz,
NpgsqlDbType.Interval => DataTypeNames.Interval,
// Network types
NpgsqlDbType.Cidr => DataTypeNames.Cidr,
NpgsqlDbType.Inet => DataTypeNames.Inet,
NpgsqlDbType.MacAddr => DataTypeNames.MacAddr,
NpgsqlDbType.MacAddr8 => DataTypeNames.MacAddr8,
// Full-text search types
NpgsqlDbType.TsQuery => DataTypeNames.TsQuery,
NpgsqlDbType.TsVector => DataTypeNames.TsVector,
// Geometry types
NpgsqlDbType.Box => DataTypeNames.Box,
NpgsqlDbType.Circle => DataTypeNames.Circle,
NpgsqlDbType.Line => DataTypeNames.Line,
NpgsqlDbType.LSeg => DataTypeNames.LSeg,
NpgsqlDbType.Path => DataTypeNames.Path,
NpgsqlDbType.Point => DataTypeNames.Point,
NpgsqlDbType.Polygon => DataTypeNames.Polygon,
// UInt types
NpgsqlDbType.Oid => DataTypeNames.Oid,
NpgsqlDbType.Xid => DataTypeNames.Xid,
NpgsqlDbType.Xid8 => DataTypeNames.Xid8,
NpgsqlDbType.Cid => DataTypeNames.Cid,
NpgsqlDbType.Regtype => DataTypeNames.RegType,
NpgsqlDbType.Regconfig => DataTypeNames.RegConfig,
// Misc types
NpgsqlDbType.Boolean => DataTypeNames.Bool,
NpgsqlDbType.Bytea => DataTypeNames.Bytea,
NpgsqlDbType.Uuid => DataTypeNames.Uuid,
NpgsqlDbType.Varbit => DataTypeNames.Varbit,
NpgsqlDbType.Bit => DataTypeNames.Bit,
// Built-in range types
NpgsqlDbType.IntegerRange => DataTypeNames.Int4Range,
NpgsqlDbType.BigIntRange => DataTypeNames.Int8Range,
NpgsqlDbType.NumericRange => DataTypeNames.NumRange,
NpgsqlDbType.TimestampRange => DataTypeNames.TsRange,
NpgsqlDbType.TimestampTzRange => DataTypeNames.TsTzRange,
NpgsqlDbType.DateRange => DataTypeNames.DateRange,
// Internal types
NpgsqlDbType.Int2Vector => DataTypeNames.Int2Vector,
NpgsqlDbType.Oidvector => DataTypeNames.OidVector,
NpgsqlDbType.PgLsn => DataTypeNames.PgLsn,
NpgsqlDbType.Tid => DataTypeNames.Tid,
NpgsqlDbType.InternalChar => DataTypeNames.Char,
// Special types
NpgsqlDbType.Unknown => DataTypeNames.Unknown,
// Unknown cannot be composed
_ when npgsqlDbType.HasFlag(NpgsqlDbType.Array) && (npgsqlDbType & ~NpgsqlDbType.Array) == NpgsqlDbType.Unknown
=> DataTypeNames.Unknown,
_ when npgsqlDbType.HasFlag(NpgsqlDbType.Range) && (npgsqlDbType & ~NpgsqlDbType.Range) == NpgsqlDbType.Unknown
=> DataTypeNames.Unknown,
_ when npgsqlDbType.HasFlag(NpgsqlDbType.Multirange) && (npgsqlDbType & ~NpgsqlDbType.Multirange) == NpgsqlDbType.Unknown
=> DataTypeNames.Unknown,
// If both multirange and array are set we first remove array, so array is added to the outermost datatypename.
_ when npgsqlDbType.HasFlag(NpgsqlDbType.Array)
=> ToDataTypeName(npgsqlDbType & ~NpgsqlDbType.Array)?.ToArrayName(),
_ when npgsqlDbType.HasFlag(NpgsqlDbType.Multirange)
=> ToDataTypeName((npgsqlDbType | NpgsqlDbType.Range) & ~NpgsqlDbType.Multirange)?.ToDefaultMultirangeName(),
// Plugin types don't have a stable fully qualified name.
_ => null
};
internal static NpgsqlDbType? ToNpgsqlDbType(this DataTypeName dataTypeName) => ToNpgsqlDbType(dataTypeName.UnqualifiedName);
/// Should not be used with display names, first normalize it instead.
internal static NpgsqlDbType? ToNpgsqlDbType(string normalizedDataTypeName)
{
var unqualifiedName = normalizedDataTypeName.AsSpan();
if (unqualifiedName.IndexOf('.') is not -1 and var index)
unqualifiedName = unqualifiedName.Slice(index + 1);
return unqualifiedName switch
{
// Numeric types
"int2" => NpgsqlDbType.Smallint,
"int4" => NpgsqlDbType.Integer,
"int8" => NpgsqlDbType.Bigint,
"float4" => NpgsqlDbType.Real,
"float8" => NpgsqlDbType.Double,
"numeric" => NpgsqlDbType.Numeric,
"money" => NpgsqlDbType.Money,
// Text types
"text" => NpgsqlDbType.Text,
"xml" => NpgsqlDbType.Xml,
"varchar" => NpgsqlDbType.Varchar,
"bpchar" => NpgsqlDbType.Char,
"name" => NpgsqlDbType.Name,
"refcursor" => NpgsqlDbType.Refcursor,
"jsonb" => NpgsqlDbType.Jsonb,
"json" => NpgsqlDbType.Json,
"jsonpath" => NpgsqlDbType.JsonPath,
// Date/time types
"timestamp" => NpgsqlDbType.Timestamp,
"timestamptz" => NpgsqlDbType.TimestampTz,
"date" => NpgsqlDbType.Date,
"time" => NpgsqlDbType.Time,
"timetz" => NpgsqlDbType.TimeTz,
"interval" => NpgsqlDbType.Interval,
// Network types
"cidr" => NpgsqlDbType.Cidr,
"inet" => NpgsqlDbType.Inet,
"macaddr" => NpgsqlDbType.MacAddr,
"macaddr8" => NpgsqlDbType.MacAddr8,
// Full-text search types
"tsquery" => NpgsqlDbType.TsQuery,
"tsvector" => NpgsqlDbType.TsVector,
// Geometry types
"box" => NpgsqlDbType.Box,
"circle" => NpgsqlDbType.Circle,
"line" => NpgsqlDbType.Line,
"lseg" => NpgsqlDbType.LSeg,
"path" => NpgsqlDbType.Path,
"point" => NpgsqlDbType.Point,
"polygon" => NpgsqlDbType.Polygon,
// UInt types
"oid" => NpgsqlDbType.Oid,
"xid" => NpgsqlDbType.Xid,
"xid8" => NpgsqlDbType.Xid8,
"cid" => NpgsqlDbType.Cid,
"regtype" => NpgsqlDbType.Regtype,
"regconfig" => NpgsqlDbType.Regconfig,
// Misc types
"bool" => NpgsqlDbType.Boolean,
"bytea" => NpgsqlDbType.Bytea,
"uuid" => NpgsqlDbType.Uuid,
"varbit" => NpgsqlDbType.Varbit,
"bit" => NpgsqlDbType.Bit,
// Built-in range types
"int4range" => NpgsqlDbType.IntegerRange,
"int8range" => NpgsqlDbType.BigIntRange,
"numrange" => NpgsqlDbType.NumericRange,
"tsrange" => NpgsqlDbType.TimestampRange,
"tstzrange" => NpgsqlDbType.TimestampTzRange,
"daterange" => NpgsqlDbType.DateRange,
// Built-in multirange types
"int4multirange" => NpgsqlDbType.IntegerMultirange,
"int8multirange" => NpgsqlDbType.BigIntMultirange,
"nummultirange" => NpgsqlDbType.NumericMultirange,
"tsmultirange" => NpgsqlDbType.TimestampMultirange,
"tstzmultirange" => NpgsqlDbType.TimestampTzMultirange,
"datemultirange" => NpgsqlDbType.DateMultirange,
// Internal types
"int2vector" => NpgsqlDbType.Int2Vector,
"oidvector" => NpgsqlDbType.Oidvector,
"pg_lsn" => NpgsqlDbType.PgLsn,
"tid" => NpgsqlDbType.Tid,
"char" => NpgsqlDbType.InternalChar,
// Plugin types
"citext" => NpgsqlDbType.Citext,
"cube" => NpgsqlDbType.Cube,
"lquery" => NpgsqlDbType.LQuery,
"ltree" => NpgsqlDbType.LTree,
"ltxtquery" => NpgsqlDbType.LTxtQuery,
"hstore" => NpgsqlDbType.Hstore,
"geometry" => NpgsqlDbType.Geometry,
"geography" => NpgsqlDbType.Geography,
_ when unqualifiedName.IndexOf("unknown") != -1
=> !unqualifiedName.StartsWith("_", StringComparison.Ordinal)
? NpgsqlDbType.Unknown
: null,
_ when unqualifiedName.StartsWith("_", StringComparison.Ordinal)
=> ToNpgsqlDbType(unqualifiedName.Slice(1).ToString()) is { } elementNpgsqlDbType
? elementNpgsqlDbType | NpgsqlDbType.Array
: null,
// e.g. custom ranges, plugin types etc.
_ => null
};
}
}