forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPostgresMinimalDatabaseInfo.cs
More file actions
87 lines (74 loc) · 3.07 KB
/
PostgresMinimalDatabaseInfo.cs
File metadata and controls
87 lines (74 loc) · 3.07 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
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Npgsql.Internal;
using Npgsql.PostgresTypes;
using Npgsql.Util;
using NpgsqlTypes;
namespace Npgsql;
sealed class PostgresMinimalDatabaseInfoFactory : INpgsqlDatabaseInfoFactory
{
public Task<NpgsqlDatabaseInfo?> Load(NpgsqlConnector conn, NpgsqlTimeout timeout, bool async)
=> Task.FromResult(
conn.Settings.ServerCompatibilityMode == ServerCompatibilityMode.NoTypeLoading
? (NpgsqlDatabaseInfo)new PostgresMinimalDatabaseInfo(conn)
: null);
}
sealed class PostgresMinimalDatabaseInfo : PostgresDatabaseInfo
{
static PostgresType[]? _typesWithMultiranges, _typesWithoutMultiranges;
static PostgresType[] CreateTypes(bool withMultiranges)
{
var builtinTypes = new List<BuiltInPostgresType>();
foreach (var field in typeof(NpgsqlDbType).GetFields())
if (field.GetCustomAttribute<BuiltInPostgresType>() is { } attr)
builtinTypes.Add(attr);
var pgTypes = new List<PostgresType>();
foreach (var attr in builtinTypes)
{
var baseType = new PostgresBaseType("pg_catalog", attr.Name, attr.BaseOID);
var arrayType = new PostgresArrayType("pg_catalog", "_" + attr.Name, attr.ArrayOID, baseType);
if (attr.RangeName is null)
pgTypes.AddRange(new PostgresType[] { baseType, arrayType });
else
{
var rangeType = new PostgresRangeType("pg_catalog", attr.RangeName, attr.RangeOID, baseType);
pgTypes.AddRange(withMultiranges
? new PostgresType[]
{
baseType, arrayType, rangeType,
new PostgresMultirangeType("pg_catalog", attr.MultirangeName!, attr.MultirangeOID, rangeType)
}
: new PostgresType[] { baseType, arrayType, rangeType });
}
}
return pgTypes.ToArray();
}
protected override IEnumerable<PostgresType> GetTypes()
=> SupportsMultirangeTypes
? _typesWithMultiranges ??= CreateTypes(withMultiranges: true)
: _typesWithoutMultiranges ??= CreateTypes(withMultiranges: false);
internal PostgresMinimalDatabaseInfo(NpgsqlConnector conn)
: base(conn)
{
HasIntegerDateTimes = !conn.PostgresParameters.TryGetValue("integer_datetimes", out var intDateTimes) ||
intDateTimes == "on";
}
// TODO, split database info and type catalog.
internal PostgresMinimalDatabaseInfo()
: base("minimal", 5432, "minimal", "14")
{
}
static PostgresMinimalDatabaseInfo? _defaultTypeCatalog;
internal static PostgresMinimalDatabaseInfo DefaultTypeCatalog
{
get
{
if (_defaultTypeCatalog is not null)
return _defaultTypeCatalog;
var catalog = new PostgresMinimalDatabaseInfo();
catalog.ProcessTypes();
return _defaultTypeCatalog = catalog;
}
}
}