-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathPostgresBaseType.cs
More file actions
80 lines (71 loc) · 2.97 KB
/
PostgresBaseType.cs
File metadata and controls
80 lines (71 loc) · 2.97 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
using Npgsql.Internal.Postgres;
namespace Npgsql.PostgresTypes;
/// <summary>
/// Represents a PostgreSQL base data type, which is a simple scalar value.
/// </summary>
public class PostgresBaseType : PostgresType
{
/// <summary>
/// Constructs a representation of a PostgreSQL base data type.
/// </summary>
protected internal PostgresBaseType(string ns, string name, uint oid)
: base(ns, name, oid) {}
/// <summary>
/// Constructs a representation of a PostgreSQL base data type.
/// </summary>
internal PostgresBaseType(DataTypeName dataTypeName, Oid oid)
: base(dataTypeName, oid) {}
/// <inheritdoc/>
internal override string GetPartialNameWithFacets(int typeModifier)
{
var facets = GetFacets(typeModifier);
if (facets == PostgresFacets.None)
return Name;
return Name switch
{
// Special case for time, timestamp, timestamptz and timetz where the facet is embedded in the middle
"timestamp without time zone" => $"timestamp{facets} without time zone",
"time without time zone" => $"time{facets} without time zone",
"timestamp with time zone" => $"timestamp{facets} with time zone",
"time with time zone" => $"time{facets} with time zone",
// We normalize character(1) to character - they mean the same
"character" when facets.Size == 1 => "character",
_ => $"{Name}{facets}"
};
}
internal override PostgresFacets GetFacets(int typeModifier)
{
if (typeModifier == -1)
return PostgresFacets.None;
switch (Name)
{
case "character":
return new PostgresFacets(typeModifier - 4, null, null);
case "character varying":
return new PostgresFacets(typeModifier - 4, null, null); // Max length
case "numeric":
case "decimal":
// See https://stackoverflow.com/questions/3350148/where-are-numeric-precision-and-scale-for-a-field-found-in-the-pg-catalog-tables
var precision = ((typeModifier - 4) >> 16) & 65535;
var scale = (typeModifier - 4) & 65535;
return new PostgresFacets(null, precision, scale == 0 ? (int?)null : scale);
case "timestamp without time zone":
case "time without time zone":
case "interval":
precision = typeModifier & 0xFFFF;
return new PostgresFacets(null, precision, null);
case "timestamp with time zone":
precision = typeModifier & 0xFFFF;
return new PostgresFacets(null, precision, null);
case "time with time zone":
precision = typeModifier & 0xFFFF;
return new PostgresFacets(null, precision, null);
case "bit":
case "bit varying":
return new PostgresFacets(typeModifier, null, null);
default:
return PostgresFacets.None;
}
}
}