dbgate-tools 5.2.9 → 5.3.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.
@@ -19,13 +19,14 @@ export declare class DatabaseAnalyser {
19
19
  incrementalAnalysis(structure: any): Promise<DatabaseInfo>;
20
20
  mergeAnalyseResult(newlyAnalysed: any): any;
21
21
  getRequestedObjectPureNames(objectTypeField: any, allPureNames: any): any;
22
- createQuery(template: any, typeFields: any): any;
22
+ createQuery(template: any, typeFields: any, replacements?: {}): any;
23
+ processQueryReplacements(query: any, replacements: any): any;
23
24
  createQueryCore(template: any, typeFields: any): any;
24
25
  getDeletedObjectsForField(snapshot: any, objectTypeField: any): any;
25
26
  getDeletedObjects(snapshot: any): any[];
26
27
  feedback(obj: any): void;
27
28
  getModifications(): Promise<any[]>;
28
- analyserQuery(template: any, typeFields: any): Promise<import("dbgate-types").QueryResult>;
29
+ analyserQuery(template: any, typeFields: any, replacements?: {}): Promise<import("dbgate-types").QueryResult>;
29
30
  static createEmptyStructure(): DatabaseInfo;
30
31
  static byTableFilter(table: any): (x: any) => boolean;
31
32
  static extractPrimaryKeys(table: any, pkColumns: any): {
@@ -171,8 +171,14 @@ class DatabaseAnalyser {
171
171
  // containsObjectIdCondition(typeFields) {
172
172
  // return this.createQueryCore('=OBJECT_ID_CONDITION', typeFields) != ' is not null';
173
173
  // }
174
- createQuery(template, typeFields) {
175
- return this.createQueryCore(template, typeFields);
174
+ createQuery(template, typeFields, replacements = {}) {
175
+ return this.createQueryCore(this.processQueryReplacements(template, replacements), typeFields);
176
+ }
177
+ processQueryReplacements(query, replacements) {
178
+ for (const repl in replacements) {
179
+ query = query.replaceAll(repl, replacements[repl]);
180
+ }
181
+ return query;
176
182
  }
177
183
  createQueryCore(template, typeFields) {
178
184
  // let res = template;
@@ -288,16 +294,18 @@ class DatabaseAnalyser {
288
294
  return [...(0, compact_1.default)(res), ...this.getDeletedObjects(snapshot)];
289
295
  });
290
296
  }
291
- analyserQuery(template, typeFields) {
297
+ analyserQuery(template, typeFields, replacements = {}) {
292
298
  return __awaiter(this, void 0, void 0, function* () {
293
- const sql = this.createQuery(template, typeFields);
299
+ const sql = this.createQuery(template, typeFields, replacements);
294
300
  if (!sql) {
295
301
  return {
296
302
  rows: [],
297
303
  };
298
304
  }
299
305
  try {
300
- return yield this.driver.query(this.pool, sql);
306
+ const res = yield this.driver.query(this.pool, sql);
307
+ this.logger.debug({ rows: res.rows.length, template }, `Loaded analyser query`);
308
+ return res;
301
309
  }
302
310
  catch (err) {
303
311
  logger.error({ err }, 'Error running analyser query');
@@ -22,6 +22,7 @@ export declare class SqlDumper implements AlterProcessor {
22
22
  dropDatabase(name: string): void;
23
23
  specialColumnOptions(column: any): void;
24
24
  selectScopeIdentity(table: TableInfo): void;
25
+ columnType(dataType: string): void;
25
26
  columnDefinition(column: ColumnInfo, { includeDefault, includeNullable, includeCollate }?: {
26
27
  includeDefault?: boolean;
27
28
  includeNullable?: boolean;
package/lib/SqlDumper.js CHANGED
@@ -184,15 +184,8 @@ class SqlDumper {
184
184
  }
185
185
  specialColumnOptions(column) { }
186
186
  selectScopeIdentity(table) { }
187
- columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
188
- var _a;
189
- if (column.computedExpression) {
190
- this.put('^as %s', column.computedExpression);
191
- if (column.isPersisted)
192
- this.put(' ^persisted');
193
- return;
194
- }
195
- const type = column.dataType || this.dialect.fallbackDataType;
187
+ columnType(dataType) {
188
+ const type = dataType || this.dialect.fallbackDataType;
196
189
  const typeWithValues = type.match(/([^(]+)(\(.+[^)]\))/);
197
190
  if (typeWithValues === null || typeWithValues === void 0 ? void 0 : typeWithValues.length) {
198
191
  typeWithValues.shift();
@@ -202,6 +195,16 @@ class SqlDumper {
202
195
  else {
203
196
  this.putRaw(SqlDumper.convertKeywordCase(type));
204
197
  }
198
+ }
199
+ columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
200
+ var _a;
201
+ if (column.computedExpression) {
202
+ this.put('^as %s', column.computedExpression);
203
+ if (column.isPersisted)
204
+ this.put(' ^persisted');
205
+ return;
206
+ }
207
+ this.columnType(column.dataType);
205
208
  if (column.autoIncrement) {
206
209
  this.autoIncrement();
207
210
  }
@@ -59,23 +59,42 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
59
59
  writable.send = () => __awaiter(this, void 0, void 0, function* () {
60
60
  const rows = writable.buffer;
61
61
  writable.buffer = [];
62
- const dmp = driver.createDumper();
63
- dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
64
- dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
65
- dmp.putRaw(')\n VALUES\n');
66
- let wasRow = false;
67
- for (const row of rows) {
68
- if (wasRow)
69
- dmp.putRaw(',\n');
70
- dmp.putRaw('(');
71
- dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
72
- dmp.putRaw(')');
73
- wasRow = true;
62
+ if (driver.dialect.allowMultipleValuesInsert) {
63
+ const dmp = driver.createDumper();
64
+ dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
65
+ dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
66
+ dmp.putRaw(')\n VALUES\n');
67
+ let wasRow = false;
68
+ for (const row of rows) {
69
+ if (wasRow)
70
+ dmp.putRaw(',\n');
71
+ dmp.putRaw('(');
72
+ dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
73
+ dmp.putRaw(')');
74
+ wasRow = true;
75
+ }
76
+ dmp.putRaw(';');
77
+ // require('fs').writeFileSync('/home/jena/test.sql', dmp.s);
78
+ // console.log(dmp.s);
79
+ yield driver.query(pool, dmp.s, { discardResult: true });
80
+ }
81
+ else {
82
+ for (const row of rows) {
83
+ const dmp = driver.createDumper();
84
+ dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
85
+ dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
86
+ dmp.putRaw(')\n VALUES\n');
87
+ dmp.putRaw('(');
88
+ dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
89
+ dmp.putRaw(')');
90
+ yield driver.query(pool, dmp.s, { discardResult: true });
91
+ }
92
+ }
93
+ if (options.commitAfterInsert) {
94
+ const dmp = driver.createDumper();
95
+ dmp.commitTransaction();
96
+ yield driver.query(pool, dmp.s, { discardResult: true });
74
97
  }
75
- dmp.putRaw(';');
76
- // require('fs').writeFileSync('/home/jena/test.sql', dmp.s);
77
- // console.log(dmp.s);
78
- yield driver.query(pool, dmp.s, { discardResult: true });
79
98
  });
80
99
  writable.sendIfFull = () => __awaiter(this, void 0, void 0, function* () {
81
100
  if (writable.buffer.length > 100) {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "5.2.9",
2
+ "version": "5.3.0",
3
3
  "name": "dbgate-tools",
4
4
  "main": "lib/index.js",
5
5
  "typings": "lib/index.d.ts",
@@ -25,14 +25,14 @@
25
25
  ],
26
26
  "devDependencies": {
27
27
  "@types/node": "^13.7.0",
28
- "dbgate-types": "^5.2.9",
28
+ "dbgate-types": "^5.3.0",
29
29
  "jest": "^24.9.0",
30
30
  "ts-jest": "^25.2.1",
31
31
  "typescript": "^4.4.3"
32
32
  },
33
33
  "dependencies": {
34
- "dbgate-query-splitter": "^4.9.3",
35
- "dbgate-sqltree": "^5.2.9",
34
+ "dbgate-query-splitter": "^4.10.1",
35
+ "dbgate-sqltree": "^5.3.0",
36
36
  "debug": "^4.3.4",
37
37
  "json-stable-stringify": "^1.0.1",
38
38
  "lodash": "^4.17.21",