-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.type_map
162 lines (149 loc) · 3.9 KB
/
README.type_map
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
CREATE TABLE dbi_link.type_map (
pg_type TEXT NOT NULL DEFAULT 'TEXT',
remote_type TEXT,
remote_to_pg TEXT,
pg_to_remote TEXT,
remote_driver TEXT,
data_source_id INTEGER
REFERENCES dbi_link.dbi_connection(data_source_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
remote_table TEXT,
remote_column TEXT,
CHECK (
COALESCE(
remote_driver,
data_source_id,
remote_table,
remote_column
) IS NULL
OR
(
remote_driver IS NOT NULL
AND
data_source_id IS NULL
AND
remote_table IS NULL
AND
remote_column IS NULL
)
OR
(
remote_driver IS NULL
AND
data_source_id IS NOT NULL
AND
remote_table IS NULL
AND
remote_column IS NULL
)
OR
(
remote_driver IS NULL
AND
data_source_id IS NOT NULL
AND
remote_table IS NOT NULL
AND
remote_column IS NULL
)
OR
(
remote_driver IS NULL
AND
data_source_id IS NOT NULL
AND
remote_table IS NOT NULL
AND
remote_column IS NOT NULL
)
)
);
COMMENT ON TABLE dbi_link.type_map IS $$
This is far and away the most byzantine table in the system, but at
that, I have not yet figured out how to make it simpler. Its job is
to drive all the pg <-> remote type mappings.
For each type on the remote side, have a Postgres type to which it
maps. Rules about mapping are embodied in the table as follows:
1. If a per-column mapping exists, use it.
2. If a per-column mapping does not exist, but a per-table mapping
does, use it.
3. If neither a per-column nor a per-table mapping exists and a
per-connection mapping does, use it.
4. If none of the above exist, use a per-driver (Oracle, e.g.)
default mapping. The driver column is not NULL only for the default
type. This row will always exist, although in the case of typeless
systems (CSV, Excel, SQLite, etc.), it will be set to TEXT.
5. For a completely unknown system, default to TEXT for all columns.
$$;
INSERT INTO dbi_link.type_map (
pg_type,
remote_type,
remote_to_pg,
pg_to_remote,
remote_driver,
data_source_id,
remote_table,
remote_column
)
VALUES (
DEFAULT,
DEFAULT,
DEFAULT,
DEFAULT,
DEFAULT,
DEFAULT,
DEFAULT,
DEFAULT
);
CREATE UNIQUE INDEX only_one_default_default
ON dbi_link.type_map(pg_type)
WHERE
COALESCE(
remote_driver,
data_source_id,
remote_table,
remote_column
) IS NULL;
CREATE UNIQUE INDEX only_one_default_remote_null_type
ON dbi_link.type_map(pg_type, remote_driver)
WHERE
remote_type IS NULL
AND
remote_driver IS NOT NULL;
CREATE UNIQUE INDEX only_one_data_source_remote_null_type
ON dbi_link.type_map(pg_type, data_source_id)
WHERE
remote_type IS NULL
AND
remote_driver IS NULL
AND
data_source_id IS NOT NULL
AND
remote_table IS NULL
AND
remote_column IS NULL;
CREATE UNIQUE INDEX only_one_per_table_remote_null_type
ON dbi_link.type_map(pg_type, data_source_id, remote_table)
WHERE
remote_type IS NULL
AND
remote_driver IS NULL
AND
data_source_id IS NOT NULL
AND
remote_table IS NOT NULL
AND
remote_column IS NULL;
CREATE UNIQUE INDEX only_one_per_column_remote_null_type
ON dbi_link.type_map(pg_type, data_source_id, remote_table, remote_column)
WHERE
remote_type IS NULL
AND
remote_driver IS NULL
AND
data_source_id IS NOT NULL
AND
remote_table IS NOT NULL
AND
remote_column IS NOT NULL;