forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresBaseType.cs
More file actions
87 lines (82 loc) · 3.56 KB
/
PostgresBaseType.cs
File metadata and controls
87 lines (82 loc) · 3.56 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
namespace Npgsql.PostgresTypes
{
/// <summary>
/// Represents a PostgreSQL base data type, which is a simple scalar value.
/// </summary>
public class PostgresBaseType : PostgresType
{
/// <inheritdoc/>
protected internal PostgresBaseType(string ns, string internalName, uint oid)
: base(ns, TranslateInternalName(internalName), internalName, oid)
{}
/// <inheritdoc/>
internal override string GetPartialNameWithFacets(int typeModifier)
{
var facets = GetFacets(typeModifier);
if (facets == PostgresFacets.None)
return Name;
return Name switch
{
// Special case for timestamptz and timetz where the facet is embedded in the middle
"timestamp with time zone" => $"timestamp{facets} with time zone",
"time with time zone" => $"time{facets} with time zone",
_ => $"{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 http://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":
case "time":
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;
}
}
// The type names returned by PostgreSQL are internal names (int4 instead of
// integer). We perform translation to the user-facing standard names.
// https://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-TABLE
static string TranslateInternalName(string internalName)
=> internalName switch
{
"bool" => "boolean",
"bpchar" => "character",
"decimal" => "numeric",
"float4" => "real",
"float8" => "double precision",
"int2" => "smallint",
"int4" => "integer",
"int8" => "bigint",
"timetz" => "time with time zone",
"timestamptz" => "timestamp with time zone",
"varbit" => "bit varying",
"varchar" => "character varying",
_ => internalName
};
}
}