dbgate-tools 6.1.6 → 6.2.1

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.
@@ -12,7 +12,7 @@ export declare class SqlDumper implements AlterProcessor {
12
12
  escapeString(value: any): string;
13
13
  putStringValue(value: any): void;
14
14
  putByteArrayValue(value: any): void;
15
- putValue(value: any): void;
15
+ putValue(value: any, dataType?: any): void;
16
16
  putCmd(format: any, ...args: any[]): void;
17
17
  putFormattedValue(c: any, value: any): void;
18
18
  putFormattedList(c: any, collection: any): void;
@@ -39,6 +39,7 @@ export declare class SqlDumper implements AlterProcessor {
39
39
  transform(type: TransformType, dumpExpr: any): void;
40
40
  allowIdentityInsert(table: NamedObjectInfo, allow: boolean): void;
41
41
  enableConstraints(table: NamedObjectInfo, enabled: boolean): void;
42
+ enableAllForeignKeys(enabled: boolean): void;
42
43
  comment(value: string): void;
43
44
  createView(obj: ViewInfo): void;
44
45
  dropView(obj: ViewInfo, { testIfExists }: {
package/lib/SqlDumper.js CHANGED
@@ -50,7 +50,7 @@ class SqlDumper {
50
50
  putByteArrayValue(value) {
51
51
  this.put('^null');
52
52
  }
53
- putValue(value) {
53
+ putValue(value, dataType = null) {
54
54
  if (value === null)
55
55
  this.put('^null');
56
56
  else if (value === true)
@@ -106,6 +106,9 @@ class SqlDumper {
106
106
  case 'v':
107
107
  this.putValue(value);
108
108
  break;
109
+ case 'V':
110
+ this.putValue(value.value, value.dataType);
111
+ break;
109
112
  case 'c':
110
113
  value(this);
111
114
  break;
@@ -221,7 +224,7 @@ class SqlDumper {
221
224
  }
222
225
  }
223
226
  columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
224
- var _a, _b, _c;
227
+ var _a, _b, _c, _d, _e, _f, _g, _h;
225
228
  if (column.computedExpression) {
226
229
  this.put('^as %s', column.computedExpression);
227
230
  if (column.isPersisted)
@@ -229,16 +232,26 @@ class SqlDumper {
229
232
  return;
230
233
  }
231
234
  this.columnType(column.dataType);
232
- if (column.autoIncrement) {
235
+ if (column.autoIncrement && !((_a = this.dialect) === null || _a === void 0 ? void 0 : _a.disableAutoIncrement)) {
233
236
  this.autoIncrement();
234
237
  }
235
238
  this.putRaw(' ');
236
239
  this.specialColumnOptions(column);
237
- if (includeNullable && !((_a = this.dialect) === null || _a === void 0 ? void 0 : _a.specificNullabilityImplementation)) {
238
- this.put(column.notNull ? '^not ^null' : '^null');
240
+ if ((_b = this.dialect) === null || _b === void 0 ? void 0 : _b.defaultValueBeforeNullability) {
241
+ if (includeDefault && ((_d = (_c = column.defaultValue) === null || _c === void 0 ? void 0 : _c.toString()) === null || _d === void 0 ? void 0 : _d.trim())) {
242
+ this.columnDefault(column);
243
+ }
244
+ if (includeNullable && !((_e = this.dialect) === null || _e === void 0 ? void 0 : _e.specificNullabilityImplementation)) {
245
+ this.put(column.notNull ? '^not ^null' : '^null');
246
+ }
239
247
  }
240
- if (includeDefault && ((_c = (_b = column.defaultValue) === null || _b === void 0 ? void 0 : _b.toString()) === null || _c === void 0 ? void 0 : _c.trim())) {
241
- this.columnDefault(column);
248
+ else {
249
+ if (includeNullable && !((_f = this.dialect) === null || _f === void 0 ? void 0 : _f.specificNullabilityImplementation)) {
250
+ this.put(column.notNull ? '^not ^null' : '^null');
251
+ }
252
+ if (includeDefault && ((_h = (_g = column.defaultValue) === null || _g === void 0 ? void 0 : _g.toString()) === null || _h === void 0 ? void 0 : _h.trim())) {
253
+ this.columnDefault(column);
254
+ }
242
255
  }
243
256
  }
244
257
  columnDefault(column) {
@@ -307,8 +320,9 @@ class SqlDumper {
307
320
  }
308
321
  }
309
322
  createForeignKeyFore(fk) {
310
- if (fk.constraintName != null)
323
+ if (fk.constraintName != null && !this.dialect.anonymousForeignKey) {
311
324
  this.put('^constraint %i ', fk.constraintName);
325
+ }
312
326
  this.put('^foreign ^key (%,i) ^references %f (%,i)', fk.columns.map(x => x.columnName), { schemaName: fk.refSchemaName, pureName: fk.refTableName }, fk.columns.map(x => x.refColumnName));
313
327
  if (fk.deleteAction)
314
328
  this.put(' ^on ^delete %k', fk.deleteAction);
@@ -320,6 +334,7 @@ class SqlDumper {
320
334
  }
321
335
  allowIdentityInsert(table, allow) { }
322
336
  enableConstraints(table, enabled) { }
337
+ enableAllForeignKeys(enabled) { }
323
338
  comment(value) {
324
339
  if (!value)
325
340
  return;
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.createBulkInsertStreamBase = void 0;
16
16
  const intersection_1 = __importDefault(require("lodash/intersection"));
17
+ const fromPairs_1 = __importDefault(require("lodash/fromPairs"));
17
18
  const getLogger_1 = require("./getLogger");
18
19
  const tableTransforms_1 = require("./tableTransforms");
19
20
  const logger = (0, getLogger_1.getLogger)('bulkStreamBase');
@@ -24,9 +25,11 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
24
25
  const writable = new stream.Writable({
25
26
  objectMode: true,
26
27
  });
28
+ writable.fullNameQuoted = fullNameQuoted;
27
29
  writable.buffer = [];
28
30
  writable.structure = null;
29
31
  writable.columnNames = null;
32
+ writable.columnDataTypes = null;
30
33
  writable.requireFixedStructure = driver.databaseEngineTypes.includes('sql');
31
34
  writable.addRow = (row) => __awaiter(this, void 0, void 0, function* () {
32
35
  if (writable.structure) {
@@ -38,8 +41,11 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
38
41
  }
39
42
  });
40
43
  writable.checkStructure = () => __awaiter(this, void 0, void 0, function* () {
41
- let structure = yield driver.analyseSingleTable(dbhan, name);
42
- // console.log('ANALYSING', name, structure);
44
+ var _a;
45
+ let structure = (_a = options.targetTableStructure) !== null && _a !== void 0 ? _a : (yield driver.analyseSingleTable(dbhan, name));
46
+ if (structure) {
47
+ writable.structure = structure;
48
+ }
43
49
  if (structure && options.dropIfExists) {
44
50
  logger.info(`Dropping table ${fullNameQuoted}`);
45
51
  yield driver.script(dbhan, `DROP TABLE ${fullNameQuoted}`);
@@ -51,11 +57,22 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
51
57
  logger.info({ sql: dmp.s }, `Creating table ${fullNameQuoted}`);
52
58
  yield driver.script(dbhan, dmp.s);
53
59
  structure = yield driver.analyseSingleTable(dbhan, name);
60
+ writable.structure = structure;
61
+ }
62
+ if (!writable.structure) {
63
+ throw new Error(`Error importing table - ${fullNameQuoted} not found`);
54
64
  }
55
65
  if (options.truncate) {
56
66
  yield driver.script(dbhan, `TRUNCATE TABLE ${fullNameQuoted}`);
57
67
  }
58
68
  writable.columnNames = (0, intersection_1.default)(structure.columns.map(x => x.columnName), writable.structure.columns.map(x => x.columnName));
69
+ writable.columnDataTypes = (0, fromPairs_1.default)(writable.columnNames.map(colName => {
70
+ var _a;
71
+ return [
72
+ colName,
73
+ (_a = writable.structure.columns.find(x => x.columnName == colName)) === null || _a === void 0 ? void 0 : _a.dataType,
74
+ ];
75
+ }));
59
76
  });
60
77
  writable.send = () => __awaiter(this, void 0, void 0, function* () {
61
78
  const rows = writable.buffer;
@@ -70,7 +87,7 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
70
87
  if (wasRow)
71
88
  dmp.putRaw(',\n');
72
89
  dmp.putRaw('(');
73
- dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
90
+ dmp.putCollection(',', writable.columnNames, col => { var _a; return dmp.putValue(row[col], (_a = writable.columnDataTypes) === null || _a === void 0 ? void 0 : _a[col]); });
74
91
  dmp.putRaw(')');
75
92
  wasRow = true;
76
93
  }
@@ -86,8 +103,9 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
86
103
  dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
87
104
  dmp.putRaw(')\n VALUES\n');
88
105
  dmp.putRaw('(');
89
- dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
106
+ dmp.putCollection(',', writable.columnNames, col => { var _a; return dmp.putValue(row[col], (_a = writable.columnDataTypes) === null || _a === void 0 ? void 0 : _a[col]); });
90
107
  dmp.putRaw(')');
108
+ // console.log(dmp.s);
91
109
  yield driver.query(dbhan, dmp.s, { discardResult: true });
92
110
  }
93
111
  }
@@ -3,6 +3,7 @@ import { EngineDriver, QueryResult, RunScriptOptions } from 'dbgate-types';
3
3
  export declare function runCommandOnDriver(pool: any, driver: EngineDriver, cmd: (dmp: SqlDumper) => void | string): Promise<void>;
4
4
  export declare function runQueryOnDriver(pool: any, driver: EngineDriver, cmd: (dmp: SqlDumper) => void): Promise<QueryResult>;
5
5
  export declare function formatQueryWithoutParams(driver: EngineDriver, sql: string): string;
6
+ export declare function runQueryFmt(driver: any, conn: any, query: any, ...args: any[]): Promise<void>;
6
7
  export declare const driverBase: {
7
8
  analyserClass: any;
8
9
  dumperClass: typeof SqlDumper;
@@ -33,7 +34,7 @@ export declare const driverBase: {
33
34
  label: string;
34
35
  sql: string;
35
36
  }[];
36
- loadFieldValues(pool: any, name: any, columnName: any, search: any): Promise<any>;
37
+ loadFieldValues(pool: any, name: any, columnName: any, search: any, dataType: any): Promise<any>;
37
38
  readJsonQuery(pool: any, select: any, structure: any): any;
38
39
  showConnectionField: (field: any, values: any) => boolean;
39
40
  showConnectionTab: (field: any) => boolean;
@@ -48,6 +49,7 @@ export declare const driverBase: {
48
49
  parseHexAsBuffer: boolean;
49
50
  };
50
51
  createSaveChangeSetScript(changeSet: any, dbinfo: any, defaultCreator: any): any;
52
+ adaptDataType(dataType: string): string;
51
53
  adaptTableInfo(table: any): any;
52
54
  listSchemas(pool: any): Promise<any>;
53
55
  writeQueryFromStream(dbhan: any, sql: any): Promise<any>;
package/lib/driverBase.js CHANGED
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.driverBase = exports.formatQueryWithoutParams = exports.runQueryOnDriver = exports.runCommandOnDriver = void 0;
15
+ exports.driverBase = exports.runQueryFmt = exports.formatQueryWithoutParams = exports.runQueryOnDriver = exports.runCommandOnDriver = void 0;
16
16
  const compact_1 = __importDefault(require("lodash/compact"));
17
17
  const isString_1 = __importDefault(require("lodash/isString"));
18
18
  const SqlDumper_1 = require("./SqlDumper");
@@ -72,6 +72,14 @@ function formatQueryWithoutParams(driver, sql) {
72
72
  return dmp.s;
73
73
  }
74
74
  exports.formatQueryWithoutParams = formatQueryWithoutParams;
75
+ function runQueryFmt(driver, conn, query, ...args) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ const dmp = driver.createDumper();
78
+ dmp.put(query, ...args);
79
+ yield driver.query(conn, dmp.s);
80
+ });
81
+ }
82
+ exports.runQueryFmt = runQueryFmt;
75
83
  exports.driverBase = {
76
84
  analyserClass: null,
77
85
  dumperClass: SqlDumper_1.SqlDumper,
@@ -147,30 +155,41 @@ exports.driverBase = {
147
155
  }
148
156
  return [];
149
157
  },
150
- loadFieldValues(pool, name, columnName, search) {
158
+ loadFieldValues(pool, name, columnName, search, dataType) {
151
159
  return __awaiter(this, void 0, void 0, function* () {
152
160
  const dmp = this.createDumper();
161
+ let expr;
162
+ if (this.dialect.createColumnViewExpression) {
163
+ expr = this.dialect.createColumnViewExpression(columnName, dataType, { name }, 'value');
164
+ }
165
+ if (!expr) {
166
+ expr = {
167
+ exprType: 'column',
168
+ columnName,
169
+ alias: 'value',
170
+ };
171
+ }
153
172
  const select = {
154
173
  commandType: 'select',
155
174
  distinct: true,
156
- topRecords: 100,
157
175
  from: {
158
176
  name,
159
177
  },
160
- columns: [
161
- {
162
- exprType: 'column',
163
- columnName,
164
- alias: 'value',
165
- },
166
- ],
178
+ columns: [expr],
167
179
  orderBy: [
168
180
  {
169
181
  exprType: 'column',
170
182
  columnName,
183
+ direction: 'ASC',
171
184
  },
172
185
  ],
173
186
  };
187
+ if (this.dialect.topRecords) {
188
+ select.topRecords = 100;
189
+ }
190
+ else {
191
+ select.range = { offset: 0, limit: 100 };
192
+ }
174
193
  if (search) {
175
194
  const tokens = (0, compact_1.default)(search.split(' ').map(x => x.trim()));
176
195
  if (tokens.length > 0) {
@@ -227,8 +246,12 @@ exports.driverBase = {
227
246
  createSaveChangeSetScript(changeSet, dbinfo, defaultCreator) {
228
247
  return defaultCreator(changeSet, dbinfo);
229
248
  },
249
+ adaptDataType(dataType) {
250
+ return dataType;
251
+ },
230
252
  adaptTableInfo(table) {
231
- return table;
253
+ var _a;
254
+ return Object.assign(Object.assign({}, table), { columns: (_a = table.columns) === null || _a === void 0 ? void 0 : _a.map(col => (Object.assign(Object.assign({}, col), { dataType: this.adaptDataType(col.dataType) }))) });
232
255
  },
233
256
  listSchemas(pool) {
234
257
  return __awaiter(this, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "6.1.6",
2
+ "version": "6.2.1",
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": "^6.1.6",
28
+ "dbgate-types": "^6.2.1",
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
34
  "dbgate-query-splitter": "^4.11.3",
35
- "dbgate-sqltree": "^6.1.6",
35
+ "dbgate-sqltree": "^6.2.1",
36
36
  "debug": "^4.3.4",
37
37
  "json-stable-stringify": "^1.0.1",
38
38
  "lodash": "^4.17.21",