@nymphjs/driver-postgresql 1.0.0-beta.15 → 1.0.0-beta.151
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 +683 -0
- package/README.md +5 -3
- package/dist/PostgreSQLDriver.d.ts +110 -20
- package/dist/PostgreSQLDriver.js +1759 -776
- 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 +2734 -1255
- 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,80 +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
806
|
const sTable = `s${tableSuffix}`;
|
|
491
|
-
const sort = options.sort
|
|
492
|
-
const queryParts = this.iterateSelectorsForQuery(formattedSelectors, (key, value, typeIsOr, typeIsNot) => {
|
|
807
|
+
const sort = options.sort == null ? 'cdate' : `${options.sort}`;
|
|
808
|
+
const queryParts = this.iterateSelectorsForQuery(formattedSelectors, ({ key, value, typeIsOr, typeIsNot }) => {
|
|
493
809
|
const clauseNot = key.startsWith('!');
|
|
494
810
|
let curQuery = '';
|
|
495
811
|
for (const curValue of value) {
|
|
@@ -502,7 +818,7 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
502
818
|
}
|
|
503
819
|
const guid = `param${++count.i}`;
|
|
504
820
|
curQuery +=
|
|
505
|
-
(
|
|
821
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
506
822
|
ieTable +
|
|
507
823
|
'."guid"=decode(@' +
|
|
508
824
|
guid +
|
|
@@ -518,7 +834,7 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
518
834
|
}
|
|
519
835
|
const tag = `param${++count.i}`;
|
|
520
836
|
curQuery +=
|
|
521
|
-
(
|
|
837
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
522
838
|
'@' +
|
|
523
839
|
tag +
|
|
524
840
|
' <@ ie."tags"';
|
|
@@ -531,17 +847,38 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
531
847
|
if (curQuery) {
|
|
532
848
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
533
849
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
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
|
+
}
|
|
545
882
|
}
|
|
546
883
|
break;
|
|
547
884
|
case 'truthy':
|
|
@@ -550,30 +887,34 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
550
887
|
if (curQuery) {
|
|
551
888
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
552
889
|
}
|
|
553
|
-
if (curVar === 'cdate'
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
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'))) {
|
|
562
901
|
curQuery +=
|
|
563
|
-
(
|
|
902
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
564
903
|
'(' +
|
|
565
904
|
ieTable +
|
|
566
|
-
'.
|
|
567
|
-
|
|
905
|
+
'.' +
|
|
906
|
+
PostgreSQLDriver.escape(curVar) +
|
|
907
|
+
' IS NOT NULL)';
|
|
568
908
|
}
|
|
569
909
|
else {
|
|
570
910
|
const name = `param${++count.i}`;
|
|
571
911
|
curQuery +=
|
|
572
|
-
(
|
|
912
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
913
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
914
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
915
|
+
' WHERE "guid"=' +
|
|
573
916
|
ieTable +
|
|
574
|
-
'."guid"
|
|
575
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
576
|
-
' WHERE "name"=@' +
|
|
917
|
+
'."guid" AND "name"=@' +
|
|
577
918
|
name +
|
|
578
919
|
' AND "truthy"=TRUE)';
|
|
579
920
|
params[name] = curVar;
|
|
@@ -582,35 +923,63 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
582
923
|
break;
|
|
583
924
|
case 'equal':
|
|
584
925
|
case '!equal':
|
|
585
|
-
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'))) {
|
|
586
932
|
if (curQuery) {
|
|
587
933
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
588
934
|
}
|
|
589
|
-
const
|
|
935
|
+
const value = `param${++count.i}`;
|
|
590
936
|
curQuery +=
|
|
591
|
-
(
|
|
937
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
592
938
|
ieTable +
|
|
593
|
-
'.
|
|
594
|
-
|
|
595
|
-
|
|
939
|
+
'.' +
|
|
940
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
941
|
+
'=@' +
|
|
942
|
+
value;
|
|
943
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
596
944
|
? null
|
|
597
945
|
: Number(curValue[1]);
|
|
598
|
-
break;
|
|
599
946
|
}
|
|
600
|
-
else if (
|
|
947
|
+
else if (this.nymph.tilmeld != null &&
|
|
948
|
+
(curValue[0] === 'user' || curValue[0] === 'group')) {
|
|
601
949
|
if (curQuery) {
|
|
602
950
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
603
951
|
}
|
|
604
|
-
const
|
|
952
|
+
const value = `param${++count.i}`;
|
|
605
953
|
curQuery +=
|
|
606
|
-
(
|
|
954
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
607
955
|
ieTable +
|
|
608
|
-
'.
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
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] : '';
|
|
614
983
|
}
|
|
615
984
|
else if (typeof curValue[1] === 'number') {
|
|
616
985
|
if (curQuery) {
|
|
@@ -619,11 +988,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
619
988
|
const name = `param${++count.i}`;
|
|
620
989
|
const value = `param${++count.i}`;
|
|
621
990
|
curQuery +=
|
|
622
|
-
(
|
|
991
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
992
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
993
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
994
|
+
' WHERE "guid"=' +
|
|
623
995
|
ieTable +
|
|
624
|
-
'."guid"
|
|
625
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
626
|
-
' WHERE "name"=@' +
|
|
996
|
+
'."guid" AND "name"=@' +
|
|
627
997
|
name +
|
|
628
998
|
' AND "number"=@' +
|
|
629
999
|
value +
|
|
@@ -638,17 +1008,20 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
638
1008
|
const name = `param${++count.i}`;
|
|
639
1009
|
const value = `param${++count.i}`;
|
|
640
1010
|
curQuery +=
|
|
641
|
-
(
|
|
1011
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1012
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1013
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1014
|
+
' WHERE "guid"=' +
|
|
642
1015
|
ieTable +
|
|
643
|
-
'."guid"
|
|
644
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
645
|
-
' WHERE "name"=@' +
|
|
1016
|
+
'."guid" AND "name"=@' +
|
|
646
1017
|
name +
|
|
647
|
-
' AND "string"
|
|
648
|
-
|
|
1018
|
+
' AND "string"=' +
|
|
1019
|
+
(curValue[1].length < 512
|
|
1020
|
+
? 'LEFT(@' + value + ', 512)'
|
|
1021
|
+
: '@' + value) +
|
|
649
1022
|
')';
|
|
650
1023
|
params[name] = curValue[0];
|
|
651
|
-
params[value] = curValue[1];
|
|
1024
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
652
1025
|
}
|
|
653
1026
|
else {
|
|
654
1027
|
if (curQuery) {
|
|
@@ -665,138 +1038,272 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
665
1038
|
const name = `param${++count.i}`;
|
|
666
1039
|
const value = `param${++count.i}`;
|
|
667
1040
|
curQuery +=
|
|
668
|
-
(
|
|
669
|
-
|
|
670
|
-
'."guid" IN (SELECT "guid" FROM ' +
|
|
1041
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1042
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
671
1043
|
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
672
|
-
' WHERE "
|
|
1044
|
+
' WHERE "guid"=' +
|
|
1045
|
+
ieTable +
|
|
1046
|
+
'."guid" AND "name"=@' +
|
|
673
1047
|
name +
|
|
674
|
-
' AND "
|
|
1048
|
+
' AND "json"=@' +
|
|
675
1049
|
value +
|
|
676
1050
|
')';
|
|
677
1051
|
params[name] = curValue[0];
|
|
678
|
-
params[value] = svalue;
|
|
1052
|
+
params[value] = PostgreSQLDriver.escapeNullSequences(svalue);
|
|
679
1053
|
}
|
|
680
1054
|
break;
|
|
681
1055
|
case 'contain':
|
|
682
1056
|
case '!contain':
|
|
683
|
-
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'))) {
|
|
684
1063
|
if (curQuery) {
|
|
685
1064
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
686
1065
|
}
|
|
687
|
-
const
|
|
1066
|
+
const value = `param${++count.i}`;
|
|
688
1067
|
curQuery +=
|
|
689
|
-
(
|
|
1068
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
690
1069
|
ieTable +
|
|
691
|
-
'.
|
|
692
|
-
|
|
693
|
-
|
|
1070
|
+
'.' +
|
|
1071
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1072
|
+
'=@' +
|
|
1073
|
+
value;
|
|
1074
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
694
1075
|
? null
|
|
695
1076
|
: Number(curValue[1]);
|
|
696
|
-
break;
|
|
697
1077
|
}
|
|
698
|
-
else if (
|
|
1078
|
+
else if (this.nymph.tilmeld != null &&
|
|
1079
|
+
(curValue[0] === 'user' || curValue[0] === 'group')) {
|
|
699
1080
|
if (curQuery) {
|
|
700
1081
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
701
1082
|
}
|
|
702
|
-
const
|
|
1083
|
+
const value = `param${++count.i}`;
|
|
703
1084
|
curQuery +=
|
|
704
|
-
(
|
|
1085
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
705
1086
|
ieTable +
|
|
706
|
-
'.
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
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]}`;
|
|
712
1112
|
}
|
|
713
1113
|
else {
|
|
714
1114
|
if (curQuery) {
|
|
715
1115
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
716
1116
|
}
|
|
717
1117
|
let svalue;
|
|
718
|
-
let stringValue;
|
|
719
1118
|
if (curValue[1] instanceof Object &&
|
|
720
1119
|
typeof curValue[1].toReference === 'function') {
|
|
721
1120
|
svalue = JSON.stringify(curValue[1].toReference());
|
|
722
|
-
stringValue = `${curValue[1].toReference()}`;
|
|
723
1121
|
}
|
|
724
|
-
else
|
|
1122
|
+
else if (typeof curValue[1] === 'string' ||
|
|
1123
|
+
typeof curValue[1] === 'number') {
|
|
725
1124
|
svalue = JSON.stringify(curValue[1]);
|
|
726
|
-
|
|
1125
|
+
}
|
|
1126
|
+
else {
|
|
1127
|
+
svalue = JSON.stringify([curValue[1]]);
|
|
727
1128
|
}
|
|
728
1129
|
const name = `param${++count.i}`;
|
|
729
1130
|
const value = `param${++count.i}`;
|
|
730
|
-
|
|
731
|
-
|
|
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) {
|
|
732
1171
|
curQuery +=
|
|
733
|
-
(
|
|
734
|
-
|
|
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"=' +
|
|
735
1182
|
ieTable +
|
|
736
|
-
'."guid"
|
|
737
|
-
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
738
|
-
' WHERE "name"=@' +
|
|
1183
|
+
'."guid" AND "name"=@' +
|
|
739
1184
|
name +
|
|
740
|
-
' AND
|
|
1185
|
+
' AND "token"=@' +
|
|
741
1186
|
value +
|
|
742
|
-
'
|
|
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"=' +
|
|
743
1241
|
ieTable +
|
|
744
|
-
'."guid"
|
|
745
|
-
|
|
746
|
-
'
|
|
1242
|
+
'."guid" AND t' +
|
|
1243
|
+
tokenTableSuffix +
|
|
1244
|
+
'0."name"=@' +
|
|
747
1245
|
name +
|
|
748
|
-
' AND
|
|
749
|
-
|
|
750
|
-
'))
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
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
|
+
}
|
|
754
1271
|
curQuery +=
|
|
755
|
-
(
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
name +
|
|
761
|
-
' AND position(@' +
|
|
762
|
-
value +
|
|
763
|
-
' IN "value")>0)';
|
|
1272
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1273
|
+
'(' +
|
|
1274
|
+
termStrings.join(' AND ') +
|
|
1275
|
+
')';
|
|
1276
|
+
params[name] = curValue[0];
|
|
764
1277
|
}
|
|
765
|
-
params[name] = curValue[0];
|
|
766
|
-
params[value] = svalue;
|
|
767
1278
|
}
|
|
768
1279
|
break;
|
|
769
1280
|
case 'match':
|
|
770
1281
|
case '!match':
|
|
771
|
-
if (curValue[0] === 'cdate'
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
'
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
')';
|
|
783
|
-
params[cdate] = curValue[1];
|
|
784
|
-
break;
|
|
785
|
-
}
|
|
786
|
-
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'))) {
|
|
787
1293
|
if (curQuery) {
|
|
788
1294
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
789
1295
|
}
|
|
790
|
-
const
|
|
1296
|
+
const value = `param${++count.i}`;
|
|
791
1297
|
curQuery +=
|
|
792
|
-
(
|
|
1298
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
793
1299
|
'(' +
|
|
794
1300
|
ieTable +
|
|
795
|
-
'.
|
|
796
|
-
|
|
1301
|
+
'.' +
|
|
1302
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1303
|
+
' ~ @' +
|
|
1304
|
+
value +
|
|
797
1305
|
')';
|
|
798
|
-
params[
|
|
799
|
-
break;
|
|
1306
|
+
params[value] = curValue[1];
|
|
800
1307
|
}
|
|
801
1308
|
else {
|
|
802
1309
|
if (curQuery) {
|
|
@@ -805,50 +1312,47 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
805
1312
|
const name = `param${++count.i}`;
|
|
806
1313
|
const value = `param${++count.i}`;
|
|
807
1314
|
curQuery +=
|
|
808
|
-
(
|
|
1315
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1316
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1317
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1318
|
+
' WHERE "guid"=' +
|
|
809
1319
|
ieTable +
|
|
810
|
-
'."guid"
|
|
811
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
812
|
-
' WHERE "name"=@' +
|
|
1320
|
+
'."guid" AND "name"=@' +
|
|
813
1321
|
name +
|
|
814
1322
|
' AND "string" ~ @' +
|
|
815
1323
|
value +
|
|
816
1324
|
')';
|
|
817
1325
|
params[name] = curValue[0];
|
|
818
|
-
params[value] = curValue[1];
|
|
1326
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
819
1327
|
}
|
|
820
1328
|
break;
|
|
821
1329
|
case 'imatch':
|
|
822
1330
|
case '!imatch':
|
|
823
|
-
if (curValue[0] === 'cdate'
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
'
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
')';
|
|
835
|
-
params[cdate] = curValue[1];
|
|
836
|
-
break;
|
|
837
|
-
}
|
|
838
|
-
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'))) {
|
|
839
1342
|
if (curQuery) {
|
|
840
1343
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
841
1344
|
}
|
|
842
|
-
const
|
|
1345
|
+
const value = `param${++count.i}`;
|
|
843
1346
|
curQuery +=
|
|
844
|
-
(
|
|
1347
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
845
1348
|
'(' +
|
|
846
1349
|
ieTable +
|
|
847
|
-
'.
|
|
848
|
-
|
|
1350
|
+
'.' +
|
|
1351
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1352
|
+
' ~* @' +
|
|
1353
|
+
value +
|
|
849
1354
|
')';
|
|
850
|
-
params[
|
|
851
|
-
break;
|
|
1355
|
+
params[value] = curValue[1];
|
|
852
1356
|
}
|
|
853
1357
|
else {
|
|
854
1358
|
if (curQuery) {
|
|
@@ -857,50 +1361,47 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
857
1361
|
const name = `param${++count.i}`;
|
|
858
1362
|
const value = `param${++count.i}`;
|
|
859
1363
|
curQuery +=
|
|
860
|
-
(
|
|
1364
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1365
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1366
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1367
|
+
' WHERE "guid"=' +
|
|
861
1368
|
ieTable +
|
|
862
|
-
'."guid"
|
|
863
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
864
|
-
' WHERE "name"=@' +
|
|
1369
|
+
'."guid" AND "name"=@' +
|
|
865
1370
|
name +
|
|
866
1371
|
' AND "string" ~* @' +
|
|
867
1372
|
value +
|
|
868
1373
|
')';
|
|
869
1374
|
params[name] = curValue[0];
|
|
870
|
-
params[value] = curValue[1];
|
|
1375
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
871
1376
|
}
|
|
872
1377
|
break;
|
|
873
1378
|
case 'like':
|
|
874
1379
|
case '!like':
|
|
875
|
-
if (curValue[0] === 'cdate'
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
'
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
')';
|
|
887
|
-
params[cdate] = curValue[1];
|
|
888
|
-
break;
|
|
889
|
-
}
|
|
890
|
-
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'))) {
|
|
891
1391
|
if (curQuery) {
|
|
892
1392
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
893
1393
|
}
|
|
894
|
-
const
|
|
1394
|
+
const value = `param${++count.i}`;
|
|
895
1395
|
curQuery +=
|
|
896
|
-
(
|
|
1396
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
897
1397
|
'(' +
|
|
898
1398
|
ieTable +
|
|
899
|
-
'.
|
|
900
|
-
|
|
1399
|
+
'.' +
|
|
1400
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1401
|
+
' LIKE @' +
|
|
1402
|
+
value +
|
|
901
1403
|
')';
|
|
902
|
-
params[
|
|
903
|
-
break;
|
|
1404
|
+
params[value] = curValue[1];
|
|
904
1405
|
}
|
|
905
1406
|
else {
|
|
906
1407
|
if (curQuery) {
|
|
@@ -909,50 +1410,47 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
909
1410
|
const name = `param${++count.i}`;
|
|
910
1411
|
const value = `param${++count.i}`;
|
|
911
1412
|
curQuery +=
|
|
912
|
-
(
|
|
1413
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1414
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1415
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1416
|
+
' WHERE "guid"=' +
|
|
913
1417
|
ieTable +
|
|
914
|
-
'."guid"
|
|
915
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
916
|
-
' WHERE "name"=@' +
|
|
1418
|
+
'."guid" AND "name"=@' +
|
|
917
1419
|
name +
|
|
918
1420
|
' AND "string" LIKE @' +
|
|
919
1421
|
value +
|
|
920
1422
|
')';
|
|
921
1423
|
params[name] = curValue[0];
|
|
922
|
-
params[value] = curValue[1];
|
|
1424
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
923
1425
|
}
|
|
924
1426
|
break;
|
|
925
1427
|
case 'ilike':
|
|
926
1428
|
case '!ilike':
|
|
927
|
-
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'))) {
|
|
928
1440
|
if (curQuery) {
|
|
929
1441
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
930
1442
|
}
|
|
931
|
-
const
|
|
932
|
-
curQuery +=
|
|
933
|
-
((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
934
|
-
'(' +
|
|
935
|
-
ieTable +
|
|
936
|
-
'."cdate" ILIKE @' +
|
|
937
|
-
cdate +
|
|
938
|
-
')';
|
|
939
|
-
params[cdate] = curValue[1];
|
|
940
|
-
break;
|
|
941
|
-
}
|
|
942
|
-
else if (curValue[0] === 'mdate') {
|
|
943
|
-
if (curQuery) {
|
|
944
|
-
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
945
|
-
}
|
|
946
|
-
const mdate = `param${++count.i}`;
|
|
1443
|
+
const value = `param${++count.i}`;
|
|
947
1444
|
curQuery +=
|
|
948
|
-
(
|
|
1445
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
949
1446
|
'(' +
|
|
950
1447
|
ieTable +
|
|
951
|
-
'.
|
|
952
|
-
|
|
1448
|
+
'.' +
|
|
1449
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1450
|
+
' ILIKE @' +
|
|
1451
|
+
value +
|
|
953
1452
|
')';
|
|
954
|
-
params[
|
|
955
|
-
break;
|
|
1453
|
+
params[value] = curValue[1];
|
|
956
1454
|
}
|
|
957
1455
|
else {
|
|
958
1456
|
if (curQuery) {
|
|
@@ -961,50 +1459,47 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
961
1459
|
const name = `param${++count.i}`;
|
|
962
1460
|
const value = `param${++count.i}`;
|
|
963
1461
|
curQuery +=
|
|
964
|
-
(
|
|
1462
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1463
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1464
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1465
|
+
' WHERE "guid"=' +
|
|
965
1466
|
ieTable +
|
|
966
|
-
'."guid"
|
|
967
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
968
|
-
' WHERE "name"=@' +
|
|
1467
|
+
'."guid" AND "name"=@' +
|
|
969
1468
|
name +
|
|
970
1469
|
' AND "string" ILIKE @' +
|
|
971
1470
|
value +
|
|
972
1471
|
')';
|
|
973
1472
|
params[name] = curValue[0];
|
|
974
|
-
params[value] = curValue[1];
|
|
1473
|
+
params[value] = PostgreSQLDriver.escapeNulls(curValue[1]);
|
|
975
1474
|
}
|
|
976
1475
|
break;
|
|
977
1476
|
case 'gt':
|
|
978
1477
|
case '!gt':
|
|
979
|
-
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'))) {
|
|
980
1489
|
if (curQuery) {
|
|
981
1490
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
982
1491
|
}
|
|
983
|
-
const
|
|
984
|
-
curQuery +=
|
|
985
|
-
((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
986
|
-
ieTable +
|
|
987
|
-
'."cdate">@' +
|
|
988
|
-
cdate;
|
|
989
|
-
params[cdate] = isNaN(Number(curValue[1]))
|
|
990
|
-
? null
|
|
991
|
-
: Number(curValue[1]);
|
|
992
|
-
break;
|
|
993
|
-
}
|
|
994
|
-
else if (curValue[0] === 'mdate') {
|
|
995
|
-
if (curQuery) {
|
|
996
|
-
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
997
|
-
}
|
|
998
|
-
const mdate = `param${++count.i}`;
|
|
1492
|
+
const value = `param${++count.i}`;
|
|
999
1493
|
curQuery +=
|
|
1000
|
-
(
|
|
1494
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1001
1495
|
ieTable +
|
|
1002
|
-
'.
|
|
1003
|
-
|
|
1004
|
-
|
|
1496
|
+
'.' +
|
|
1497
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1498
|
+
'>@' +
|
|
1499
|
+
value;
|
|
1500
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
1005
1501
|
? null
|
|
1006
1502
|
: Number(curValue[1]);
|
|
1007
|
-
break;
|
|
1008
1503
|
}
|
|
1009
1504
|
else {
|
|
1010
1505
|
if (curQuery) {
|
|
@@ -1013,11 +1508,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1013
1508
|
const name = `param${++count.i}`;
|
|
1014
1509
|
const value = `param${++count.i}`;
|
|
1015
1510
|
curQuery +=
|
|
1016
|
-
(
|
|
1511
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1512
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1513
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1514
|
+
' WHERE "guid"=' +
|
|
1017
1515
|
ieTable +
|
|
1018
|
-
'."guid"
|
|
1019
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
1020
|
-
' WHERE "name"=@' +
|
|
1516
|
+
'."guid" AND "name"=@' +
|
|
1021
1517
|
name +
|
|
1022
1518
|
' AND "number">@' +
|
|
1023
1519
|
value +
|
|
@@ -1030,35 +1526,31 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1030
1526
|
break;
|
|
1031
1527
|
case 'gte':
|
|
1032
1528
|
case '!gte':
|
|
1033
|
-
if (curValue[0] === 'cdate'
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
'
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
? null
|
|
1045
|
-
: Number(curValue[1]);
|
|
1046
|
-
break;
|
|
1047
|
-
}
|
|
1048
|
-
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'))) {
|
|
1049
1540
|
if (curQuery) {
|
|
1050
1541
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1051
1542
|
}
|
|
1052
|
-
const
|
|
1543
|
+
const value = `param${++count.i}`;
|
|
1053
1544
|
curQuery +=
|
|
1054
|
-
(
|
|
1545
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1055
1546
|
ieTable +
|
|
1056
|
-
'.
|
|
1057
|
-
|
|
1058
|
-
|
|
1547
|
+
'.' +
|
|
1548
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1549
|
+
'>=@' +
|
|
1550
|
+
value;
|
|
1551
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
1059
1552
|
? null
|
|
1060
1553
|
: Number(curValue[1]);
|
|
1061
|
-
break;
|
|
1062
1554
|
}
|
|
1063
1555
|
else {
|
|
1064
1556
|
if (curQuery) {
|
|
@@ -1067,11 +1559,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1067
1559
|
const name = `param${++count.i}`;
|
|
1068
1560
|
const value = `param${++count.i}`;
|
|
1069
1561
|
curQuery +=
|
|
1070
|
-
(
|
|
1562
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1563
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1564
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1565
|
+
' WHERE "guid"=' +
|
|
1071
1566
|
ieTable +
|
|
1072
|
-
'."guid"
|
|
1073
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
1074
|
-
' WHERE "name"=@' +
|
|
1567
|
+
'."guid" AND "name"=@' +
|
|
1075
1568
|
name +
|
|
1076
1569
|
' AND "number">=@' +
|
|
1077
1570
|
value +
|
|
@@ -1084,35 +1577,31 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1084
1577
|
break;
|
|
1085
1578
|
case 'lt':
|
|
1086
1579
|
case '!lt':
|
|
1087
|
-
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'))) {
|
|
1088
1591
|
if (curQuery) {
|
|
1089
1592
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1090
1593
|
}
|
|
1091
|
-
const
|
|
1092
|
-
curQuery +=
|
|
1093
|
-
((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1094
|
-
ieTable +
|
|
1095
|
-
'."cdate"<@' +
|
|
1096
|
-
cdate;
|
|
1097
|
-
params[cdate] = isNaN(Number(curValue[1]))
|
|
1098
|
-
? null
|
|
1099
|
-
: Number(curValue[1]);
|
|
1100
|
-
break;
|
|
1101
|
-
}
|
|
1102
|
-
else if (curValue[0] === 'mdate') {
|
|
1103
|
-
if (curQuery) {
|
|
1104
|
-
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1105
|
-
}
|
|
1106
|
-
const mdate = `param${++count.i}`;
|
|
1594
|
+
const value = `param${++count.i}`;
|
|
1107
1595
|
curQuery +=
|
|
1108
|
-
(
|
|
1596
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1109
1597
|
ieTable +
|
|
1110
|
-
'.
|
|
1111
|
-
|
|
1112
|
-
|
|
1598
|
+
'.' +
|
|
1599
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1600
|
+
'<@' +
|
|
1601
|
+
value;
|
|
1602
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
1113
1603
|
? null
|
|
1114
1604
|
: Number(curValue[1]);
|
|
1115
|
-
break;
|
|
1116
1605
|
}
|
|
1117
1606
|
else {
|
|
1118
1607
|
if (curQuery) {
|
|
@@ -1121,11 +1610,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1121
1610
|
const name = `param${++count.i}`;
|
|
1122
1611
|
const value = `param${++count.i}`;
|
|
1123
1612
|
curQuery +=
|
|
1124
|
-
(
|
|
1613
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1614
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1615
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1616
|
+
' WHERE "guid"=' +
|
|
1125
1617
|
ieTable +
|
|
1126
|
-
'."guid"
|
|
1127
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
1128
|
-
' WHERE "name"=@' +
|
|
1618
|
+
'."guid" AND "name"=@' +
|
|
1129
1619
|
name +
|
|
1130
1620
|
' AND "number"<@' +
|
|
1131
1621
|
value +
|
|
@@ -1138,35 +1628,31 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1138
1628
|
break;
|
|
1139
1629
|
case 'lte':
|
|
1140
1630
|
case '!lte':
|
|
1141
|
-
if (curValue[0] === 'cdate'
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
'
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
? null
|
|
1153
|
-
: Number(curValue[1]);
|
|
1154
|
-
break;
|
|
1155
|
-
}
|
|
1156
|
-
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'))) {
|
|
1157
1642
|
if (curQuery) {
|
|
1158
1643
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1159
1644
|
}
|
|
1160
|
-
const
|
|
1645
|
+
const value = `param${++count.i}`;
|
|
1161
1646
|
curQuery +=
|
|
1162
|
-
(
|
|
1647
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1163
1648
|
ieTable +
|
|
1164
|
-
'.
|
|
1165
|
-
|
|
1166
|
-
|
|
1649
|
+
'.' +
|
|
1650
|
+
PostgreSQLDriver.escape(curValue[0]) +
|
|
1651
|
+
'<=@' +
|
|
1652
|
+
value;
|
|
1653
|
+
params[value] = isNaN(Number(curValue[1]))
|
|
1167
1654
|
? null
|
|
1168
1655
|
: Number(curValue[1]);
|
|
1169
|
-
break;
|
|
1170
1656
|
}
|
|
1171
1657
|
else {
|
|
1172
1658
|
if (curQuery) {
|
|
@@ -1175,11 +1661,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1175
1661
|
const name = `param${++count.i}`;
|
|
1176
1662
|
const value = `param${++count.i}`;
|
|
1177
1663
|
curQuery +=
|
|
1178
|
-
(
|
|
1664
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1665
|
+
'EXISTS (SELECT "guid" FROM ' +
|
|
1666
|
+
PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
|
|
1667
|
+
' WHERE "guid"=' +
|
|
1179
1668
|
ieTable +
|
|
1180
|
-
'."guid"
|
|
1181
|
-
PostgreSQLDriver.escape(this.prefix + 'comparisons_' + etype) +
|
|
1182
|
-
' WHERE "name"=@' +
|
|
1669
|
+
'."guid" AND "name"=@' +
|
|
1183
1670
|
name +
|
|
1184
1671
|
' AND "number"<=@' +
|
|
1185
1672
|
value +
|
|
@@ -1205,54 +1692,147 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1205
1692
|
if (curQuery) {
|
|
1206
1693
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1207
1694
|
}
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
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
|
+
}
|
|
1222
1741
|
break;
|
|
1223
1742
|
case 'selector':
|
|
1224
1743
|
case '!selector':
|
|
1225
|
-
const
|
|
1744
|
+
const innerquery = this.makeEntityQuery({ ...options, sort: null, limit: undefined }, [curValue], etype, count, params, true, tableSuffix, etypes);
|
|
1226
1745
|
if (curQuery) {
|
|
1227
1746
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1228
1747
|
}
|
|
1229
1748
|
curQuery +=
|
|
1230
|
-
(
|
|
1749
|
+
(xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
|
|
1231
1750
|
'(' +
|
|
1232
|
-
|
|
1751
|
+
innerquery.query +
|
|
1233
1752
|
')';
|
|
1234
1753
|
break;
|
|
1235
1754
|
case 'qref':
|
|
1236
1755
|
case '!qref':
|
|
1756
|
+
const referenceTableSuffix = makeTableSuffix();
|
|
1237
1757
|
const [qrefOptions, ...qrefSelectors] = curValue[1];
|
|
1238
1758
|
const QrefEntityClass = qrefOptions.class;
|
|
1239
1759
|
etypes.push(QrefEntityClass.ETYPE);
|
|
1240
|
-
const qrefQuery = this.makeEntityQuery({ ...qrefOptions, return: 'guid', class: QrefEntityClass }, qrefSelectors, QrefEntityClass.ETYPE, count, params, false, (0, guid_1.makeTableSuffix)(), etypes);
|
|
1241
1760
|
if (curQuery) {
|
|
1242
1761
|
curQuery += typeIsOr ? ' OR ' : ' AND ';
|
|
1243
1762
|
}
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
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(' +
|
|
1247
1799
|
ieTable +
|
|
1248
|
-
'.
|
|
1249
|
-
PostgreSQLDriver.escape(
|
|
1250
|
-
'
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
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
|
+
}
|
|
1256
1836
|
break;
|
|
1257
1837
|
}
|
|
1258
1838
|
}
|
|
@@ -1263,33 +1843,39 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1263
1843
|
let sortJoin = '';
|
|
1264
1844
|
let sortJoinInner = '';
|
|
1265
1845
|
const order = options.reverse ? ' DESC' : '';
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
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
|
+
}
|
|
1293
1879
|
}
|
|
1294
1880
|
let query;
|
|
1295
1881
|
if (queryParts.length) {
|
|
@@ -1298,26 +1884,31 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1298
1884
|
}
|
|
1299
1885
|
else {
|
|
1300
1886
|
let limit = '';
|
|
1301
|
-
if ('limit' in options) {
|
|
1887
|
+
if ('limit' in options && options.limit != null) {
|
|
1302
1888
|
limit = ` LIMIT ${Math.floor(isNaN(Number(options.limit)) ? 0 : Number(options.limit))}`;
|
|
1303
1889
|
}
|
|
1304
1890
|
let offset = '';
|
|
1305
|
-
if ('offset' in options) {
|
|
1891
|
+
if ('offset' in options && options.offset != null) {
|
|
1306
1892
|
offset = ` OFFSET ${Math.floor(isNaN(Number(options.offset)) ? 0 : Number(options.offset))}`;
|
|
1307
1893
|
}
|
|
1308
1894
|
const whereClause = queryParts.join(') AND (');
|
|
1895
|
+
const guidClause = guidExplicitSelector
|
|
1896
|
+
? `${guidExplicitSelector(`${ieTable}."guid"`)} AND `
|
|
1897
|
+
: guidSelector
|
|
1898
|
+
? `${ieTable}."guid"=${guidSelector} AND `
|
|
1899
|
+
: '';
|
|
1309
1900
|
if (options.return === 'count') {
|
|
1310
1901
|
if (limit || offset) {
|
|
1311
1902
|
query = `SELECT COUNT(${countTable}."guid") AS "count" FROM (
|
|
1312
|
-
SELECT
|
|
1903
|
+
SELECT ${ieTable}."guid" AS "guid"
|
|
1313
1904
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1314
|
-
WHERE (${whereClause})${limit}${offset}
|
|
1905
|
+
WHERE ${guidClause}(${whereClause})${limit}${offset}
|
|
1315
1906
|
) ${countTable}`;
|
|
1316
1907
|
}
|
|
1317
1908
|
else {
|
|
1318
1909
|
query = `SELECT COUNT(${ieTable}."guid") AS "count"
|
|
1319
1910
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1320
|
-
WHERE (${whereClause})`;
|
|
1911
|
+
WHERE ${guidClause}(${whereClause})`;
|
|
1321
1912
|
}
|
|
1322
1913
|
}
|
|
1323
1914
|
else if (options.return === 'guid') {
|
|
@@ -1327,8 +1918,8 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1327
1918
|
query = `SELECT ${guidColumn} AS "guid"
|
|
1328
1919
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1329
1920
|
${sortJoinInner}
|
|
1330
|
-
WHERE (${whereClause})
|
|
1331
|
-
ORDER BY
|
|
1921
|
+
WHERE ${guidClause}(${whereClause})
|
|
1922
|
+
${sortByInner ? sortByInner + ', ' : 'ORDER BY '}${ieTable}."guid"${limit}${offset}`;
|
|
1332
1923
|
}
|
|
1333
1924
|
else {
|
|
1334
1925
|
query = `SELECT
|
|
@@ -1336,22 +1927,30 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1336
1927
|
${eTable}."tags",
|
|
1337
1928
|
${eTable}."cdate",
|
|
1338
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",
|
|
1339
1938
|
${dTable}."name",
|
|
1340
1939
|
${dTable}."value",
|
|
1341
|
-
${
|
|
1342
|
-
${
|
|
1940
|
+
${dTable}."json",
|
|
1941
|
+
${dTable}."string",
|
|
1942
|
+
${dTable}."number"
|
|
1343
1943
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
|
|
1344
1944
|
LEFT JOIN ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
|
|
1345
|
-
INNER JOIN ${PostgreSQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}."guid"=${cTable}."guid" AND ${dTable}."name"=${cTable}."name"
|
|
1346
1945
|
${sortJoin}
|
|
1347
1946
|
INNER JOIN (
|
|
1348
1947
|
SELECT ${ieTable}."guid"
|
|
1349
1948
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1350
1949
|
${sortJoinInner}
|
|
1351
|
-
WHERE (${whereClause})
|
|
1352
|
-
|
|
1950
|
+
WHERE ${guidClause}(${whereClause})
|
|
1951
|
+
${sortByInner}${limit}${offset}
|
|
1353
1952
|
) ${fTable} ON ${eTable}."guid"=${fTable}."guid"
|
|
1354
|
-
ORDER BY
|
|
1953
|
+
${sortBy ? sortBy + ', ' : 'ORDER BY '}${eTable}."guid"`;
|
|
1355
1954
|
}
|
|
1356
1955
|
}
|
|
1357
1956
|
}
|
|
@@ -1361,23 +1960,28 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1361
1960
|
}
|
|
1362
1961
|
else {
|
|
1363
1962
|
let limit = '';
|
|
1364
|
-
if ('limit' in options) {
|
|
1963
|
+
if ('limit' in options && options.limit != null) {
|
|
1365
1964
|
limit = ` LIMIT ${Math.floor(isNaN(Number(options.limit)) ? 0 : Number(options.limit))}`;
|
|
1366
1965
|
}
|
|
1367
1966
|
let offset = '';
|
|
1368
|
-
if ('offset' in options) {
|
|
1967
|
+
if ('offset' in options && options.offset != null) {
|
|
1369
1968
|
offset = ` OFFSET ${Math.floor(isNaN(Number(options.offset)) ? 0 : Number(options.offset))}`;
|
|
1370
1969
|
}
|
|
1970
|
+
const guidClause = guidExplicitSelector
|
|
1971
|
+
? ` WHERE ${guidExplicitSelector(`${ieTable}."guid"`)}`
|
|
1972
|
+
: guidSelector
|
|
1973
|
+
? ` WHERE ${ieTable}."guid"=${guidSelector}`
|
|
1974
|
+
: '';
|
|
1371
1975
|
if (options.return === 'count') {
|
|
1372
1976
|
if (limit || offset) {
|
|
1373
1977
|
query = `SELECT COUNT(${countTable}."guid") AS "count" FROM (
|
|
1374
|
-
SELECT
|
|
1375
|
-
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}
|
|
1376
1980
|
) ${countTable}`;
|
|
1377
1981
|
}
|
|
1378
1982
|
else {
|
|
1379
1983
|
query = `SELECT COUNT(${ieTable}."guid") AS "count"
|
|
1380
|
-
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}`;
|
|
1984
|
+
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}${guidClause}`;
|
|
1381
1985
|
}
|
|
1382
1986
|
}
|
|
1383
1987
|
else if (options.return === 'guid') {
|
|
@@ -1387,7 +1991,8 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1387
1991
|
query = `SELECT ${guidColumn} AS "guid"
|
|
1388
1992
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1389
1993
|
${sortJoinInner}
|
|
1390
|
-
|
|
1994
|
+
${guidClause}
|
|
1995
|
+
${sortByInner ? sortByInner + ', ' : 'ORDER BY '}${ieTable}."guid"${limit}${offset}`;
|
|
1391
1996
|
}
|
|
1392
1997
|
else {
|
|
1393
1998
|
if (limit || offset) {
|
|
@@ -1396,21 +2001,30 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1396
2001
|
${eTable}."tags",
|
|
1397
2002
|
${eTable}."cdate",
|
|
1398
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",
|
|
1399
2012
|
${dTable}."name",
|
|
1400
2013
|
${dTable}."value",
|
|
1401
|
-
${
|
|
1402
|
-
${
|
|
2014
|
+
${dTable}."json",
|
|
2015
|
+
${dTable}."string",
|
|
2016
|
+
${dTable}."number"
|
|
1403
2017
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
|
|
1404
2018
|
LEFT JOIN ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
|
|
1405
|
-
INNER JOIN ${PostgreSQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}."guid"=${cTable}."guid" AND ${dTable}."name"=${cTable}."name"
|
|
1406
2019
|
${sortJoin}
|
|
1407
2020
|
INNER JOIN (
|
|
1408
2021
|
SELECT ${ieTable}."guid"
|
|
1409
2022
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
|
|
1410
2023
|
${sortJoinInner}
|
|
1411
|
-
|
|
2024
|
+
${guidClause}
|
|
2025
|
+
${sortByInner}${limit}${offset}
|
|
1412
2026
|
) ${fTable} ON ${eTable}."guid"=${fTable}."guid"
|
|
1413
|
-
ORDER BY
|
|
2027
|
+
${sortBy ? sortBy + ', ' : 'ORDER BY '}${eTable}."guid"`;
|
|
1414
2028
|
}
|
|
1415
2029
|
else {
|
|
1416
2030
|
query = `SELECT
|
|
@@ -1418,15 +2032,24 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1418
2032
|
${eTable}."tags",
|
|
1419
2033
|
${eTable}."cdate",
|
|
1420
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",
|
|
1421
2043
|
${dTable}."name",
|
|
1422
2044
|
${dTable}."value",
|
|
1423
|
-
${
|
|
1424
|
-
${
|
|
2045
|
+
${dTable}."json",
|
|
2046
|
+
${dTable}."string",
|
|
2047
|
+
${dTable}."number"
|
|
1425
2048
|
FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
|
|
1426
2049
|
LEFT JOIN ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
|
|
1427
|
-
INNER JOIN ${PostgreSQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}."guid"=${cTable}."guid" AND ${dTable}."name"=${cTable}."name"
|
|
1428
2050
|
${sortJoin}
|
|
1429
|
-
|
|
2051
|
+
${guidExplicitSelector ? `WHERE ${guidExplicitSelector(`${eTable}."guid"`)}` : guidSelector ? `WHERE ${eTable}."guid"=${guidSelector}` : ''}
|
|
2052
|
+
${sortBy ? sortBy + ', ' : 'ORDER BY '}${eTable}."guid"`;
|
|
1430
2053
|
}
|
|
1431
2054
|
}
|
|
1432
2055
|
}
|
|
@@ -1442,33 +2065,38 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1442
2065
|
}
|
|
1443
2066
|
performQuery(options, formattedSelectors, etype) {
|
|
1444
2067
|
const { query, params, etypes } = this.makeEntityQuery(options, formattedSelectors, etype);
|
|
1445
|
-
const result = this.
|
|
1446
|
-
return {
|
|
1447
|
-
result,
|
|
1448
|
-
};
|
|
1449
|
-
}
|
|
1450
|
-
performQuerySync(options, formattedSelectors, etype) {
|
|
1451
|
-
const { query, params, etypes } = this.makeEntityQuery(options, formattedSelectors, etype);
|
|
1452
|
-
const result = (this.queryIterSync(query, { etypes, params }) || [])[Symbol.iterator]();
|
|
2068
|
+
const result = this.queryArray(query, { etypes, params }).then((val) => val[Symbol.iterator]());
|
|
1453
2069
|
return {
|
|
1454
2070
|
result,
|
|
1455
2071
|
};
|
|
1456
2072
|
}
|
|
1457
2073
|
async getEntities(options = {}, ...selectors) {
|
|
1458
|
-
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), () => {
|
|
1459
2077
|
const next = result.next();
|
|
1460
2078
|
return next.done ? null : next.value;
|
|
1461
2079
|
}, () => undefined, (row) => Number(row.count), (row) => row.guid, (row) => ({
|
|
1462
|
-
tags: row.tags,
|
|
1463
|
-
cdate: isNaN(Number(row.cdate)) ?
|
|
1464
|
-
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) ?? [],
|
|
1465
2091
|
}), (row) => ({
|
|
1466
2092
|
name: row.name,
|
|
1467
2093
|
svalue: row.value === 'N'
|
|
1468
2094
|
? JSON.stringify(Number(row.number))
|
|
1469
2095
|
: row.value === 'S'
|
|
1470
|
-
? JSON.stringify(row.string)
|
|
1471
|
-
: row.value
|
|
2096
|
+
? JSON.stringify(PostgreSQLDriver.unescapeNulls(row.string))
|
|
2097
|
+
: row.value === 'J'
|
|
2098
|
+
? PostgreSQLDriver.unescapeNullSequences(JSON.stringify(row.json))
|
|
2099
|
+
: row.value,
|
|
1472
2100
|
}));
|
|
1473
2101
|
const result = await resultPromise;
|
|
1474
2102
|
const value = process();
|
|
@@ -1477,31 +2105,12 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1477
2105
|
}
|
|
1478
2106
|
return value;
|
|
1479
2107
|
}
|
|
1480
|
-
getEntitiesSync(options = {}, ...selectors) {
|
|
1481
|
-
const { result, process } = this.getEntitesRowLike(options, selectors, (options, formattedSelectors, etype) => this.performQuerySync(options, formattedSelectors, etype), () => {
|
|
1482
|
-
const next = result.next();
|
|
1483
|
-
return next.done ? null : next.value;
|
|
1484
|
-
}, () => undefined, (row) => Number(row.count), (row) => row.guid, (row) => ({
|
|
1485
|
-
tags: row.tags,
|
|
1486
|
-
cdate: isNaN(Number(row.cdate)) ? null : Number(row.cdate),
|
|
1487
|
-
mdate: isNaN(Number(row.mdate)) ? null : Number(row.mdate),
|
|
1488
|
-
}), (row) => ({
|
|
1489
|
-
name: row.name,
|
|
1490
|
-
svalue: row.value === 'N'
|
|
1491
|
-
? JSON.stringify(Number(row.number))
|
|
1492
|
-
: row.value === 'S'
|
|
1493
|
-
? JSON.stringify(row.string)
|
|
1494
|
-
: row.value,
|
|
1495
|
-
}));
|
|
1496
|
-
const value = process();
|
|
1497
|
-
if (value instanceof Error) {
|
|
1498
|
-
throw value;
|
|
1499
|
-
}
|
|
1500
|
-
return value;
|
|
1501
|
-
}
|
|
1502
2108
|
async getUID(name) {
|
|
1503
2109
|
if (name == null) {
|
|
1504
|
-
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.');
|
|
1505
2114
|
}
|
|
1506
2115
|
const result = await this.queryGet(`SELECT "cur_uid" FROM ${PostgreSQLDriver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
|
|
1507
2116
|
params: {
|
|
@@ -1510,51 +2119,81 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1510
2119
|
});
|
|
1511
2120
|
return result?.cur_uid == null ? null : Number(result.cur_uid);
|
|
1512
2121
|
}
|
|
1513
|
-
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 } = {}) {
|
|
1514
2132
|
try {
|
|
1515
|
-
|
|
1516
|
-
|
|
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');`, {
|
|
1517
2136
|
etypes: [etype],
|
|
1518
2137
|
params: {
|
|
1519
2138
|
guid,
|
|
1520
2139
|
},
|
|
1521
|
-
});
|
|
1522
|
-
|
|
2140
|
+
}));
|
|
2141
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1523
2142
|
etypes: [etype],
|
|
1524
2143
|
params: {
|
|
1525
2144
|
guid,
|
|
1526
|
-
tags,
|
|
1527
|
-
cdate: isNaN(Number(JSON.parse(sdata.cdate)))
|
|
1528
|
-
? null
|
|
1529
|
-
: Number(JSON.parse(sdata.cdate)),
|
|
1530
|
-
mdate: isNaN(Number(JSON.parse(sdata.mdate)))
|
|
1531
|
-
? null
|
|
1532
|
-
: Number(JSON.parse(sdata.mdate)),
|
|
1533
2145
|
},
|
|
1534
|
-
});
|
|
1535
|
-
|
|
1536
|
-
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');`, {
|
|
1537
2148
|
etypes: [etype],
|
|
1538
2149
|
params: {
|
|
1539
2150
|
guid,
|
|
1540
2151
|
},
|
|
1541
2152
|
}));
|
|
1542
|
-
|
|
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');`, {
|
|
1543
2156
|
etypes: [etype],
|
|
1544
2157
|
params: {
|
|
1545
2158
|
guid,
|
|
1546
2159
|
},
|
|
1547
2160
|
}));
|
|
1548
|
-
|
|
2161
|
+
}
|
|
2162
|
+
if (only == null) {
|
|
2163
|
+
promises.push(this.queryRun(`DELETE FROM ${PostgreSQLDriver.escape(`${this.prefix}uniques_${etype}`)} WHERE "guid"=decode(@guid, 'hex');`, {
|
|
1549
2164
|
etypes: [etype],
|
|
1550
2165
|
params: {
|
|
1551
2166
|
guid,
|
|
1552
2167
|
},
|
|
1553
2168
|
}));
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
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
|
+
});
|
|
1557
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
|
+
}
|
|
1558
2197
|
const value = sdata[name];
|
|
1559
2198
|
const uvalue = JSON.parse(value);
|
|
1560
2199
|
if (value === undefined) {
|
|
@@ -1564,24 +2203,22 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1564
2203
|
? 'N'
|
|
1565
2204
|
: typeof uvalue === 'string'
|
|
1566
2205
|
? 'S'
|
|
1567
|
-
:
|
|
1568
|
-
const
|
|
1569
|
-
|
|
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);`, {
|
|
1570
2211
|
etypes: [etype],
|
|
1571
2212
|
params: {
|
|
1572
2213
|
guid,
|
|
1573
2214
|
name,
|
|
1574
2215
|
storageValue,
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
params: {
|
|
1580
|
-
guid,
|
|
1581
|
-
name,
|
|
1582
|
-
truthy: !!uvalue,
|
|
1583
|
-
string: `${uvalue}`,
|
|
2216
|
+
jsonValue,
|
|
2217
|
+
string: storageValue === 'J'
|
|
2218
|
+
? null
|
|
2219
|
+
: PostgreSQLDriver.escapeNulls(`${uvalue}`),
|
|
1584
2220
|
number: isNaN(Number(uvalue)) ? null : Number(uvalue),
|
|
2221
|
+
truthy: !!uvalue,
|
|
1585
2222
|
},
|
|
1586
2223
|
}));
|
|
1587
2224
|
const references = this.findReferences(value);
|
|
@@ -1596,43 +2233,136 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1596
2233
|
}));
|
|
1597
2234
|
}
|
|
1598
2235
|
}
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
},
|
|
1605
|
-
});
|
|
1606
|
-
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],
|
|
1607
2241
|
params: {
|
|
1608
|
-
|
|
1609
|
-
|
|
2242
|
+
user,
|
|
2243
|
+
group,
|
|
2244
|
+
acUser,
|
|
2245
|
+
acGroup,
|
|
2246
|
+
acOther,
|
|
2247
|
+
acRead,
|
|
2248
|
+
acWrite,
|
|
2249
|
+
acFull,
|
|
2250
|
+
guid,
|
|
1610
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,
|
|
1611
2302
|
});
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
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
|
+
},
|
|
1616
2335
|
});
|
|
1617
|
-
|
|
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}`);
|
|
1618
2343
|
}
|
|
1619
2344
|
catch (e) {
|
|
1620
|
-
|
|
1621
|
-
|
|
2345
|
+
this.nymph.config.debugError('postgresql', `Import UID error: "${e}"`);
|
|
2346
|
+
await this.rollback(`nymph-import-uid-${name}`);
|
|
2347
|
+
throw e;
|
|
1622
2348
|
}
|
|
1623
2349
|
}
|
|
1624
2350
|
async newUID(name) {
|
|
1625
2351
|
if (name == null) {
|
|
1626
|
-
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.');
|
|
1627
2356
|
}
|
|
1628
2357
|
await this.internalTransaction('nymph-newuid');
|
|
2358
|
+
let curUid = undefined;
|
|
1629
2359
|
try {
|
|
1630
2360
|
const lock = await this.queryGet(`SELECT "cur_uid" FROM ${PostgreSQLDriver.escape(`${this.prefix}uids`)} WHERE "name"=@name FOR UPDATE;`, {
|
|
1631
2361
|
params: {
|
|
1632
2362
|
name,
|
|
1633
2363
|
},
|
|
1634
2364
|
});
|
|
1635
|
-
|
|
2365
|
+
curUid = lock?.cur_uid == null ? undefined : Number(lock.cur_uid);
|
|
1636
2366
|
if (curUid == null) {
|
|
1637
2367
|
curUid = 1;
|
|
1638
2368
|
await this.queryRun(`INSERT INTO ${PostgreSQLDriver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
|
|
@@ -1651,17 +2381,21 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1651
2381
|
},
|
|
1652
2382
|
});
|
|
1653
2383
|
}
|
|
1654
|
-
await this.commit('nymph-newuid');
|
|
1655
|
-
return curUid;
|
|
1656
2384
|
}
|
|
1657
2385
|
catch (e) {
|
|
2386
|
+
this.nymph.config.debugError('postgresql', `New UID error: "${e}"`);
|
|
1658
2387
|
await this.rollback('nymph-newuid');
|
|
1659
2388
|
throw e;
|
|
1660
2389
|
}
|
|
2390
|
+
await this.commit('nymph-newuid');
|
|
2391
|
+
return curUid;
|
|
1661
2392
|
}
|
|
1662
2393
|
async renameUID(oldName, newName) {
|
|
1663
2394
|
if (oldName == null || newName == null) {
|
|
1664
|
-
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.');
|
|
1665
2399
|
}
|
|
1666
2400
|
await this.queryRun(`UPDATE ${PostgreSQLDriver.escape(`${this.prefix}uids`)} SET "name"=@newName WHERE "name"=@oldName;`, {
|
|
1667
2401
|
params: {
|
|
@@ -1673,7 +2407,7 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1673
2407
|
}
|
|
1674
2408
|
async rollback(name) {
|
|
1675
2409
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
1676
|
-
throw new
|
|
2410
|
+
throw new InvalidParametersError('Transaction rollback attempted without a name.');
|
|
1677
2411
|
}
|
|
1678
2412
|
if (!this.transaction || this.transaction.count === 0) {
|
|
1679
2413
|
this.transaction = null;
|
|
@@ -1690,8 +2424,14 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1690
2424
|
return true;
|
|
1691
2425
|
}
|
|
1692
2426
|
async saveEntity(entity) {
|
|
1693
|
-
const insertData = async (guid, data, sdata, etype) => {
|
|
2427
|
+
const insertData = async (guid, data, sdata, uniques, etype) => {
|
|
2428
|
+
const EntityClass = this.nymph.getEntityClassByEtype(etype);
|
|
1694
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
|
+
}
|
|
1695
2435
|
if (value === undefined) {
|
|
1696
2436
|
return;
|
|
1697
2437
|
}
|
|
@@ -1699,24 +2439,23 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1699
2439
|
? 'N'
|
|
1700
2440
|
: typeof value === 'string'
|
|
1701
2441
|
? 'S'
|
|
1702
|
-
:
|
|
2442
|
+
: 'J';
|
|
2443
|
+
const jsonValue = storageValue === 'J'
|
|
2444
|
+
? PostgreSQLDriver.escapeNullSequences(svalue)
|
|
2445
|
+
: null;
|
|
1703
2446
|
const promises = [];
|
|
1704
|
-
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);`, {
|
|
1705
2448
|
etypes: [etype],
|
|
1706
2449
|
params: {
|
|
1707
2450
|
guid,
|
|
1708
2451
|
name,
|
|
1709
2452
|
storageValue,
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
params: {
|
|
1715
|
-
guid,
|
|
1716
|
-
name,
|
|
1717
|
-
truthy: !!value,
|
|
1718
|
-
string: `${value}`,
|
|
2453
|
+
jsonValue,
|
|
2454
|
+
string: storageValue === 'J'
|
|
2455
|
+
? null
|
|
2456
|
+
: PostgreSQLDriver.escapeNulls(`${value}`),
|
|
1719
2457
|
number: isNaN(Number(value)) ? null : Number(value),
|
|
2458
|
+
truthy: !!value,
|
|
1720
2459
|
},
|
|
1721
2460
|
}));
|
|
1722
2461
|
const references = this.findReferences(svalue);
|
|
@@ -1730,8 +2469,60 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1730
2469
|
},
|
|
1731
2470
|
}));
|
|
1732
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
|
+
}
|
|
1733
2507
|
await Promise.all(promises);
|
|
1734
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
|
+
}
|
|
1735
2526
|
for (const name in data) {
|
|
1736
2527
|
await runInsertQuery(name, data[name], JSON.stringify(data[name]));
|
|
1737
2528
|
}
|
|
@@ -1739,19 +2530,56 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1739
2530
|
await runInsertQuery(name, JSON.parse(sdata[name]), sdata[name]);
|
|
1740
2531
|
}
|
|
1741
2532
|
};
|
|
2533
|
+
let inTransaction = false;
|
|
1742
2534
|
try {
|
|
1743
|
-
const result = await this.saveEntityRowLike(entity, async (
|
|
1744
|
-
|
|
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)"});`, {
|
|
1745
2551
|
etypes: [etype],
|
|
1746
2552
|
params: {
|
|
1747
2553
|
guid,
|
|
1748
2554
|
tags,
|
|
1749
2555
|
cdate,
|
|
2556
|
+
user,
|
|
2557
|
+
group,
|
|
2558
|
+
acUser,
|
|
2559
|
+
acGroup,
|
|
2560
|
+
acOther,
|
|
2561
|
+
acRead,
|
|
2562
|
+
acWrite,
|
|
2563
|
+
acFull,
|
|
1750
2564
|
},
|
|
1751
2565
|
});
|
|
1752
|
-
await insertData(guid, data, sdata, etype);
|
|
2566
|
+
await insertData(guid, data, sdata, uniques, etype);
|
|
1753
2567
|
return true;
|
|
1754
|
-
}, 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;
|
|
1755
2583
|
const promises = [];
|
|
1756
2584
|
promises.push(this.queryRun(`SELECT 1 FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=decode(@guid, 'hex') FOR UPDATE;`, {
|
|
1757
2585
|
etypes: [etype],
|
|
@@ -1765,24 +2593,38 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1765
2593
|
guid,
|
|
1766
2594
|
},
|
|
1767
2595
|
}));
|
|
1768
|
-
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;`, {
|
|
1769
2597
|
etypes: [etype],
|
|
1770
2598
|
params: {
|
|
1771
2599
|
guid,
|
|
1772
2600
|
},
|
|
1773
2601
|
}));
|
|
1774
|
-
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;`, {
|
|
1775
2609
|
etypes: [etype],
|
|
1776
2610
|
params: {
|
|
1777
2611
|
guid,
|
|
1778
2612
|
},
|
|
1779
2613
|
}));
|
|
1780
2614
|
await Promise.all(promises);
|
|
1781
|
-
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;`, {
|
|
1782
2616
|
etypes: [etype],
|
|
1783
2617
|
params: {
|
|
1784
2618
|
tags,
|
|
1785
2619
|
mdate,
|
|
2620
|
+
user,
|
|
2621
|
+
group,
|
|
2622
|
+
acUser,
|
|
2623
|
+
acGroup,
|
|
2624
|
+
acOther,
|
|
2625
|
+
acRead,
|
|
2626
|
+
acWrite,
|
|
2627
|
+
acFull,
|
|
1786
2628
|
guid,
|
|
1787
2629
|
emdate: isNaN(Number(entity.mdate)) ? 0 : Number(entity.mdate),
|
|
1788
2630
|
},
|
|
@@ -1796,44 +2638,63 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1796
2638
|
guid,
|
|
1797
2639
|
},
|
|
1798
2640
|
}));
|
|
1799
|
-
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');`, {
|
|
1800
2642
|
etypes: [etype],
|
|
1801
2643
|
params: {
|
|
1802
2644
|
guid,
|
|
1803
2645
|
},
|
|
1804
2646
|
}));
|
|
1805
|
-
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');`, {
|
|
1806
2654
|
etypes: [etype],
|
|
1807
2655
|
params: {
|
|
1808
2656
|
guid,
|
|
1809
2657
|
},
|
|
1810
2658
|
}));
|
|
1811
2659
|
await Promise.all(promises);
|
|
1812
|
-
await insertData(guid, data, sdata, etype);
|
|
2660
|
+
await insertData(guid, data, sdata, uniques, etype);
|
|
1813
2661
|
success = true;
|
|
1814
2662
|
}
|
|
2663
|
+
if (!success && info.rowCount === 0) {
|
|
2664
|
+
throw new EntityConflictError("Entity either doesn't exist or is newer in the DB.");
|
|
2665
|
+
}
|
|
1815
2666
|
return success;
|
|
1816
2667
|
}, async () => {
|
|
1817
2668
|
await this.internalTransaction('nymph-save');
|
|
2669
|
+
inTransaction = true;
|
|
1818
2670
|
}, async (success) => {
|
|
1819
|
-
if (
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
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
|
+
}
|
|
1824
2679
|
}
|
|
1825
2680
|
return success;
|
|
1826
2681
|
});
|
|
1827
2682
|
return result;
|
|
1828
2683
|
}
|
|
1829
2684
|
catch (e) {
|
|
1830
|
-
|
|
2685
|
+
this.nymph.config.debugError('postgresql', `Save entity error: "${e}"`);
|
|
2686
|
+
if (inTransaction) {
|
|
2687
|
+
await this.rollback('nymph-save');
|
|
2688
|
+
}
|
|
1831
2689
|
throw e;
|
|
1832
2690
|
}
|
|
1833
2691
|
}
|
|
1834
2692
|
async setUID(name, curUid) {
|
|
1835
2693
|
if (name == null) {
|
|
1836
|
-
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.');
|
|
1837
2698
|
}
|
|
1838
2699
|
await this.internalTransaction('nymph-setuid');
|
|
1839
2700
|
try {
|
|
@@ -1849,39 +2710,161 @@ class PostgreSQLDriver extends nymph_1.NymphDriver {
|
|
|
1849
2710
|
curUid,
|
|
1850
2711
|
},
|
|
1851
2712
|
});
|
|
1852
|
-
await this.commit('nymph-setuid');
|
|
1853
|
-
return true;
|
|
1854
2713
|
}
|
|
1855
2714
|
catch (e) {
|
|
1856
2715
|
await this.rollback('nymph-setuid');
|
|
1857
2716
|
throw e;
|
|
1858
2717
|
}
|
|
2718
|
+
await this.commit('nymph-setuid');
|
|
2719
|
+
return true;
|
|
1859
2720
|
}
|
|
1860
|
-
async internalTransaction(name) {
|
|
2721
|
+
async internalTransaction(name, setOnSelf = true) {
|
|
1861
2722
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
1862
|
-
throw new
|
|
2723
|
+
throw new InvalidParametersError('Transaction start attempted without a name.');
|
|
1863
2724
|
}
|
|
1864
|
-
|
|
1865
|
-
|
|
2725
|
+
let transaction = this.transaction;
|
|
2726
|
+
if (!transaction) {
|
|
2727
|
+
// Lock to one connection.
|
|
2728
|
+
transaction = {
|
|
1866
2729
|
count: 0,
|
|
1867
2730
|
connection: await this.getConnection(),
|
|
2731
|
+
latestPromise: Promise.resolve(),
|
|
1868
2732
|
};
|
|
1869
|
-
|
|
2733
|
+
if (setOnSelf) {
|
|
2734
|
+
this.transaction = transaction;
|
|
2735
|
+
}
|
|
1870
2736
|
}
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
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;
|
|
1874
2746
|
}
|
|
1875
2747
|
async startTransaction(name) {
|
|
1876
|
-
const
|
|
1877
|
-
const transaction = await this.internalTransaction(name);
|
|
1878
|
-
if (!inTransaction) {
|
|
1879
|
-
this.transaction = null;
|
|
1880
|
-
}
|
|
2748
|
+
const transaction = await this.internalTransaction(name, false);
|
|
1881
2749
|
const nymph = this.nymph.clone();
|
|
1882
2750
|
nymph.driver.transaction = transaction;
|
|
1883
2751
|
return nymph;
|
|
1884
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
|
+
}
|
|
1885
2869
|
}
|
|
1886
|
-
exports.default = PostgreSQLDriver;
|
|
1887
2870
|
//# sourceMappingURL=PostgreSQLDriver.js.map
|