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,222 +1,222 @@
|
|
|
1
|
-
// SQLite3 Query Builder & Compiler
|
|
2
|
-
|
|
3
|
-
const QueryCompiler = require('../../../query/compiler');
|
|
4
|
-
|
|
5
|
-
const noop = require('../../../util/noop');
|
|
6
|
-
const constant = require('lodash/constant');
|
|
7
|
-
const each = require('lodash/each');
|
|
8
|
-
const identity = require('lodash/identity');
|
|
9
|
-
const isEmpty = require('lodash/isEmpty');
|
|
10
|
-
const reduce = require('lodash/reduce');
|
|
11
|
-
const { isString } = require('../../../util/is');
|
|
12
|
-
|
|
13
|
-
const emptyStr = constant('');
|
|
14
|
-
|
|
15
|
-
class QueryCompiler_SQLite3 extends QueryCompiler {
|
|
16
|
-
constructor(client, builder) {
|
|
17
|
-
super(client, builder);
|
|
18
|
-
|
|
19
|
-
const { returning } = this.single;
|
|
20
|
-
|
|
21
|
-
if (returning) {
|
|
22
|
-
this.client.logger.warn(
|
|
23
|
-
'.returning() is not supported by sqlite3 and will not have any effect.'
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// The locks are not applicable in SQLite3
|
|
28
|
-
this.forShare = emptyStr;
|
|
29
|
-
this.forUpdate = emptyStr;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// SQLite requires us to build the multi-row insert as a listing of select with
|
|
33
|
-
// unions joining them together. So we'll build out this list of columns and
|
|
34
|
-
// then join them all together with select unions to complete the queries.
|
|
35
|
-
insert() {
|
|
36
|
-
const insertValues = this.single.insert || [];
|
|
37
|
-
let sql = this.with() + `insert into ${this.tableName} `;
|
|
38
|
-
|
|
39
|
-
if (Array.isArray(insertValues)) {
|
|
40
|
-
if (insertValues.length === 0) {
|
|
41
|
-
return '';
|
|
42
|
-
} else if (
|
|
43
|
-
insertValues.length === 1 &&
|
|
44
|
-
insertValues[0] &&
|
|
45
|
-
isEmpty(insertValues[0])
|
|
46
|
-
) {
|
|
47
|
-
return sql + this._emptyInsertValue;
|
|
48
|
-
}
|
|
49
|
-
} else if (typeof insertValues === 'object' && isEmpty(insertValues)) {
|
|
50
|
-
return sql + this._emptyInsertValue;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const insertData = this._prepInsert(insertValues);
|
|
54
|
-
|
|
55
|
-
if (isString(insertData)) {
|
|
56
|
-
return sql + insertData;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (insertData.columns.length === 0) {
|
|
60
|
-
return '';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
sql += `(${this.formatter.columnize(insertData.columns)})`;
|
|
64
|
-
|
|
65
|
-
// backwards compatible error
|
|
66
|
-
if (this.client.valueForUndefined !== null) {
|
|
67
|
-
insertData.values.forEach((bindings) => {
|
|
68
|
-
each(bindings, (binding) => {
|
|
69
|
-
if (binding === undefined)
|
|
70
|
-
throw new TypeError(
|
|
71
|
-
'`sqlite` does not support inserting default values. Specify ' +
|
|
72
|
-
'values explicitly or use the `useNullAsDefault` config flag. ' +
|
|
73
|
-
'(see docs http://knexjs.org/#Builder-insert).'
|
|
74
|
-
);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (insertData.values.length === 1) {
|
|
80
|
-
const parameters = this.formatter.parameterize(
|
|
81
|
-
insertData.values[0],
|
|
82
|
-
this.client.valueForUndefined
|
|
83
|
-
);
|
|
84
|
-
sql += ` values (${parameters})`;
|
|
85
|
-
|
|
86
|
-
const { onConflict, ignore, merge } = this.single;
|
|
87
|
-
if (onConflict && ignore) sql += this._ignore(onConflict);
|
|
88
|
-
else if (onConflict && merge) {
|
|
89
|
-
sql += this._merge(merge.updates, onConflict, insertValues);
|
|
90
|
-
const wheres = this.where();
|
|
91
|
-
if (wheres) sql += ` ${wheres}`;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return sql;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const blocks = [];
|
|
98
|
-
let i = -1;
|
|
99
|
-
while (++i < insertData.values.length) {
|
|
100
|
-
let i2 = -1;
|
|
101
|
-
const block = (blocks[i] = []);
|
|
102
|
-
let current = insertData.values[i];
|
|
103
|
-
current = current === undefined ? this.client.valueForUndefined : current;
|
|
104
|
-
while (++i2 < insertData.columns.length) {
|
|
105
|
-
block.push(
|
|
106
|
-
this.formatter.alias(
|
|
107
|
-
this.formatter.parameter(current[i2]),
|
|
108
|
-
this.formatter.wrap(insertData.columns[i2])
|
|
109
|
-
)
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
blocks[i] = block.join(', ');
|
|
113
|
-
}
|
|
114
|
-
sql += ' select ' + blocks.join(' union all select ');
|
|
115
|
-
|
|
116
|
-
const { onConflict, ignore, merge } = this.single;
|
|
117
|
-
if (onConflict && ignore) sql += ' where true' + this._ignore(onConflict);
|
|
118
|
-
else if (onConflict && merge) {
|
|
119
|
-
sql +=
|
|
120
|
-
' where true' + this._merge(merge.updates, onConflict, insertValues);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return sql;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
_ignore(columns) {
|
|
127
|
-
return ` on conflict (${this.formatter.columnize(columns)}) do nothing`;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
_merge(updates, columns, insert) {
|
|
131
|
-
let sql = ` on conflict (${this.formatter.columnize(
|
|
132
|
-
columns
|
|
133
|
-
)}) do update set `;
|
|
134
|
-
if (updates) {
|
|
135
|
-
const updateData = this._prepUpdate(updates);
|
|
136
|
-
if (typeof updateData === 'string') {
|
|
137
|
-
sql += updateData;
|
|
138
|
-
} else {
|
|
139
|
-
sql += updateData.join(',');
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return sql;
|
|
143
|
-
} else {
|
|
144
|
-
const insertData = this._prepInsert(insert);
|
|
145
|
-
if (typeof insertData === 'string') {
|
|
146
|
-
throw new Error(
|
|
147
|
-
'If using merge with a raw insert query, then updates must be provided'
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
sql += insertData.columns
|
|
152
|
-
.map((column) => this.formatter.wrapString(column.split('.').pop()))
|
|
153
|
-
.map((column) => `${column} = excluded.${column}`)
|
|
154
|
-
.join(', ');
|
|
155
|
-
|
|
156
|
-
return sql;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Compile a truncate table statement into SQL.
|
|
161
|
-
truncate() {
|
|
162
|
-
const { table } = this.single;
|
|
163
|
-
return {
|
|
164
|
-
sql: `delete from ${this.tableName}`,
|
|
165
|
-
output() {
|
|
166
|
-
return this.query({
|
|
167
|
-
sql: `delete from sqlite_sequence where name = '${table}'`,
|
|
168
|
-
}).catch(noop);
|
|
169
|
-
},
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Compiles a `columnInfo` query
|
|
174
|
-
columnInfo() {
|
|
175
|
-
const column = this.single.columnInfo;
|
|
176
|
-
|
|
177
|
-
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
178
|
-
// need to run the identifiers through that function, but not format them as
|
|
179
|
-
// identifiers otherwise.
|
|
180
|
-
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
181
|
-
|
|
182
|
-
return {
|
|
183
|
-
sql: `PRAGMA table_info(\`${table}\`)`,
|
|
184
|
-
output(resp) {
|
|
185
|
-
const maxLengthRegex = /.*\((\d+)\)/;
|
|
186
|
-
const out = reduce(
|
|
187
|
-
resp,
|
|
188
|
-
function(columns, val) {
|
|
189
|
-
let { type } = val;
|
|
190
|
-
let maxLength = type.match(maxLengthRegex);
|
|
191
|
-
if (maxLength) {
|
|
192
|
-
maxLength = maxLength[1];
|
|
193
|
-
}
|
|
194
|
-
type = maxLength ? type.split('(')[0] : type;
|
|
195
|
-
columns[val.name] = {
|
|
196
|
-
type: type.toLowerCase(),
|
|
197
|
-
maxLength,
|
|
198
|
-
nullable: !val.notnull,
|
|
199
|
-
defaultValue: val.dflt_value,
|
|
200
|
-
};
|
|
201
|
-
return columns;
|
|
202
|
-
},
|
|
203
|
-
{}
|
|
204
|
-
);
|
|
205
|
-
return (column && out[column]) || out;
|
|
206
|
-
},
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
limit() {
|
|
211
|
-
const noLimit = !this.single.limit && this.single.limit !== 0;
|
|
212
|
-
if (noLimit && !this.single.offset) return '';
|
|
213
|
-
|
|
214
|
-
// Workaround for offset only,
|
|
215
|
-
// see http://stackoverflow.com/questions/10491492/sqllite-with-skip-offset-only-not-limit
|
|
216
|
-
return `limit ${this.formatter.parameter(
|
|
217
|
-
noLimit ? -1 : this.single.limit
|
|
218
|
-
)}`;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
module.exports = QueryCompiler_SQLite3;
|
|
1
|
+
// SQLite3 Query Builder & Compiler
|
|
2
|
+
|
|
3
|
+
const QueryCompiler = require('../../../query/compiler');
|
|
4
|
+
|
|
5
|
+
const noop = require('../../../util/noop');
|
|
6
|
+
const constant = require('lodash/constant');
|
|
7
|
+
const each = require('lodash/each');
|
|
8
|
+
const identity = require('lodash/identity');
|
|
9
|
+
const isEmpty = require('lodash/isEmpty');
|
|
10
|
+
const reduce = require('lodash/reduce');
|
|
11
|
+
const { isString } = require('../../../util/is');
|
|
12
|
+
|
|
13
|
+
const emptyStr = constant('');
|
|
14
|
+
|
|
15
|
+
class QueryCompiler_SQLite3 extends QueryCompiler {
|
|
16
|
+
constructor(client, builder) {
|
|
17
|
+
super(client, builder);
|
|
18
|
+
|
|
19
|
+
const { returning } = this.single;
|
|
20
|
+
|
|
21
|
+
if (returning) {
|
|
22
|
+
this.client.logger.warn(
|
|
23
|
+
'.returning() is not supported by sqlite3 and will not have any effect.'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// The locks are not applicable in SQLite3
|
|
28
|
+
this.forShare = emptyStr;
|
|
29
|
+
this.forUpdate = emptyStr;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// SQLite requires us to build the multi-row insert as a listing of select with
|
|
33
|
+
// unions joining them together. So we'll build out this list of columns and
|
|
34
|
+
// then join them all together with select unions to complete the queries.
|
|
35
|
+
insert() {
|
|
36
|
+
const insertValues = this.single.insert || [];
|
|
37
|
+
let sql = this.with() + `insert into ${this.tableName} `;
|
|
38
|
+
|
|
39
|
+
if (Array.isArray(insertValues)) {
|
|
40
|
+
if (insertValues.length === 0) {
|
|
41
|
+
return '';
|
|
42
|
+
} else if (
|
|
43
|
+
insertValues.length === 1 &&
|
|
44
|
+
insertValues[0] &&
|
|
45
|
+
isEmpty(insertValues[0])
|
|
46
|
+
) {
|
|
47
|
+
return sql + this._emptyInsertValue;
|
|
48
|
+
}
|
|
49
|
+
} else if (typeof insertValues === 'object' && isEmpty(insertValues)) {
|
|
50
|
+
return sql + this._emptyInsertValue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const insertData = this._prepInsert(insertValues);
|
|
54
|
+
|
|
55
|
+
if (isString(insertData)) {
|
|
56
|
+
return sql + insertData;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (insertData.columns.length === 0) {
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
sql += `(${this.formatter.columnize(insertData.columns)})`;
|
|
64
|
+
|
|
65
|
+
// backwards compatible error
|
|
66
|
+
if (this.client.valueForUndefined !== null) {
|
|
67
|
+
insertData.values.forEach((bindings) => {
|
|
68
|
+
each(bindings, (binding) => {
|
|
69
|
+
if (binding === undefined)
|
|
70
|
+
throw new TypeError(
|
|
71
|
+
'`sqlite` does not support inserting default values. Specify ' +
|
|
72
|
+
'values explicitly or use the `useNullAsDefault` config flag. ' +
|
|
73
|
+
'(see docs http://knexjs.org/#Builder-insert).'
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (insertData.values.length === 1) {
|
|
80
|
+
const parameters = this.formatter.parameterize(
|
|
81
|
+
insertData.values[0],
|
|
82
|
+
this.client.valueForUndefined
|
|
83
|
+
);
|
|
84
|
+
sql += ` values (${parameters})`;
|
|
85
|
+
|
|
86
|
+
const { onConflict, ignore, merge } = this.single;
|
|
87
|
+
if (onConflict && ignore) sql += this._ignore(onConflict);
|
|
88
|
+
else if (onConflict && merge) {
|
|
89
|
+
sql += this._merge(merge.updates, onConflict, insertValues);
|
|
90
|
+
const wheres = this.where();
|
|
91
|
+
if (wheres) sql += ` ${wheres}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return sql;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const blocks = [];
|
|
98
|
+
let i = -1;
|
|
99
|
+
while (++i < insertData.values.length) {
|
|
100
|
+
let i2 = -1;
|
|
101
|
+
const block = (blocks[i] = []);
|
|
102
|
+
let current = insertData.values[i];
|
|
103
|
+
current = current === undefined ? this.client.valueForUndefined : current;
|
|
104
|
+
while (++i2 < insertData.columns.length) {
|
|
105
|
+
block.push(
|
|
106
|
+
this.formatter.alias(
|
|
107
|
+
this.formatter.parameter(current[i2]),
|
|
108
|
+
this.formatter.wrap(insertData.columns[i2])
|
|
109
|
+
)
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
blocks[i] = block.join(', ');
|
|
113
|
+
}
|
|
114
|
+
sql += ' select ' + blocks.join(' union all select ');
|
|
115
|
+
|
|
116
|
+
const { onConflict, ignore, merge } = this.single;
|
|
117
|
+
if (onConflict && ignore) sql += ' where true' + this._ignore(onConflict);
|
|
118
|
+
else if (onConflict && merge) {
|
|
119
|
+
sql +=
|
|
120
|
+
' where true' + this._merge(merge.updates, onConflict, insertValues);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return sql;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
_ignore(columns) {
|
|
127
|
+
return ` on conflict (${this.formatter.columnize(columns)}) do nothing`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
_merge(updates, columns, insert) {
|
|
131
|
+
let sql = ` on conflict (${this.formatter.columnize(
|
|
132
|
+
columns
|
|
133
|
+
)}) do update set `;
|
|
134
|
+
if (updates) {
|
|
135
|
+
const updateData = this._prepUpdate(updates);
|
|
136
|
+
if (typeof updateData === 'string') {
|
|
137
|
+
sql += updateData;
|
|
138
|
+
} else {
|
|
139
|
+
sql += updateData.join(',');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return sql;
|
|
143
|
+
} else {
|
|
144
|
+
const insertData = this._prepInsert(insert);
|
|
145
|
+
if (typeof insertData === 'string') {
|
|
146
|
+
throw new Error(
|
|
147
|
+
'If using merge with a raw insert query, then updates must be provided'
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
sql += insertData.columns
|
|
152
|
+
.map((column) => this.formatter.wrapString(column.split('.').pop()))
|
|
153
|
+
.map((column) => `${column} = excluded.${column}`)
|
|
154
|
+
.join(', ');
|
|
155
|
+
|
|
156
|
+
return sql;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Compile a truncate table statement into SQL.
|
|
161
|
+
truncate() {
|
|
162
|
+
const { table } = this.single;
|
|
163
|
+
return {
|
|
164
|
+
sql: `delete from ${this.tableName}`,
|
|
165
|
+
output() {
|
|
166
|
+
return this.query({
|
|
167
|
+
sql: `delete from sqlite_sequence where name = '${table}'`,
|
|
168
|
+
}).catch(noop);
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Compiles a `columnInfo` query
|
|
174
|
+
columnInfo() {
|
|
175
|
+
const column = this.single.columnInfo;
|
|
176
|
+
|
|
177
|
+
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
178
|
+
// need to run the identifiers through that function, but not format them as
|
|
179
|
+
// identifiers otherwise.
|
|
180
|
+
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
sql: `PRAGMA table_info(\`${table}\`)`,
|
|
184
|
+
output(resp) {
|
|
185
|
+
const maxLengthRegex = /.*\((\d+)\)/;
|
|
186
|
+
const out = reduce(
|
|
187
|
+
resp,
|
|
188
|
+
function(columns, val) {
|
|
189
|
+
let { type } = val;
|
|
190
|
+
let maxLength = type.match(maxLengthRegex);
|
|
191
|
+
if (maxLength) {
|
|
192
|
+
maxLength = maxLength[1];
|
|
193
|
+
}
|
|
194
|
+
type = maxLength ? type.split('(')[0] : type;
|
|
195
|
+
columns[val.name] = {
|
|
196
|
+
type: type.toLowerCase(),
|
|
197
|
+
maxLength,
|
|
198
|
+
nullable: !val.notnull,
|
|
199
|
+
defaultValue: val.dflt_value,
|
|
200
|
+
};
|
|
201
|
+
return columns;
|
|
202
|
+
},
|
|
203
|
+
{}
|
|
204
|
+
);
|
|
205
|
+
return (column && out[column]) || out;
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
limit() {
|
|
211
|
+
const noLimit = !this.single.limit && this.single.limit !== 0;
|
|
212
|
+
if (noLimit && !this.single.offset) return '';
|
|
213
|
+
|
|
214
|
+
// Workaround for offset only,
|
|
215
|
+
// see http://stackoverflow.com/questions/10491492/sqllite-with-skip-offset-only-not-limit
|
|
216
|
+
return `limit ${this.formatter.parameter(
|
|
217
|
+
noLimit ? -1 : this.single.limit
|
|
218
|
+
)}`;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
module.exports = QueryCompiler_SQLite3;
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
const { inherits } = require('util');
|
|
2
|
-
const ColumnCompiler = require('../../../schema/columncompiler');
|
|
3
|
-
|
|
4
|
-
// Column Compiler
|
|
5
|
-
// -------
|
|
6
|
-
|
|
7
|
-
function ColumnCompiler_SQLite3() {
|
|
8
|
-
ColumnCompiler.apply(this, arguments);
|
|
9
|
-
this.modifiers = ['nullable', 'defaultTo'];
|
|
10
|
-
}
|
|
11
|
-
inherits(ColumnCompiler_SQLite3, ColumnCompiler);
|
|
12
|
-
|
|
13
|
-
// Types
|
|
14
|
-
// -------
|
|
15
|
-
|
|
16
|
-
ColumnCompiler_SQLite3.prototype.double = ColumnCompiler_SQLite3.prototype.decimal = ColumnCompiler_SQLite3.prototype.floating =
|
|
17
|
-
'float';
|
|
18
|
-
ColumnCompiler_SQLite3.prototype.timestamp = 'datetime';
|
|
19
|
-
ColumnCompiler_SQLite3.prototype.enu = function (allowed) {
|
|
20
|
-
return `text check (${this.formatter.wrap(this.args[0])} in ('${allowed.join(
|
|
21
|
-
"', '"
|
|
22
|
-
)}'))`;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
ColumnCompiler_SQLite3.prototype.json = 'json';
|
|
26
|
-
|
|
27
|
-
module.exports = ColumnCompiler_SQLite3;
|
|
1
|
+
const { inherits } = require('util');
|
|
2
|
+
const ColumnCompiler = require('../../../schema/columncompiler');
|
|
3
|
+
|
|
4
|
+
// Column Compiler
|
|
5
|
+
// -------
|
|
6
|
+
|
|
7
|
+
function ColumnCompiler_SQLite3() {
|
|
8
|
+
ColumnCompiler.apply(this, arguments);
|
|
9
|
+
this.modifiers = ['nullable', 'defaultTo'];
|
|
10
|
+
}
|
|
11
|
+
inherits(ColumnCompiler_SQLite3, ColumnCompiler);
|
|
12
|
+
|
|
13
|
+
// Types
|
|
14
|
+
// -------
|
|
15
|
+
|
|
16
|
+
ColumnCompiler_SQLite3.prototype.double = ColumnCompiler_SQLite3.prototype.decimal = ColumnCompiler_SQLite3.prototype.floating =
|
|
17
|
+
'float';
|
|
18
|
+
ColumnCompiler_SQLite3.prototype.timestamp = 'datetime';
|
|
19
|
+
ColumnCompiler_SQLite3.prototype.enu = function (allowed) {
|
|
20
|
+
return `text check (${this.formatter.wrap(this.args[0])} in ('${allowed.join(
|
|
21
|
+
"', '"
|
|
22
|
+
)}'))`;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
ColumnCompiler_SQLite3.prototype.json = 'json';
|
|
26
|
+
|
|
27
|
+
module.exports = ColumnCompiler_SQLite3;
|
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
// SQLite3: Column Builder & Compiler
|
|
2
|
-
// -------
|
|
3
|
-
const { inherits } = require('util');
|
|
4
|
-
const SchemaCompiler = require('../../../schema/compiler');
|
|
5
|
-
|
|
6
|
-
const some = require('lodash/some');
|
|
7
|
-
|
|
8
|
-
// Schema Compiler
|
|
9
|
-
// -------
|
|
10
|
-
|
|
11
|
-
function SchemaCompiler_SQLite3() {
|
|
12
|
-
SchemaCompiler.apply(this, arguments);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
inherits(SchemaCompiler_SQLite3, SchemaCompiler);
|
|
16
|
-
|
|
17
|
-
// Compile the query to determine if a table exists.
|
|
18
|
-
SchemaCompiler_SQLite3.prototype.hasTable = function (tableName) {
|
|
19
|
-
const sql =
|
|
20
|
-
`select * from sqlite_master ` +
|
|
21
|
-
`where type = 'table' and name = ${this.formatter.parameter(tableName)}`;
|
|
22
|
-
this.pushQuery({ sql, output: (resp) => resp.length > 0 });
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
// Compile the query to determine if a column exists.
|
|
26
|
-
SchemaCompiler_SQLite3.prototype.hasColumn = function (tableName, column) {
|
|
27
|
-
this.pushQuery({
|
|
28
|
-
sql: `PRAGMA table_info(${this.formatter.wrap(tableName)})`,
|
|
29
|
-
output(resp) {
|
|
30
|
-
return some(resp, (col) => {
|
|
31
|
-
return (
|
|
32
|
-
this.client.wrapIdentifier(col.name.toLowerCase()) ===
|
|
33
|
-
this.client.wrapIdentifier(column.toLowerCase())
|
|
34
|
-
);
|
|
35
|
-
});
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// Compile a rename table command.
|
|
41
|
-
SchemaCompiler_SQLite3.prototype.renameTable = function (from, to) {
|
|
42
|
-
this.pushQuery(
|
|
43
|
-
`alter table ${this.formatter.wrap(from)} rename to ${this.formatter.wrap(
|
|
44
|
-
to
|
|
45
|
-
)}`
|
|
46
|
-
);
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
module.exports = SchemaCompiler_SQLite3;
|
|
1
|
+
// SQLite3: Column Builder & Compiler
|
|
2
|
+
// -------
|
|
3
|
+
const { inherits } = require('util');
|
|
4
|
+
const SchemaCompiler = require('../../../schema/compiler');
|
|
5
|
+
|
|
6
|
+
const some = require('lodash/some');
|
|
7
|
+
|
|
8
|
+
// Schema Compiler
|
|
9
|
+
// -------
|
|
10
|
+
|
|
11
|
+
function SchemaCompiler_SQLite3() {
|
|
12
|
+
SchemaCompiler.apply(this, arguments);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
inherits(SchemaCompiler_SQLite3, SchemaCompiler);
|
|
16
|
+
|
|
17
|
+
// Compile the query to determine if a table exists.
|
|
18
|
+
SchemaCompiler_SQLite3.prototype.hasTable = function (tableName) {
|
|
19
|
+
const sql =
|
|
20
|
+
`select * from sqlite_master ` +
|
|
21
|
+
`where type = 'table' and name = ${this.formatter.parameter(tableName)}`;
|
|
22
|
+
this.pushQuery({ sql, output: (resp) => resp.length > 0 });
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Compile the query to determine if a column exists.
|
|
26
|
+
SchemaCompiler_SQLite3.prototype.hasColumn = function (tableName, column) {
|
|
27
|
+
this.pushQuery({
|
|
28
|
+
sql: `PRAGMA table_info(${this.formatter.wrap(tableName)})`,
|
|
29
|
+
output(resp) {
|
|
30
|
+
return some(resp, (col) => {
|
|
31
|
+
return (
|
|
32
|
+
this.client.wrapIdentifier(col.name.toLowerCase()) ===
|
|
33
|
+
this.client.wrapIdentifier(column.toLowerCase())
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Compile a rename table command.
|
|
41
|
+
SchemaCompiler_SQLite3.prototype.renameTable = function (from, to) {
|
|
42
|
+
this.pushQuery(
|
|
43
|
+
`alter table ${this.formatter.wrap(from)} rename to ${this.formatter.wrap(
|
|
44
|
+
to
|
|
45
|
+
)}`
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
module.exports = SchemaCompiler_SQLite3;
|