-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathindex.ts
139 lines (121 loc) · 4.83 KB
/
index.ts
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
import { Types } from '../../datatypes';
import { DataType, FunctionDefinition, _IDb, _ISchema } from '../../interfaces-private';
import { PgAttributeTable } from './pg-attribute-list';
import { PgClassListTable } from './pg-class';
import { PgConstraintTable } from './pg-constraints-list';
import { PgEnumTable } from './pg-enum-list';
import { PgIndexTable } from './pg-index-list';
import { PgNamespaceTable } from './pg-namespace-list';
import { PgSequencesTable } from './pg-sequences-list';
import { PgTypeTable } from './pg-type-list';
import { PgUserTable } from './pg-user-list';
import { allFunctions } from '../../functions';
import { PgRange } from './pg-range';
import { sqlSubstring } from '../../parser/expression-builder';
import { PgDatabaseTable } from './pg-database';
import { registerCommonOperators } from './binary-operators';
import { registerSqlFunctionLanguage } from './sql-function-language';
import { PgProc } from './pg-proc';
import { PgStatioUserTables } from './pg_statio_user_tables';
export function setupPgCatalog(db: _IDb) {
const catalog: _ISchema = db.createSchema('pg_catalog');
catalog._registerType(Types.bool)
._registerType(Types.citext)
._registerTypeSizeable(DataType.timestamp, Types.timestamp)
._registerTypeSizeable(DataType.timestamptz, Types.timestamptz)
._registerType(Types.uuid)
._registerType(Types.date)
._registerType(Types.time)
._registerType(Types.timetz)
._registerType(Types.jsonb)
._registerType(Types.regtype)
._registerType(Types.regclass)
._registerType(Types.json)
._registerType(Types.null)
._registerType(Types.float)
._registerType(Types.integer)
._registerType(Types.bigint)
._registerType(Types.bytea)
._registerType(Types.point)
._registerType(Types.line)
._registerType(Types.lseg)
._registerType(Types.box)
._registerType(Types.path)
._registerType(Types.polygon)
._registerType(Types.circle)
._registerType(Types.interval)
._registerType(Types.inet)
._registerType(Types.record([])) // hack to support functions with record input (see row_to_json UT)
._registerTypeSizeable(DataType.text, Types.text)
new PgConstraintTable(catalog).register();
new PgClassListTable(catalog).register();
new PgNamespaceTable(catalog).register();
new PgAttributeTable(catalog).register();
new PgIndexTable(catalog).register();
new PgTypeTable(catalog).register();
new PgRange(catalog).register();
new PgProc(catalog).register();
new PgDatabaseTable(catalog).register();
new PgStatioUserTables(catalog).register();
new PgEnumTable(catalog).register();
new PgSequencesTable(catalog).register();
new PgUserTable(catalog).register();
// this is an ugly hack...
const tbl = catalog.declareTable({
name: 'current_schema',
fields: [
{ name: 'current_schema', type: Types.text() },
]
}, true);
tbl.insert({ current_schema: 'public' });
tbl.setHidden().setReadonly();
addFns(catalog, allFunctions);
catalog.registerFunction({
name: 'set_config',
args: [Types.text(), Types.text(), Types.bool],
returns: Types.text(),
impure: true,
implementation: (cfg: string, val: string, is_local: boolean) => {
// todo - implement this... used to override search_path in dumps.
// => have a dynamic search_path.
// => not trivial du to the "is_local" arg
// https://www.postgresql.org/docs/9.3/functions-admin.html
return val;
}
});
catalog.registerFunction({
name: 'substring',
args: [Types.text(), Types.integer],
returns: Types.text(),
implementation: sqlSubstring,
})
catalog.registerFunction({
name: 'substring',
args: [Types.text(), Types.integer, Types.integer],
returns: Types.text(),
implementation: sqlSubstring,
});
catalog.registerFunction({
// required for Sequelize introspection
name: 'pg_get_indexdef',
args: [Types.integer],
returns: Types.text(),
implementation: (indexId: number) => {
throw new Error('This stub implementation of "pg_get_indexdef" should not be called');
},
})
db.getSchema('pg_catalog').registerFunction({
name: 'col_description',
args: [DataType.integer, DataType.integer],
returns: DataType.text,
implementation: x => 'Fake description provided by pg-mem',
});
registerCommonOperators(catalog);
registerSqlFunctionLanguage(db);
catalog.setReadonly()
}
function addFns(catalog: _ISchema, fns: FunctionDefinition[]) {
for (const f of fns) {
catalog.registerFunction(f);
}
}