knex 0.21.20 → 0.21.21
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 +6 -0
- package/CONTRIBUTING.md +184 -184
- package/LICENSE +22 -22
- package/README.md +95 -95
- package/bin/cli.js +414 -414
- package/bin/utils/cli-config-utils.js +151 -151
- package/bin/utils/constants.js +7 -7
- package/bin/utils/migrationsLister.js +37 -37
- package/knex.js +8 -8
- package/lib/client.js +413 -413
- package/lib/config-resolver.js +61 -61
- package/lib/constants.js +44 -44
- package/lib/dialects/mssql/index.js +390 -390
- package/lib/dialects/mssql/query/compiler.js +444 -444
- package/lib/dialects/mssql/schema/columncompiler.js +103 -103
- package/lib/dialects/mssql/schema/compiler.js +59 -59
- package/lib/dialects/mssql/schema/tablecompiler.js +245 -245
- package/lib/dialects/mssql/transaction.js +97 -97
- package/lib/dialects/mysql/index.js +191 -191
- package/lib/dialects/mysql/query/compiler.js +142 -142
- package/lib/dialects/mysql/schema/columncompiler.js +171 -171
- package/lib/dialects/mysql/schema/compiler.js +60 -60
- package/lib/dialects/mysql/schema/tablecompiler.js +262 -262
- package/lib/dialects/mysql/transaction.js +48 -48
- package/lib/dialects/mysql2/index.js +35 -35
- package/lib/dialects/mysql2/transaction.js +46 -46
- package/lib/dialects/oracle/DEAD_CODE.md +5 -5
- package/lib/dialects/oracle/formatter.js +20 -20
- package/lib/dialects/oracle/index.js +79 -79
- package/lib/dialects/oracle/query/compiler.js +327 -327
- package/lib/dialects/oracle/schema/columnbuilder.js +18 -18
- package/lib/dialects/oracle/schema/columncompiler.js +139 -139
- package/lib/dialects/oracle/schema/compiler.js +81 -81
- package/lib/dialects/oracle/schema/tablecompiler.js +165 -165
- package/lib/dialects/oracle/schema/trigger.js +126 -126
- package/lib/dialects/oracle/utils.js +86 -86
- package/lib/dialects/oracledb/index.js +489 -489
- package/lib/dialects/oracledb/query/compiler.js +363 -363
- package/lib/dialects/oracledb/schema/columncompiler.js +35 -35
- package/lib/dialects/oracledb/transaction.js +76 -76
- package/lib/dialects/oracledb/utils.js +14 -14
- package/lib/dialects/postgres/index.js +319 -319
- package/lib/dialects/postgres/query/compiler.js +206 -206
- package/lib/dialects/postgres/schema/columncompiler.js +125 -125
- package/lib/dialects/postgres/schema/compiler.js +109 -109
- package/lib/dialects/postgres/schema/tablecompiler.js +183 -183
- package/lib/dialects/redshift/index.js +73 -73
- package/lib/dialects/redshift/query/compiler.js +119 -119
- package/lib/dialects/redshift/schema/columnbuilder.js +20 -20
- package/lib/dialects/redshift/schema/columncompiler.js +60 -60
- package/lib/dialects/redshift/schema/compiler.js +14 -14
- package/lib/dialects/redshift/schema/tablecompiler.js +123 -123
- package/lib/dialects/redshift/transaction.js +18 -18
- package/lib/dialects/sqlite3/formatter.js +21 -21
- package/lib/dialects/sqlite3/index.js +169 -169
- package/lib/dialects/sqlite3/query/compiler.js +222 -222
- package/lib/dialects/sqlite3/schema/columncompiler.js +27 -27
- package/lib/dialects/sqlite3/schema/compiler.js +49 -49
- package/lib/dialects/sqlite3/schema/ddl.js +525 -525
- package/lib/dialects/sqlite3/schema/tablecompiler.js +238 -238
- package/lib/formatter.js +295 -295
- package/lib/functionhelper.js +14 -14
- package/lib/helpers.js +92 -92
- package/lib/index.js +3 -3
- package/lib/interface.js +115 -115
- package/lib/knex.js +42 -42
- package/lib/logger.js +76 -76
- package/lib/migrate/MigrationGenerator.js +82 -82
- package/lib/migrate/Migrator.js +611 -611
- package/lib/migrate/configuration-merger.js +60 -60
- package/lib/migrate/migrate-stub.js +17 -17
- package/lib/migrate/migration-list-resolver.js +36 -36
- package/lib/migrate/sources/fs-migrations.js +99 -99
- package/lib/migrate/stub/cjs.stub +15 -15
- package/lib/migrate/stub/coffee.stub +13 -13
- package/lib/migrate/stub/eg.stub +14 -14
- package/lib/migrate/stub/js.stub +15 -15
- package/lib/migrate/stub/knexfile-coffee.stub +34 -34
- package/lib/migrate/stub/knexfile-eg.stub +43 -43
- package/lib/migrate/stub/knexfile-js.stub +44 -44
- package/lib/migrate/stub/knexfile-ls.stub +35 -35
- package/lib/migrate/stub/knexfile-ts.stub +44 -44
- package/lib/migrate/stub/ls.stub +14 -14
- package/lib/migrate/stub/ts.stub +21 -21
- package/lib/migrate/table-creator.js +67 -67
- package/lib/migrate/table-resolver.js +27 -27
- package/lib/query/builder.js +1372 -1372
- package/lib/query/compiler.js +889 -889
- package/lib/query/constants.js +13 -13
- package/lib/query/joinclause.js +263 -263
- package/lib/query/methods.js +92 -92
- package/lib/query/string.js +190 -190
- package/lib/raw.js +188 -188
- package/lib/ref.js +39 -39
- package/lib/runner.js +285 -285
- package/lib/schema/builder.js +82 -82
- package/lib/schema/columnbuilder.js +117 -117
- package/lib/schema/columncompiler.js +177 -177
- package/lib/schema/compiler.js +101 -101
- package/lib/schema/helpers.js +51 -51
- package/lib/schema/tablebuilder.js +288 -288
- package/lib/schema/tablecompiler.js +296 -296
- package/lib/seed/Seeder.js +203 -203
- package/lib/seed/seed-stub.js +13 -13
- package/lib/seed/stub/coffee.stub +9 -9
- package/lib/seed/stub/eg.stub +11 -11
- package/lib/seed/stub/js.stub +13 -13
- package/lib/seed/stub/ls.stub +11 -11
- package/lib/seed/stub/ts.stub +13 -13
- package/lib/transaction.js +363 -363
- package/lib/util/batchInsert.js +59 -59
- package/lib/util/delay.js +6 -6
- package/lib/util/fake-client.js +9 -9
- package/lib/util/finally-mixin.js +13 -13
- package/lib/util/fs.js +76 -76
- package/lib/util/import-file.js +13 -13
- package/lib/util/is-module-type.js +14 -14
- package/lib/util/is.js +32 -32
- package/lib/util/make-knex.js +338 -338
- package/lib/util/nanoid.js +29 -29
- package/lib/util/noop.js +1 -1
- package/lib/util/parse-connection.js +66 -66
- package/lib/util/save-async-stack.js +14 -14
- package/lib/util/template.js +52 -52
- package/lib/util/timeout.js +29 -29
- package/lib/util/timestamp.js +16 -16
- package/package.json +1 -1
- package/scripts/build.js +125 -125
- package/scripts/docker-compose.yml +111 -111
- package/scripts/next-release-howto.md +24 -24
- package/scripts/release.sh +34 -34
- package/scripts/runkit-example.js +34 -34
- package/scripts/stress-test/README.txt +18 -18
- package/scripts/stress-test/docker-compose.yml +47 -47
- package/scripts/stress-test/knex-stress-test.js +196 -196
- package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
- package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
- package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
- package/types/index.d.ts +2249 -2249
- package/types/result.d.ts +27 -27
- package/types/tables.d.ts +4 -4
|
@@ -1,206 +1,206 @@
|
|
|
1
|
-
// PostgreSQL Query Builder & Compiler
|
|
2
|
-
// ------
|
|
3
|
-
const QueryCompiler = require('../../../query/compiler');
|
|
4
|
-
|
|
5
|
-
const identity = require('lodash/identity');
|
|
6
|
-
const reduce = require('lodash/reduce');
|
|
7
|
-
|
|
8
|
-
class QueryCompiler_PG extends QueryCompiler {
|
|
9
|
-
constructor(client, builder) {
|
|
10
|
-
super(client, builder);
|
|
11
|
-
this._defaultInsertValue = 'default';
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// Compiles a truncate query.
|
|
15
|
-
truncate() {
|
|
16
|
-
return `truncate ${this.tableName} restart identity`;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// is used if the an array with multiple empty values supplied
|
|
20
|
-
|
|
21
|
-
// Compiles an `insert` query, allowing for multiple
|
|
22
|
-
// inserts using a single query statement.
|
|
23
|
-
insert() {
|
|
24
|
-
let sql = QueryCompiler.prototype.insert.call(this);
|
|
25
|
-
if (sql === '') return sql;
|
|
26
|
-
|
|
27
|
-
const { returning, onConflict, ignore, merge, insert } = this.single;
|
|
28
|
-
if (onConflict && ignore) sql += this._ignore(onConflict);
|
|
29
|
-
if (onConflict && merge) {
|
|
30
|
-
sql += this._merge(merge.updates, onConflict, insert);
|
|
31
|
-
const wheres = this.where();
|
|
32
|
-
if (wheres) sql += ` ${wheres}`;
|
|
33
|
-
}
|
|
34
|
-
if (returning) sql += this._returning(returning);
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
sql: sql,
|
|
38
|
-
returning,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Compiles an `update` query, allowing for a return value.
|
|
43
|
-
update() {
|
|
44
|
-
const withSQL = this.with();
|
|
45
|
-
const updateData = this._prepUpdate(this.single.update);
|
|
46
|
-
const wheres = this.where();
|
|
47
|
-
const { returning } = this.single;
|
|
48
|
-
return {
|
|
49
|
-
sql:
|
|
50
|
-
withSQL +
|
|
51
|
-
`update ${this.single.only ? 'only ' : ''}${this.tableName} ` +
|
|
52
|
-
`set ${updateData.join(', ')}` +
|
|
53
|
-
(wheres ? ` ${wheres}` : '') +
|
|
54
|
-
this._returning(returning),
|
|
55
|
-
returning,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Compiles an `update` query, allowing for a return value.
|
|
60
|
-
del() {
|
|
61
|
-
const sql = QueryCompiler.prototype.del.apply(this, arguments);
|
|
62
|
-
const { returning } = this.single;
|
|
63
|
-
return {
|
|
64
|
-
sql: sql + this._returning(returning),
|
|
65
|
-
returning,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
aggregate(stmt) {
|
|
70
|
-
return this._aggregate(stmt, { distinctParentheses: true });
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
_returning(value) {
|
|
74
|
-
return value ? ` returning ${this.formatter.columnize(value)}` : '';
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
_ignore(columns) {
|
|
78
|
-
return ` on conflict (${this.formatter.columnize(columns)}) do nothing`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
_merge(updates, columns, insert) {
|
|
82
|
-
let sql = ` on conflict (${this.formatter.columnize(
|
|
83
|
-
columns
|
|
84
|
-
)}) do update set `;
|
|
85
|
-
if (updates) {
|
|
86
|
-
const updateData = this._prepUpdate(updates);
|
|
87
|
-
if (typeof updateData === 'string') {
|
|
88
|
-
sql += updateData;
|
|
89
|
-
} else {
|
|
90
|
-
sql += updateData.join(',');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return sql;
|
|
94
|
-
} else {
|
|
95
|
-
const insertData = this._prepInsert(insert);
|
|
96
|
-
if (typeof insertData === 'string') {
|
|
97
|
-
throw new Error(
|
|
98
|
-
'If using merge with a raw insert query, then updates must be provided'
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
sql += insertData.columns
|
|
103
|
-
.map((column) => this.formatter.wrapString(column.split('.').pop()))
|
|
104
|
-
.map((column) => `${column} = excluded.${column}`)
|
|
105
|
-
.join(', ');
|
|
106
|
-
|
|
107
|
-
return sql;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Join array of table names and apply default schema.
|
|
112
|
-
_tableNames(tables) {
|
|
113
|
-
const schemaName = this.single.schema;
|
|
114
|
-
const sql = [];
|
|
115
|
-
|
|
116
|
-
for (let i = 0; i < tables.length; i++) {
|
|
117
|
-
let tableName = tables[i];
|
|
118
|
-
|
|
119
|
-
if (tableName) {
|
|
120
|
-
if (schemaName) {
|
|
121
|
-
tableName = `${schemaName}.${tableName}`;
|
|
122
|
-
}
|
|
123
|
-
sql.push(this.formatter.wrap(tableName));
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return sql.join(', ');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
forUpdate() {
|
|
131
|
-
const tables = this.single.lockTables || [];
|
|
132
|
-
|
|
133
|
-
return (
|
|
134
|
-
'for update' + (tables.length ? ' of ' + this._tableNames(tables) : '')
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
forShare() {
|
|
139
|
-
const tables = this.single.lockTables || [];
|
|
140
|
-
|
|
141
|
-
return (
|
|
142
|
-
'for share' + (tables.length ? ' of ' + this._tableNames(tables) : '')
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
skipLocked() {
|
|
147
|
-
return 'skip locked';
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
noWait() {
|
|
151
|
-
return 'nowait';
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Compiles a columnInfo query
|
|
155
|
-
columnInfo() {
|
|
156
|
-
const column = this.single.columnInfo;
|
|
157
|
-
let schema = this.single.schema;
|
|
158
|
-
|
|
159
|
-
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
160
|
-
// need to run the identifiers through that function, but not format them as
|
|
161
|
-
// identifiers otherwise.
|
|
162
|
-
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
163
|
-
|
|
164
|
-
if (schema) {
|
|
165
|
-
schema = this.client.customWrapIdentifier(schema, identity);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
let sql =
|
|
169
|
-
'select * from information_schema.columns where table_name = ? and table_catalog = ?';
|
|
170
|
-
const bindings = [table, this.client.database()];
|
|
171
|
-
|
|
172
|
-
if (schema) {
|
|
173
|
-
sql += ' and table_schema = ?';
|
|
174
|
-
bindings.push(schema);
|
|
175
|
-
} else {
|
|
176
|
-
sql += ' and table_schema = current_schema()';
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return {
|
|
180
|
-
sql,
|
|
181
|
-
bindings,
|
|
182
|
-
output(resp) {
|
|
183
|
-
const out = reduce(
|
|
184
|
-
resp.rows,
|
|
185
|
-
function (columns, val) {
|
|
186
|
-
columns[val.column_name] = {
|
|
187
|
-
type: val.data_type,
|
|
188
|
-
maxLength: val.character_maximum_length,
|
|
189
|
-
nullable: val.is_nullable === 'YES',
|
|
190
|
-
defaultValue: val.column_default,
|
|
191
|
-
};
|
|
192
|
-
return columns;
|
|
193
|
-
},
|
|
194
|
-
{}
|
|
195
|
-
);
|
|
196
|
-
return (column && out[column]) || out;
|
|
197
|
-
},
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
distinctOn(value) {
|
|
202
|
-
return 'distinct on (' + this.formatter.columnize(value) + ') ';
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
module.exports = QueryCompiler_PG;
|
|
1
|
+
// PostgreSQL Query Builder & Compiler
|
|
2
|
+
// ------
|
|
3
|
+
const QueryCompiler = require('../../../query/compiler');
|
|
4
|
+
|
|
5
|
+
const identity = require('lodash/identity');
|
|
6
|
+
const reduce = require('lodash/reduce');
|
|
7
|
+
|
|
8
|
+
class QueryCompiler_PG extends QueryCompiler {
|
|
9
|
+
constructor(client, builder) {
|
|
10
|
+
super(client, builder);
|
|
11
|
+
this._defaultInsertValue = 'default';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Compiles a truncate query.
|
|
15
|
+
truncate() {
|
|
16
|
+
return `truncate ${this.tableName} restart identity`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// is used if the an array with multiple empty values supplied
|
|
20
|
+
|
|
21
|
+
// Compiles an `insert` query, allowing for multiple
|
|
22
|
+
// inserts using a single query statement.
|
|
23
|
+
insert() {
|
|
24
|
+
let sql = QueryCompiler.prototype.insert.call(this);
|
|
25
|
+
if (sql === '') return sql;
|
|
26
|
+
|
|
27
|
+
const { returning, onConflict, ignore, merge, insert } = this.single;
|
|
28
|
+
if (onConflict && ignore) sql += this._ignore(onConflict);
|
|
29
|
+
if (onConflict && merge) {
|
|
30
|
+
sql += this._merge(merge.updates, onConflict, insert);
|
|
31
|
+
const wheres = this.where();
|
|
32
|
+
if (wheres) sql += ` ${wheres}`;
|
|
33
|
+
}
|
|
34
|
+
if (returning) sql += this._returning(returning);
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
sql: sql,
|
|
38
|
+
returning,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Compiles an `update` query, allowing for a return value.
|
|
43
|
+
update() {
|
|
44
|
+
const withSQL = this.with();
|
|
45
|
+
const updateData = this._prepUpdate(this.single.update);
|
|
46
|
+
const wheres = this.where();
|
|
47
|
+
const { returning } = this.single;
|
|
48
|
+
return {
|
|
49
|
+
sql:
|
|
50
|
+
withSQL +
|
|
51
|
+
`update ${this.single.only ? 'only ' : ''}${this.tableName} ` +
|
|
52
|
+
`set ${updateData.join(', ')}` +
|
|
53
|
+
(wheres ? ` ${wheres}` : '') +
|
|
54
|
+
this._returning(returning),
|
|
55
|
+
returning,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Compiles an `update` query, allowing for a return value.
|
|
60
|
+
del() {
|
|
61
|
+
const sql = QueryCompiler.prototype.del.apply(this, arguments);
|
|
62
|
+
const { returning } = this.single;
|
|
63
|
+
return {
|
|
64
|
+
sql: sql + this._returning(returning),
|
|
65
|
+
returning,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
aggregate(stmt) {
|
|
70
|
+
return this._aggregate(stmt, { distinctParentheses: true });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
_returning(value) {
|
|
74
|
+
return value ? ` returning ${this.formatter.columnize(value)}` : '';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_ignore(columns) {
|
|
78
|
+
return ` on conflict (${this.formatter.columnize(columns)}) do nothing`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
_merge(updates, columns, insert) {
|
|
82
|
+
let sql = ` on conflict (${this.formatter.columnize(
|
|
83
|
+
columns
|
|
84
|
+
)}) do update set `;
|
|
85
|
+
if (updates) {
|
|
86
|
+
const updateData = this._prepUpdate(updates);
|
|
87
|
+
if (typeof updateData === 'string') {
|
|
88
|
+
sql += updateData;
|
|
89
|
+
} else {
|
|
90
|
+
sql += updateData.join(',');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return sql;
|
|
94
|
+
} else {
|
|
95
|
+
const insertData = this._prepInsert(insert);
|
|
96
|
+
if (typeof insertData === 'string') {
|
|
97
|
+
throw new Error(
|
|
98
|
+
'If using merge with a raw insert query, then updates must be provided'
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
sql += insertData.columns
|
|
103
|
+
.map((column) => this.formatter.wrapString(column.split('.').pop()))
|
|
104
|
+
.map((column) => `${column} = excluded.${column}`)
|
|
105
|
+
.join(', ');
|
|
106
|
+
|
|
107
|
+
return sql;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Join array of table names and apply default schema.
|
|
112
|
+
_tableNames(tables) {
|
|
113
|
+
const schemaName = this.single.schema;
|
|
114
|
+
const sql = [];
|
|
115
|
+
|
|
116
|
+
for (let i = 0; i < tables.length; i++) {
|
|
117
|
+
let tableName = tables[i];
|
|
118
|
+
|
|
119
|
+
if (tableName) {
|
|
120
|
+
if (schemaName) {
|
|
121
|
+
tableName = `${schemaName}.${tableName}`;
|
|
122
|
+
}
|
|
123
|
+
sql.push(this.formatter.wrap(tableName));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return sql.join(', ');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
forUpdate() {
|
|
131
|
+
const tables = this.single.lockTables || [];
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
'for update' + (tables.length ? ' of ' + this._tableNames(tables) : '')
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
forShare() {
|
|
139
|
+
const tables = this.single.lockTables || [];
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
'for share' + (tables.length ? ' of ' + this._tableNames(tables) : '')
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
skipLocked() {
|
|
147
|
+
return 'skip locked';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
noWait() {
|
|
151
|
+
return 'nowait';
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Compiles a columnInfo query
|
|
155
|
+
columnInfo() {
|
|
156
|
+
const column = this.single.columnInfo;
|
|
157
|
+
let schema = this.single.schema;
|
|
158
|
+
|
|
159
|
+
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
160
|
+
// need to run the identifiers through that function, but not format them as
|
|
161
|
+
// identifiers otherwise.
|
|
162
|
+
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
163
|
+
|
|
164
|
+
if (schema) {
|
|
165
|
+
schema = this.client.customWrapIdentifier(schema, identity);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let sql =
|
|
169
|
+
'select * from information_schema.columns where table_name = ? and table_catalog = ?';
|
|
170
|
+
const bindings = [table, this.client.database()];
|
|
171
|
+
|
|
172
|
+
if (schema) {
|
|
173
|
+
sql += ' and table_schema = ?';
|
|
174
|
+
bindings.push(schema);
|
|
175
|
+
} else {
|
|
176
|
+
sql += ' and table_schema = current_schema()';
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
sql,
|
|
181
|
+
bindings,
|
|
182
|
+
output(resp) {
|
|
183
|
+
const out = reduce(
|
|
184
|
+
resp.rows,
|
|
185
|
+
function (columns, val) {
|
|
186
|
+
columns[val.column_name] = {
|
|
187
|
+
type: val.data_type,
|
|
188
|
+
maxLength: val.character_maximum_length,
|
|
189
|
+
nullable: val.is_nullable === 'YES',
|
|
190
|
+
defaultValue: val.column_default,
|
|
191
|
+
};
|
|
192
|
+
return columns;
|
|
193
|
+
},
|
|
194
|
+
{}
|
|
195
|
+
);
|
|
196
|
+
return (column && out[column]) || out;
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
distinctOn(value) {
|
|
202
|
+
return 'distinct on (' + this.formatter.columnize(value) + ') ';
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
module.exports = QueryCompiler_PG;
|
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
// PostgreSQL Column Compiler
|
|
2
|
-
// -------
|
|
3
|
-
|
|
4
|
-
const { inherits } = require('util');
|
|
5
|
-
const ColumnCompiler = require('../../../schema/columncompiler');
|
|
6
|
-
const { isObject } = require('../../../util/is');
|
|
7
|
-
const commentEscapeRegex = /(?<!')'(?!')/g;
|
|
8
|
-
|
|
9
|
-
function ColumnCompiler_PG() {
|
|
10
|
-
ColumnCompiler.apply(this, arguments);
|
|
11
|
-
this.modifiers = ['nullable', 'defaultTo', 'comment'];
|
|
12
|
-
}
|
|
13
|
-
inherits(ColumnCompiler_PG, ColumnCompiler);
|
|
14
|
-
|
|
15
|
-
Object.assign(ColumnCompiler_PG.prototype, {
|
|
16
|
-
// Types
|
|
17
|
-
// ------
|
|
18
|
-
bigincrements: 'bigserial primary key',
|
|
19
|
-
bigint: 'bigint',
|
|
20
|
-
binary: 'bytea',
|
|
21
|
-
|
|
22
|
-
bit(column) {
|
|
23
|
-
return column.length !== false ? `bit(${column.length})` : 'bit';
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
bool: 'boolean',
|
|
27
|
-
|
|
28
|
-
// Create the column definition for an enum type.
|
|
29
|
-
// Using method "2" here: http://stackoverflow.com/a/10984951/525714
|
|
30
|
-
enu(allowed, options) {
|
|
31
|
-
options = options || {};
|
|
32
|
-
|
|
33
|
-
const values =
|
|
34
|
-
options.useNative && options.existingType
|
|
35
|
-
? undefined
|
|
36
|
-
: allowed.join("', '");
|
|
37
|
-
|
|
38
|
-
if (options.useNative) {
|
|
39
|
-
let enumName = '';
|
|
40
|
-
const schemaName = options.schemaName || this.tableCompiler.schemaNameRaw;
|
|
41
|
-
|
|
42
|
-
if (schemaName) {
|
|
43
|
-
enumName += `"${schemaName}".`;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
enumName += `"${options.enumName}"`;
|
|
47
|
-
|
|
48
|
-
if (!options.existingType) {
|
|
49
|
-
this.tableCompiler.unshiftQuery(
|
|
50
|
-
`create type ${enumName} as enum ('${values}')`
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return enumName;
|
|
55
|
-
}
|
|
56
|
-
return `text check (${this.formatter.wrap(this.args[0])} in ('${values}'))`;
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
double: 'double precision',
|
|
60
|
-
decimal(precision, scale) {
|
|
61
|
-
if (precision === null) return 'decimal';
|
|
62
|
-
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
|
63
|
-
},
|
|
64
|
-
floating: 'real',
|
|
65
|
-
increments: 'serial primary key',
|
|
66
|
-
json(jsonb) {
|
|
67
|
-
if (jsonb) this.client.logger.deprecate('json(true)', 'jsonb()');
|
|
68
|
-
return jsonColumn(this.client, jsonb);
|
|
69
|
-
},
|
|
70
|
-
jsonb() {
|
|
71
|
-
return jsonColumn(this.client, true);
|
|
72
|
-
},
|
|
73
|
-
smallint: 'smallint',
|
|
74
|
-
tinyint: 'smallint',
|
|
75
|
-
datetime(withoutTz = false, precision) {
|
|
76
|
-
let useTz;
|
|
77
|
-
if (isObject(withoutTz)) {
|
|
78
|
-
({ useTz, precision } = withoutTz);
|
|
79
|
-
} else {
|
|
80
|
-
useTz = !withoutTz;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return `${useTz ? 'timestamptz' : 'timestamp'}${
|
|
84
|
-
precision ? '(' + precision + ')' : ''
|
|
85
|
-
}`;
|
|
86
|
-
},
|
|
87
|
-
timestamp(withoutTz = false, precision) {
|
|
88
|
-
let useTz;
|
|
89
|
-
if (isObject(withoutTz)) {
|
|
90
|
-
({ useTz, precision } = withoutTz);
|
|
91
|
-
} else {
|
|
92
|
-
useTz = !withoutTz;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return `${useTz ? 'timestamptz' : 'timestamp'}${
|
|
96
|
-
precision ? '(' + precision + ')' : ''
|
|
97
|
-
}`;
|
|
98
|
-
},
|
|
99
|
-
uuid: 'uuid',
|
|
100
|
-
|
|
101
|
-
// Modifiers:
|
|
102
|
-
// ------
|
|
103
|
-
comment(comment) {
|
|
104
|
-
const columnName = this.args[0] || this.defaults('columnName');
|
|
105
|
-
const escapedComment = comment
|
|
106
|
-
? `'${comment.replace(commentEscapeRegex, "''")}'`
|
|
107
|
-
: 'NULL';
|
|
108
|
-
|
|
109
|
-
this.pushAdditional(function () {
|
|
110
|
-
this.pushQuery(
|
|
111
|
-
`comment on column ${this.tableCompiler.tableName()}.` +
|
|
112
|
-
this.formatter.wrap(columnName) +
|
|
113
|
-
` is ${escapedComment}`
|
|
114
|
-
);
|
|
115
|
-
}, comment);
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
function jsonColumn(client, jsonb) {
|
|
120
|
-
if (!client.version || parseFloat(client.version) >= 9.2)
|
|
121
|
-
return jsonb ? 'jsonb' : 'json';
|
|
122
|
-
return 'text';
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
module.exports = ColumnCompiler_PG;
|
|
1
|
+
// PostgreSQL Column Compiler
|
|
2
|
+
// -------
|
|
3
|
+
|
|
4
|
+
const { inherits } = require('util');
|
|
5
|
+
const ColumnCompiler = require('../../../schema/columncompiler');
|
|
6
|
+
const { isObject } = require('../../../util/is');
|
|
7
|
+
const commentEscapeRegex = /(?<!')'(?!')/g;
|
|
8
|
+
|
|
9
|
+
function ColumnCompiler_PG() {
|
|
10
|
+
ColumnCompiler.apply(this, arguments);
|
|
11
|
+
this.modifiers = ['nullable', 'defaultTo', 'comment'];
|
|
12
|
+
}
|
|
13
|
+
inherits(ColumnCompiler_PG, ColumnCompiler);
|
|
14
|
+
|
|
15
|
+
Object.assign(ColumnCompiler_PG.prototype, {
|
|
16
|
+
// Types
|
|
17
|
+
// ------
|
|
18
|
+
bigincrements: 'bigserial primary key',
|
|
19
|
+
bigint: 'bigint',
|
|
20
|
+
binary: 'bytea',
|
|
21
|
+
|
|
22
|
+
bit(column) {
|
|
23
|
+
return column.length !== false ? `bit(${column.length})` : 'bit';
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
bool: 'boolean',
|
|
27
|
+
|
|
28
|
+
// Create the column definition for an enum type.
|
|
29
|
+
// Using method "2" here: http://stackoverflow.com/a/10984951/525714
|
|
30
|
+
enu(allowed, options) {
|
|
31
|
+
options = options || {};
|
|
32
|
+
|
|
33
|
+
const values =
|
|
34
|
+
options.useNative && options.existingType
|
|
35
|
+
? undefined
|
|
36
|
+
: allowed.join("', '");
|
|
37
|
+
|
|
38
|
+
if (options.useNative) {
|
|
39
|
+
let enumName = '';
|
|
40
|
+
const schemaName = options.schemaName || this.tableCompiler.schemaNameRaw;
|
|
41
|
+
|
|
42
|
+
if (schemaName) {
|
|
43
|
+
enumName += `"${schemaName}".`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
enumName += `"${options.enumName}"`;
|
|
47
|
+
|
|
48
|
+
if (!options.existingType) {
|
|
49
|
+
this.tableCompiler.unshiftQuery(
|
|
50
|
+
`create type ${enumName} as enum ('${values}')`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return enumName;
|
|
55
|
+
}
|
|
56
|
+
return `text check (${this.formatter.wrap(this.args[0])} in ('${values}'))`;
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
double: 'double precision',
|
|
60
|
+
decimal(precision, scale) {
|
|
61
|
+
if (precision === null) return 'decimal';
|
|
62
|
+
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
|
63
|
+
},
|
|
64
|
+
floating: 'real',
|
|
65
|
+
increments: 'serial primary key',
|
|
66
|
+
json(jsonb) {
|
|
67
|
+
if (jsonb) this.client.logger.deprecate('json(true)', 'jsonb()');
|
|
68
|
+
return jsonColumn(this.client, jsonb);
|
|
69
|
+
},
|
|
70
|
+
jsonb() {
|
|
71
|
+
return jsonColumn(this.client, true);
|
|
72
|
+
},
|
|
73
|
+
smallint: 'smallint',
|
|
74
|
+
tinyint: 'smallint',
|
|
75
|
+
datetime(withoutTz = false, precision) {
|
|
76
|
+
let useTz;
|
|
77
|
+
if (isObject(withoutTz)) {
|
|
78
|
+
({ useTz, precision } = withoutTz);
|
|
79
|
+
} else {
|
|
80
|
+
useTz = !withoutTz;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return `${useTz ? 'timestamptz' : 'timestamp'}${
|
|
84
|
+
precision ? '(' + precision + ')' : ''
|
|
85
|
+
}`;
|
|
86
|
+
},
|
|
87
|
+
timestamp(withoutTz = false, precision) {
|
|
88
|
+
let useTz;
|
|
89
|
+
if (isObject(withoutTz)) {
|
|
90
|
+
({ useTz, precision } = withoutTz);
|
|
91
|
+
} else {
|
|
92
|
+
useTz = !withoutTz;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return `${useTz ? 'timestamptz' : 'timestamp'}${
|
|
96
|
+
precision ? '(' + precision + ')' : ''
|
|
97
|
+
}`;
|
|
98
|
+
},
|
|
99
|
+
uuid: 'uuid',
|
|
100
|
+
|
|
101
|
+
// Modifiers:
|
|
102
|
+
// ------
|
|
103
|
+
comment(comment) {
|
|
104
|
+
const columnName = this.args[0] || this.defaults('columnName');
|
|
105
|
+
const escapedComment = comment
|
|
106
|
+
? `'${comment.replace(commentEscapeRegex, "''")}'`
|
|
107
|
+
: 'NULL';
|
|
108
|
+
|
|
109
|
+
this.pushAdditional(function () {
|
|
110
|
+
this.pushQuery(
|
|
111
|
+
`comment on column ${this.tableCompiler.tableName()}.` +
|
|
112
|
+
this.formatter.wrap(columnName) +
|
|
113
|
+
` is ${escapedComment}`
|
|
114
|
+
);
|
|
115
|
+
}, comment);
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
function jsonColumn(client, jsonb) {
|
|
120
|
+
if (!client.version || parseFloat(client.version) >= 9.2)
|
|
121
|
+
return jsonb ? 'jsonb' : 'json';
|
|
122
|
+
return 'text';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = ColumnCompiler_PG;
|