@oino-ts/db-postgresql 0.21.1 → 1.0.0
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/dist/cjs/OINODbPostgresql.js +74 -73
- package/dist/esm/OINODbPostgresql.js +68 -67
- package/dist/types/OINODbPostgresql.d.ts +12 -12
- package/package.json +38 -38
- package/src/OINODbPostgresql.ts +492 -491
- package/src/index.ts +1 -1
|
@@ -10,10 +10,10 @@ const common_1 = require("@oino-ts/common");
|
|
|
10
10
|
const db_1 = require("@oino-ts/db");
|
|
11
11
|
const pg_1 = require("pg");
|
|
12
12
|
/**
|
|
13
|
-
* Implmentation of
|
|
13
|
+
* Implmentation of OINODataSet for Postgresql.
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
|
-
class OINOPostgresqlData extends
|
|
16
|
+
class OINOPostgresqlData extends common_1.OINODataSet {
|
|
17
17
|
_rows;
|
|
18
18
|
/**
|
|
19
19
|
* OINOPostgresqlData constructor
|
|
@@ -72,7 +72,7 @@ class OINOPostgresqlData extends db_1.OINODbDataSet {
|
|
|
72
72
|
return this._rows[this._currentRow];
|
|
73
73
|
}
|
|
74
74
|
else {
|
|
75
|
-
return
|
|
75
|
+
return common_1.OINO_EMPTY_ROW;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
@@ -95,12 +95,12 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
95
95
|
*/
|
|
96
96
|
constructor(params) {
|
|
97
97
|
super(params);
|
|
98
|
-
if (this.
|
|
99
|
-
throw new Error(common_1.OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this.
|
|
98
|
+
if (this.dbParams.type !== "OINODbPostgresql") {
|
|
99
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this.dbParams.type);
|
|
100
100
|
}
|
|
101
|
-
const ssl_enabled = !(this.
|
|
102
|
-
this._pool = new pg_1.Pool({ host: this.
|
|
103
|
-
delete this.
|
|
101
|
+
const ssl_enabled = !(this.dbParams.url == "localhost" || this.dbParams.url == "127.0.0.1");
|
|
102
|
+
this._pool = new pg_1.Pool({ host: this.dbParams.url, database: this.dbParams.database, port: this.dbParams.port, user: this.dbParams.user, password: this.dbParams.password, ssl: ssl_enabled });
|
|
103
|
+
delete this.dbParams.password;
|
|
104
104
|
this._pool.on("error", (err) => {
|
|
105
105
|
common_1.OINOLog.error("@oino-ts/db-postgresql", "OINODbPostgresql", ".on(error)", "Error-event", { err: err });
|
|
106
106
|
});
|
|
@@ -125,12 +125,12 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
125
125
|
rows = query_result.rows;
|
|
126
126
|
}
|
|
127
127
|
else {
|
|
128
|
-
rows =
|
|
128
|
+
rows = common_1.OINO_EMPTY_ROWS; // return empty row if no rows returned
|
|
129
129
|
}
|
|
130
130
|
return new OINOPostgresqlData(rows, []);
|
|
131
131
|
}
|
|
132
132
|
catch (e) {
|
|
133
|
-
return new OINOPostgresqlData(
|
|
133
|
+
return new OINOPostgresqlData(common_1.OINO_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbPostgresql._query");
|
|
134
134
|
}
|
|
135
135
|
finally {
|
|
136
136
|
if (connection) {
|
|
@@ -151,13 +151,13 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
151
151
|
rows = query_result.rows;
|
|
152
152
|
}
|
|
153
153
|
else {
|
|
154
|
-
rows =
|
|
154
|
+
rows = common_1.OINO_EMPTY_ROWS; // return empty row if no rows returned
|
|
155
155
|
}
|
|
156
156
|
// if (rows.length > 0) { console.log("OINODbPostgresql._exec: rows", rows) }
|
|
157
157
|
return new OINOPostgresqlData(rows, []);
|
|
158
158
|
}
|
|
159
159
|
catch (e) {
|
|
160
|
-
return new OINOPostgresqlData(
|
|
160
|
+
return new OINOPostgresqlData(common_1.OINO_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbPostgresql._exec");
|
|
161
161
|
}
|
|
162
162
|
finally {
|
|
163
163
|
if (connection) {
|
|
@@ -171,7 +171,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
171
171
|
* @param sqlTable name of the table
|
|
172
172
|
*
|
|
173
173
|
*/
|
|
174
|
-
|
|
174
|
+
printTableName(sqlTable) {
|
|
175
175
|
return "\"" + sqlTable.toLowerCase() + "\"";
|
|
176
176
|
}
|
|
177
177
|
/**
|
|
@@ -180,7 +180,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
180
180
|
* @param sqlColumn name of the column
|
|
181
181
|
*
|
|
182
182
|
*/
|
|
183
|
-
|
|
183
|
+
printColumnName(sqlColumn) {
|
|
184
184
|
return "\"" + sqlColumn + "\"";
|
|
185
185
|
}
|
|
186
186
|
/**
|
|
@@ -188,20 +188,20 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
188
188
|
* type with the correct SQL escaping.
|
|
189
189
|
*
|
|
190
190
|
* @param cellValue data from sql results
|
|
191
|
-
* @param
|
|
191
|
+
* @param nativeType native type name for table column
|
|
192
192
|
*
|
|
193
193
|
*/
|
|
194
|
-
|
|
194
|
+
printCellAsValue(cellValue, nativeType) {
|
|
195
195
|
if (cellValue === null) {
|
|
196
196
|
return "NULL";
|
|
197
197
|
}
|
|
198
198
|
else if (cellValue === undefined) {
|
|
199
199
|
return "UNDEFINED";
|
|
200
200
|
}
|
|
201
|
-
else if ((
|
|
201
|
+
else if ((nativeType == "integer") || (nativeType == "smallint") || (nativeType == "real")) {
|
|
202
202
|
return cellValue.toString();
|
|
203
203
|
}
|
|
204
|
-
else if (
|
|
204
|
+
else if (nativeType == "bytea") {
|
|
205
205
|
if (cellValue instanceof Buffer) {
|
|
206
206
|
return "'\\x" + cellValue.toString("hex") + "'";
|
|
207
207
|
}
|
|
@@ -212,7 +212,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
212
212
|
return "\'" + cellValue?.toString() + "\'";
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
|
-
else if (
|
|
215
|
+
else if (nativeType == "boolean") {
|
|
216
216
|
if (cellValue == null || cellValue == "" || cellValue.toString().toLowerCase() == "false" || cellValue == "0") {
|
|
217
217
|
return "false";
|
|
218
218
|
}
|
|
@@ -220,11 +220,11 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
220
220
|
return "true";
|
|
221
221
|
}
|
|
222
222
|
}
|
|
223
|
-
else if ((
|
|
223
|
+
else if ((nativeType == "date") && (cellValue instanceof Date)) {
|
|
224
224
|
return "\'" + cellValue.toISOString() + "\'";
|
|
225
225
|
}
|
|
226
226
|
else {
|
|
227
|
-
return this.
|
|
227
|
+
return this.printStringValue(cellValue.toString());
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
/**
|
|
@@ -233,7 +233,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
233
233
|
* @param sqlString string value
|
|
234
234
|
*
|
|
235
235
|
*/
|
|
236
|
-
|
|
236
|
+
printStringValue(sqlString) {
|
|
237
237
|
return "\'" + sqlString.replaceAll("'", "''") + "\'";
|
|
238
238
|
}
|
|
239
239
|
/**
|
|
@@ -241,17 +241,17 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
241
241
|
* type.
|
|
242
242
|
*
|
|
243
243
|
* @param sqlValue data from serialization
|
|
244
|
-
* @param
|
|
244
|
+
* @param nativeType native type name for table column
|
|
245
245
|
*
|
|
246
246
|
*/
|
|
247
|
-
|
|
247
|
+
parseValueAsCell(sqlValue, nativeType) {
|
|
248
248
|
if ((sqlValue === null) || (sqlValue == "NULL")) {
|
|
249
249
|
return null;
|
|
250
250
|
}
|
|
251
251
|
else if (sqlValue === undefined) {
|
|
252
252
|
return undefined;
|
|
253
253
|
}
|
|
254
|
-
else if (((
|
|
254
|
+
else if (((nativeType == "date")) && (typeof (sqlValue) == "string") && (sqlValue != "")) {
|
|
255
255
|
return new Date(sqlValue);
|
|
256
256
|
}
|
|
257
257
|
else {
|
|
@@ -292,7 +292,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
292
292
|
common_1.OINOBenchmark.startMetric("OINODb", "validate");
|
|
293
293
|
let result = new common_1.OINOResult();
|
|
294
294
|
try {
|
|
295
|
-
const sql = this._getValidateSql(this.
|
|
295
|
+
const sql = this._getValidateSql(this.dbParams.database);
|
|
296
296
|
const sql_res = await this._query(sql);
|
|
297
297
|
if (sql_res.isEmpty()) {
|
|
298
298
|
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
|
|
@@ -358,59 +358,60 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
358
358
|
return result;
|
|
359
359
|
}
|
|
360
360
|
_getSchemaSql(dbName, tableName) {
|
|
361
|
-
const sql = `SELECT
|
|
362
|
-
col.column_name,
|
|
363
|
-
col.data_type,
|
|
364
|
-
col.character_maximum_length,
|
|
365
|
-
col.is_nullable,
|
|
366
|
-
con.constraint_type,
|
|
367
|
-
col.numeric_precision,
|
|
368
|
-
col.numeric_scale,
|
|
369
|
-
col.column_default
|
|
370
|
-
FROM information_schema.columns col
|
|
371
|
-
LEFT JOIN LATERAL
|
|
372
|
-
(select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
|
|
373
|
-
from
|
|
374
|
-
information_schema.table_constraints tco,
|
|
375
|
-
information_schema.key_column_usage kcu
|
|
376
|
-
where
|
|
377
|
-
kcu.constraint_name = tco.constraint_name
|
|
378
|
-
and kcu.constraint_schema = tco.constraint_schema
|
|
379
|
-
and tco.table_catalog = col.table_catalog
|
|
380
|
-
and tco.table_name = col.table_name
|
|
381
|
-
and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
|
|
382
|
-
group by kcu.column_name) con on col.column_name = con.column_name
|
|
361
|
+
const sql = `SELECT
|
|
362
|
+
col.column_name,
|
|
363
|
+
col.data_type,
|
|
364
|
+
col.character_maximum_length,
|
|
365
|
+
col.is_nullable,
|
|
366
|
+
con.constraint_type,
|
|
367
|
+
col.numeric_precision,
|
|
368
|
+
col.numeric_scale,
|
|
369
|
+
col.column_default
|
|
370
|
+
FROM information_schema.columns col
|
|
371
|
+
LEFT JOIN LATERAL
|
|
372
|
+
(select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
|
|
373
|
+
from
|
|
374
|
+
information_schema.table_constraints tco,
|
|
375
|
+
information_schema.key_column_usage kcu
|
|
376
|
+
where
|
|
377
|
+
kcu.constraint_name = tco.constraint_name
|
|
378
|
+
and kcu.constraint_schema = tco.constraint_schema
|
|
379
|
+
and tco.table_catalog = col.table_catalog
|
|
380
|
+
and tco.table_name = col.table_name
|
|
381
|
+
and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
|
|
382
|
+
group by kcu.column_name) con on col.column_name = con.column_name
|
|
383
383
|
WHERE col.table_catalog = '${dbName}' AND col.table_name = '${tableName}'`;
|
|
384
384
|
return sql;
|
|
385
385
|
}
|
|
386
386
|
_getValidateSql(dbName) {
|
|
387
|
-
const sql = `SELECT
|
|
388
|
-
count(col.column_name) AS column_count
|
|
389
|
-
FROM information_schema.columns col
|
|
390
|
-
LEFT JOIN LATERAL
|
|
391
|
-
(select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
|
|
392
|
-
from
|
|
393
|
-
information_schema.table_constraints tco,
|
|
394
|
-
information_schema.key_column_usage kcu
|
|
395
|
-
where
|
|
396
|
-
kcu.constraint_name = tco.constraint_name
|
|
397
|
-
and kcu.constraint_schema = tco.constraint_schema
|
|
398
|
-
and tco.table_catalog = col.table_catalog
|
|
399
|
-
and tco.table_name = col.table_name
|
|
400
|
-
and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
|
|
401
|
-
group by kcu.column_name) con on col.column_name = con.column_name
|
|
387
|
+
const sql = `SELECT
|
|
388
|
+
count(col.column_name) AS column_count
|
|
389
|
+
FROM information_schema.columns col
|
|
390
|
+
LEFT JOIN LATERAL
|
|
391
|
+
(select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
|
|
392
|
+
from
|
|
393
|
+
information_schema.table_constraints tco,
|
|
394
|
+
information_schema.key_column_usage kcu
|
|
395
|
+
where
|
|
396
|
+
kcu.constraint_name = tco.constraint_name
|
|
397
|
+
and kcu.constraint_schema = tco.constraint_schema
|
|
398
|
+
and tco.table_catalog = col.table_catalog
|
|
399
|
+
and tco.table_name = col.table_name
|
|
400
|
+
and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
|
|
401
|
+
group by kcu.column_name) con on col.column_name = con.column_name
|
|
402
402
|
WHERE col.table_catalog = '${dbName}'`;
|
|
403
403
|
return sql;
|
|
404
404
|
}
|
|
405
405
|
/**
|
|
406
|
-
* Initialize a data model by getting the SQL schema and populating
|
|
406
|
+
* Initialize a data model by getting the SQL schema and populating OINODataFields of
|
|
407
407
|
* the model.
|
|
408
408
|
*
|
|
409
409
|
* @param api api which data model to initialize.
|
|
410
410
|
*
|
|
411
411
|
*/
|
|
412
412
|
async initializeApiDatamodel(api) {
|
|
413
|
-
|
|
413
|
+
api.initializeDatamodel(new db_1.OINODbDataModel(api));
|
|
414
|
+
const schema_res = await this._query(this._getSchemaSql(this.dbParams.database, api.params.tableName.toLowerCase()));
|
|
414
415
|
while (!schema_res.isEof()) {
|
|
415
416
|
const row = schema_res.getRow();
|
|
416
417
|
const field_name = row[0]?.toString() || "";
|
|
@@ -434,31 +435,31 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
434
435
|
}
|
|
435
436
|
else {
|
|
436
437
|
if ((sql_type == "integer") || (sql_type == "smallint") || (sql_type == "real")) {
|
|
437
|
-
api.datamodel.addField(new
|
|
438
|
+
api.datamodel.addField(new common_1.OINONumberDataField(this, field_name, sql_type, field_params));
|
|
438
439
|
}
|
|
439
440
|
else if ((sql_type == "date")) {
|
|
440
441
|
if (api.params.useDatesAsString) {
|
|
441
|
-
api.datamodel.addField(new
|
|
442
|
+
api.datamodel.addField(new common_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
442
443
|
}
|
|
443
444
|
else {
|
|
444
|
-
api.datamodel.addField(new
|
|
445
|
+
api.datamodel.addField(new common_1.OINODatetimeDataField(this, field_name, sql_type, field_params));
|
|
445
446
|
}
|
|
446
447
|
}
|
|
447
448
|
else if ((sql_type == "character") || (sql_type == "character varying") || (sql_type == "varchar") || (sql_type == "text")) {
|
|
448
|
-
api.datamodel.addField(new
|
|
449
|
+
api.datamodel.addField(new common_1.OINOStringDataField(this, field_name, sql_type, field_params, field_length));
|
|
449
450
|
}
|
|
450
451
|
else if ((sql_type == "bytea")) {
|
|
451
|
-
api.datamodel.addField(new
|
|
452
|
+
api.datamodel.addField(new common_1.OINOBlobDataField(this, field_name, sql_type, field_params, field_length));
|
|
452
453
|
}
|
|
453
454
|
else if ((sql_type == "boolean")) {
|
|
454
|
-
api.datamodel.addField(new
|
|
455
|
+
api.datamodel.addField(new common_1.OINOBooleanDataField(this, field_name, sql_type, field_params));
|
|
455
456
|
}
|
|
456
457
|
else if ((sql_type == "decimal") || (sql_type == "numeric")) {
|
|
457
|
-
api.datamodel.addField(new
|
|
458
|
+
api.datamodel.addField(new common_1.OINOStringDataField(this, field_name, sql_type, field_params, numeric_precision + numeric_scale + 1));
|
|
458
459
|
}
|
|
459
460
|
else {
|
|
460
461
|
common_1.OINOLog.info("@oino-ts/db-postgresql", "OINODbPostgresql", "initializeApiDatamodel", "Unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length: field_length, field_params: field_params });
|
|
461
|
-
api.datamodel.addField(new
|
|
462
|
+
api.datamodel.addField(new common_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
462
463
|
}
|
|
463
464
|
}
|
|
464
465
|
await schema_res.next();
|
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
|
-
import { OINO_ERROR_PREFIX, OINOBenchmark, OINOLog, OINOResult } from "@oino-ts/common";
|
|
7
|
-
import { OINODb,
|
|
6
|
+
import { OINO_ERROR_PREFIX, OINOBenchmark, OINOLog, OINOResult, OINODataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODatetimeDataField, OINOBlobDataField, OINO_EMPTY_ROW, OINO_EMPTY_ROWS } from "@oino-ts/common";
|
|
7
|
+
import { OINODb, OINODbDataModel } from "@oino-ts/db";
|
|
8
8
|
import { Pool } from "pg";
|
|
9
9
|
/**
|
|
10
|
-
* Implmentation of
|
|
10
|
+
* Implmentation of OINODataSet for Postgresql.
|
|
11
11
|
*
|
|
12
12
|
*/
|
|
13
|
-
class OINOPostgresqlData extends
|
|
13
|
+
class OINOPostgresqlData extends OINODataSet {
|
|
14
14
|
_rows;
|
|
15
15
|
/**
|
|
16
16
|
* OINOPostgresqlData constructor
|
|
@@ -69,7 +69,7 @@ class OINOPostgresqlData extends OINODbDataSet {
|
|
|
69
69
|
return this._rows[this._currentRow];
|
|
70
70
|
}
|
|
71
71
|
else {
|
|
72
|
-
return
|
|
72
|
+
return OINO_EMPTY_ROW;
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
@@ -92,12 +92,12 @@ export class OINODbPostgresql extends OINODb {
|
|
|
92
92
|
*/
|
|
93
93
|
constructor(params) {
|
|
94
94
|
super(params);
|
|
95
|
-
if (this.
|
|
96
|
-
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this.
|
|
95
|
+
if (this.dbParams.type !== "OINODbPostgresql") {
|
|
96
|
+
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this.dbParams.type);
|
|
97
97
|
}
|
|
98
|
-
const ssl_enabled = !(this.
|
|
99
|
-
this._pool = new Pool({ host: this.
|
|
100
|
-
delete this.
|
|
98
|
+
const ssl_enabled = !(this.dbParams.url == "localhost" || this.dbParams.url == "127.0.0.1");
|
|
99
|
+
this._pool = new Pool({ host: this.dbParams.url, database: this.dbParams.database, port: this.dbParams.port, user: this.dbParams.user, password: this.dbParams.password, ssl: ssl_enabled });
|
|
100
|
+
delete this.dbParams.password;
|
|
101
101
|
this._pool.on("error", (err) => {
|
|
102
102
|
OINOLog.error("@oino-ts/db-postgresql", "OINODbPostgresql", ".on(error)", "Error-event", { err: err });
|
|
103
103
|
});
|
|
@@ -122,12 +122,12 @@ export class OINODbPostgresql extends OINODb {
|
|
|
122
122
|
rows = query_result.rows;
|
|
123
123
|
}
|
|
124
124
|
else {
|
|
125
|
-
rows =
|
|
125
|
+
rows = OINO_EMPTY_ROWS; // return empty row if no rows returned
|
|
126
126
|
}
|
|
127
127
|
return new OINOPostgresqlData(rows, []);
|
|
128
128
|
}
|
|
129
129
|
catch (e) {
|
|
130
|
-
return new OINOPostgresqlData(
|
|
130
|
+
return new OINOPostgresqlData(OINO_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db query: " + e.message, "OINODbPostgresql._query");
|
|
131
131
|
}
|
|
132
132
|
finally {
|
|
133
133
|
if (connection) {
|
|
@@ -148,13 +148,13 @@ export class OINODbPostgresql extends OINODb {
|
|
|
148
148
|
rows = query_result.rows;
|
|
149
149
|
}
|
|
150
150
|
else {
|
|
151
|
-
rows =
|
|
151
|
+
rows = OINO_EMPTY_ROWS; // return empty row if no rows returned
|
|
152
152
|
}
|
|
153
153
|
// if (rows.length > 0) { console.log("OINODbPostgresql._exec: rows", rows) }
|
|
154
154
|
return new OINOPostgresqlData(rows, []);
|
|
155
155
|
}
|
|
156
156
|
catch (e) {
|
|
157
|
-
return new OINOPostgresqlData(
|
|
157
|
+
return new OINOPostgresqlData(OINO_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbPostgresql._exec");
|
|
158
158
|
}
|
|
159
159
|
finally {
|
|
160
160
|
if (connection) {
|
|
@@ -168,7 +168,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
168
168
|
* @param sqlTable name of the table
|
|
169
169
|
*
|
|
170
170
|
*/
|
|
171
|
-
|
|
171
|
+
printTableName(sqlTable) {
|
|
172
172
|
return "\"" + sqlTable.toLowerCase() + "\"";
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
@@ -177,7 +177,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
177
177
|
* @param sqlColumn name of the column
|
|
178
178
|
*
|
|
179
179
|
*/
|
|
180
|
-
|
|
180
|
+
printColumnName(sqlColumn) {
|
|
181
181
|
return "\"" + sqlColumn + "\"";
|
|
182
182
|
}
|
|
183
183
|
/**
|
|
@@ -185,20 +185,20 @@ export class OINODbPostgresql extends OINODb {
|
|
|
185
185
|
* type with the correct SQL escaping.
|
|
186
186
|
*
|
|
187
187
|
* @param cellValue data from sql results
|
|
188
|
-
* @param
|
|
188
|
+
* @param nativeType native type name for table column
|
|
189
189
|
*
|
|
190
190
|
*/
|
|
191
|
-
|
|
191
|
+
printCellAsValue(cellValue, nativeType) {
|
|
192
192
|
if (cellValue === null) {
|
|
193
193
|
return "NULL";
|
|
194
194
|
}
|
|
195
195
|
else if (cellValue === undefined) {
|
|
196
196
|
return "UNDEFINED";
|
|
197
197
|
}
|
|
198
|
-
else if ((
|
|
198
|
+
else if ((nativeType == "integer") || (nativeType == "smallint") || (nativeType == "real")) {
|
|
199
199
|
return cellValue.toString();
|
|
200
200
|
}
|
|
201
|
-
else if (
|
|
201
|
+
else if (nativeType == "bytea") {
|
|
202
202
|
if (cellValue instanceof Buffer) {
|
|
203
203
|
return "'\\x" + cellValue.toString("hex") + "'";
|
|
204
204
|
}
|
|
@@ -209,7 +209,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
209
209
|
return "\'" + cellValue?.toString() + "\'";
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
|
-
else if (
|
|
212
|
+
else if (nativeType == "boolean") {
|
|
213
213
|
if (cellValue == null || cellValue == "" || cellValue.toString().toLowerCase() == "false" || cellValue == "0") {
|
|
214
214
|
return "false";
|
|
215
215
|
}
|
|
@@ -217,11 +217,11 @@ export class OINODbPostgresql extends OINODb {
|
|
|
217
217
|
return "true";
|
|
218
218
|
}
|
|
219
219
|
}
|
|
220
|
-
else if ((
|
|
220
|
+
else if ((nativeType == "date") && (cellValue instanceof Date)) {
|
|
221
221
|
return "\'" + cellValue.toISOString() + "\'";
|
|
222
222
|
}
|
|
223
223
|
else {
|
|
224
|
-
return this.
|
|
224
|
+
return this.printStringValue(cellValue.toString());
|
|
225
225
|
}
|
|
226
226
|
}
|
|
227
227
|
/**
|
|
@@ -230,7 +230,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
230
230
|
* @param sqlString string value
|
|
231
231
|
*
|
|
232
232
|
*/
|
|
233
|
-
|
|
233
|
+
printStringValue(sqlString) {
|
|
234
234
|
return "\'" + sqlString.replaceAll("'", "''") + "\'";
|
|
235
235
|
}
|
|
236
236
|
/**
|
|
@@ -238,17 +238,17 @@ export class OINODbPostgresql extends OINODb {
|
|
|
238
238
|
* type.
|
|
239
239
|
*
|
|
240
240
|
* @param sqlValue data from serialization
|
|
241
|
-
* @param
|
|
241
|
+
* @param nativeType native type name for table column
|
|
242
242
|
*
|
|
243
243
|
*/
|
|
244
|
-
|
|
244
|
+
parseValueAsCell(sqlValue, nativeType) {
|
|
245
245
|
if ((sqlValue === null) || (sqlValue == "NULL")) {
|
|
246
246
|
return null;
|
|
247
247
|
}
|
|
248
248
|
else if (sqlValue === undefined) {
|
|
249
249
|
return undefined;
|
|
250
250
|
}
|
|
251
|
-
else if (((
|
|
251
|
+
else if (((nativeType == "date")) && (typeof (sqlValue) == "string") && (sqlValue != "")) {
|
|
252
252
|
return new Date(sqlValue);
|
|
253
253
|
}
|
|
254
254
|
else {
|
|
@@ -289,7 +289,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
289
289
|
OINOBenchmark.startMetric("OINODb", "validate");
|
|
290
290
|
let result = new OINOResult();
|
|
291
291
|
try {
|
|
292
|
-
const sql = this._getValidateSql(this.
|
|
292
|
+
const sql = this._getValidateSql(this.dbParams.database);
|
|
293
293
|
const sql_res = await this._query(sql);
|
|
294
294
|
if (sql_res.isEmpty()) {
|
|
295
295
|
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
|
|
@@ -355,59 +355,60 @@ export class OINODbPostgresql extends OINODb {
|
|
|
355
355
|
return result;
|
|
356
356
|
}
|
|
357
357
|
_getSchemaSql(dbName, tableName) {
|
|
358
|
-
const sql = `SELECT
|
|
359
|
-
col.column_name,
|
|
360
|
-
col.data_type,
|
|
361
|
-
col.character_maximum_length,
|
|
362
|
-
col.is_nullable,
|
|
363
|
-
con.constraint_type,
|
|
364
|
-
col.numeric_precision,
|
|
365
|
-
col.numeric_scale,
|
|
366
|
-
col.column_default
|
|
367
|
-
FROM information_schema.columns col
|
|
368
|
-
LEFT JOIN LATERAL
|
|
369
|
-
(select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
|
|
370
|
-
from
|
|
371
|
-
information_schema.table_constraints tco,
|
|
372
|
-
information_schema.key_column_usage kcu
|
|
373
|
-
where
|
|
374
|
-
kcu.constraint_name = tco.constraint_name
|
|
375
|
-
and kcu.constraint_schema = tco.constraint_schema
|
|
376
|
-
and tco.table_catalog = col.table_catalog
|
|
377
|
-
and tco.table_name = col.table_name
|
|
378
|
-
and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
|
|
379
|
-
group by kcu.column_name) con on col.column_name = con.column_name
|
|
358
|
+
const sql = `SELECT
|
|
359
|
+
col.column_name,
|
|
360
|
+
col.data_type,
|
|
361
|
+
col.character_maximum_length,
|
|
362
|
+
col.is_nullable,
|
|
363
|
+
con.constraint_type,
|
|
364
|
+
col.numeric_precision,
|
|
365
|
+
col.numeric_scale,
|
|
366
|
+
col.column_default
|
|
367
|
+
FROM information_schema.columns col
|
|
368
|
+
LEFT JOIN LATERAL
|
|
369
|
+
(select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
|
|
370
|
+
from
|
|
371
|
+
information_schema.table_constraints tco,
|
|
372
|
+
information_schema.key_column_usage kcu
|
|
373
|
+
where
|
|
374
|
+
kcu.constraint_name = tco.constraint_name
|
|
375
|
+
and kcu.constraint_schema = tco.constraint_schema
|
|
376
|
+
and tco.table_catalog = col.table_catalog
|
|
377
|
+
and tco.table_name = col.table_name
|
|
378
|
+
and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
|
|
379
|
+
group by kcu.column_name) con on col.column_name = con.column_name
|
|
380
380
|
WHERE col.table_catalog = '${dbName}' AND col.table_name = '${tableName}'`;
|
|
381
381
|
return sql;
|
|
382
382
|
}
|
|
383
383
|
_getValidateSql(dbName) {
|
|
384
|
-
const sql = `SELECT
|
|
385
|
-
count(col.column_name) AS column_count
|
|
386
|
-
FROM information_schema.columns col
|
|
387
|
-
LEFT JOIN LATERAL
|
|
388
|
-
(select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
|
|
389
|
-
from
|
|
390
|
-
information_schema.table_constraints tco,
|
|
391
|
-
information_schema.key_column_usage kcu
|
|
392
|
-
where
|
|
393
|
-
kcu.constraint_name = tco.constraint_name
|
|
394
|
-
and kcu.constraint_schema = tco.constraint_schema
|
|
395
|
-
and tco.table_catalog = col.table_catalog
|
|
396
|
-
and tco.table_name = col.table_name
|
|
397
|
-
and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
|
|
398
|
-
group by kcu.column_name) con on col.column_name = con.column_name
|
|
384
|
+
const sql = `SELECT
|
|
385
|
+
count(col.column_name) AS column_count
|
|
386
|
+
FROM information_schema.columns col
|
|
387
|
+
LEFT JOIN LATERAL
|
|
388
|
+
(select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
|
|
389
|
+
from
|
|
390
|
+
information_schema.table_constraints tco,
|
|
391
|
+
information_schema.key_column_usage kcu
|
|
392
|
+
where
|
|
393
|
+
kcu.constraint_name = tco.constraint_name
|
|
394
|
+
and kcu.constraint_schema = tco.constraint_schema
|
|
395
|
+
and tco.table_catalog = col.table_catalog
|
|
396
|
+
and tco.table_name = col.table_name
|
|
397
|
+
and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
|
|
398
|
+
group by kcu.column_name) con on col.column_name = con.column_name
|
|
399
399
|
WHERE col.table_catalog = '${dbName}'`;
|
|
400
400
|
return sql;
|
|
401
401
|
}
|
|
402
402
|
/**
|
|
403
|
-
* Initialize a data model by getting the SQL schema and populating
|
|
403
|
+
* Initialize a data model by getting the SQL schema and populating OINODataFields of
|
|
404
404
|
* the model.
|
|
405
405
|
*
|
|
406
406
|
* @param api api which data model to initialize.
|
|
407
407
|
*
|
|
408
408
|
*/
|
|
409
409
|
async initializeApiDatamodel(api) {
|
|
410
|
-
|
|
410
|
+
api.initializeDatamodel(new OINODbDataModel(api));
|
|
411
|
+
const schema_res = await this._query(this._getSchemaSql(this.dbParams.database, api.params.tableName.toLowerCase()));
|
|
411
412
|
while (!schema_res.isEof()) {
|
|
412
413
|
const row = schema_res.getRow();
|
|
413
414
|
const field_name = row[0]?.toString() || "";
|