@nymphjs/driver-postgresql 1.0.0-beta.14 → 1.0.0-beta.141
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +647 -0
- package/README.md +5 -3
- package/dist/PostgreSQLDriver.d.ts +110 -20
- package/dist/PostgreSQLDriver.js +1767 -756
- package/dist/PostgreSQLDriver.js.map +1 -1
- package/dist/PostgreSQLDriver.test.js +13 -12
- package/dist/PostgreSQLDriver.test.js.map +1 -1
- package/dist/conf/defaults.d.ts +1 -1
- package/dist/conf/defaults.js +1 -3
- package/dist/conf/defaults.js.map +1 -1
- package/dist/conf/index.d.ts +37 -2
- package/dist/conf/index.js +2 -8
- package/dist/conf/index.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -24
- package/dist/index.js.map +1 -1
- package/jest.config.js +31 -6
- package/package.json +40 -26
- package/src/PostgreSQLDriver.test.ts +16 -5
- package/src/PostgreSQLDriver.ts +2742 -1231
- package/src/conf/defaults.ts +1 -1
- package/src/conf/index.ts +38 -2
- package/src/index.ts +2 -2
- package/tsconfig.json +6 -3
- package/typedoc.json +4 -0
- package/dist/conf/d.d.ts +0 -10
- package/dist/conf/d.js +0 -3
- package/dist/conf/d.js.map +0 -1
- package/dist/runPostgresqlSync.js +0 -35
- package/src/conf/d.ts +0 -37
- package/src/runPostgresqlSync.js +0 -35
- package/src/testpostgresql.js +0 -59
package/dist/PostgreSQLDriver.js
CHANGED
|
@@ -1,26 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
import format from 'pg-format';
|
|
3
|
+
import Cursor from 'pg-cursor';
|
|
4
|
+
import { NymphDriver, EntityConflictError, EntityInvalidDataError, EntityUniqueConstraintError, InvalidParametersError, NotConfiguredError, QueryFailedError, UnableToConnectError, xor, } from '@nymphjs/nymph';
|
|
5
|
+
import { makeTableSuffix } from '@nymphjs/guid';
|
|
6
|
+
import { PostgreSQLDriverConfigDefaults as defaults, } from './conf/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* The PostgreSQL Nymph database driver.
|
|
9
|
+
*/
|
|
10
|
+
export default class PostgreSQLDriver extends NymphDriver {
|
|
11
|
+
config;
|
|
12
|
+
postgresqlConfig;
|
|
13
|
+
prefix;
|
|
14
|
+
connected = false;
|
|
15
|
+
// @ts-ignore: this is assigned in connect(), which is called by the constructor.
|
|
16
|
+
link;
|
|
17
|
+
transaction = null;
|
|
13
18
|
static escape(input) {
|
|
14
|
-
return
|
|
19
|
+
return format.ident(input);
|
|
15
20
|
}
|
|
16
21
|
static escapeValue(input) {
|
|
17
|
-
return
|
|
22
|
+
return format.literal(input);
|
|
23
|
+
}
|
|
24
|
+
static escapeNullSequences(input) {
|
|
25
|
+
// Postgres doesn't support null bytes in `text`, and it converts strings
|
|
26
|
+
// in JSON to `text`, so we need to escape the escape sequences for null
|
|
27
|
+
// bytes.
|
|
28
|
+
return (input
|
|
29
|
+
.replace(/\uFFFD/g, () => '\uFFFD\uFFFD')
|
|
30
|
+
// n so that if there's already an escape, it turns into \n
|
|
31
|
+
// - so that it won't match a \uFFFD that got turned into \uFFFD\uFFFD
|
|
32
|
+
.replace(/\\u0000/g, () => 'nu\uFFFD-')
|
|
33
|
+
.replace(/\\x00/g, () => 'nx\uFFFD-'));
|
|
34
|
+
}
|
|
35
|
+
static unescapeNullSequences(input) {
|
|
36
|
+
return input
|
|
37
|
+
.replace(/nu\uFFFD-/g, () => '\\u0000')
|
|
38
|
+
.replace(/nx\uFFFD-/g, () => '\\x00')
|
|
39
|
+
.replace(/\uFFFD\uFFFD/g, () => '\uFFFD');
|
|
40
|
+
}
|
|
41
|
+
static escapeNulls(input) {
|
|
42
|
+
// Postgres doesn't support null bytes in `text`.
|
|
43
|
+
return input
|
|
44
|
+
.replace(/\uFFFD/g, () => '\uFFFD\uFFFD')
|
|
45
|
+
.replace(/\x00/g, () => '-\uFFFD-');
|
|
46
|
+
}
|
|
47
|
+
static unescapeNulls(input) {
|
|
48
|
+
return input
|
|
49
|
+
.replace(/-\uFFFD-/g, () => '\x00')
|
|
50
|
+
.replace(/\uFFFD\uFFFD/g, () => '\uFFFD');
|
|
18
51
|
}
|
|
19
52
|
constructor(config, link, transaction) {
|
|
20
53
|
super();
|
|
21
|
-
this.
|
|
22
|
-
this.transaction = null;
|
|
23
|
-
this.config = { ...conf_1.PostgreSQLDriverConfigDefaults, ...config };
|
|
54
|
+
this.config = { ...defaults, ...config };
|
|
24
55
|
const { host, user, password, database, port, customPoolConfig } = this.config;
|
|
25
56
|
this.postgresqlConfig = customPoolConfig ?? {
|
|
26
57
|
host,
|
|
@@ -41,19 +72,40 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
41
72
|
this.connect();
|
|
42
73
|
}
|
|
43
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* This is used internally by Nymph. Don't call it yourself.
|
|
77
|
+
*
|
|
78
|
+
* @returns A clone of this instance.
|
|
79
|
+
*/
|
|
44
80
|
clone() {
|
|
45
81
|
return new PostgreSQLDriver(this.config, this.link, this.transaction ?? undefined);
|
|
46
82
|
}
|
|
47
|
-
getConnection() {
|
|
48
|
-
if (this.transaction != null &&
|
|
83
|
+
getConnection(outsideTransaction = false) {
|
|
84
|
+
if (this.transaction != null &&
|
|
85
|
+
this.transaction.connection != null &&
|
|
86
|
+
!outsideTransaction) {
|
|
49
87
|
return Promise.resolve(this.transaction.connection);
|
|
50
88
|
}
|
|
51
|
-
return new Promise((resolve, reject) => this.link.connect((err, client, done) => err
|
|
89
|
+
return new Promise((resolve, reject) => this.link.connect((err, client, done) => err
|
|
90
|
+
? reject(err)
|
|
91
|
+
: client
|
|
92
|
+
? resolve({ client, done })
|
|
93
|
+
: reject('No client returned from connect.')));
|
|
52
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Connect to the PostgreSQL database.
|
|
97
|
+
*
|
|
98
|
+
* @returns Whether this instance is connected to a PostgreSQL database.
|
|
99
|
+
*/
|
|
53
100
|
async connect() {
|
|
101
|
+
// If we think we're connected, try pinging the server.
|
|
54
102
|
try {
|
|
55
103
|
if (this.connected) {
|
|
56
|
-
const connection = await new Promise((resolve, reject) => this.link.connect((err, client, done) => err
|
|
104
|
+
const connection = await new Promise((resolve, reject) => this.link.connect((err, client, done) => err
|
|
105
|
+
? reject(err)
|
|
106
|
+
: client
|
|
107
|
+
? resolve({ client, done })
|
|
108
|
+
: reject('No client returned from connect.')));
|
|
57
109
|
await new Promise((resolve, reject) => connection.client.query('SELECT 1;', [], (err, res) => {
|
|
58
110
|
if (err) {
|
|
59
111
|
reject(err);
|
|
@@ -66,9 +118,10 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
66
118
|
catch (e) {
|
|
67
119
|
this.connected = false;
|
|
68
120
|
}
|
|
121
|
+
// Connecting, selecting database
|
|
69
122
|
if (!this.connected) {
|
|
70
123
|
try {
|
|
71
|
-
this.link = new
|
|
124
|
+
this.link = new pg.Pool(this.postgresqlConfig);
|
|
72
125
|
this.connected = true;
|
|
73
126
|
}
|
|
74
127
|
catch (e) {
|
|
@@ -76,15 +129,20 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
76
129
|
this.postgresqlConfig.user === 'nymph' &&
|
|
77
130
|
this.postgresqlConfig.password === 'password' &&
|
|
78
131
|
this.postgresqlConfig.database === 'nymph') {
|
|
79
|
-
throw new
|
|
132
|
+
throw new NotConfiguredError("It seems the config hasn't been set up correctly.");
|
|
80
133
|
}
|
|
81
134
|
else {
|
|
82
|
-
throw new
|
|
135
|
+
throw new UnableToConnectError('Could not connect: ' + e?.message);
|
|
83
136
|
}
|
|
84
137
|
}
|
|
85
138
|
}
|
|
86
139
|
return this.connected;
|
|
87
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Disconnect from the PostgreSQL database.
|
|
143
|
+
*
|
|
144
|
+
* @returns Whether this instance is connected to a PostgreSQL database.
|
|
145
|
+
*/
|
|
88
146
|
async disconnect() {
|
|
89
147
|
if (this.connected) {
|
|
90
148
|
await new Promise((resolve) => this.link.end(() => resolve(0)));
|
|
@@ -93,87 +151,179 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
93
151
|
return this.connected;
|
|
94
152
|
}
|
|
95
153
|
async inTransaction() {
|
|
154
|
+
if (this.transaction && this.transaction.count === 0) {
|
|
155
|
+
this.transaction = null;
|
|
156
|
+
}
|
|
96
157
|
return !!this.transaction;
|
|
97
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Check connection status.
|
|
161
|
+
*
|
|
162
|
+
* @returns Whether this instance is connected to a PostgreSQL database.
|
|
163
|
+
*/
|
|
98
164
|
isConnected() {
|
|
99
165
|
return this.connected;
|
|
100
166
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
167
|
+
async createEntitiesTable(etype, connection) {
|
|
168
|
+
// Create the entity table.
|
|
169
|
+
await this.queryRun(`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} (
|
|
104
170
|
"guid" BYTEA NOT NULL,
|
|
105
171
|
"tags" TEXT[],
|
|
106
172
|
"cdate" DOUBLE PRECISION NOT NULL,
|
|
107
173
|
"mdate" DOUBLE PRECISION NOT NULL,
|
|
174
|
+
"user" BYTEA,
|
|
175
|
+
"group" BYTEA,
|
|
176
|
+
"acUser" SMALLINT,
|
|
177
|
+
"acGroup" SMALLINT,
|
|
178
|
+
"acOther" SMALLINT,
|
|
179
|
+
"acRead" BYTEA[],
|
|
180
|
+
"acWrite" BYTEA[],
|
|
181
|
+
"acFull" BYTEA[],
|
|
108
182
|
PRIMARY KEY ("guid")
|
|
109
|
-
) WITH ( OIDS=FALSE )
|
|
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
|
-
|
|
183
|
+
) WITH ( OIDS=FALSE );`, { connection });
|
|
184
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`, { connection });
|
|
185
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_cdate`)};`, { connection });
|
|
186
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_cdate`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING btree ("cdate");`, { connection });
|
|
187
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_mdate`)};`, { connection });
|
|
188
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_mdate`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING btree ("mdate");`, { connection });
|
|
189
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_tags`)};`, { connection });
|
|
190
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_tags`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING gin ("tags");`, { connection });
|
|
191
|
+
await this.createEntitiesTilmeldIndexes(etype, connection);
|
|
192
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} SET ( autovacuum_vacuum_scale_factor = 0.05, autovacuum_analyze_scale_factor = 0.05 );`, { connection });
|
|
193
|
+
}
|
|
194
|
+
async addTilmeldColumnsAndIndexes(etype, connection) {
|
|
195
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ADD COLUMN IF NOT EXISTS "user" BYTEA,
|
|
196
|
+
ADD COLUMN IF NOT EXISTS "group" BYTEA,
|
|
197
|
+
ADD COLUMN IF NOT EXISTS "acUser" SMALLINT,
|
|
198
|
+
ADD COLUMN IF NOT EXISTS "acGroup" SMALLINT,
|
|
199
|
+
ADD COLUMN IF NOT EXISTS "acOther" SMALLINT,
|
|
200
|
+
ADD COLUMN IF NOT EXISTS "acRead" BYTEA[],
|
|
201
|
+
ADD COLUMN IF NOT EXISTS "acWrite" BYTEA[],
|
|
202
|
+
ADD COLUMN IF NOT EXISTS "acFull" BYTEA[];`);
|
|
203
|
+
await this.createEntitiesTilmeldIndexes(etype, connection);
|
|
204
|
+
}
|
|
205
|
+
async createEntitiesTilmeldIndexes(etype, connection) {
|
|
206
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_user_acUser`)};`, { connection });
|
|
207
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_user_acUser`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING btree ("user", "acUser");`, { connection });
|
|
208
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_group_acGroup`)};`, { connection });
|
|
209
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_group_acGroup`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING btree ("group", "acGroup");`, { connection });
|
|
210
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acUser`)};`, { connection });
|
|
211
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acUser`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING btree ("acUser");`, { connection });
|
|
212
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acGroup`)};`, { connection });
|
|
213
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acGroup`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING btree ("acGroup");`, { connection });
|
|
214
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acOther`)};`, { connection });
|
|
215
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acOther`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING btree ("acOther");`, { connection });
|
|
216
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acRead`)};`, { connection });
|
|
217
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acRead`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING gin ("acRead");`, { connection });
|
|
218
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acWrite`)};`, { connection });
|
|
219
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acWrite`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING gin ("acWrite");`, { connection });
|
|
220
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acFull`)};`, { connection });
|
|
221
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}_id_acFull`)} ON ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} USING gin ("acFull");`, { connection });
|
|
222
|
+
}
|
|
223
|
+
async createDataTable(etype, connection) {
|
|
224
|
+
// Create the data table.
|
|
225
|
+
await this.queryRun(`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} (
|
|
135
226
|
"guid" BYTEA NOT NULL,
|
|
136
227
|
"name" TEXT NOT NULL,
|
|
137
|
-
"
|
|
228
|
+
"value" CHARACTER(1) NOT NULL,
|
|
229
|
+
"json" JSONB,
|
|
138
230
|
"string" TEXT,
|
|
139
231
|
"number" DOUBLE PRECISION,
|
|
232
|
+
"truthy" BOOLEAN,
|
|
140
233
|
PRIMARY KEY ("guid", "name"),
|
|
141
234
|
FOREIGN KEY ("guid")
|
|
142
235
|
REFERENCES ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ("guid") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
|
|
143
|
-
) WITH ( OIDS=FALSE )
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
236
|
+
) WITH ( OIDS=FALSE );`, { connection });
|
|
237
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`, { connection });
|
|
238
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_name_guid`)};`, { connection });
|
|
239
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_name_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING btree ("name", "guid");`, { connection });
|
|
240
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_name_string_guid`)};`, { connection });
|
|
241
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_name_string_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING btree ("name", LEFT("string", 512), "guid");`, { connection });
|
|
242
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_name_number_guid`)};`, { connection });
|
|
243
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_name_number_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING btree ("name", "number", "guid");`, { connection });
|
|
244
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_name_truthy_guid`)};`, { connection });
|
|
245
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_name_truthy_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING btree ("name", "truthy", "guid");`, { connection });
|
|
246
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_string`)};`, { connection });
|
|
247
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_string`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING gin ("string" gin_trgm_ops);`, { connection });
|
|
248
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_json`)};`, { connection });
|
|
249
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_json`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING gin ("json");`, { connection });
|
|
250
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} SET ( autovacuum_vacuum_scale_factor = 0.05, autovacuum_analyze_scale_factor = 0.05 );`, { connection });
|
|
251
|
+
}
|
|
252
|
+
async createReferencesTable(etype, connection) {
|
|
253
|
+
// Create the references table.
|
|
254
|
+
await this.queryRun(`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} (
|
|
154
255
|
"guid" BYTEA NOT NULL,
|
|
155
256
|
"name" TEXT NOT NULL,
|
|
156
257
|
"reference" BYTEA NOT NULL,
|
|
157
258
|
PRIMARY KEY ("guid", "name", "reference"),
|
|
158
259
|
FOREIGN KEY ("guid")
|
|
159
260
|
REFERENCES ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ("guid") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
|
|
160
|
-
) WITH ( OIDS=FALSE )
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
261
|
+
) WITH ( OIDS=FALSE );`, { connection });
|
|
262
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`, { connection });
|
|
263
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}_id_name_reference_guid`)};`, { connection });
|
|
264
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}_id_name_reference_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} USING btree ("name", "reference", "guid");`, { connection });
|
|
265
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} SET ( autovacuum_vacuum_scale_factor = 0.05, autovacuum_analyze_scale_factor = 0.05 );`, { connection });
|
|
266
|
+
}
|
|
267
|
+
async createTokensTable(etype, connection) {
|
|
268
|
+
// Create the tokens table.
|
|
269
|
+
await this.queryRun(`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} (
|
|
270
|
+
"guid" BYTEA NOT NULL,
|
|
271
|
+
"name" TEXT NOT NULL,
|
|
272
|
+
"token" INTEGER NOT NULL,
|
|
273
|
+
"position" INTEGER NOT NULL,
|
|
274
|
+
"stem" BOOLEAN NOT NULL,
|
|
275
|
+
PRIMARY KEY ("guid", "name", "token", "position"),
|
|
276
|
+
FOREIGN KEY ("guid")
|
|
277
|
+
REFERENCES ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ("guid") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
|
|
278
|
+
) WITH ( OIDS=FALSE );`, { connection });
|
|
279
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`, { connection });
|
|
280
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}_id_name_token_guid`)};`, { connection });
|
|
281
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}_id_name_token_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} USING btree ("name", "token", "guid");`, { connection });
|
|
282
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} SET ( autovacuum_vacuum_scale_factor = 0.05, autovacuum_analyze_scale_factor = 0.05 );`, { connection });
|
|
283
|
+
}
|
|
284
|
+
async createUniquesTable(etype, connection) {
|
|
285
|
+
// Create the unique strings table.
|
|
286
|
+
await this.queryRun(`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} (
|
|
287
|
+
"guid" BYTEA NOT NULL,
|
|
288
|
+
"unique" TEXT NOT NULL UNIQUE,
|
|
289
|
+
PRIMARY KEY ("guid", "unique"),
|
|
290
|
+
FOREIGN KEY ("guid")
|
|
291
|
+
REFERENCES ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ("guid") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
|
|
292
|
+
) WITH ( OIDS=FALSE );`, { connection });
|
|
293
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`, { connection });
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Create entity tables in the database.
|
|
297
|
+
*
|
|
298
|
+
* @param etype The entity type to create a table for. If this is blank, the default tables are created.
|
|
299
|
+
* @returns True on success, false on failure.
|
|
300
|
+
*/
|
|
301
|
+
async createTables(etype = null) {
|
|
302
|
+
const connection = await this.getConnection(true);
|
|
303
|
+
if (etype != null) {
|
|
304
|
+
await this.createEntitiesTable(etype, connection);
|
|
305
|
+
await this.createDataTable(etype, connection);
|
|
306
|
+
await this.createReferencesTable(etype, connection);
|
|
307
|
+
await this.createTokensTable(etype, connection);
|
|
308
|
+
await this.createUniquesTable(etype, connection);
|
|
168
309
|
}
|
|
169
310
|
else {
|
|
170
|
-
|
|
311
|
+
// Add trigram extensions.
|
|
312
|
+
try {
|
|
313
|
+
await this.queryRun(`CREATE EXTENSION pg_trgm;`, { connection });
|
|
314
|
+
}
|
|
315
|
+
catch (e) {
|
|
316
|
+
// Ignore errors.
|
|
317
|
+
}
|
|
318
|
+
// Create the UID table.
|
|
319
|
+
await this.queryRun(`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(`${this.prefix}uids`)} (
|
|
171
320
|
"name" TEXT NOT NULL,
|
|
172
321
|
"cur_uid" BIGINT NOT NULL,
|
|
173
322
|
PRIMARY KEY ("name")
|
|
174
|
-
) WITH ( OIDS = FALSE )
|
|
175
|
-
this.
|
|
323
|
+
) WITH ( OIDS = FALSE );`, { connection });
|
|
324
|
+
await this.queryRun(`ALTER TABLE ${PostgreSQLDriver.escape(`${this.prefix}uids`)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`, { connection });
|
|
176
325
|
}
|
|
326
|
+
connection.done();
|
|
177
327
|
return true;
|
|
178
328
|
}
|
|
179
329
|
translateQuery(origQuery, origParams) {
|
|
@@ -191,158 +341,159 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
191
341
|
}
|
|
192
342
|
async query(runQuery, query, etypes = []) {
|
|
193
343
|
try {
|
|
344
|
+
this.nymph.config.debugInfo('postgresql:query', query);
|
|
194
345
|
return await runQuery();
|
|
195
346
|
}
|
|
196
347
|
catch (e) {
|
|
197
348
|
const errorCode = e?.code;
|
|
198
|
-
if (errorCode === '42P01' && this.createTables()) {
|
|
349
|
+
if (errorCode === '42P01' && (await this.createTables())) {
|
|
350
|
+
// If the tables don't exist yet, create them.
|
|
199
351
|
for (let etype of etypes) {
|
|
200
|
-
this.createTables(etype);
|
|
352
|
+
await this.createTables(etype);
|
|
201
353
|
}
|
|
202
354
|
try {
|
|
203
355
|
return await runQuery();
|
|
204
356
|
}
|
|
205
357
|
catch (e2) {
|
|
206
|
-
throw new
|
|
358
|
+
throw new QueryFailedError('Query failed: ' + e2?.code + ' - ' + e2?.message, query, e2?.code);
|
|
207
359
|
}
|
|
208
360
|
}
|
|
209
|
-
else {
|
|
210
|
-
throw
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
querySync(runQuery, query, etypes = []) {
|
|
215
|
-
try {
|
|
216
|
-
return runQuery();
|
|
217
|
-
}
|
|
218
|
-
catch (e) {
|
|
219
|
-
const errorCode = e?.code;
|
|
220
|
-
if (errorCode === '42P01' && this.createTables()) {
|
|
221
|
-
for (let etype of etypes) {
|
|
222
|
-
this.createTables(etype);
|
|
223
|
-
}
|
|
224
|
-
try {
|
|
225
|
-
return runQuery();
|
|
226
|
-
}
|
|
227
|
-
catch (e2) {
|
|
228
|
-
throw new nymph_1.QueryFailedError('Query failed: ' + e2?.code + ' - ' + e2?.message, query);
|
|
229
|
-
}
|
|
361
|
+
else if (errorCode === '23505') {
|
|
362
|
+
throw new EntityUniqueConstraintError(`Unique constraint violation.`);
|
|
230
363
|
}
|
|
231
364
|
else {
|
|
232
|
-
throw e;
|
|
365
|
+
throw new QueryFailedError('Query failed: ' + e?.code + ' - ' + e?.message, query, e?.code);
|
|
233
366
|
}
|
|
234
367
|
}
|
|
235
368
|
}
|
|
236
|
-
|
|
369
|
+
queryArray(query, { etypes = [], params = {}, } = {}) {
|
|
237
370
|
const { query: newQuery, params: newParams } = this.translateQuery(query, params);
|
|
238
371
|
return this.query(async () => {
|
|
239
|
-
const results = await new Promise((resolve, reject) => {
|
|
372
|
+
const results = await new Promise(async (resolve, reject) => {
|
|
373
|
+
// Only do one query at a time in transations. The Postgres
|
|
374
|
+
// package will only support one query at a time on a given
|
|
375
|
+
// connection in a future release.
|
|
376
|
+
let myResolve = () => { };
|
|
377
|
+
const myPromise = new Promise((resolve) => {
|
|
378
|
+
myResolve = resolve;
|
|
379
|
+
});
|
|
380
|
+
if (this.transaction) {
|
|
381
|
+
const latestPromise = this.transaction.latestPromise;
|
|
382
|
+
this.transaction.latestPromise = myPromise;
|
|
383
|
+
await latestPromise;
|
|
384
|
+
}
|
|
240
385
|
try {
|
|
241
|
-
(this.transaction?.connection?.client ?? this.link)
|
|
242
|
-
|
|
243
|
-
.then((results) => resolve(results), (error) => reject(error));
|
|
386
|
+
const results = await (this.transaction?.connection?.client ?? this.link).query(newQuery, newParams);
|
|
387
|
+
resolve(results);
|
|
244
388
|
}
|
|
245
389
|
catch (e) {
|
|
246
390
|
reject(e);
|
|
247
391
|
}
|
|
392
|
+
myResolve();
|
|
248
393
|
});
|
|
249
394
|
return results.rows;
|
|
250
395
|
}, `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
251
396
|
}
|
|
252
|
-
|
|
397
|
+
async queryIter(query, { etypes = [], params = {}, } = {}) {
|
|
253
398
|
const { query: newQuery, params: newParams } = this.translateQuery(query, params);
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
maxBuffer: 100 * 1024 * 1024,
|
|
263
|
-
encoding: 'utf8',
|
|
264
|
-
windowsHide: true,
|
|
399
|
+
const that = this;
|
|
400
|
+
return this.query(async function* () {
|
|
401
|
+
// Only do one query at a time in transations. The Postgres
|
|
402
|
+
// package will only support one query at a time on a given
|
|
403
|
+
// connection in a future release.
|
|
404
|
+
let myResolve = () => { };
|
|
405
|
+
const myPromise = new Promise((resolve) => {
|
|
406
|
+
myResolve = resolve;
|
|
265
407
|
});
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
408
|
+
if (that.transaction) {
|
|
409
|
+
const latestPromise = that.transaction.latestPromise;
|
|
410
|
+
that.transaction.latestPromise = myPromise;
|
|
411
|
+
await latestPromise;
|
|
270
412
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
413
|
+
const transaction = !!that.transaction?.connection;
|
|
414
|
+
const connection = await that.getConnection();
|
|
415
|
+
const cursor = new Cursor(newQuery, newParams);
|
|
416
|
+
const iter = connection.client.query(cursor);
|
|
417
|
+
while (true) {
|
|
418
|
+
const rows = await iter.read(100);
|
|
419
|
+
myResolve();
|
|
420
|
+
if (!rows.length) {
|
|
421
|
+
await new Promise((resolve) => {
|
|
422
|
+
iter.close(() => {
|
|
423
|
+
if (!transaction) {
|
|
424
|
+
connection.done();
|
|
425
|
+
}
|
|
426
|
+
resolve();
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
for (let row of rows) {
|
|
432
|
+
yield row;
|
|
433
|
+
}
|
|
278
434
|
}
|
|
279
435
|
}, `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
280
436
|
}
|
|
281
437
|
queryGet(query, { etypes = [], params = {}, } = {}) {
|
|
282
438
|
const { query: newQuery, params: newParams } = this.translateQuery(query, params);
|
|
283
439
|
return this.query(async () => {
|
|
284
|
-
const results = await new Promise((resolve, reject) => {
|
|
440
|
+
const results = await new Promise(async (resolve, reject) => {
|
|
441
|
+
// Only do one query at a time in transations. The Postgres
|
|
442
|
+
// package will only support one query at a time on a given
|
|
443
|
+
// connection in a future release.
|
|
444
|
+
let myResolve = () => { };
|
|
445
|
+
const myPromise = new Promise((resolve) => {
|
|
446
|
+
myResolve = resolve;
|
|
447
|
+
});
|
|
448
|
+
if (this.transaction) {
|
|
449
|
+
const latestPromise = this.transaction.latestPromise;
|
|
450
|
+
this.transaction.latestPromise = myPromise;
|
|
451
|
+
await latestPromise;
|
|
452
|
+
}
|
|
285
453
|
try {
|
|
286
|
-
(this.transaction?.connection?.client ?? this.link)
|
|
287
|
-
|
|
288
|
-
.then((results) => resolve(results), (error) => reject(error));
|
|
454
|
+
const results = await (this.transaction?.connection?.client ?? this.link).query(newQuery, newParams);
|
|
455
|
+
resolve(results);
|
|
289
456
|
}
|
|
290
457
|
catch (e) {
|
|
291
458
|
reject(e);
|
|
292
459
|
}
|
|
460
|
+
myResolve();
|
|
293
461
|
});
|
|
294
462
|
return results.rows[0];
|
|
295
463
|
}, `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
296
464
|
}
|
|
297
|
-
queryRun(query, { etypes = [], params = {}, } = {}) {
|
|
465
|
+
queryRun(query, { etypes = [], params = {}, transaction, connection, } = {}) {
|
|
298
466
|
const { query: newQuery, params: newParams } = this.translateQuery(query, params);
|
|
299
467
|
return this.query(async () => {
|
|
300
|
-
const results = await new Promise((resolve, reject) => {
|
|
468
|
+
const results = await new Promise(async (resolve, reject) => {
|
|
469
|
+
// Only do one query at a time in transations. The Postgres
|
|
470
|
+
// package will only support one query at a time on a given
|
|
471
|
+
// connection in a future release.
|
|
472
|
+
let myResolve = () => { };
|
|
473
|
+
const myPromise = new Promise((resolve) => {
|
|
474
|
+
myResolve = resolve;
|
|
475
|
+
});
|
|
476
|
+
const trans = transaction ?? this.transaction;
|
|
477
|
+
if (trans) {
|
|
478
|
+
const latestPromise = trans.latestPromise;
|
|
479
|
+
trans.latestPromise = myPromise;
|
|
480
|
+
await latestPromise;
|
|
481
|
+
}
|
|
301
482
|
try {
|
|
302
|
-
(
|
|
303
|
-
|
|
304
|
-
.then((results) => resolve(results), (error) => reject(error));
|
|
483
|
+
const results = await ((connection ?? trans?.connection)?.client ?? this.link).query(newQuery, newParams);
|
|
484
|
+
resolve(results);
|
|
305
485
|
}
|
|
306
486
|
catch (e) {
|
|
307
487
|
reject(e);
|
|
308
488
|
}
|
|
489
|
+
myResolve();
|
|
309
490
|
});
|
|
310
491
|
return { rowCount: results.rowCount ?? 0 };
|
|
311
492
|
}, `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
312
493
|
}
|
|
313
|
-
queryRunSync(query, { etypes = [], params = {}, } = {}) {
|
|
314
|
-
const { query: newQuery, params: newParams } = this.translateQuery(query, params);
|
|
315
|
-
return this.querySync(() => {
|
|
316
|
-
const output = child_process_1.default.spawnSync(process.argv0, [__dirname + '/runPostgresqlSync.js'], {
|
|
317
|
-
input: JSON.stringify({
|
|
318
|
-
postgresqlConfig: this.postgresqlConfig,
|
|
319
|
-
query: newQuery,
|
|
320
|
-
params: newParams,
|
|
321
|
-
}),
|
|
322
|
-
timeout: 30000,
|
|
323
|
-
maxBuffer: 100 * 1024 * 1024,
|
|
324
|
-
encoding: 'utf8',
|
|
325
|
-
windowsHide: true,
|
|
326
|
-
});
|
|
327
|
-
try {
|
|
328
|
-
const results = JSON.parse(output.stdout);
|
|
329
|
-
return { rowCount: results.rowCount ?? 0 };
|
|
330
|
-
}
|
|
331
|
-
catch (e) {
|
|
332
|
-
}
|
|
333
|
-
if (output.status === 0) {
|
|
334
|
-
throw new Error('Unknown parse error.');
|
|
335
|
-
}
|
|
336
|
-
const err = JSON.parse(output.stderr);
|
|
337
|
-
const e = new Error(err.name);
|
|
338
|
-
for (const name in err) {
|
|
339
|
-
e[name] = err[name];
|
|
340
|
-
}
|
|
341
|
-
}, `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
342
|
-
}
|
|
343
494
|
async commit(name) {
|
|
344
495
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
345
|
-
throw new
|
|
496
|
+
throw new InvalidParametersError('Transaction commit attempted without a name.');
|
|
346
497
|
}
|
|
347
498
|
if (!this.transaction || this.transaction.count === 0) {
|
|
348
499
|
this.transaction = null;
|
|
@@ -382,32 +533,39 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
382
533
|
guid,
|
|
383
534
|
},
|
|
384
535
|
});
|
|
385
|
-
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}
|
|
536
|
+
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
537
|
+
etypes: [etype],
|
|
538
|
+
params: {
|
|
539
|
+
guid,
|
|
540
|
+
},
|
|
541
|
+
});
|
|
542
|
+
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
386
543
|
etypes: [etype],
|
|
387
544
|
params: {
|
|
388
545
|
guid,
|
|
389
546
|
},
|
|
390
547
|
});
|
|
391
|
-
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}
|
|
548
|
+
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
392
549
|
etypes: [etype],
|
|
393
550
|
params: {
|
|
394
551
|
guid,
|
|
395
552
|
},
|
|
396
553
|
});
|
|
397
|
-
await this.commit('nymph-delete');
|
|
398
|
-
if (this.nymph.config.cache) {
|
|
399
|
-
this.cleanCache(guid);
|
|
400
|
-
}
|
|
401
|
-
return true;
|
|
402
554
|
}
|
|
403
555
|
catch (e) {
|
|
556
|
+
this.nymph.config.debugError('postgresql', `Delete entity error: "${e}"`);
|
|
404
557
|
await this.rollback('nymph-delete');
|
|
405
558
|
throw e;
|
|
406
559
|
}
|
|
560
|
+
await this.commit('nymph-delete');
|
|
561
|
+
return true;
|
|
407
562
|
}
|
|
408
563
|
async deleteUID(name) {
|
|
409
564
|
if (!name) {
|
|
410
|
-
throw new
|
|
565
|
+
throw new InvalidParametersError('Name not given for UID');
|
|
566
|
+
}
|
|
567
|
+
if (name.length > 128) {
|
|
568
|
+
throw new InvalidParametersError('UID name too long. 128 chars max.');
|
|
411
569
|
}
|
|
412
570
|
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
|
|
413
571
|
params: {
|
|
@@ -416,79 +574,238 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
416
574
|
});
|
|
417
575
|
return true;
|
|
418
576
|
}
|
|
419
|
-
async
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
577
|
+
async getIndexes(etype) {
|
|
578
|
+
const indexes = [];
|
|
579
|
+
for (let [scope, suffix] of [
|
|
580
|
+
['data', '_string_guid'],
|
|
581
|
+
['references', '_reference_guid'],
|
|
582
|
+
['tokens', '_token_guid'],
|
|
583
|
+
]) {
|
|
584
|
+
const indexDefinitions = await this.queryArray(`SELECT * FROM "pg_indexes" WHERE "indexname" LIKE @pattern;`, {
|
|
585
|
+
params: {
|
|
586
|
+
pattern: `${this.prefix}${scope}_${etype}_id_custom_%${suffix}`,
|
|
587
|
+
},
|
|
588
|
+
});
|
|
589
|
+
for (const indexDefinition of indexDefinitions) {
|
|
590
|
+
indexes.push({
|
|
591
|
+
scope,
|
|
592
|
+
name: indexDefinition.indexname.substring(`${this.prefix}${scope}_${etype}_id_custom_`.length, indexDefinition.indexname.length - suffix.length),
|
|
593
|
+
property: (indexDefinition.indexdef.match(/WHERE\s+\(?\s*name\s*=\s*'(.*)'/) ?? [])[1] ?? '',
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return indexes;
|
|
598
|
+
}
|
|
599
|
+
async addIndex(etype, definition) {
|
|
600
|
+
this.checkIndexName(definition.name);
|
|
601
|
+
await this.deleteIndex(etype, definition.scope, definition.name);
|
|
602
|
+
const connection = await this.getConnection(true);
|
|
603
|
+
if (definition.scope === 'data') {
|
|
604
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${definition.name}_string_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING btree (LEFT("string", 1024), "guid") WHERE "name"=${PostgreSQLDriver.escapeValue(definition.property)};`, { connection });
|
|
605
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${definition.name}_number_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING btree ("number", "guid") WHERE "name"=${PostgreSQLDriver.escapeValue(definition.property)};`, { connection });
|
|
606
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${definition.name}_truthy_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING btree ("truthy", "guid") WHERE "name"=${PostgreSQLDriver.escapeValue(definition.property)};`, { connection });
|
|
607
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${definition.name}_string_gin`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING gin ("string" gin_trgm_ops) WHERE "name"=${PostgreSQLDriver.escapeValue(definition.property)};`, { connection });
|
|
608
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${definition.name}_json`)} ON ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} USING gin ("json") WHERE "name"=${PostgreSQLDriver.escapeValue(definition.property)};`, { connection });
|
|
609
|
+
}
|
|
610
|
+
else if (definition.scope === 'references') {
|
|
611
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}_id_custom_${definition.name}_reference_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} USING btree ("reference", "guid") WHERE "name"=${PostgreSQLDriver.escapeValue(definition.property)};`, { connection });
|
|
612
|
+
}
|
|
613
|
+
else if (definition.scope === 'tokens') {
|
|
614
|
+
await this.queryRun(`CREATE INDEX ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}_id_custom_${definition.name}_token_guid`)} ON ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} USING btree ("token", "guid") WHERE "name"=${PostgreSQLDriver.escapeValue(definition.property)};`, { connection });
|
|
615
|
+
}
|
|
616
|
+
connection.done();
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
619
|
+
async deleteIndex(etype, scope, name) {
|
|
620
|
+
this.checkIndexName(name);
|
|
621
|
+
const connection = await this.getConnection(true);
|
|
622
|
+
if (scope === 'data') {
|
|
623
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${name}_string_guid`)};`, { connection });
|
|
624
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${name}_number_guid`)};`, { connection });
|
|
625
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${name}_truthy_guid`)};`, { connection });
|
|
626
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${name}_string_gin`)};`, { connection });
|
|
627
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}_id_custom_${name}_json`)};`, { connection });
|
|
628
|
+
}
|
|
629
|
+
else if (scope === 'references') {
|
|
630
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}_id_custom_${name}_reference_guid`)};`, { connection });
|
|
631
|
+
}
|
|
632
|
+
else if (scope === 'tokens') {
|
|
633
|
+
await this.queryRun(`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}_id_custom_${name}_token_guid`)};`, { connection });
|
|
634
|
+
}
|
|
635
|
+
connection.done();
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
async getEtypes() {
|
|
639
|
+
const tables = await this.queryArray('SELECT "table_name" AS "table_name" FROM "information_schema"."tables" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name" LIKE @prefix;', {
|
|
640
|
+
params: {
|
|
641
|
+
db: this.config.database,
|
|
642
|
+
prefix: this.prefix + 'entities_' + '%',
|
|
643
|
+
},
|
|
644
|
+
});
|
|
440
645
|
const etypes = [];
|
|
441
|
-
for (const
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
646
|
+
for (const table of tables) {
|
|
647
|
+
etypes.push(table.table_name.substr((this.prefix + 'entities_').length));
|
|
648
|
+
}
|
|
649
|
+
return etypes;
|
|
650
|
+
}
|
|
651
|
+
async *exportDataIterator() {
|
|
652
|
+
if (yield {
|
|
653
|
+
type: 'comment',
|
|
654
|
+
content: `#nex2
|
|
655
|
+
# Nymph Entity Exchange v2
|
|
656
|
+
# http://nymph.io
|
|
657
|
+
#
|
|
658
|
+
# Generation Time: ${new Date().toLocaleString()}
|
|
659
|
+
`,
|
|
660
|
+
}) {
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
if (yield {
|
|
664
|
+
type: 'comment',
|
|
665
|
+
content: `
|
|
666
|
+
|
|
667
|
+
#
|
|
668
|
+
# UIDs
|
|
669
|
+
#
|
|
670
|
+
|
|
671
|
+
`,
|
|
672
|
+
}) {
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
// Export UIDs.
|
|
676
|
+
let uids = await this.queryIter(`SELECT * FROM ${PostgreSQLDriver.escape(`${this.prefix}uids`)} ORDER BY "name";`);
|
|
677
|
+
for await (const uid of uids) {
|
|
678
|
+
if (yield { type: 'uid', content: `<${uid.name}>[${uid.cur_uid}]\n` }) {
|
|
679
|
+
return;
|
|
445
680
|
}
|
|
446
681
|
}
|
|
682
|
+
if (yield {
|
|
683
|
+
type: 'comment',
|
|
684
|
+
content: `
|
|
685
|
+
|
|
686
|
+
#
|
|
687
|
+
# Entities
|
|
688
|
+
#
|
|
689
|
+
|
|
690
|
+
`,
|
|
691
|
+
}) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
// Get the etypes.
|
|
695
|
+
const etypes = await this.getEtypes();
|
|
447
696
|
for (const etype of etypes) {
|
|
448
|
-
|
|
697
|
+
// Export entities.
|
|
698
|
+
const entityIterator = this.exportEntityInternal(etype);
|
|
699
|
+
for await (let content of entityIterator) {
|
|
700
|
+
yield { type: 'entity', content };
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
async exportEntity(etype, guid) {
|
|
705
|
+
const entityIterator = this.exportEntityInternal(etype, guid);
|
|
706
|
+
let exportContent = '';
|
|
707
|
+
for await (let content of entityIterator) {
|
|
708
|
+
exportContent += content;
|
|
709
|
+
}
|
|
710
|
+
return exportContent;
|
|
711
|
+
}
|
|
712
|
+
async *exportEntityInternal(etype, guid) {
|
|
713
|
+
// Export entities.
|
|
714
|
+
const dataIterator = await this.queryIter(`SELECT encode(e."guid", 'hex') AS "guid", e."tags", e."cdate", e."mdate", encode(e."user", 'hex') AS "user", encode(e."group", 'hex') AS "group", e."acUser", e."acGroup", e."acOther", array(SELECT encode(n, 'hex') FROM unnest(e."acRead") AS n) as "acRead", array(SELECT encode(n, 'hex') FROM unnest(e."acWrite") AS n) as "acWrite", array(SELECT encode(n, 'hex') FROM unnest(e."acFull") AS n) as "acFull", d."name", d."value", d."json", d."string", d."number"
|
|
449
715
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} e
|
|
450
|
-
LEFT JOIN ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} d ON e."guid"=d."guid"
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
716
|
+
LEFT JOIN ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} d ON e."guid"=d."guid"${guid == null ? '' : ` WHERE e."guid"=decode(@guid, 'hex')`}
|
|
717
|
+
ORDER BY e."guid", d."name";`, guid == null ? undefined : { params: { guid } });
|
|
718
|
+
let datum = await dataIterator.next();
|
|
719
|
+
while (!datum.done) {
|
|
720
|
+
const guid = datum.value.guid;
|
|
721
|
+
const tags = datum.value.tags.filter((tag) => tag).join(',');
|
|
722
|
+
const cdate = datum.value.cdate;
|
|
723
|
+
const mdate = datum.value.mdate;
|
|
724
|
+
const user = datum.value.user;
|
|
725
|
+
const group = datum.value.group;
|
|
726
|
+
const acUser = datum.value.acUser;
|
|
727
|
+
const acGroup = datum.value.acGroup;
|
|
728
|
+
const acOther = datum.value.acOther;
|
|
729
|
+
const acRead = datum.value.acRead?.filter((guid) => guid);
|
|
730
|
+
const acWrite = datum.value.acWrite?.filter((guid) => guid);
|
|
731
|
+
const acFull = datum.value.acFull?.filter((guid) => guid);
|
|
732
|
+
let currentEntityExport = [];
|
|
733
|
+
currentEntityExport.push(`{${guid}}<${etype}>[${tags.replace(/\n/g, () => '\\n')}]`);
|
|
734
|
+
currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
|
|
735
|
+
currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
|
|
736
|
+
if (this.nymph.tilmeld != null) {
|
|
737
|
+
if (user != null) {
|
|
738
|
+
currentEntityExport.push(`\tuser=${JSON.stringify(['nymph_entity_reference', user, 'User'])}`);
|
|
472
739
|
}
|
|
473
|
-
|
|
474
|
-
|
|
740
|
+
if (group != null) {
|
|
741
|
+
currentEntityExport.push(`\tgroup=${JSON.stringify(['nymph_entity_reference', group, 'Group'])}`);
|
|
742
|
+
}
|
|
743
|
+
if (acUser != null) {
|
|
744
|
+
currentEntityExport.push(`\tacUser=${JSON.stringify(acUser)}`);
|
|
745
|
+
}
|
|
746
|
+
if (acGroup != null) {
|
|
747
|
+
currentEntityExport.push(`\tacGroup=${JSON.stringify(acGroup)}`);
|
|
748
|
+
}
|
|
749
|
+
if (acOther != null) {
|
|
750
|
+
currentEntityExport.push(`\tacOther=${JSON.stringify(acOther)}`);
|
|
751
|
+
}
|
|
752
|
+
if (acRead != null) {
|
|
753
|
+
currentEntityExport.push(`\tacRead=${JSON.stringify(acRead)}`);
|
|
754
|
+
}
|
|
755
|
+
if (acWrite != null) {
|
|
756
|
+
currentEntityExport.push(`\tacWrite=${JSON.stringify(acWrite)}`);
|
|
475
757
|
}
|
|
758
|
+
if (acFull != null) {
|
|
759
|
+
currentEntityExport.push(`\tacFull=${JSON.stringify(acFull)}`);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
if (datum.value.name != null) {
|
|
763
|
+
// This do will keep going and adding the data until the
|
|
764
|
+
// next entity is reached. $row will end on the next entity.
|
|
765
|
+
do {
|
|
766
|
+
const value = datum.value.value === 'N'
|
|
767
|
+
? JSON.stringify(Number(datum.value.number))
|
|
768
|
+
: datum.value.value === 'S'
|
|
769
|
+
? JSON.stringify(PostgreSQLDriver.unescapeNulls(datum.value.string))
|
|
770
|
+
: datum.value.value === 'J'
|
|
771
|
+
? PostgreSQLDriver.unescapeNullSequences(JSON.stringify(datum.value.json))
|
|
772
|
+
: datum.value.value;
|
|
773
|
+
currentEntityExport.push(`\t${datum.value.name.replace(/\n/g, () => '\\n')}=${value}`);
|
|
774
|
+
datum = await dataIterator.next();
|
|
775
|
+
} while (!datum.done && datum.value.guid === guid);
|
|
776
|
+
}
|
|
777
|
+
else {
|
|
778
|
+
// Make sure that datum is incremented :)
|
|
779
|
+
datum = await dataIterator.next();
|
|
780
|
+
}
|
|
781
|
+
currentEntityExport.push('');
|
|
782
|
+
if (yield currentEntityExport.join('\n')) {
|
|
783
|
+
return;
|
|
476
784
|
}
|
|
477
785
|
}
|
|
478
|
-
return;
|
|
479
786
|
}
|
|
480
|
-
|
|
787
|
+
/**
|
|
788
|
+
* Generate the PostgreSQL query.
|
|
789
|
+
* @param options The options array.
|
|
790
|
+
* @param formattedSelectors The formatted selector array.
|
|
791
|
+
* @param etype
|
|
792
|
+
* @param count Used to track internal params.
|
|
793
|
+
* @param params Used to store internal params.
|
|
794
|
+
* @param subquery Whether only a subquery should be returned.
|
|
795
|
+
* @returns The SQL query.
|
|
796
|
+
*/
|
|
797
|
+
makeEntityQuery(options, formattedSelectors, etype, count = { i: 0 }, params = {}, subquery = false, tableSuffix = '', etypes = [], guidSelector = undefined, guidExplicitSelector = undefined) {
|
|
481
798
|
if (typeof options.class?.alterOptions === 'function') {
|
|
482
799
|
options = options.class.alterOptions(options);
|
|
483
800
|
}
|
|
484
801
|
const eTable = `e${tableSuffix}`;
|
|
485
802
|
const dTable = `d${tableSuffix}`;
|
|
486
|
-
const cTable = `c${tableSuffix}`;
|
|
487
803
|
const fTable = `f${tableSuffix}`;
|
|
488
804
|
const ieTable = `ie${tableSuffix}`;
|
|
489
805
|
const countTable = `count${tableSuffix}`;
|
|
490
|
-
const
|
|
491
|
-
const
|
|
806
|
+
const sTable = `s${tableSuffix}`;
|
|
807
|
+
const sort = options.sort === undefined ? 'cdate' : options.sort;
|
|
808
|
+
const queryParts = this.iterateSelectorsForQuery(formattedSelectors, ({ key, value, typeIsOr, typeIsNot }) => {
|
|
492
809
|
const clauseNot = key.startsWith('!');
|
|
493
810
|
let curQuery = '';
|
|
494
811
|
for (const curValue of value) {
|
|
@@ -501,7 +818,7 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
501
818
|
}
|
|
502
819
|
const guid = `param${++count.i}`;
|
|
503
820
|
curQuery +=
|
|
504
|
-
(
|
|
821
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
505
822
|
ieTable +
|
|
506
823
|
'."guid"=decode(@' +
|
|
507
824
|
guid +
|
|
@@ -517,7 +834,7 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
517
834
|
}
|
|
518
835
|
const tag = `param${++count.i}`;
|
|
519
836
|
curQuery +=
|
|
520
|
-
(
|
|
837
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
521
838
|
'@' +
|
|
522
839
|
tag +
|
|
523
840
|
' <@ ie."tags"';
|
|
@@ -530,17 +847,38 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
530
847
|
if (curQuery) {
|
|
531
848
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
532
849
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
850
|
+
if (curVar === 'cdate' ||
|
|
851
|
+
curVar === 'mdate' ||
|
|
852
|
+
(this.nymph.tilmeld != null &&
|
|
853
|
+
(curVar === 'user' ||
|
|
854
|
+
curVar === 'group' ||
|
|
855
|
+
curVar === 'acUser' ||
|
|
856
|
+
curVar === 'acGroup' ||
|
|
857
|
+
curVar === 'acOther' ||
|
|
858
|
+
curVar === 'acRead' ||
|
|
859
|
+
curVar === 'acWrite' ||
|
|
860
|
+
curVar === 'acFull'))) {
|
|
861
|
+
curQuery +=
|
|
862
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
863
|
+
'(' +
|
|
864
|
+
ieTable +
|
|
865
|
+
'.' +
|
|
866
|
+
PostgreSQLDriver.escape(curVar) +
|
|
867
|
+
' IS NOT NULL)';
|
|
868
|
+
}
|
|
869
|
+
else {
|
|
870
|
+
const name = `param${++count.i}`;
|
|
871
|
+
curQuery +=
|
|
872
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
873
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
874
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
875
|
+
' WHERE "guid"=' +
|
|
876
|
+
ieTable +
|
|
877
|
+
'."guid" AND "name"=@' +
|
|
878
|
+
name +
|
|
879
|
+
')';
|
|
880
|
+
params[name] = curVar;
|
|
881
|
+
}
|
|
544
882
|
}
|
|
545
883
|
break;
|
|
546
884
|
case 'truthy':
|
|
@@ -549,30 +887,34 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
549
887
|
if (curQuery) {
|
|
550
888
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
551
889
|
}
|
|
552
|
-
if (curVar === 'cdate'
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
890
|
+
if (curVar === 'cdate' ||
|
|
891
|
+
curVar === 'mdate' ||
|
|
892
|
+
(this.nymph.tilmeld != null &&
|
|
893
|
+
(curVar === 'user' ||
|
|
894
|
+
curVar === 'group' ||
|
|
895
|
+
curVar === 'acUser' ||
|
|
896
|
+
curVar === 'acGroup' ||
|
|
897
|
+
curVar === 'acOther' ||
|
|
898
|
+
curVar === 'acRead' ||
|
|
899
|
+
curVar === 'acWrite' ||
|
|
900
|
+
curVar === 'acFull'))) {
|
|
561
901
|
curQuery +=
|
|
562
|
-
(
|
|
902
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
563
903
|
'(' +
|
|
564
904
|
ieTable +
|
|
565
|
-
'.
|
|
566
|
-
|
|
905
|
+
'.' +
|
|
906
|
+
PostgreSQLDriver.escape(curVar) +
|
|
907
|
+
' IS NOT NULL)';
|
|
567
908
|
}
|
|
568
909
|
else {
|
|
569
910
|
const name = `param${++count.i}`;
|
|
570
911
|
curQuery +=
|
|
571
|
-
(
|
|
912
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
913
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
914
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
915
|
+
' WHERE "guid"=' +
|
|
572
916
|
ieTable +
|
|
573
|
-
'."guid"
|
|
574
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
575
|
-
' WHERE "name"=@' +
|
|
917
|
+
'."guid" AND "name"=@' +
|
|
576
918
|
name +
|
|
577
919
|
' AND "truthy"=TRUE)';
|
|
578
920
|
params[name] = curVar;
|
|
@@ -581,35 +923,63 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
581
923
|
break;
|
|
582
924
|
case 'equal':
|
|
583
925
|
case '!equal':
|
|
584
|
-
if (curValue[0] === 'cdate'
|
|
926
|
+
if (curValue[0] === 'cdate' ||
|
|
927
|
+
curValue[0] === 'mdate' ||
|
|
928
|
+
(this.nymph.tilmeld != null &&
|
|
929
|
+
(curValue[0] === 'acUser' ||
|
|
930
|
+
curValue[0] === 'acGroup' ||
|
|
931
|
+
curValue[0] === 'acOther'))) {
|
|
585
932
|
if (curQuery) {
|
|
586
933
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
587
934
|
}
|
|
588
|
-
const
|
|
935
|
+
const value = `param${++count.i}`;
|
|
589
936
|
curQuery +=
|
|
590
|
-
(
|
|
937
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
591
938
|
ieTable +
|
|
592
|
-
'.
|
|
593
|
-
|
|
594
|
-
|
|
939
|
+
'.' +
|
|
940
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
941
|
+
'=@' +
|
|
942
|
+
value;
|
|
943
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
595
944
|
? null
|
|
596
945
|
: Number(curValue[1]);
|
|
597
|
-
break;
|
|
598
946
|
}
|
|
599
|
-
else if (
|
|
947
|
+
else if (this.nymph.tilmeld != null &&
|
|
948
|
+
(curValue[0] === 'user' || curValue[0] === 'group')) {
|
|
600
949
|
if (curQuery) {
|
|
601
950
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
602
951
|
}
|
|
603
|
-
const
|
|
952
|
+
const value = `param${++count.i}`;
|
|
604
953
|
curQuery +=
|
|
605
|
-
(
|
|
954
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
606
955
|
ieTable +
|
|
607
|
-
'.
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
956
|
+
'.' +
|
|
957
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
958
|
+
'=decode(@' +
|
|
959
|
+
value +
|
|
960
|
+
", 'hex')";
|
|
961
|
+
params[value] = `${curValue[1]}`;
|
|
962
|
+
}
|
|
963
|
+
else if (this.nymph.tilmeld != null &&
|
|
964
|
+
(curValue[0] === 'acRead' ||
|
|
965
|
+
curValue[0] === 'acWrite' ||
|
|
966
|
+
curValue[0] === 'acFull')) {
|
|
967
|
+
if (curQuery) {
|
|
968
|
+
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
969
|
+
}
|
|
970
|
+
const value = `param${++count.i}`;
|
|
971
|
+
curQuery +=
|
|
972
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
973
|
+
ieTable +
|
|
974
|
+
'.' +
|
|
975
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
976
|
+
'=' +
|
|
977
|
+
!curValue[1]?.length
|
|
978
|
+
? '@' + value
|
|
979
|
+
: "array(SELECT decode(n, 'hex') FROM unnest(@" +
|
|
980
|
+
value +
|
|
981
|
+
'::text[]) AS n)';
|
|
982
|
+
params[value] = Array.isArray(curValue[1]) ? curValue[1] : '';
|
|
613
983
|
}
|
|
614
984
|
else if (typeof curValue[1] === 'number') {
|
|
615
985
|
if (curQuery) {
|
|
@@ -618,11 +988,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
618
988
|
const name = `param${++count.i}`;
|
|
619
989
|
const value = `param${++count.i}`;
|
|
620
990
|
curQuery +=
|
|
621
|
-
(
|
|
991
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
992
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
993
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
994
|
+
' WHERE "guid"=' +
|
|
622
995
|
ieTable +
|
|
623
|
-
'."guid"
|
|
624
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
625
|
-
' WHERE "name"=@' +
|
|
996
|
+
'."guid" AND "name"=@' +
|
|
626
997
|
name +
|
|
627
998
|
' AND "number"=@' +
|
|
628
999
|
value +
|
|
@@ -637,17 +1008,20 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
637
1008
|
const name = `param${++count.i}`;
|
|
638
1009
|
const value = `param${++count.i}`;
|
|
639
1010
|
curQuery +=
|
|
640
|
-
(
|
|
1011
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1012
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1013
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1014
|
+
' WHERE "guid"=' +
|
|
641
1015
|
ieTable +
|
|
642
|
-
'."guid"
|
|
643
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
644
|
-
' WHERE "name"=@' +
|
|
1016
|
+
'."guid" AND "name"=@' +
|
|
645
1017
|
name +
|
|
646
|
-
' AND "string"
|
|
647
|
-
|
|
1018
|
+
' AND "string"=' +
|
|
1019
|
+
(curValue[1].length < 512
|
|
1020
|
+
? 'LEFT(@' + value + ', 512)'
|
|
1021
|
+
: '@' + value) +
|
|
648
1022
|
')';
|
|
649
1023
|
params[name] = curValue[0];
|
|
650
|
-
params[value] = curValue[1];
|
|
1024
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
651
1025
|
}
|
|
652
1026
|
else {
|
|
653
1027
|
if (curQuery) {
|
|
@@ -664,138 +1038,272 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
664
1038
|
const name = `param${++count.i}`;
|
|
665
1039
|
const value = `param${++count.i}`;
|
|
666
1040
|
curQuery +=
|
|
667
|
-
(
|
|
668
|
-
|
|
669
|
-
'."guid" IN (SELECT "guid" FROM ' +
|
|
1041
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1042
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
670
1043
|
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
671
|
-
' WHERE "
|
|
1044
|
+
' WHERE "guid"=' +
|
|
1045
|
+
ieTable +
|
|
1046
|
+
'."guid" AND "name"=@' +
|
|
672
1047
|
name +
|
|
673
|
-
' AND "
|
|
1048
|
+
' AND "json"=@' +
|
|
674
1049
|
value +
|
|
675
1050
|
')';
|
|
676
1051
|
params[name] = curValue[0];
|
|
677
|
-
params[value] = svalue;
|
|
1052
|
+
params[value] = PostgreSQLDriver.escapeNullSequences(svalue);
|
|
678
1053
|
}
|
|
679
1054
|
break;
|
|
680
1055
|
case 'contain':
|
|
681
1056
|
case '!contain':
|
|
682
|
-
if (curValue[0] === 'cdate'
|
|
1057
|
+
if (curValue[0] === 'cdate' ||
|
|
1058
|
+
curValue[0] === 'mdate' ||
|
|
1059
|
+
(this.nymph.tilmeld != null &&
|
|
1060
|
+
(curValue[0] === 'acUser' ||
|
|
1061
|
+
curValue[0] === 'acGroup' ||
|
|
1062
|
+
curValue[0] === 'acOther'))) {
|
|
683
1063
|
if (curQuery) {
|
|
684
1064
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
685
1065
|
}
|
|
686
|
-
const
|
|
1066
|
+
const value = `param${++count.i}`;
|
|
687
1067
|
curQuery +=
|
|
688
|
-
(
|
|
1068
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
689
1069
|
ieTable +
|
|
690
|
-
'.
|
|
691
|
-
|
|
692
|
-
|
|
1070
|
+
'.' +
|
|
1071
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1072
|
+
'=@' +
|
|
1073
|
+
value;
|
|
1074
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
693
1075
|
? null
|
|
694
1076
|
: Number(curValue[1]);
|
|
695
|
-
break;
|
|
696
1077
|
}
|
|
697
|
-
else if (
|
|
1078
|
+
else if (this.nymph.tilmeld != null &&
|
|
1079
|
+
(curValue[0] === 'user' || curValue[0] === 'group')) {
|
|
698
1080
|
if (curQuery) {
|
|
699
1081
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
700
1082
|
}
|
|
701
|
-
const
|
|
1083
|
+
const value = `param${++count.i}`;
|
|
702
1084
|
curQuery +=
|
|
703
|
-
(
|
|
1085
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
704
1086
|
ieTable +
|
|
705
|
-
'.
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
1087
|
+
'.' +
|
|
1088
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1089
|
+
'=decode(@' +
|
|
1090
|
+
value +
|
|
1091
|
+
", 'hex')";
|
|
1092
|
+
params[value] = `${curValue[1]}`;
|
|
1093
|
+
}
|
|
1094
|
+
else if (this.nymph.tilmeld != null &&
|
|
1095
|
+
(curValue[0] === 'acRead' ||
|
|
1096
|
+
curValue[0] === 'acWrite' ||
|
|
1097
|
+
curValue[0] === 'acFull')) {
|
|
1098
|
+
if (curQuery) {
|
|
1099
|
+
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1100
|
+
}
|
|
1101
|
+
const value = `param${++count.i}`;
|
|
1102
|
+
curQuery +=
|
|
1103
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1104
|
+
'decode(@' +
|
|
1105
|
+
value +
|
|
1106
|
+
", 'hex')=ANY(" +
|
|
1107
|
+
ieTable +
|
|
1108
|
+
'.' +
|
|
1109
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1110
|
+
')';
|
|
1111
|
+
params[value] = `${curValue[1]}`;
|
|
711
1112
|
}
|
|
712
1113
|
else {
|
|
713
1114
|
if (curQuery) {
|
|
714
1115
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
715
1116
|
}
|
|
716
1117
|
let svalue;
|
|
717
|
-
let stringValue;
|
|
718
1118
|
if (curValue[1] instanceof Object &&
|
|
719
1119
|
typeof curValue[1].toReference === 'function') {
|
|
720
1120
|
svalue = JSON.stringify(curValue[1].toReference());
|
|
721
|
-
stringValue = `${curValue[1].toReference()}`;
|
|
722
1121
|
}
|
|
723
|
-
else
|
|
1122
|
+
else if (typeof curValue[1] === 'string' ||
|
|
1123
|
+
typeof curValue[1] === 'number') {
|
|
724
1124
|
svalue = JSON.stringify(curValue[1]);
|
|
725
|
-
|
|
1125
|
+
}
|
|
1126
|
+
else {
|
|
1127
|
+
svalue = JSON.stringify([curValue[1]]);
|
|
726
1128
|
}
|
|
727
1129
|
const name = `param${++count.i}`;
|
|
728
1130
|
const value = `param${++count.i}`;
|
|
729
|
-
|
|
730
|
-
|
|
1131
|
+
curQuery +=
|
|
1132
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1133
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1134
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1135
|
+
' WHERE "guid"=' +
|
|
1136
|
+
ieTable +
|
|
1137
|
+
'."guid" AND "name"=@' +
|
|
1138
|
+
name +
|
|
1139
|
+
' AND "json" @> @' +
|
|
1140
|
+
value +
|
|
1141
|
+
')';
|
|
1142
|
+
params[name] = curValue[0];
|
|
1143
|
+
params[value] = PostgreSQLDriver.escapeNullSequences(svalue);
|
|
1144
|
+
}
|
|
1145
|
+
break;
|
|
1146
|
+
case 'search':
|
|
1147
|
+
case '!search':
|
|
1148
|
+
if (curValue[0] === 'cdate' ||
|
|
1149
|
+
curValue[0] === 'mdate' ||
|
|
1150
|
+
(this.nymph.tilmeld != null &&
|
|
1151
|
+
(curValue[0] === 'user' ||
|
|
1152
|
+
curValue[0] === 'group' ||
|
|
1153
|
+
curValue[0] === 'acUser' ||
|
|
1154
|
+
curValue[0] === 'acGroup' ||
|
|
1155
|
+
curValue[0] === 'acOther' ||
|
|
1156
|
+
curValue[0] === 'acRead' ||
|
|
1157
|
+
curValue[0] === 'acWrite' ||
|
|
1158
|
+
curValue[0] === 'acFull'))) {
|
|
1159
|
+
if (curQuery) {
|
|
1160
|
+
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1161
|
+
}
|
|
1162
|
+
curQuery +=
|
|
1163
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') + '(FALSE)';
|
|
1164
|
+
}
|
|
1165
|
+
else {
|
|
1166
|
+
if (curQuery) {
|
|
1167
|
+
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1168
|
+
}
|
|
1169
|
+
const parsedFTSQuery = this.tokenizer.parseSearchQuery(curValue[1]);
|
|
1170
|
+
if (!parsedFTSQuery.length) {
|
|
731
1171
|
curQuery +=
|
|
732
|
-
(
|
|
733
|
-
|
|
1172
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') + '(FALSE)';
|
|
1173
|
+
}
|
|
1174
|
+
else {
|
|
1175
|
+
const name = `param${++count.i}`;
|
|
1176
|
+
const queryPartToken = (term) => {
|
|
1177
|
+
const value = `param${++count.i}`;
|
|
1178
|
+
params[value] = term.token;
|
|
1179
|
+
return ('EXISTS (SELECT "guid" FROM ' +
|
|
1180
|
+
PostgreSQLDriver.escape(this.prefix + 'tokens_' + etype) +
|
|
1181
|
+
' WHERE "guid"=' +
|
|
734
1182
|
ieTable +
|
|
735
|
-
'."guid"
|
|
736
|
-
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
737
|
-
' WHERE "name"=@' +
|
|
1183
|
+
'."guid" AND "name"=@' +
|
|
738
1184
|
name +
|
|
739
|
-
' AND
|
|
1185
|
+
' AND "token"=@' +
|
|
740
1186
|
value +
|
|
741
|
-
'
|
|
1187
|
+
(term.nostemmed ? ' AND "stem"=FALSE' : '') +
|
|
1188
|
+
')');
|
|
1189
|
+
};
|
|
1190
|
+
const queryPartSeries = (series) => {
|
|
1191
|
+
const tokenTableSuffix = makeTableSuffix();
|
|
1192
|
+
const tokenParts = series.tokens.map((token, i) => {
|
|
1193
|
+
const value = `param${++count.i}`;
|
|
1194
|
+
params[value] = token.token;
|
|
1195
|
+
return {
|
|
1196
|
+
fromClause: i === 0
|
|
1197
|
+
? 'FROM ' +
|
|
1198
|
+
PostgreSQLDriver.escape(this.prefix + 'tokens_' + etype) +
|
|
1199
|
+
' t' +
|
|
1200
|
+
tokenTableSuffix +
|
|
1201
|
+
'0'
|
|
1202
|
+
: 'JOIN ' +
|
|
1203
|
+
PostgreSQLDriver.escape(this.prefix + 'tokens_' + etype) +
|
|
1204
|
+
' t' +
|
|
1205
|
+
tokenTableSuffix +
|
|
1206
|
+
i +
|
|
1207
|
+
' ON t' +
|
|
1208
|
+
tokenTableSuffix +
|
|
1209
|
+
i +
|
|
1210
|
+
'."guid" = t' +
|
|
1211
|
+
tokenTableSuffix +
|
|
1212
|
+
'0."guid" AND t' +
|
|
1213
|
+
tokenTableSuffix +
|
|
1214
|
+
i +
|
|
1215
|
+
'."name" = t' +
|
|
1216
|
+
tokenTableSuffix +
|
|
1217
|
+
'0."name" AND t' +
|
|
1218
|
+
tokenTableSuffix +
|
|
1219
|
+
i +
|
|
1220
|
+
'."position" = t' +
|
|
1221
|
+
tokenTableSuffix +
|
|
1222
|
+
'0."position" + ' +
|
|
1223
|
+
i,
|
|
1224
|
+
whereClause: 't' +
|
|
1225
|
+
tokenTableSuffix +
|
|
1226
|
+
i +
|
|
1227
|
+
'."token"=@' +
|
|
1228
|
+
value +
|
|
1229
|
+
(token.nostemmed
|
|
1230
|
+
? ' AND t' + tokenTableSuffix + i + '."stem"=FALSE'
|
|
1231
|
+
: ''),
|
|
1232
|
+
};
|
|
1233
|
+
});
|
|
1234
|
+
return ('EXISTS (SELECT t' +
|
|
1235
|
+
tokenTableSuffix +
|
|
1236
|
+
'0."guid" ' +
|
|
1237
|
+
tokenParts.map((part) => part.fromClause).join(' ') +
|
|
1238
|
+
' WHERE t' +
|
|
1239
|
+
tokenTableSuffix +
|
|
1240
|
+
'0."guid"=' +
|
|
742
1241
|
ieTable +
|
|
743
|
-
'."guid"
|
|
744
|
-
|
|
745
|
-
'
|
|
1242
|
+
'."guid" AND t' +
|
|
1243
|
+
tokenTableSuffix +
|
|
1244
|
+
'0."name"=@' +
|
|
746
1245
|
name +
|
|
747
|
-
' AND
|
|
748
|
-
|
|
749
|
-
'))
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
1246
|
+
' AND ' +
|
|
1247
|
+
tokenParts.map((part) => part.whereClause).join(' AND ') +
|
|
1248
|
+
')');
|
|
1249
|
+
};
|
|
1250
|
+
const queryPartTerm = (term) => {
|
|
1251
|
+
if (term.type === 'series') {
|
|
1252
|
+
return queryPartSeries(term);
|
|
1253
|
+
}
|
|
1254
|
+
else if (term.type === 'not') {
|
|
1255
|
+
return 'NOT ' + queryPartTerm(term.operand);
|
|
1256
|
+
}
|
|
1257
|
+
else if (term.type === 'or') {
|
|
1258
|
+
let queryParts = [];
|
|
1259
|
+
for (let operand of term.operands) {
|
|
1260
|
+
queryParts.push(queryPartTerm(operand));
|
|
1261
|
+
}
|
|
1262
|
+
return '(' + queryParts.join(' OR ') + ')';
|
|
1263
|
+
}
|
|
1264
|
+
return queryPartToken(term);
|
|
1265
|
+
};
|
|
1266
|
+
// Run through the query and add terms.
|
|
1267
|
+
let termStrings = [];
|
|
1268
|
+
for (let term of parsedFTSQuery) {
|
|
1269
|
+
termStrings.push(queryPartTerm(term));
|
|
1270
|
+
}
|
|
753
1271
|
curQuery +=
|
|
754
|
-
(
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
name +
|
|
760
|
-
' AND position(@' +
|
|
761
|
-
value +
|
|
762
|
-
' IN "value")>0)';
|
|
1272
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1273
|
+
'(' +
|
|
1274
|
+
termStrings.join(' AND ') +
|
|
1275
|
+
')';
|
|
1276
|
+
params[name] = curValue[0];
|
|
763
1277
|
}
|
|
764
|
-
params[name] = curValue[0];
|
|
765
|
-
params[value] = svalue;
|
|
766
1278
|
}
|
|
767
1279
|
break;
|
|
768
1280
|
case 'match':
|
|
769
1281
|
case '!match':
|
|
770
|
-
if (curValue[0] === 'cdate'
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
'
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
')';
|
|
782
|
-
params[cdate] = curValue[1];
|
|
783
|
-
break;
|
|
784
|
-
}
|
|
785
|
-
else if (curValue[0] === 'mdate') {
|
|
1282
|
+
if (curValue[0] === 'cdate' ||
|
|
1283
|
+
curValue[0] === 'mdate' ||
|
|
1284
|
+
(this.nymph.tilmeld != null &&
|
|
1285
|
+
(curValue[0] === 'user' ||
|
|
1286
|
+
curValue[0] === 'group' ||
|
|
1287
|
+
curValue[0] === 'acUser' ||
|
|
1288
|
+
curValue[0] === 'acGroup' ||
|
|
1289
|
+
curValue[0] === 'acOther' ||
|
|
1290
|
+
curValue[0] === 'acRead' ||
|
|
1291
|
+
curValue[0] === 'acWrite' ||
|
|
1292
|
+
curValue[0] === 'acFull'))) {
|
|
786
1293
|
if (curQuery) {
|
|
787
1294
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
788
1295
|
}
|
|
789
|
-
const
|
|
1296
|
+
const value = `param${++count.i}`;
|
|
790
1297
|
curQuery +=
|
|
791
|
-
(
|
|
1298
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
792
1299
|
'(' +
|
|
793
1300
|
ieTable +
|
|
794
|
-
'.
|
|
795
|
-
|
|
1301
|
+
'.' +
|
|
1302
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1303
|
+
' ~ @' +
|
|
1304
|
+
value +
|
|
796
1305
|
')';
|
|
797
|
-
params[
|
|
798
|
-
break;
|
|
1306
|
+
params[value] = curValue[1];
|
|
799
1307
|
}
|
|
800
1308
|
else {
|
|
801
1309
|
if (curQuery) {
|
|
@@ -804,50 +1312,47 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
804
1312
|
const name = `param${++count.i}`;
|
|
805
1313
|
const value = `param${++count.i}`;
|
|
806
1314
|
curQuery +=
|
|
807
|
-
(
|
|
1315
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1316
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1317
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1318
|
+
' WHERE "guid"=' +
|
|
808
1319
|
ieTable +
|
|
809
|
-
'."guid"
|
|
810
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
811
|
-
' WHERE "name"=@' +
|
|
1320
|
+
'."guid" AND "name"=@' +
|
|
812
1321
|
name +
|
|
813
1322
|
' AND "string" ~ @' +
|
|
814
1323
|
value +
|
|
815
1324
|
')';
|
|
816
1325
|
params[name] = curValue[0];
|
|
817
|
-
params[value] = curValue[1];
|
|
1326
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
818
1327
|
}
|
|
819
1328
|
break;
|
|
820
1329
|
case 'imatch':
|
|
821
1330
|
case '!imatch':
|
|
822
|
-
if (curValue[0] === 'cdate'
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
'
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
')';
|
|
834
|
-
params[cdate] = curValue[1];
|
|
835
|
-
break;
|
|
836
|
-
}
|
|
837
|
-
else if (curValue[0] === 'mdate') {
|
|
1331
|
+
if (curValue[0] === 'cdate' ||
|
|
1332
|
+
curValue[0] === 'mdate' ||
|
|
1333
|
+
(this.nymph.tilmeld != null &&
|
|
1334
|
+
(curValue[0] === 'user' ||
|
|
1335
|
+
curValue[0] === 'group' ||
|
|
1336
|
+
curValue[0] === 'acUser' ||
|
|
1337
|
+
curValue[0] === 'acGroup' ||
|
|
1338
|
+
curValue[0] === 'acOther' ||
|
|
1339
|
+
curValue[0] === 'acRead' ||
|
|
1340
|
+
curValue[0] === 'acWrite' ||
|
|
1341
|
+
curValue[0] === 'acFull'))) {
|
|
838
1342
|
if (curQuery) {
|
|
839
1343
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
840
1344
|
}
|
|
841
|
-
const
|
|
1345
|
+
const value = `param${++count.i}`;
|
|
842
1346
|
curQuery +=
|
|
843
|
-
(
|
|
1347
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
844
1348
|
'(' +
|
|
845
1349
|
ieTable +
|
|
846
|
-
'.
|
|
847
|
-
|
|
1350
|
+
'.' +
|
|
1351
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1352
|
+
' ~* @' +
|
|
1353
|
+
value +
|
|
848
1354
|
')';
|
|
849
|
-
params[
|
|
850
|
-
break;
|
|
1355
|
+
params[value] = curValue[1];
|
|
851
1356
|
}
|
|
852
1357
|
else {
|
|
853
1358
|
if (curQuery) {
|
|
@@ -856,50 +1361,47 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
856
1361
|
const name = `param${++count.i}`;
|
|
857
1362
|
const value = `param${++count.i}`;
|
|
858
1363
|
curQuery +=
|
|
859
|
-
(
|
|
1364
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1365
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1366
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1367
|
+
' WHERE "guid"=' +
|
|
860
1368
|
ieTable +
|
|
861
|
-
'."guid"
|
|
862
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
863
|
-
' WHERE "name"=@' +
|
|
1369
|
+
'."guid" AND "name"=@' +
|
|
864
1370
|
name +
|
|
865
1371
|
' AND "string" ~* @' +
|
|
866
1372
|
value +
|
|
867
1373
|
')';
|
|
868
1374
|
params[name] = curValue[0];
|
|
869
|
-
params[value] = curValue[1];
|
|
1375
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
870
1376
|
}
|
|
871
1377
|
break;
|
|
872
1378
|
case 'like':
|
|
873
1379
|
case '!like':
|
|
874
|
-
if (curValue[0] === 'cdate'
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
'
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
')';
|
|
886
|
-
params[cdate] = curValue[1];
|
|
887
|
-
break;
|
|
888
|
-
}
|
|
889
|
-
else if (curValue[0] === 'mdate') {
|
|
1380
|
+
if (curValue[0] === 'cdate' ||
|
|
1381
|
+
curValue[0] === 'mdate' ||
|
|
1382
|
+
(this.nymph.tilmeld != null &&
|
|
1383
|
+
(curValue[0] === 'user' ||
|
|
1384
|
+
curValue[0] === 'group' ||
|
|
1385
|
+
curValue[0] === 'acUser' ||
|
|
1386
|
+
curValue[0] === 'acGroup' ||
|
|
1387
|
+
curValue[0] === 'acOther' ||
|
|
1388
|
+
curValue[0] === 'acRead' ||
|
|
1389
|
+
curValue[0] === 'acWrite' ||
|
|
1390
|
+
curValue[0] === 'acFull'))) {
|
|
890
1391
|
if (curQuery) {
|
|
891
1392
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
892
1393
|
}
|
|
893
|
-
const
|
|
1394
|
+
const value = `param${++count.i}`;
|
|
894
1395
|
curQuery +=
|
|
895
|
-
(
|
|
1396
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
896
1397
|
'(' +
|
|
897
1398
|
ieTable +
|
|
898
|
-
'.
|
|
899
|
-
|
|
1399
|
+
'.' +
|
|
1400
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1401
|
+
' LIKE @' +
|
|
1402
|
+
value +
|
|
900
1403
|
')';
|
|
901
|
-
params[
|
|
902
|
-
break;
|
|
1404
|
+
params[value] = curValue[1];
|
|
903
1405
|
}
|
|
904
1406
|
else {
|
|
905
1407
|
if (curQuery) {
|
|
@@ -908,50 +1410,47 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
908
1410
|
const name = `param${++count.i}`;
|
|
909
1411
|
const value = `param${++count.i}`;
|
|
910
1412
|
curQuery +=
|
|
911
|
-
(
|
|
1413
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1414
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1415
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1416
|
+
' WHERE "guid"=' +
|
|
912
1417
|
ieTable +
|
|
913
|
-
'."guid"
|
|
914
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
915
|
-
' WHERE "name"=@' +
|
|
1418
|
+
'."guid" AND "name"=@' +
|
|
916
1419
|
name +
|
|
917
1420
|
' AND "string" LIKE @' +
|
|
918
1421
|
value +
|
|
919
1422
|
')';
|
|
920
1423
|
params[name] = curValue[0];
|
|
921
|
-
params[value] = curValue[1];
|
|
1424
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
922
1425
|
}
|
|
923
1426
|
break;
|
|
924
1427
|
case 'ilike':
|
|
925
1428
|
case '!ilike':
|
|
926
|
-
if (curValue[0] === 'cdate'
|
|
1429
|
+
if (curValue[0] === 'cdate' ||
|
|
1430
|
+
curValue[0] === 'mdate' ||
|
|
1431
|
+
(this.nymph.tilmeld != null &&
|
|
1432
|
+
(curValue[0] === 'user' ||
|
|
1433
|
+
curValue[0] === 'group' ||
|
|
1434
|
+
curValue[0] === 'acUser' ||
|
|
1435
|
+
curValue[0] === 'acGroup' ||
|
|
1436
|
+
curValue[0] === 'acOther' ||
|
|
1437
|
+
curValue[0] === 'acRead' ||
|
|
1438
|
+
curValue[0] === 'acWrite' ||
|
|
1439
|
+
curValue[0] === 'acFull'))) {
|
|
927
1440
|
if (curQuery) {
|
|
928
1441
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
929
1442
|
}
|
|
930
|
-
const
|
|
931
|
-
curQuery +=
|
|
932
|
-
((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
933
|
-
'(' +
|
|
934
|
-
ieTable +
|
|
935
|
-
'."cdate" ILIKE @' +
|
|
936
|
-
cdate +
|
|
937
|
-
')';
|
|
938
|
-
params[cdate] = curValue[1];
|
|
939
|
-
break;
|
|
940
|
-
}
|
|
941
|
-
else if (curValue[0] === 'mdate') {
|
|
942
|
-
if (curQuery) {
|
|
943
|
-
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
944
|
-
}
|
|
945
|
-
const mdate = `param${++count.i}`;
|
|
1443
|
+
const value = `param${++count.i}`;
|
|
946
1444
|
curQuery +=
|
|
947
|
-
(
|
|
1445
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
948
1446
|
'(' +
|
|
949
1447
|
ieTable +
|
|
950
|
-
'.
|
|
951
|
-
|
|
1448
|
+
'.' +
|
|
1449
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1450
|
+
' ILIKE @' +
|
|
1451
|
+
value +
|
|
952
1452
|
')';
|
|
953
|
-
params[
|
|
954
|
-
break;
|
|
1453
|
+
params[value] = curValue[1];
|
|
955
1454
|
}
|
|
956
1455
|
else {
|
|
957
1456
|
if (curQuery) {
|
|
@@ -960,50 +1459,47 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
960
1459
|
const name = `param${++count.i}`;
|
|
961
1460
|
const value = `param${++count.i}`;
|
|
962
1461
|
curQuery +=
|
|
963
|
-
(
|
|
1462
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1463
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1464
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1465
|
+
' WHERE "guid"=' +
|
|
964
1466
|
ieTable +
|
|
965
|
-
'."guid"
|
|
966
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
967
|
-
' WHERE "name"=@' +
|
|
1467
|
+
'."guid" AND "name"=@' +
|
|
968
1468
|
name +
|
|
969
1469
|
' AND "string" ILIKE @' +
|
|
970
1470
|
value +
|
|
971
1471
|
')';
|
|
972
1472
|
params[name] = curValue[0];
|
|
973
|
-
params[value] = curValue[1];
|
|
1473
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
974
1474
|
}
|
|
975
1475
|
break;
|
|
976
1476
|
case 'gt':
|
|
977
1477
|
case '!gt':
|
|
978
|
-
if (curValue[0] === 'cdate'
|
|
1478
|
+
if (curValue[0] === 'cdate' ||
|
|
1479
|
+
curValue[0] === 'mdate' ||
|
|
1480
|
+
(this.nymph.tilmeld != null &&
|
|
1481
|
+
(curValue[0] === 'user' ||
|
|
1482
|
+
curValue[0] === 'group' ||
|
|
1483
|
+
curValue[0] === 'acUser' ||
|
|
1484
|
+
curValue[0] === 'acGroup' ||
|
|
1485
|
+
curValue[0] === 'acOther' ||
|
|
1486
|
+
curValue[0] === 'acRead' ||
|
|
1487
|
+
curValue[0] === 'acWrite' ||
|
|
1488
|
+
curValue[0] === 'acFull'))) {
|
|
979
1489
|
if (curQuery) {
|
|
980
1490
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
981
1491
|
}
|
|
982
|
-
const
|
|
983
|
-
curQuery +=
|
|
984
|
-
((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
985
|
-
ieTable +
|
|
986
|
-
'."cdate">@' +
|
|
987
|
-
cdate;
|
|
988
|
-
params[cdate] = isNaN(Number(curValue[1]))
|
|
989
|
-
? null
|
|
990
|
-
: Number(curValue[1]);
|
|
991
|
-
break;
|
|
992
|
-
}
|
|
993
|
-
else if (curValue[0] === 'mdate') {
|
|
994
|
-
if (curQuery) {
|
|
995
|
-
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
996
|
-
}
|
|
997
|
-
const mdate = `param${++count.i}`;
|
|
1492
|
+
const value = `param${++count.i}`;
|
|
998
1493
|
curQuery +=
|
|
999
|
-
(
|
|
1494
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1000
1495
|
ieTable +
|
|
1001
|
-
'.
|
|
1002
|
-
|
|
1003
|
-
|
|
1496
|
+
'.' +
|
|
1497
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1498
|
+
'>@' +
|
|
1499
|
+
value;
|
|
1500
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
1004
1501
|
? null
|
|
1005
1502
|
: Number(curValue[1]);
|
|
1006
|
-
break;
|
|
1007
1503
|
}
|
|
1008
1504
|
else {
|
|
1009
1505
|
if (curQuery) {
|
|
@@ -1012,11 +1508,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1012
1508
|
const name = `param${++count.i}`;
|
|
1013
1509
|
const value = `param${++count.i}`;
|
|
1014
1510
|
curQuery +=
|
|
1015
|
-
(
|
|
1511
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1512
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1513
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1514
|
+
' WHERE "guid"=' +
|
|
1016
1515
|
ieTable +
|
|
1017
|
-
'."guid"
|
|
1018
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
1019
|
-
' WHERE "name"=@' +
|
|
1516
|
+
'."guid" AND "name"=@' +
|
|
1020
1517
|
name +
|
|
1021
1518
|
' AND "number">@' +
|
|
1022
1519
|
value +
|
|
@@ -1029,35 +1526,31 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1029
1526
|
break;
|
|
1030
1527
|
case 'gte':
|
|
1031
1528
|
case '!gte':
|
|
1032
|
-
if (curValue[0] === 'cdate'
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
'
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
? null
|
|
1044
|
-
: Number(curValue[1]);
|
|
1045
|
-
break;
|
|
1046
|
-
}
|
|
1047
|
-
else if (curValue[0] === 'mdate') {
|
|
1529
|
+
if (curValue[0] === 'cdate' ||
|
|
1530
|
+
curValue[0] === 'mdate' ||
|
|
1531
|
+
(this.nymph.tilmeld != null &&
|
|
1532
|
+
(curValue[0] === 'user' ||
|
|
1533
|
+
curValue[0] === 'group' ||
|
|
1534
|
+
curValue[0] === 'acUser' ||
|
|
1535
|
+
curValue[0] === 'acGroup' ||
|
|
1536
|
+
curValue[0] === 'acOther' ||
|
|
1537
|
+
curValue[0] === 'acRead' ||
|
|
1538
|
+
curValue[0] === 'acWrite' ||
|
|
1539
|
+
curValue[0] === 'acFull'))) {
|
|
1048
1540
|
if (curQuery) {
|
|
1049
1541
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1050
1542
|
}
|
|
1051
|
-
const
|
|
1543
|
+
const value = `param${++count.i}`;
|
|
1052
1544
|
curQuery +=
|
|
1053
|
-
(
|
|
1545
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1054
1546
|
ieTable +
|
|
1055
|
-
'.
|
|
1056
|
-
|
|
1057
|
-
|
|
1547
|
+
'.' +
|
|
1548
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1549
|
+
'>=@' +
|
|
1550
|
+
value;
|
|
1551
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
1058
1552
|
? null
|
|
1059
1553
|
: Number(curValue[1]);
|
|
1060
|
-
break;
|
|
1061
1554
|
}
|
|
1062
1555
|
else {
|
|
1063
1556
|
if (curQuery) {
|
|
@@ -1066,11 +1559,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1066
1559
|
const name = `param${++count.i}`;
|
|
1067
1560
|
const value = `param${++count.i}`;
|
|
1068
1561
|
curQuery +=
|
|
1069
|
-
(
|
|
1562
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1563
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1564
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1565
|
+
' WHERE "guid"=' +
|
|
1070
1566
|
ieTable +
|
|
1071
|
-
'."guid"
|
|
1072
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
1073
|
-
' WHERE "name"=@' +
|
|
1567
|
+
'."guid" AND "name"=@' +
|
|
1074
1568
|
name +
|
|
1075
1569
|
' AND "number">=@' +
|
|
1076
1570
|
value +
|
|
@@ -1083,35 +1577,31 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1083
1577
|
break;
|
|
1084
1578
|
case 'lt':
|
|
1085
1579
|
case '!lt':
|
|
1086
|
-
if (curValue[0] === 'cdate'
|
|
1580
|
+
if (curValue[0] === 'cdate' ||
|
|
1581
|
+
curValue[0] === 'mdate' ||
|
|
1582
|
+
(this.nymph.tilmeld != null &&
|
|
1583
|
+
(curValue[0] === 'user' ||
|
|
1584
|
+
curValue[0] === 'group' ||
|
|
1585
|
+
curValue[0] === 'acUser' ||
|
|
1586
|
+
curValue[0] === 'acGroup' ||
|
|
1587
|
+
curValue[0] === 'acOther' ||
|
|
1588
|
+
curValue[0] === 'acRead' ||
|
|
1589
|
+
curValue[0] === 'acWrite' ||
|
|
1590
|
+
curValue[0] === 'acFull'))) {
|
|
1087
1591
|
if (curQuery) {
|
|
1088
1592
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1089
1593
|
}
|
|
1090
|
-
const
|
|
1091
|
-
curQuery +=
|
|
1092
|
-
((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1093
|
-
ieTable +
|
|
1094
|
-
'."cdate"<@' +
|
|
1095
|
-
cdate;
|
|
1096
|
-
params[cdate] = isNaN(Number(curValue[1]))
|
|
1097
|
-
? null
|
|
1098
|
-
: Number(curValue[1]);
|
|
1099
|
-
break;
|
|
1100
|
-
}
|
|
1101
|
-
else if (curValue[0] === 'mdate') {
|
|
1102
|
-
if (curQuery) {
|
|
1103
|
-
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1104
|
-
}
|
|
1105
|
-
const mdate = `param${++count.i}`;
|
|
1594
|
+
const value = `param${++count.i}`;
|
|
1106
1595
|
curQuery +=
|
|
1107
|
-
(
|
|
1596
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1108
1597
|
ieTable +
|
|
1109
|
-
'.
|
|
1110
|
-
|
|
1111
|
-
|
|
1598
|
+
'.' +
|
|
1599
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1600
|
+
'<@' +
|
|
1601
|
+
value;
|
|
1602
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
1112
1603
|
? null
|
|
1113
1604
|
: Number(curValue[1]);
|
|
1114
|
-
break;
|
|
1115
1605
|
}
|
|
1116
1606
|
else {
|
|
1117
1607
|
if (curQuery) {
|
|
@@ -1120,11 +1610,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1120
1610
|
const name = `param${++count.i}`;
|
|
1121
1611
|
const value = `param${++count.i}`;
|
|
1122
1612
|
curQuery +=
|
|
1123
|
-
(
|
|
1613
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1614
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1615
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1616
|
+
' WHERE "guid"=' +
|
|
1124
1617
|
ieTable +
|
|
1125
|
-
'."guid"
|
|
1126
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
1127
|
-
' WHERE "name"=@' +
|
|
1618
|
+
'."guid" AND "name"=@' +
|
|
1128
1619
|
name +
|
|
1129
1620
|
' AND "number"<@' +
|
|
1130
1621
|
value +
|
|
@@ -1137,35 +1628,31 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1137
1628
|
break;
|
|
1138
1629
|
case 'lte':
|
|
1139
1630
|
case '!lte':
|
|
1140
|
-
if (curValue[0] === 'cdate'
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
'
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
? null
|
|
1152
|
-
: Number(curValue[1]);
|
|
1153
|
-
break;
|
|
1154
|
-
}
|
|
1155
|
-
else if (curValue[0] === 'mdate') {
|
|
1631
|
+
if (curValue[0] === 'cdate' ||
|
|
1632
|
+
curValue[0] === 'mdate' ||
|
|
1633
|
+
(this.nymph.tilmeld != null &&
|
|
1634
|
+
(curValue[0] === 'user' ||
|
|
1635
|
+
curValue[0] === 'group' ||
|
|
1636
|
+
curValue[0] === 'acUser' ||
|
|
1637
|
+
curValue[0] === 'acGroup' ||
|
|
1638
|
+
curValue[0] === 'acOther' ||
|
|
1639
|
+
curValue[0] === 'acRead' ||
|
|
1640
|
+
curValue[0] === 'acWrite' ||
|
|
1641
|
+
curValue[0] === 'acFull'))) {
|
|
1156
1642
|
if (curQuery) {
|
|
1157
1643
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1158
1644
|
}
|
|
1159
|
-
const
|
|
1645
|
+
const value = `param${++count.i}`;
|
|
1160
1646
|
curQuery +=
|
|
1161
|
-
(
|
|
1647
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1162
1648
|
ieTable +
|
|
1163
|
-
'.
|
|
1164
|
-
|
|
1165
|
-
|
|
1649
|
+
'.' +
|
|
1650
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1651
|
+
'<=@' +
|
|
1652
|
+
value;
|
|
1653
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
1166
1654
|
? null
|
|
1167
1655
|
: Number(curValue[1]);
|
|
1168
|
-
break;
|
|
1169
1656
|
}
|
|
1170
1657
|
else {
|
|
1171
1658
|
if (curQuery) {
|
|
@@ -1174,11 +1661,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1174
1661
|
const name = `param${++count.i}`;
|
|
1175
1662
|
const value = `param${++count.i}`;
|
|
1176
1663
|
curQuery +=
|
|
1177
|
-
(
|
|
1664
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1665
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1666
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1667
|
+
' WHERE "guid"=' +
|
|
1178
1668
|
ieTable +
|
|
1179
|
-
'."guid"
|
|
1180
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
1181
|
-
' WHERE "name"=@' +
|
|
1669
|
+
'."guid" AND "name"=@' +
|
|
1182
1670
|
name +
|
|
1183
1671
|
' AND "number"<=@' +
|
|
1184
1672
|
value +
|
|
@@ -1204,71 +1692,190 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1204
1692
|
if (curQuery) {
|
|
1205
1693
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1206
1694
|
}
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1695
|
+
if (this.nymph.tilmeld != null &&
|
|
1696
|
+
(curValue[0] === 'user' || curValue[0] === 'group')) {
|
|
1697
|
+
const guid = `param${++count.i}`;
|
|
1698
|
+
curQuery +=
|
|
1699
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1700
|
+
ieTable +
|
|
1701
|
+
'.' +
|
|
1702
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1703
|
+
'=decode(@' +
|
|
1704
|
+
guid +
|
|
1705
|
+
", 'hex')";
|
|
1706
|
+
params[guid] = curQguid;
|
|
1707
|
+
}
|
|
1708
|
+
else if (this.nymph.tilmeld != null &&
|
|
1709
|
+
(curValue[0] === 'acRead' ||
|
|
1710
|
+
curValue[0] === 'acWrite' ||
|
|
1711
|
+
curValue[0] === 'acFull')) {
|
|
1712
|
+
const guid = `param${++count.i}`;
|
|
1713
|
+
curQuery +=
|
|
1714
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1715
|
+
'decode(@' +
|
|
1716
|
+
guid +
|
|
1717
|
+
", 'hex')=ANY(" +
|
|
1718
|
+
ieTable +
|
|
1719
|
+
'.' +
|
|
1720
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1721
|
+
')';
|
|
1722
|
+
params[guid] = curQguid;
|
|
1723
|
+
}
|
|
1724
|
+
else {
|
|
1725
|
+
const name = `param${++count.i}`;
|
|
1726
|
+
const guid = `param${++count.i}`;
|
|
1727
|
+
curQuery +=
|
|
1728
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1729
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1730
|
+
PostgreSQLDriver.escape(this.prefix + 'references_' + etype) +
|
|
1731
|
+
' WHERE "guid"=' +
|
|
1732
|
+
ieTable +
|
|
1733
|
+
'."guid" AND "name"=@' +
|
|
1734
|
+
name +
|
|
1735
|
+
' AND "reference"=decode(@' +
|
|
1736
|
+
guid +
|
|
1737
|
+
", 'hex'))";
|
|
1738
|
+
params[name] = curValue[0];
|
|
1739
|
+
params[guid] = curQguid;
|
|
1740
|
+
}
|
|
1221
1741
|
break;
|
|
1222
1742
|
case 'selector':
|
|
1223
1743
|
case '!selector':
|
|
1224
|
-
const
|
|
1744
|
+
const innerquery = this.makeEntityQuery({ ...options, sort: null, limit: undefined }, [curValue], etype, count, params, true, tableSuffix, etypes);
|
|
1225
1745
|
if (curQuery) {
|
|
1226
1746
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1227
1747
|
}
|
|
1228
1748
|
curQuery +=
|
|
1229
|
-
(
|
|
1749
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1230
1750
|
'(' +
|
|
1231
|
-
|
|
1751
|
+
innerquery.query +
|
|
1232
1752
|
')';
|
|
1233
1753
|
break;
|
|
1234
1754
|
case 'qref':
|
|
1235
1755
|
case '!qref':
|
|
1756
|
+
const referenceTableSuffix = makeTableSuffix();
|
|
1236
1757
|
const [qrefOptions, ...qrefSelectors] = curValue[1];
|
|
1237
1758
|
const QrefEntityClass = qrefOptions.class;
|
|
1238
1759
|
etypes.push(QrefEntityClass.ETYPE);
|
|
1239
|
-
const qrefQuery = this.makeEntityQuery({ ...qrefOptions, return: 'guid', class: QrefEntityClass }, qrefSelectors, QrefEntityClass.ETYPE, count, params, false, (0, guid_1.makeTableSuffix)(), etypes);
|
|
1240
1760
|
if (curQuery) {
|
|
1241
1761
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1242
1762
|
}
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1763
|
+
if (this.nymph.tilmeld != null &&
|
|
1764
|
+
(curValue[0] === 'user' || curValue[0] === 'group')) {
|
|
1765
|
+
const qrefQuery = this.makeEntityQuery({
|
|
1766
|
+
...qrefOptions,
|
|
1767
|
+
sort: qrefOptions.sort ?? null,
|
|
1768
|
+
return: 'guid',
|
|
1769
|
+
class: QrefEntityClass,
|
|
1770
|
+
}, qrefSelectors, QrefEntityClass.ETYPE, count, params, false, makeTableSuffix(), etypes, 'r' +
|
|
1771
|
+
referenceTableSuffix +
|
|
1772
|
+
'.' +
|
|
1773
|
+
PostgreSQLDriver.escape(curValue[0]));
|
|
1774
|
+
curQuery +=
|
|
1775
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1776
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1777
|
+
PostgreSQLDriver.escape(this.prefix + 'entities_' + etype) +
|
|
1778
|
+
' r' +
|
|
1779
|
+
referenceTableSuffix +
|
|
1780
|
+
' WHERE r' +
|
|
1781
|
+
referenceTableSuffix +
|
|
1782
|
+
'."guid"=' +
|
|
1783
|
+
ieTable +
|
|
1784
|
+
'."guid" AND EXISTS (' +
|
|
1785
|
+
qrefQuery.query +
|
|
1786
|
+
'))';
|
|
1787
|
+
}
|
|
1788
|
+
else if (this.nymph.tilmeld != null &&
|
|
1789
|
+
(curValue[0] === 'acRead' ||
|
|
1790
|
+
curValue[0] === 'acWrite' ||
|
|
1791
|
+
curValue[0] === 'acFull')) {
|
|
1792
|
+
const qrefQuery = this.makeEntityQuery({
|
|
1793
|
+
...qrefOptions,
|
|
1794
|
+
sort: qrefOptions.sort ?? null,
|
|
1795
|
+
return: 'guid',
|
|
1796
|
+
class: QrefEntityClass,
|
|
1797
|
+
}, qrefSelectors, QrefEntityClass.ETYPE, count, params, false, makeTableSuffix(), etypes, undefined, (guid) => guid +
|
|
1798
|
+
'=ANY(' +
|
|
1246
1799
|
ieTable +
|
|
1247
|
-
'.
|
|
1248
|
-
PostgreSQLDriver.escape(
|
|
1249
|
-
'
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1800
|
+
'.' +
|
|
1801
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1802
|
+
')');
|
|
1803
|
+
curQuery +=
|
|
1804
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1805
|
+
'EXISTS (' +
|
|
1806
|
+
qrefQuery.query +
|
|
1807
|
+
')';
|
|
1808
|
+
}
|
|
1809
|
+
else {
|
|
1810
|
+
const qrefQuery = this.makeEntityQuery({
|
|
1811
|
+
...qrefOptions,
|
|
1812
|
+
sort: qrefOptions.sort ?? null,
|
|
1813
|
+
return: 'guid',
|
|
1814
|
+
class: QrefEntityClass,
|
|
1815
|
+
}, qrefSelectors, QrefEntityClass.ETYPE, count, params, false, makeTableSuffix(), etypes, 'r' + referenceTableSuffix + '."reference"');
|
|
1816
|
+
const qrefName = `param${++count.i}`;
|
|
1817
|
+
curQuery +=
|
|
1818
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1819
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1820
|
+
PostgreSQLDriver.escape(this.prefix + 'references_' + etype) +
|
|
1821
|
+
' r' +
|
|
1822
|
+
referenceTableSuffix +
|
|
1823
|
+
' WHERE r' +
|
|
1824
|
+
referenceTableSuffix +
|
|
1825
|
+
'."guid"=' +
|
|
1826
|
+
ieTable +
|
|
1827
|
+
'."guid" AND r' +
|
|
1828
|
+
referenceTableSuffix +
|
|
1829
|
+
'."name"=@' +
|
|
1830
|
+
qrefName +
|
|
1831
|
+
' AND EXISTS (' +
|
|
1832
|
+
qrefQuery.query +
|
|
1833
|
+
'))';
|
|
1834
|
+
params[qrefName] = curValue[0];
|
|
1835
|
+
}
|
|
1255
1836
|
break;
|
|
1256
1837
|
}
|
|
1257
1838
|
}
|
|
1258
1839
|
return curQuery;
|
|
1259
1840
|
});
|
|
1260
1841
|
let sortBy;
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1842
|
+
let sortByInner;
|
|
1843
|
+
let sortJoin = '';
|
|
1844
|
+
let sortJoinInner = '';
|
|
1845
|
+
const order = options.reverse ? ' DESC' : '';
|
|
1846
|
+
if (sort == null) {
|
|
1847
|
+
sortBy = '';
|
|
1848
|
+
sortByInner = '';
|
|
1849
|
+
}
|
|
1850
|
+
else {
|
|
1851
|
+
switch (sort) {
|
|
1852
|
+
case 'mdate':
|
|
1853
|
+
sortBy = `ORDER BY ${eTable}."mdate"${order}`;
|
|
1854
|
+
sortByInner = `ORDER BY ${ieTable}."mdate"${order}`;
|
|
1855
|
+
break;
|
|
1856
|
+
case 'cdate':
|
|
1857
|
+
sortBy = `ORDER BY ${eTable}."cdate"${order}`;
|
|
1858
|
+
sortByInner = `ORDER BY ${ieTable}."cdate"${order}`;
|
|
1859
|
+
break;
|
|
1860
|
+
default:
|
|
1861
|
+
const name = `param${++count.i}`;
|
|
1862
|
+
sortJoin = `LEFT JOIN (
|
|
1863
|
+
SELECT "guid", "string", "number"
|
|
1864
|
+
FROM ${PostgreSQLDriver.escape(this.prefix + 'data_' + etype)}
|
|
1865
|
+
WHERE "name"=@${name}
|
|
1866
|
+
ORDER BY "number"${order}, "string"${order}
|
|
1867
|
+
) ${sTable} ON ${eTable}."guid"=${sTable}."guid"`;
|
|
1868
|
+
sortJoinInner = `LEFT JOIN (
|
|
1869
|
+
SELECT "guid", "string", "number"
|
|
1870
|
+
FROM ${PostgreSQLDriver.escape(this.prefix + 'data_' + etype)}
|
|
1871
|
+
WHERE "name"=@${name}
|
|
1872
|
+
ORDER BY "number"${order}, "string"${order}
|
|
1873
|
+
) ${sTable} ON ${ieTable}."guid"=${sTable}."guid"`;
|
|
1874
|
+
sortBy = `ORDER BY ${sTable}."number"${order}, ${sTable}."string"${order}`;
|
|
1875
|
+
sortByInner = sortBy;
|
|
1876
|
+
params[name] = sort;
|
|
1877
|
+
break;
|
|
1878
|
+
}
|
|
1272
1879
|
}
|
|
1273
1880
|
let query;
|
|
1274
1881
|
if (queryParts.length) {
|
|
@@ -1285,18 +1892,23 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1285
1892
|
offset = ` OFFSET ${Math.floor(isNaN(Number(options.offset)) ? 0 : Number(options.offset))}`;
|
|
1286
1893
|
}
|
|
1287
1894
|
const whereClause = queryParts.join(') AND (');
|
|
1895
|
+
const guidClause = guidExplicitSelector
|
|
1896
|
+
? `${guidExplicitSelector(`${ieTable}."guid"`)} AND `
|
|
1897
|
+
: guidSelector
|
|
1898
|
+
? `${ieTable}."guid"=${guidSelector} AND `
|
|
1899
|
+
: '';
|
|
1288
1900
|
if (options.return === 'count') {
|
|
1289
1901
|
if (limit || offset) {
|
|
1290
1902
|
query = `SELECT COUNT(${countTable}."guid") AS "count" FROM (
|
|
1291
|
-
SELECT
|
|
1903
|
+
SELECT ${ieTable}."guid" AS "guid"
|
|
1292
1904
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1293
|
-
WHERE (${whereClause})${limit}${offset}
|
|
1905
|
+
WHERE ${guidClause}(${whereClause})${limit}${offset}
|
|
1294
1906
|
) ${countTable}`;
|
|
1295
1907
|
}
|
|
1296
1908
|
else {
|
|
1297
1909
|
query = `SELECT COUNT(${ieTable}."guid") AS "count"
|
|
1298
1910
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1299
|
-
WHERE (${whereClause})`;
|
|
1911
|
+
WHERE ${guidClause}(${whereClause})`;
|
|
1300
1912
|
}
|
|
1301
1913
|
}
|
|
1302
1914
|
else if (options.return === 'guid') {
|
|
@@ -1305,8 +1917,9 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1305
1917
|
: `${ieTable}."guid"`;
|
|
1306
1918
|
query = `SELECT ${guidColumn} AS "guid"
|
|
1307
1919
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1308
|
-
|
|
1309
|
-
|
|
1920
|
+
${sortJoinInner}
|
|
1921
|
+
WHERE ${guidClause}(${whereClause})
|
|
1922
|
+
${sortByInner ? sortByInner + ', ' : 'ORDER BY '}${ieTable}."guid"${limit}${offset}`;
|
|
1310
1923
|
}
|
|
1311
1924
|
else {
|
|
1312
1925
|
query = `SELECT
|
|
@@ -1314,20 +1927,30 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1314
1927
|
${eTable}."tags",
|
|
1315
1928
|
${eTable}."cdate",
|
|
1316
1929
|
${eTable}."mdate",
|
|
1930
|
+
encode(${eTable}."user", 'hex') AS "user",
|
|
1931
|
+
encode(${eTable}."group", 'hex') AS "group",
|
|
1932
|
+
${eTable}."acUser",
|
|
1933
|
+
${eTable}."acGroup",
|
|
1934
|
+
${eTable}."acOther",
|
|
1935
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acRead") AS n) as "acRead",
|
|
1936
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acWrite") AS n) as "acWrite",
|
|
1937
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acFull") AS n) as "acFull",
|
|
1317
1938
|
${dTable}."name",
|
|
1318
1939
|
${dTable}."value",
|
|
1319
|
-
${
|
|
1320
|
-
${
|
|
1940
|
+
${dTable}."json",
|
|
1941
|
+
${dTable}."string",
|
|
1942
|
+
${dTable}."number"
|
|
1321
1943
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
|
|
1322
1944
|
LEFT JOIN ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
|
|
1323
|
-
|
|
1945
|
+
${sortJoin}
|
|
1324
1946
|
INNER JOIN (
|
|
1325
1947
|
SELECT ${ieTable}."guid"
|
|
1326
1948
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1327
|
-
|
|
1328
|
-
|
|
1949
|
+
${sortJoinInner}
|
|
1950
|
+
WHERE ${guidClause}(${whereClause})
|
|
1951
|
+
${sortByInner}${limit}${offset}
|
|
1329
1952
|
) ${fTable} ON ${eTable}."guid"=${fTable}."guid"
|
|
1330
|
-
ORDER BY ${eTable}
|
|
1953
|
+
${sortBy ? sortBy + ', ' : 'ORDER BY '}${eTable}."guid"`;
|
|
1331
1954
|
}
|
|
1332
1955
|
}
|
|
1333
1956
|
}
|
|
@@ -1344,16 +1967,21 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1344
1967
|
if ('offset' in options) {
|
|
1345
1968
|
offset = ` OFFSET ${Math.floor(isNaN(Number(options.offset)) ? 0 : Number(options.offset))}`;
|
|
1346
1969
|
}
|
|
1970
|
+
const guidClause = guidExplicitSelector
|
|
1971
|
+
? ` WHERE ${guidExplicitSelector(`${ieTable}."guid"`)}`
|
|
1972
|
+
: guidSelector
|
|
1973
|
+
? ` WHERE ${ieTable}."guid"=${guidSelector}`
|
|
1974
|
+
: '';
|
|
1347
1975
|
if (options.return === 'count') {
|
|
1348
1976
|
if (limit || offset) {
|
|
1349
1977
|
query = `SELECT COUNT(${countTable}."guid") AS "count" FROM (
|
|
1350
|
-
SELECT
|
|
1351
|
-
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}${limit}${offset}
|
|
1978
|
+
SELECT ${ieTable}."guid" AS "guid"
|
|
1979
|
+
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}${guidClause}${limit}${offset}
|
|
1352
1980
|
) ${countTable}`;
|
|
1353
1981
|
}
|
|
1354
1982
|
else {
|
|
1355
1983
|
query = `SELECT COUNT(${ieTable}."guid") AS "count"
|
|
1356
|
-
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}`;
|
|
1984
|
+
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}${guidClause}`;
|
|
1357
1985
|
}
|
|
1358
1986
|
}
|
|
1359
1987
|
else if (options.return === 'guid') {
|
|
@@ -1362,7 +1990,9 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1362
1990
|
: `${ieTable}."guid"`;
|
|
1363
1991
|
query = `SELECT ${guidColumn} AS "guid"
|
|
1364
1992
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1365
|
-
|
|
1993
|
+
${sortJoinInner}
|
|
1994
|
+
${guidClause}
|
|
1995
|
+
${sortByInner ? sortByInner + ', ' : 'ORDER BY '}${ieTable}."guid"${limit}${offset}`;
|
|
1366
1996
|
}
|
|
1367
1997
|
else {
|
|
1368
1998
|
if (limit || offset) {
|
|
@@ -1371,19 +2001,30 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1371
2001
|
${eTable}."tags",
|
|
1372
2002
|
${eTable}."cdate",
|
|
1373
2003
|
${eTable}."mdate",
|
|
2004
|
+
encode(${eTable}."user", 'hex') AS "user",
|
|
2005
|
+
encode(${eTable}."group", 'hex') AS "group",
|
|
2006
|
+
${eTable}."acUser",
|
|
2007
|
+
${eTable}."acGroup",
|
|
2008
|
+
${eTable}."acOther",
|
|
2009
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acRead") AS n) as "acRead",
|
|
2010
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acWrite") AS n) as "acWrite",
|
|
2011
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acFull") AS n) as "acFull",
|
|
1374
2012
|
${dTable}."name",
|
|
1375
2013
|
${dTable}."value",
|
|
1376
|
-
${
|
|
1377
|
-
${
|
|
2014
|
+
${dTable}."json",
|
|
2015
|
+
${dTable}."string",
|
|
2016
|
+
${dTable}."number"
|
|
1378
2017
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
|
|
1379
2018
|
LEFT JOIN ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
|
|
1380
|
-
|
|
2019
|
+
${sortJoin}
|
|
1381
2020
|
INNER JOIN (
|
|
1382
2021
|
SELECT ${ieTable}."guid"
|
|
1383
2022
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1384
|
-
|
|
2023
|
+
${sortJoinInner}
|
|
2024
|
+
${guidClause}
|
|
2025
|
+
${sortByInner}${limit}${offset}
|
|
1385
2026
|
) ${fTable} ON ${eTable}."guid"=${fTable}."guid"
|
|
1386
|
-
ORDER BY ${eTable}
|
|
2027
|
+
${sortBy ? sortBy + ', ' : 'ORDER BY '}${eTable}."guid"`;
|
|
1387
2028
|
}
|
|
1388
2029
|
else {
|
|
1389
2030
|
query = `SELECT
|
|
@@ -1391,14 +2032,24 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1391
2032
|
${eTable}."tags",
|
|
1392
2033
|
${eTable}."cdate",
|
|
1393
2034
|
${eTable}."mdate",
|
|
2035
|
+
encode(${eTable}."user", 'hex') AS "user",
|
|
2036
|
+
encode(${eTable}."group", 'hex') AS "group",
|
|
2037
|
+
${eTable}."acUser",
|
|
2038
|
+
${eTable}."acGroup",
|
|
2039
|
+
${eTable}."acOther",
|
|
2040
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acRead") AS n) as "acRead",
|
|
2041
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acWrite") AS n) as "acWrite",
|
|
2042
|
+
array(SELECT encode(n, 'hex') FROM unnest(${eTable}."acFull") AS n) as "acFull",
|
|
1394
2043
|
${dTable}."name",
|
|
1395
2044
|
${dTable}."value",
|
|
1396
|
-
${
|
|
1397
|
-
${
|
|
2045
|
+
${dTable}."json",
|
|
2046
|
+
${dTable}."string",
|
|
2047
|
+
${dTable}."number"
|
|
1398
2048
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
|
|
1399
2049
|
LEFT JOIN ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
|
|
1400
|
-
|
|
1401
|
-
|
|
2050
|
+
${sortJoin}
|
|
2051
|
+
${guidExplicitSelector ? `WHERE ${guidExplicitSelector(`${eTable}."guid"`)}` : guidSelector ? `WHERE ${eTable}."guid"=${guidSelector}` : ''}
|
|
2052
|
+
${sortBy ? sortBy + ', ' : 'ORDER BY '}${eTable}."guid"`;
|
|
1402
2053
|
}
|
|
1403
2054
|
}
|
|
1404
2055
|
}
|
|
@@ -1414,33 +2065,38 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1414
2065
|
}
|
|
1415
2066
|
performQuery(options, formattedSelectors, etype) {
|
|
1416
2067
|
const { query, params, etypes } = this.makeEntityQuery(options, formattedSelectors, etype);
|
|
1417
|
-
const result = this.
|
|
1418
|
-
return {
|
|
1419
|
-
result,
|
|
1420
|
-
};
|
|
1421
|
-
}
|
|
1422
|
-
performQuerySync(options, formattedSelectors, etype) {
|
|
1423
|
-
const { query, params, etypes } = this.makeEntityQuery(options, formattedSelectors, etype);
|
|
1424
|
-
const result = (this.queryIterSync(query, { etypes, params }) || [])[Symbol.iterator]();
|
|
2068
|
+
const result = this.queryArray(query, { etypes, params }).then((val) => val[Symbol.iterator]());
|
|
1425
2069
|
return {
|
|
1426
2070
|
result,
|
|
1427
2071
|
};
|
|
1428
2072
|
}
|
|
1429
2073
|
async getEntities(options = {}, ...selectors) {
|
|
1430
|
-
const { result: resultPromise, process } = this.
|
|
2074
|
+
const { result: resultPromise, process } = this.getEntitiesRowLike(
|
|
2075
|
+
// @ts-ignore: options is correct here.
|
|
2076
|
+
options, selectors, ({ options, selectors, etype }) => this.performQuery(options, selectors, etype), () => {
|
|
1431
2077
|
const next = result.next();
|
|
1432
2078
|
return next.done ? null : next.value;
|
|
1433
2079
|
}, () => undefined, (row) => Number(row.count), (row) => row.guid, (row) => ({
|
|
1434
|
-
tags: row.tags,
|
|
1435
|
-
cdate: isNaN(Number(row.cdate)) ?
|
|
1436
|
-
mdate: isNaN(Number(row.mdate)) ?
|
|
2080
|
+
tags: row.tags.filter((tag) => tag),
|
|
2081
|
+
cdate: isNaN(Number(row.cdate)) ? Date.now() : Number(row.cdate),
|
|
2082
|
+
mdate: isNaN(Number(row.mdate)) ? Date.now() : Number(row.mdate),
|
|
2083
|
+
user: row.user,
|
|
2084
|
+
group: row.group,
|
|
2085
|
+
acUser: row.acUser,
|
|
2086
|
+
acGroup: row.acGroup,
|
|
2087
|
+
acOther: row.acOther,
|
|
2088
|
+
acRead: row.acRead?.filter((guid) => guid) ?? [],
|
|
2089
|
+
acWrite: row.acWrite?.filter((guid) => guid) ?? [],
|
|
2090
|
+
acFull: row.acFull?.filter((guid) => guid) ?? [],
|
|
1437
2091
|
}), (row) => ({
|
|
1438
2092
|
name: row.name,
|
|
1439
2093
|
svalue: row.value === 'N'
|
|
1440
2094
|
? JSON.stringify(Number(row.number))
|
|
1441
2095
|
: row.value === 'S'
|
|
1442
|
-
? JSON.stringify(row.string)
|
|
1443
|
-
: row.value
|
|
2096
|
+
? JSON.stringify(PostgreSQLDriver.unescapeNulls(row.string))
|
|
2097
|
+
: row.value === 'J'
|
|
2098
|
+
? PostgreSQLDriver.unescapeNullSequences(JSON.stringify(row.json))
|
|
2099
|
+
: row.value,
|
|
1444
2100
|
}));
|
|
1445
2101
|
const result = await resultPromise;
|
|
1446
2102
|
const value = process();
|
|
@@ -1449,31 +2105,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1449
2105
|
}
|
|
1450
2106
|
return value;
|
|
1451
2107
|
}
|
|
1452
|
-
getEntitiesSync(options = {}, ...selectors) {
|
|
1453
|
-
const { result, process } = this.getEntitesRowLike(options, selectors, (options, formattedSelectors, etype) => this.performQuerySync(options, formattedSelectors, etype), () => {
|
|
1454
|
-
const next = result.next();
|
|
1455
|
-
return next.done ? null : next.value;
|
|
1456
|
-
}, () => undefined, (row) => Number(row.count), (row) => row.guid, (row) => ({
|
|
1457
|
-
tags: row.tags,
|
|
1458
|
-
cdate: isNaN(Number(row.cdate)) ? null : Number(row.cdate),
|
|
1459
|
-
mdate: isNaN(Number(row.mdate)) ? null : Number(row.mdate),
|
|
1460
|
-
}), (row) => ({
|
|
1461
|
-
name: row.name,
|
|
1462
|
-
svalue: row.value === 'N'
|
|
1463
|
-
? JSON.stringify(Number(row.number))
|
|
1464
|
-
: row.value === 'S'
|
|
1465
|
-
? JSON.stringify(row.string)
|
|
1466
|
-
: row.value,
|
|
1467
|
-
}));
|
|
1468
|
-
const value = process();
|
|
1469
|
-
if (value instanceof Error) {
|
|
1470
|
-
throw value;
|
|
1471
|
-
}
|
|
1472
|
-
return value;
|
|
1473
|
-
}
|
|
1474
2108
|
async getUID(name) {
|
|
1475
2109
|
if (name == null) {
|
|
1476
|
-
throw new
|
|
2110
|
+
throw new InvalidParametersError('Name not given for UID.');
|
|
2111
|
+
}
|
|
2112
|
+
if (name.length > 128) {
|
|
2113
|
+
throw new InvalidParametersError('UID name too long. 128 chars max.');
|
|
1477
2114
|
}
|
|
1478
2115
|
const result = await this.queryGet(`SELECT "cur_uid" FROM ${PostgreSQLDriver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
|
|
1479
2116
|
params: {
|
|
@@ -1482,51 +2119,81 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1482
2119
|
});
|
|
1483
2120
|
return result?.cur_uid == null ? null : Number(result.cur_uid);
|
|
1484
2121
|
}
|
|
1485
|
-
async
|
|
2122
|
+
async importEntity(entity) {
|
|
2123
|
+
return await this.importEntityInternal(entity);
|
|
2124
|
+
}
|
|
2125
|
+
async importEntityTokens(entity) {
|
|
2126
|
+
return await this.importEntityInternal(entity, { only: 'tokens' });
|
|
2127
|
+
}
|
|
2128
|
+
async importEntityTilmeldAC(entity) {
|
|
2129
|
+
return await this.importEntityInternal(entity, { only: 'tilmeldAC' });
|
|
2130
|
+
}
|
|
2131
|
+
async importEntityInternal({ guid, cdate, mdate, tags, sdata, etype, }, { only = undefined } = {}) {
|
|
1486
2132
|
try {
|
|
1487
|
-
|
|
1488
|
-
|
|
2133
|
+
let promises = [];
|
|
2134
|
+
if (only == null) {
|
|
2135
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1489
2136
|
etypes: [etype],
|
|
1490
2137
|
params: {
|
|
1491
2138
|
guid,
|
|
1492
2139
|
},
|
|
1493
|
-
});
|
|
1494
|
-
|
|
2140
|
+
}));
|
|
2141
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1495
2142
|
etypes: [etype],
|
|
1496
2143
|
params: {
|
|
1497
2144
|
guid,
|
|
1498
|
-
tags,
|
|
1499
|
-
cdate: isNaN(Number(JSON.parse(sdata.cdate)))
|
|
1500
|
-
? null
|
|
1501
|
-
: Number(JSON.parse(sdata.cdate)),
|
|
1502
|
-
mdate: isNaN(Number(JSON.parse(sdata.mdate)))
|
|
1503
|
-
? null
|
|
1504
|
-
: Number(JSON.parse(sdata.mdate)),
|
|
1505
2145
|
},
|
|
1506
|
-
});
|
|
1507
|
-
|
|
1508
|
-
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
2146
|
+
}));
|
|
2147
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1509
2148
|
etypes: [etype],
|
|
1510
2149
|
params: {
|
|
1511
2150
|
guid,
|
|
1512
2151
|
},
|
|
1513
2152
|
}));
|
|
1514
|
-
|
|
2153
|
+
}
|
|
2154
|
+
if (only == null || only === 'tokens') {
|
|
2155
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1515
2156
|
etypes: [etype],
|
|
1516
2157
|
params: {
|
|
1517
2158
|
guid,
|
|
1518
2159
|
},
|
|
1519
2160
|
}));
|
|
1520
|
-
|
|
2161
|
+
}
|
|
2162
|
+
if (only == null) {
|
|
2163
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1521
2164
|
etypes: [etype],
|
|
1522
2165
|
params: {
|
|
1523
2166
|
guid,
|
|
1524
2167
|
},
|
|
1525
2168
|
}));
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
2169
|
+
}
|
|
2170
|
+
await Promise.all(promises);
|
|
2171
|
+
promises = [];
|
|
2172
|
+
if (only == null) {
|
|
2173
|
+
let { user, group, acUser, acGroup, acOther, acRead, acWrite, acFull } = this.removeAndReturnACValues(etype, {}, sdata);
|
|
2174
|
+
await this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate", "user", "group", "acUser", "acGroup", "acOther", "acRead", "acWrite", "acFull") VALUES (decode(@guid, 'hex'), @tags, @cdate, @mdate, ${user == null ? '@user' : "decode(@user, 'hex')"}, ${group == null ? '@group' : "decode(@group, 'hex')"}, @acUser, @acGroup, @acOther, ${!acRead?.length ? '@acRead' : "array(SELECT decode(n, 'hex') FROM unnest(@acRead::text[]) AS n)"}, ${!acWrite?.length ? '@acWrite' : "array(SELECT decode(n, 'hex') FROM unnest(@acWrite::text[]) AS n)"}, ${!acFull?.length ? '@acFull' : "array(SELECT decode(n, 'hex') FROM unnest(@acFull::text[]) AS n)"});`, {
|
|
2175
|
+
etypes: [etype],
|
|
2176
|
+
params: {
|
|
2177
|
+
guid,
|
|
2178
|
+
tags,
|
|
2179
|
+
cdate: isNaN(cdate) ? null : cdate,
|
|
2180
|
+
mdate: isNaN(mdate) ? null : mdate,
|
|
2181
|
+
user,
|
|
2182
|
+
group,
|
|
2183
|
+
acUser,
|
|
2184
|
+
acGroup,
|
|
2185
|
+
acOther,
|
|
2186
|
+
acRead,
|
|
2187
|
+
acWrite,
|
|
2188
|
+
acFull,
|
|
2189
|
+
},
|
|
2190
|
+
});
|
|
1529
2191
|
for (const name in sdata) {
|
|
2192
|
+
if (typeof name !== 'string' ||
|
|
2193
|
+
!name.length ||
|
|
2194
|
+
name.match(/^\s|=|\s$/)) {
|
|
2195
|
+
throw new EntityInvalidDataError('Property names must be non-empty strings without leading or trailing spaces or equals signs.');
|
|
2196
|
+
}
|
|
1530
2197
|
const value = sdata[name];
|
|
1531
2198
|
const uvalue = JSON.parse(value);
|
|
1532
2199
|
if (value === undefined) {
|
|
@@ -1536,24 +2203,22 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1536
2203
|
? 'N'
|
|
1537
2204
|
: typeof uvalue === 'string'
|
|
1538
2205
|
? 'S'
|
|
1539
|
-
:
|
|
1540
|
-
const
|
|
1541
|
-
|
|
2206
|
+
: 'J';
|
|
2207
|
+
const jsonValue = storageValue === 'J'
|
|
2208
|
+
? PostgreSQLDriver.escapeNullSequences(value)
|
|
2209
|
+
: null;
|
|
2210
|
+
promises.push(this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value", "json", "string", "number", "truthy") VALUES (decode(@guid, 'hex'), @name, @storageValue, @jsonValue, @string, @number, @truthy);`, {
|
|
1542
2211
|
etypes: [etype],
|
|
1543
2212
|
params: {
|
|
1544
2213
|
guid,
|
|
1545
2214
|
name,
|
|
1546
2215
|
storageValue,
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
params: {
|
|
1552
|
-
guid,
|
|
1553
|
-
name,
|
|
1554
|
-
truthy: !!uvalue,
|
|
1555
|
-
string: `${uvalue}`,
|
|
2216
|
+
jsonValue,
|
|
2217
|
+
string: storageValue === 'J'
|
|
2218
|
+
? null
|
|
2219
|
+
: PostgreSQLDriver.escapeNulls(`${uvalue}`),
|
|
1556
2220
|
number: isNaN(Number(uvalue)) ? null : Number(uvalue),
|
|
2221
|
+
truthy: !!uvalue,
|
|
1557
2222
|
},
|
|
1558
2223
|
}));
|
|
1559
2224
|
const references = this.findReferences(value);
|
|
@@ -1568,43 +2233,136 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1568
2233
|
}));
|
|
1569
2234
|
}
|
|
1570
2235
|
}
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
},
|
|
1577
|
-
});
|
|
1578
|
-
await this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
|
|
2236
|
+
}
|
|
2237
|
+
if (only === 'tilmeldAC') {
|
|
2238
|
+
let { user, group, acUser, acGroup, acOther, acRead, acWrite, acFull } = this.removeAndReturnACValues(etype, {}, sdata);
|
|
2239
|
+
promises.push(this.queryRun(`UPDATE ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} SET "user"=${user == null ? '@user' : "decode(@user, 'hex')"}, "group"=${group == null ? '@group' : "decode(@group, 'hex')"}, "acUser"=@acUser, "acGroup"=@acGroup, "acOther"=@acOther, "acRead"=${!acRead?.length ? '@acRead' : "array(SELECT decode(n, 'hex') FROM unnest(@acRead::text[]) AS n)"}, "acWrite"=${!acWrite?.length ? '@acWrite' : "array(SELECT decode(n, 'hex') FROM unnest(@acWrite::text[]) AS n)"}, "acFull"=${!acFull?.length ? '@acFull' : "array(SELECT decode(n, 'hex') FROM unnest(@acFull::text[]) AS n)"} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
2240
|
+
etypes: [etype],
|
|
1579
2241
|
params: {
|
|
1580
|
-
|
|
1581
|
-
|
|
2242
|
+
user,
|
|
2243
|
+
group,
|
|
2244
|
+
acUser,
|
|
2245
|
+
acGroup,
|
|
2246
|
+
acOther,
|
|
2247
|
+
acRead,
|
|
2248
|
+
acWrite,
|
|
2249
|
+
acFull,
|
|
2250
|
+
guid,
|
|
1582
2251
|
},
|
|
2252
|
+
}));
|
|
2253
|
+
}
|
|
2254
|
+
const EntityClass = this.nymph.getEntityClassByEtype(etype);
|
|
2255
|
+
if (only == null || only === 'tokens') {
|
|
2256
|
+
for (let name in sdata) {
|
|
2257
|
+
let tokenString = null;
|
|
2258
|
+
try {
|
|
2259
|
+
tokenString = EntityClass.getFTSText(name, JSON.parse(sdata[name]));
|
|
2260
|
+
}
|
|
2261
|
+
catch (e) {
|
|
2262
|
+
// Ignore error.
|
|
2263
|
+
}
|
|
2264
|
+
if (tokenString != null) {
|
|
2265
|
+
const tokens = this.tokenizer.tokenize(tokenString);
|
|
2266
|
+
while (tokens.length) {
|
|
2267
|
+
const currentTokens = tokens.splice(0, 100);
|
|
2268
|
+
const params = {
|
|
2269
|
+
guid,
|
|
2270
|
+
name,
|
|
2271
|
+
};
|
|
2272
|
+
const values = [];
|
|
2273
|
+
for (let i = 0; i < currentTokens.length; i++) {
|
|
2274
|
+
const token = currentTokens[i];
|
|
2275
|
+
params['token' + i] = token.token;
|
|
2276
|
+
params['position' + i] = token.position;
|
|
2277
|
+
params['stem' + i] = token.stem;
|
|
2278
|
+
values.push("(decode(@guid, 'hex'), @name, @token" +
|
|
2279
|
+
i +
|
|
2280
|
+
', @position' +
|
|
2281
|
+
i +
|
|
2282
|
+
', @stem' +
|
|
2283
|
+
i +
|
|
2284
|
+
')');
|
|
2285
|
+
}
|
|
2286
|
+
promises.push(this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} ("guid", "name", "token", "position", "stem") VALUES ${values.join(', ')};`, {
|
|
2287
|
+
etypes: [etype],
|
|
2288
|
+
params,
|
|
2289
|
+
}));
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
if (only == null) {
|
|
2295
|
+
const uniques = await EntityClass.getUniques({
|
|
2296
|
+
guid,
|
|
2297
|
+
cdate,
|
|
2298
|
+
mdate,
|
|
2299
|
+
tags,
|
|
2300
|
+
data: {},
|
|
2301
|
+
sdata,
|
|
1583
2302
|
});
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
2303
|
+
for (const unique of uniques) {
|
|
2304
|
+
promises.push(this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} ("guid", "unique") VALUES (decode(@guid, 'hex'), @unique);`, {
|
|
2305
|
+
etypes: [etype],
|
|
2306
|
+
params: {
|
|
2307
|
+
guid,
|
|
2308
|
+
unique,
|
|
2309
|
+
},
|
|
2310
|
+
}).catch((e) => {
|
|
2311
|
+
if (e instanceof EntityUniqueConstraintError) {
|
|
2312
|
+
this.nymph.config.debugError('postgresql', `Import entity unique constraint violation for GUID "${guid}" on etype "${etype}": "${unique}"`);
|
|
2313
|
+
}
|
|
2314
|
+
return e;
|
|
2315
|
+
}));
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
await Promise.all(promises);
|
|
2319
|
+
}
|
|
2320
|
+
catch (e) {
|
|
2321
|
+
this.nymph.config.debugError('postgresql', `Import entity error: "${e}"`);
|
|
2322
|
+
throw e;
|
|
2323
|
+
}
|
|
2324
|
+
}
|
|
2325
|
+
async importUID({ name, value }) {
|
|
2326
|
+
if (name.length > 128) {
|
|
2327
|
+
throw new InvalidParametersError('UID name too long. 128 chars max.');
|
|
2328
|
+
}
|
|
2329
|
+
try {
|
|
2330
|
+
await this.internalTransaction(`nymph-import-uid-${name}`);
|
|
2331
|
+
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
|
|
2332
|
+
params: {
|
|
2333
|
+
name,
|
|
2334
|
+
},
|
|
1588
2335
|
});
|
|
1589
|
-
|
|
2336
|
+
await this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @value);`, {
|
|
2337
|
+
params: {
|
|
2338
|
+
name,
|
|
2339
|
+
value,
|
|
2340
|
+
},
|
|
2341
|
+
});
|
|
2342
|
+
await this.commit(`nymph-import-uid-${name}`);
|
|
1590
2343
|
}
|
|
1591
2344
|
catch (e) {
|
|
1592
|
-
|
|
1593
|
-
|
|
2345
|
+
this.nymph.config.debugError('postgresql', `Import UID error: "${e}"`);
|
|
2346
|
+
await this.rollback(`nymph-import-uid-${name}`);
|
|
2347
|
+
throw e;
|
|
1594
2348
|
}
|
|
1595
2349
|
}
|
|
1596
2350
|
async newUID(name) {
|
|
1597
2351
|
if (name == null) {
|
|
1598
|
-
throw new
|
|
2352
|
+
throw new InvalidParametersError('Name not given for UID.');
|
|
2353
|
+
}
|
|
2354
|
+
if (name.length > 128) {
|
|
2355
|
+
throw new InvalidParametersError('UID name too long. 128 chars max.');
|
|
1599
2356
|
}
|
|
1600
2357
|
await this.internalTransaction('nymph-newuid');
|
|
2358
|
+
let curUid = undefined;
|
|
1601
2359
|
try {
|
|
1602
2360
|
const lock = await this.queryGet(`SELECT "cur_uid" FROM ${PostgreSQLDriver.escape(`${this.prefix}uids`)} WHERE "name"=@name FOR UPDATE;`, {
|
|
1603
2361
|
params: {
|
|
1604
2362
|
name,
|
|
1605
2363
|
},
|
|
1606
2364
|
});
|
|
1607
|
-
|
|
2365
|
+
curUid = lock?.cur_uid == null ? undefined : Number(lock.cur_uid);
|
|
1608
2366
|
if (curUid == null) {
|
|
1609
2367
|
curUid = 1;
|
|
1610
2368
|
await this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
|
|
@@ -1623,17 +2381,21 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1623
2381
|
},
|
|
1624
2382
|
});
|
|
1625
2383
|
}
|
|
1626
|
-
await this.commit('nymph-newuid');
|
|
1627
|
-
return curUid;
|
|
1628
2384
|
}
|
|
1629
2385
|
catch (e) {
|
|
2386
|
+
this.nymph.config.debugError('postgresql', `New UID error: "${e}"`);
|
|
1630
2387
|
await this.rollback('nymph-newuid');
|
|
1631
2388
|
throw e;
|
|
1632
2389
|
}
|
|
2390
|
+
await this.commit('nymph-newuid');
|
|
2391
|
+
return curUid;
|
|
1633
2392
|
}
|
|
1634
2393
|
async renameUID(oldName, newName) {
|
|
1635
2394
|
if (oldName == null || newName == null) {
|
|
1636
|
-
throw new
|
|
2395
|
+
throw new InvalidParametersError('Name not given for UID.');
|
|
2396
|
+
}
|
|
2397
|
+
if (oldName.length > 128 || newName.length > 128) {
|
|
2398
|
+
throw new InvalidParametersError('UID name too long. 128 chars max.');
|
|
1637
2399
|
}
|
|
1638
2400
|
await this.queryRun(`UPDATE ${PostgreSQLDriver.escape(`${this.prefix}uids`)} SET "name"=@newName WHERE "name"=@oldName;`, {
|
|
1639
2401
|
params: {
|
|
@@ -1645,7 +2407,7 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1645
2407
|
}
|
|
1646
2408
|
async rollback(name) {
|
|
1647
2409
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
1648
|
-
throw new
|
|
2410
|
+
throw new InvalidParametersError('Transaction rollback attempted without a name.');
|
|
1649
2411
|
}
|
|
1650
2412
|
if (!this.transaction || this.transaction.count === 0) {
|
|
1651
2413
|
this.transaction = null;
|
|
@@ -1662,8 +2424,14 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1662
2424
|
return true;
|
|
1663
2425
|
}
|
|
1664
2426
|
async saveEntity(entity) {
|
|
1665
|
-
const insertData = async (guid, data, sdata, etype) => {
|
|
2427
|
+
const insertData = async (guid, data, sdata, uniques, etype) => {
|
|
2428
|
+
const EntityClass = this.nymph.getEntityClassByEtype(etype);
|
|
1666
2429
|
const runInsertQuery = async (name, value, svalue) => {
|
|
2430
|
+
if (typeof name !== 'string' ||
|
|
2431
|
+
!name.length ||
|
|
2432
|
+
name.match(/^\s|=|\s$/)) {
|
|
2433
|
+
throw new EntityInvalidDataError('Property names must be non-empty strings without leading or trailing spaces or equals signs.');
|
|
2434
|
+
}
|
|
1667
2435
|
if (value === undefined) {
|
|
1668
2436
|
return;
|
|
1669
2437
|
}
|
|
@@ -1671,24 +2439,23 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1671
2439
|
? 'N'
|
|
1672
2440
|
: typeof value === 'string'
|
|
1673
2441
|
? 'S'
|
|
1674
|
-
:
|
|
2442
|
+
: 'J';
|
|
2443
|
+
const jsonValue = storageValue === 'J'
|
|
2444
|
+
? PostgreSQLDriver.escapeNullSequences(svalue)
|
|
2445
|
+
: null;
|
|
1675
2446
|
const promises = [];
|
|
1676
|
-
promises.push(this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (decode(@guid, 'hex'), @name, @storageValue);`, {
|
|
2447
|
+
promises.push(this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value", "json", "string", "number", "truthy") VALUES (decode(@guid, 'hex'), @name, @storageValue, @jsonValue, @string, @number, @truthy);`, {
|
|
1677
2448
|
etypes: [etype],
|
|
1678
2449
|
params: {
|
|
1679
2450
|
guid,
|
|
1680
2451
|
name,
|
|
1681
2452
|
storageValue,
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
params: {
|
|
1687
|
-
guid,
|
|
1688
|
-
name,
|
|
1689
|
-
truthy: !!value,
|
|
1690
|
-
string: `${value}`,
|
|
2453
|
+
jsonValue,
|
|
2454
|
+
string: storageValue === 'J'
|
|
2455
|
+
? null
|
|
2456
|
+
: PostgreSQLDriver.escapeNulls(`${value}`),
|
|
1691
2457
|
number: isNaN(Number(value)) ? null : Number(value),
|
|
2458
|
+
truthy: !!value,
|
|
1692
2459
|
},
|
|
1693
2460
|
}));
|
|
1694
2461
|
const references = this.findReferences(svalue);
|
|
@@ -1702,8 +2469,60 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1702
2469
|
},
|
|
1703
2470
|
}));
|
|
1704
2471
|
}
|
|
2472
|
+
let tokenString = null;
|
|
2473
|
+
try {
|
|
2474
|
+
tokenString = EntityClass.getFTSText(name, value);
|
|
2475
|
+
}
|
|
2476
|
+
catch (e) {
|
|
2477
|
+
// Ignore error.
|
|
2478
|
+
}
|
|
2479
|
+
if (tokenString != null) {
|
|
2480
|
+
const tokens = this.tokenizer.tokenize(tokenString);
|
|
2481
|
+
while (tokens.length) {
|
|
2482
|
+
const currentTokens = tokens.splice(0, 100);
|
|
2483
|
+
const params = {
|
|
2484
|
+
guid,
|
|
2485
|
+
name,
|
|
2486
|
+
};
|
|
2487
|
+
const values = [];
|
|
2488
|
+
for (let i = 0; i < currentTokens.length; i++) {
|
|
2489
|
+
const token = currentTokens[i];
|
|
2490
|
+
params['token' + i] = token.token;
|
|
2491
|
+
params['position' + i] = token.position;
|
|
2492
|
+
params['stem' + i] = token.stem;
|
|
2493
|
+
values.push("(decode(@guid, 'hex'), @name, @token" +
|
|
2494
|
+
i +
|
|
2495
|
+
', @position' +
|
|
2496
|
+
i +
|
|
2497
|
+
', @stem' +
|
|
2498
|
+
i +
|
|
2499
|
+
')');
|
|
2500
|
+
}
|
|
2501
|
+
promises.push(this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} ("guid", "name", "token", "position", "stem") VALUES ${values.join(', ')};`, {
|
|
2502
|
+
etypes: [etype],
|
|
2503
|
+
params,
|
|
2504
|
+
}));
|
|
2505
|
+
}
|
|
2506
|
+
}
|
|
1705
2507
|
await Promise.all(promises);
|
|
1706
2508
|
};
|
|
2509
|
+
for (const unique of uniques) {
|
|
2510
|
+
try {
|
|
2511
|
+
await this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} ("guid", "unique") VALUES (decode(@guid, 'hex'), @unique);`, {
|
|
2512
|
+
etypes: [etype],
|
|
2513
|
+
params: {
|
|
2514
|
+
guid,
|
|
2515
|
+
unique,
|
|
2516
|
+
},
|
|
2517
|
+
});
|
|
2518
|
+
}
|
|
2519
|
+
catch (e) {
|
|
2520
|
+
if (e instanceof EntityUniqueConstraintError) {
|
|
2521
|
+
this.nymph.config.debugError('postgresql', `Save entity unique constraint violation for GUID "${guid}" on etype "${etype}": "${unique}"`);
|
|
2522
|
+
}
|
|
2523
|
+
throw e;
|
|
2524
|
+
}
|
|
2525
|
+
}
|
|
1707
2526
|
for (const name in data) {
|
|
1708
2527
|
await runInsertQuery(name, data[name], JSON.stringify(data[name]));
|
|
1709
2528
|
}
|
|
@@ -1711,19 +2530,56 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1711
2530
|
await runInsertQuery(name, JSON.parse(sdata[name]), sdata[name]);
|
|
1712
2531
|
}
|
|
1713
2532
|
};
|
|
2533
|
+
let inTransaction = false;
|
|
1714
2534
|
try {
|
|
1715
|
-
const result = await this.saveEntityRowLike(entity, async (
|
|
1716
|
-
|
|
2535
|
+
const result = await this.saveEntityRowLike(entity, async ({ guid, tags, data, sdata, uniques, cdate, etype }) => {
|
|
2536
|
+
if (Object.keys(data).length === 0 &&
|
|
2537
|
+
Object.keys(sdata).length === 0) {
|
|
2538
|
+
throw new EntityInvalidDataError('Entity contains no data.');
|
|
2539
|
+
}
|
|
2540
|
+
for (let tag of tags) {
|
|
2541
|
+
if (typeof tag !== 'string' ||
|
|
2542
|
+
!tag.length ||
|
|
2543
|
+
tag.match(/[\x00-\x1F\x7F\[\],]/)) {
|
|
2544
|
+
throw new EntityInvalidDataError('Tags must be non-empty strings without square brackets, control characters, or commas.');
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
let processedData = this.removeAndReturnACValues(etype, { ...data }, { ...sdata });
|
|
2548
|
+
({ data, sdata } = processedData);
|
|
2549
|
+
let { user, group, acUser, acGroup, acOther, acRead, acWrite, acFull, } = processedData;
|
|
2550
|
+
await this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate", "user", "group", "acUser", "acGroup", "acOther", "acRead", "acWrite", "acFull") VALUES (decode(@guid, 'hex'), @tags, @cdate, @cdate, ${user == null ? '@user' : "decode(@user, 'hex')"}, ${group == null ? '@group' : "decode(@group, 'hex')"}, @acUser, @acGroup, @acOther, ${!acRead?.length ? '@acRead' : "array(SELECT decode(n, 'hex') FROM unnest(@acRead::text[]) AS n)"}, ${!acWrite?.length ? '@acWrite' : "array(SELECT decode(n, 'hex') FROM unnest(@acWrite::text[]) AS n)"}, ${!acFull?.length ? '@acFull' : "array(SELECT decode(n, 'hex') FROM unnest(@acFull::text[]) AS n)"});`, {
|
|
1717
2551
|
etypes: [etype],
|
|
1718
2552
|
params: {
|
|
1719
2553
|
guid,
|
|
1720
2554
|
tags,
|
|
1721
2555
|
cdate,
|
|
2556
|
+
user,
|
|
2557
|
+
group,
|
|
2558
|
+
acUser,
|
|
2559
|
+
acGroup,
|
|
2560
|
+
acOther,
|
|
2561
|
+
acRead,
|
|
2562
|
+
acWrite,
|
|
2563
|
+
acFull,
|
|
1722
2564
|
},
|
|
1723
2565
|
});
|
|
1724
|
-
await insertData(guid, data, sdata, etype);
|
|
2566
|
+
await insertData(guid, data, sdata, uniques, etype);
|
|
1725
2567
|
return true;
|
|
1726
|
-
}, async (entity, guid, tags, data, sdata, mdate, etype) => {
|
|
2568
|
+
}, async ({ entity, guid, tags, data, sdata, uniques, mdate, etype }) => {
|
|
2569
|
+
if (Object.keys(data).length === 0 &&
|
|
2570
|
+
Object.keys(sdata).length === 0) {
|
|
2571
|
+
throw new EntityInvalidDataError('Entity contains no data.');
|
|
2572
|
+
}
|
|
2573
|
+
for (let tag of tags) {
|
|
2574
|
+
if (typeof tag !== 'string' ||
|
|
2575
|
+
!tag.length ||
|
|
2576
|
+
tag.match(/[\x00-\x1F\x7F\[\],]/)) {
|
|
2577
|
+
throw new EntityInvalidDataError('Tags must be non-empty strings without square brackets, control characters, or commas.');
|
|
2578
|
+
}
|
|
2579
|
+
}
|
|
2580
|
+
let processedData = this.removeAndReturnACValues(etype, { ...data }, { ...sdata });
|
|
2581
|
+
({ data, sdata } = processedData);
|
|
2582
|
+
let { user, group, acUser, acGroup, acOther, acRead, acWrite, acFull, } = processedData;
|
|
1727
2583
|
const promises = [];
|
|
1728
2584
|
promises.push(this.queryRun(`SELECT 1 FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=decode(@guid, 'hex') FOR UPDATE;`, {
|
|
1729
2585
|
etypes: [etype],
|
|
@@ -1737,24 +2593,38 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1737
2593
|
guid,
|
|
1738
2594
|
},
|
|
1739
2595
|
}));
|
|
1740
|
-
promises.push(this.queryRun(`SELECT 1 FROM ${PostgreSQLDriver.escape(`${this.prefix}
|
|
2596
|
+
promises.push(this.queryRun(`SELECT 1 FROM ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=decode(@guid, 'hex') FOR UPDATE;`, {
|
|
1741
2597
|
etypes: [etype],
|
|
1742
2598
|
params: {
|
|
1743
2599
|
guid,
|
|
1744
2600
|
},
|
|
1745
2601
|
}));
|
|
1746
|
-
promises.push(this.queryRun(`SELECT 1 FROM ${PostgreSQLDriver.escape(`${this.prefix}
|
|
2602
|
+
promises.push(this.queryRun(`SELECT 1 FROM ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} WHERE "guid"=decode(@guid, 'hex') FOR UPDATE;`, {
|
|
2603
|
+
etypes: [etype],
|
|
2604
|
+
params: {
|
|
2605
|
+
guid,
|
|
2606
|
+
},
|
|
2607
|
+
}));
|
|
2608
|
+
promises.push(this.queryRun(`SELECT 1 FROM ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} WHERE "guid"=decode(@guid, 'hex') FOR UPDATE;`, {
|
|
1747
2609
|
etypes: [etype],
|
|
1748
2610
|
params: {
|
|
1749
2611
|
guid,
|
|
1750
2612
|
},
|
|
1751
2613
|
}));
|
|
1752
2614
|
await Promise.all(promises);
|
|
1753
|
-
const info = await this.queryRun(`UPDATE ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} SET "tags"=@tags, "mdate"=@mdate WHERE "guid"=decode(@guid, 'hex') AND "mdate" <= @emdate;`, {
|
|
2615
|
+
const info = await this.queryRun(`UPDATE ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} SET "tags"=@tags, "mdate"=@mdate, "user"=${user == null ? '@user' : "decode(@user, 'hex')"}, "group"=${group == null ? '@group' : "decode(@group, 'hex')"}, "acUser"=@acUser, "acGroup"=@acGroup, "acOther"=@acOther, "acRead"=${!acRead?.length ? '@acRead' : "array(SELECT decode(n, 'hex') FROM unnest(@acRead::text[]) AS n)"}, "acWrite"=${!acWrite?.length ? '@acWrite' : "array(SELECT decode(n, 'hex') FROM unnest(@acWrite::text[]) AS n)"}, "acFull"=${!acFull?.length ? '@acFull' : "array(SELECT decode(n, 'hex') FROM unnest(@acFull::text[]) AS n)"} WHERE "guid"=decode(@guid, 'hex') AND "mdate" <= @emdate;`, {
|
|
1754
2616
|
etypes: [etype],
|
|
1755
2617
|
params: {
|
|
1756
2618
|
tags,
|
|
1757
2619
|
mdate,
|
|
2620
|
+
user,
|
|
2621
|
+
group,
|
|
2622
|
+
acUser,
|
|
2623
|
+
acGroup,
|
|
2624
|
+
acOther,
|
|
2625
|
+
acRead,
|
|
2626
|
+
acWrite,
|
|
2627
|
+
acFull,
|
|
1758
2628
|
guid,
|
|
1759
2629
|
emdate: isNaN(Number(entity.mdate)) ? 0 : Number(entity.mdate),
|
|
1760
2630
|
},
|
|
@@ -1768,44 +2638,63 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1768
2638
|
guid,
|
|
1769
2639
|
},
|
|
1770
2640
|
}));
|
|
1771
|
-
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}
|
|
2641
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1772
2642
|
etypes: [etype],
|
|
1773
2643
|
params: {
|
|
1774
2644
|
guid,
|
|
1775
2645
|
},
|
|
1776
2646
|
}));
|
|
1777
|
-
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}
|
|
2647
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
2648
|
+
etypes: [etype],
|
|
2649
|
+
params: {
|
|
2650
|
+
guid,
|
|
2651
|
+
},
|
|
2652
|
+
}));
|
|
2653
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1778
2654
|
etypes: [etype],
|
|
1779
2655
|
params: {
|
|
1780
2656
|
guid,
|
|
1781
2657
|
},
|
|
1782
2658
|
}));
|
|
1783
2659
|
await Promise.all(promises);
|
|
1784
|
-
await insertData(guid, data, sdata, etype);
|
|
2660
|
+
await insertData(guid, data, sdata, uniques, etype);
|
|
1785
2661
|
success = true;
|
|
1786
2662
|
}
|
|
2663
|
+
if (!success && info.rowCount === 0) {
|
|
2664
|
+
throw new EntityConflictError("Entity either doesn't exist or is newer in the DB.");
|
|
2665
|
+
}
|
|
1787
2666
|
return success;
|
|
1788
2667
|
}, async () => {
|
|
1789
2668
|
await this.internalTransaction('nymph-save');
|
|
2669
|
+
inTransaction = true;
|
|
1790
2670
|
}, async (success) => {
|
|
1791
|
-
if (
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
2671
|
+
if (inTransaction) {
|
|
2672
|
+
inTransaction = false;
|
|
2673
|
+
if (success) {
|
|
2674
|
+
success = await this.commit('nymph-save');
|
|
2675
|
+
}
|
|
2676
|
+
else {
|
|
2677
|
+
await this.rollback('nymph-save');
|
|
2678
|
+
}
|
|
1796
2679
|
}
|
|
1797
2680
|
return success;
|
|
1798
2681
|
});
|
|
1799
2682
|
return result;
|
|
1800
2683
|
}
|
|
1801
2684
|
catch (e) {
|
|
1802
|
-
|
|
2685
|
+
this.nymph.config.debugError('postgresql', `Save entity error: "${e}"`);
|
|
2686
|
+
if (inTransaction) {
|
|
2687
|
+
await this.rollback('nymph-save');
|
|
2688
|
+
}
|
|
1803
2689
|
throw e;
|
|
1804
2690
|
}
|
|
1805
2691
|
}
|
|
1806
2692
|
async setUID(name, curUid) {
|
|
1807
2693
|
if (name == null) {
|
|
1808
|
-
throw new
|
|
2694
|
+
throw new InvalidParametersError('Name not given for UID.');
|
|
2695
|
+
}
|
|
2696
|
+
if (name.length > 128) {
|
|
2697
|
+
throw new InvalidParametersError('UID name too long. 128 chars max.');
|
|
1809
2698
|
}
|
|
1810
2699
|
await this.internalTransaction('nymph-setuid');
|
|
1811
2700
|
try {
|
|
@@ -1821,39 +2710,161 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1821
2710
|
curUid,
|
|
1822
2711
|
},
|
|
1823
2712
|
});
|
|
1824
|
-
await this.commit('nymph-setuid');
|
|
1825
|
-
return true;
|
|
1826
2713
|
}
|
|
1827
2714
|
catch (e) {
|
|
1828
2715
|
await this.rollback('nymph-setuid');
|
|
1829
2716
|
throw e;
|
|
1830
2717
|
}
|
|
2718
|
+
await this.commit('nymph-setuid');
|
|
2719
|
+
return true;
|
|
1831
2720
|
}
|
|
1832
|
-
async internalTransaction(name) {
|
|
2721
|
+
async internalTransaction(name, setOnSelf = true) {
|
|
1833
2722
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
1834
|
-
throw new
|
|
2723
|
+
throw new InvalidParametersError('Transaction start attempted without a name.');
|
|
1835
2724
|
}
|
|
1836
|
-
|
|
1837
|
-
|
|
2725
|
+
let transaction = this.transaction;
|
|
2726
|
+
if (!transaction) {
|
|
2727
|
+
// Lock to one connection.
|
|
2728
|
+
transaction = {
|
|
1838
2729
|
count: 0,
|
|
1839
2730
|
connection: await this.getConnection(),
|
|
2731
|
+
latestPromise: Promise.resolve(),
|
|
1840
2732
|
};
|
|
1841
|
-
|
|
2733
|
+
if (setOnSelf) {
|
|
2734
|
+
this.transaction = transaction;
|
|
2735
|
+
}
|
|
1842
2736
|
}
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
2737
|
+
if (transaction.count === 0) {
|
|
2738
|
+
// We're not in a transaction yet, so start one.
|
|
2739
|
+
await this.queryRun('BEGIN;', { transaction });
|
|
2740
|
+
}
|
|
2741
|
+
transaction.count++;
|
|
2742
|
+
await this.queryRun(`SAVEPOINT ${PostgreSQLDriver.escape(name)};`, {
|
|
2743
|
+
transaction,
|
|
2744
|
+
});
|
|
2745
|
+
return transaction;
|
|
1846
2746
|
}
|
|
1847
2747
|
async startTransaction(name) {
|
|
1848
|
-
const
|
|
1849
|
-
const transaction = await this.internalTransaction(name);
|
|
1850
|
-
if (!inTransaction) {
|
|
1851
|
-
this.transaction = null;
|
|
1852
|
-
}
|
|
2748
|
+
const transaction = await this.internalTransaction(name, false);
|
|
1853
2749
|
const nymph = this.nymph.clone();
|
|
1854
2750
|
nymph.driver.transaction = transaction;
|
|
1855
2751
|
return nymph;
|
|
1856
2752
|
}
|
|
2753
|
+
async removeTilmeldOldRows(etype, connection) {
|
|
2754
|
+
await this.internalTransaction('nymph-remove-tilmeld-rows');
|
|
2755
|
+
try {
|
|
2756
|
+
for (let name of [
|
|
2757
|
+
'user',
|
|
2758
|
+
'group',
|
|
2759
|
+
'acUser',
|
|
2760
|
+
'acGroup',
|
|
2761
|
+
'acOther',
|
|
2762
|
+
'acRead',
|
|
2763
|
+
'acWrite',
|
|
2764
|
+
'acFull',
|
|
2765
|
+
]) {
|
|
2766
|
+
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} WHERE "name"=@name;`, {
|
|
2767
|
+
etypes: [etype],
|
|
2768
|
+
params: {
|
|
2769
|
+
name,
|
|
2770
|
+
},
|
|
2771
|
+
connection,
|
|
2772
|
+
});
|
|
2773
|
+
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE "name"=@name;`, {
|
|
2774
|
+
etypes: [etype],
|
|
2775
|
+
params: {
|
|
2776
|
+
name,
|
|
2777
|
+
},
|
|
2778
|
+
connection,
|
|
2779
|
+
});
|
|
2780
|
+
await this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}tokens_${etype}`)} WHERE "name"=@name;`, {
|
|
2781
|
+
etypes: [etype],
|
|
2782
|
+
params: {
|
|
2783
|
+
name,
|
|
2784
|
+
},
|
|
2785
|
+
connection,
|
|
2786
|
+
});
|
|
2787
|
+
}
|
|
2788
|
+
}
|
|
2789
|
+
catch (e) {
|
|
2790
|
+
this.nymph.config.debugError('postgresql', `Remove tilmeld rows error: "${e}"`);
|
|
2791
|
+
await this.rollback('nymph-remove-tilmeld-rows');
|
|
2792
|
+
throw e;
|
|
2793
|
+
}
|
|
2794
|
+
await this.commit('nymph-remove-tilmeld-rows');
|
|
2795
|
+
return true;
|
|
2796
|
+
}
|
|
2797
|
+
async needsMigration() {
|
|
2798
|
+
const table = await this.queryGet('SELECT "table_name" AS "table_name" FROM "information_schema"."tables" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name" LIKE @prefix LIMIT 1;', {
|
|
2799
|
+
params: {
|
|
2800
|
+
db: this.config.database,
|
|
2801
|
+
prefix: this.prefix + 'data_' + '%',
|
|
2802
|
+
},
|
|
2803
|
+
});
|
|
2804
|
+
if (table?.table_name) {
|
|
2805
|
+
const result = await this.queryGet('SELECT 1 AS "exists" FROM "information_schema"."columns" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name"=@table AND "column_name"=\'json\';', {
|
|
2806
|
+
params: {
|
|
2807
|
+
db: this.config.database,
|
|
2808
|
+
table: table.table_name,
|
|
2809
|
+
},
|
|
2810
|
+
});
|
|
2811
|
+
if (!result?.exists) {
|
|
2812
|
+
return 'json';
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
const table2 = await this.queryGet('SELECT "table_name" AS "table_name" FROM "information_schema"."tables" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name" LIKE @tokenTable LIMIT 1;', {
|
|
2816
|
+
params: {
|
|
2817
|
+
db: this.config.database,
|
|
2818
|
+
tokenTable: this.prefix + 'tokens_' + '%',
|
|
2819
|
+
},
|
|
2820
|
+
});
|
|
2821
|
+
if (!table2 || !table2.table_name) {
|
|
2822
|
+
return 'tokens';
|
|
2823
|
+
}
|
|
2824
|
+
const table3 = await this.queryGet('SELECT "table_name" AS "table_name" FROM "information_schema"."tables" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name" LIKE @prefix LIMIT 1;', {
|
|
2825
|
+
params: {
|
|
2826
|
+
db: this.config.database,
|
|
2827
|
+
prefix: this.prefix + 'entities_' + '%',
|
|
2828
|
+
},
|
|
2829
|
+
});
|
|
2830
|
+
if (table3?.table_name) {
|
|
2831
|
+
const result = await this.queryGet('SELECT 1 AS "exists" FROM "information_schema"."columns" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name"=@table AND "column_name"=\'user\';', {
|
|
2832
|
+
params: {
|
|
2833
|
+
db: this.config.database,
|
|
2834
|
+
table: table3.table_name,
|
|
2835
|
+
},
|
|
2836
|
+
});
|
|
2837
|
+
if (!result?.exists) {
|
|
2838
|
+
return 'tilmeldColumns';
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
return false;
|
|
2842
|
+
}
|
|
2843
|
+
async liveMigration(migrationType) {
|
|
2844
|
+
if (migrationType === 'tokenTables') {
|
|
2845
|
+
const etypes = await this.getEtypes();
|
|
2846
|
+
const connection = await this.getConnection(true);
|
|
2847
|
+
for (let etype of etypes) {
|
|
2848
|
+
await this.createTokensTable(etype, connection);
|
|
2849
|
+
}
|
|
2850
|
+
connection.done();
|
|
2851
|
+
}
|
|
2852
|
+
else if (migrationType === 'tilmeldColumns') {
|
|
2853
|
+
const etypes = await this.getEtypes();
|
|
2854
|
+
const connection = await this.getConnection(true);
|
|
2855
|
+
for (let etype of etypes) {
|
|
2856
|
+
await this.addTilmeldColumnsAndIndexes(etype, connection);
|
|
2857
|
+
}
|
|
2858
|
+
connection.done();
|
|
2859
|
+
}
|
|
2860
|
+
else if (migrationType === 'tilmeldRemoveOldRows') {
|
|
2861
|
+
const etypes = await this.getEtypes();
|
|
2862
|
+
const connection = await this.getConnection(true);
|
|
2863
|
+
for (let etype of etypes) {
|
|
2864
|
+
await this.removeTilmeldOldRows(etype, connection);
|
|
2865
|
+
}
|
|
2866
|
+
connection.done();
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
1857
2869
|
}
|
|
1858
|
-
exports.default = PostgreSQLDriver;
|
|
1859
2870
|
//# sourceMappingURL=PostgreSQLDriver.js.map
|