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.
Files changed (141) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/CONTRIBUTING.md +184 -184
  3. package/LICENSE +22 -22
  4. package/README.md +95 -95
  5. package/bin/cli.js +414 -414
  6. package/bin/utils/cli-config-utils.js +151 -151
  7. package/bin/utils/constants.js +7 -7
  8. package/bin/utils/migrationsLister.js +37 -37
  9. package/knex.js +8 -8
  10. package/lib/client.js +413 -413
  11. package/lib/config-resolver.js +61 -61
  12. package/lib/constants.js +44 -44
  13. package/lib/dialects/mssql/index.js +390 -390
  14. package/lib/dialects/mssql/query/compiler.js +444 -444
  15. package/lib/dialects/mssql/schema/columncompiler.js +103 -103
  16. package/lib/dialects/mssql/schema/compiler.js +59 -59
  17. package/lib/dialects/mssql/schema/tablecompiler.js +245 -245
  18. package/lib/dialects/mssql/transaction.js +97 -97
  19. package/lib/dialects/mysql/index.js +191 -191
  20. package/lib/dialects/mysql/query/compiler.js +142 -142
  21. package/lib/dialects/mysql/schema/columncompiler.js +171 -171
  22. package/lib/dialects/mysql/schema/compiler.js +60 -60
  23. package/lib/dialects/mysql/schema/tablecompiler.js +262 -262
  24. package/lib/dialects/mysql/transaction.js +48 -48
  25. package/lib/dialects/mysql2/index.js +35 -35
  26. package/lib/dialects/mysql2/transaction.js +46 -46
  27. package/lib/dialects/oracle/DEAD_CODE.md +5 -5
  28. package/lib/dialects/oracle/formatter.js +20 -20
  29. package/lib/dialects/oracle/index.js +79 -79
  30. package/lib/dialects/oracle/query/compiler.js +327 -327
  31. package/lib/dialects/oracle/schema/columnbuilder.js +18 -18
  32. package/lib/dialects/oracle/schema/columncompiler.js +139 -139
  33. package/lib/dialects/oracle/schema/compiler.js +81 -81
  34. package/lib/dialects/oracle/schema/tablecompiler.js +165 -165
  35. package/lib/dialects/oracle/schema/trigger.js +126 -126
  36. package/lib/dialects/oracle/utils.js +86 -86
  37. package/lib/dialects/oracledb/index.js +489 -489
  38. package/lib/dialects/oracledb/query/compiler.js +363 -363
  39. package/lib/dialects/oracledb/schema/columncompiler.js +35 -35
  40. package/lib/dialects/oracledb/transaction.js +76 -76
  41. package/lib/dialects/oracledb/utils.js +14 -14
  42. package/lib/dialects/postgres/index.js +319 -319
  43. package/lib/dialects/postgres/query/compiler.js +206 -206
  44. package/lib/dialects/postgres/schema/columncompiler.js +125 -125
  45. package/lib/dialects/postgres/schema/compiler.js +109 -109
  46. package/lib/dialects/postgres/schema/tablecompiler.js +183 -183
  47. package/lib/dialects/redshift/index.js +73 -73
  48. package/lib/dialects/redshift/query/compiler.js +119 -119
  49. package/lib/dialects/redshift/schema/columnbuilder.js +20 -20
  50. package/lib/dialects/redshift/schema/columncompiler.js +60 -60
  51. package/lib/dialects/redshift/schema/compiler.js +14 -14
  52. package/lib/dialects/redshift/schema/tablecompiler.js +123 -123
  53. package/lib/dialects/redshift/transaction.js +18 -18
  54. package/lib/dialects/sqlite3/formatter.js +21 -21
  55. package/lib/dialects/sqlite3/index.js +169 -169
  56. package/lib/dialects/sqlite3/query/compiler.js +222 -222
  57. package/lib/dialects/sqlite3/schema/columncompiler.js +27 -27
  58. package/lib/dialects/sqlite3/schema/compiler.js +49 -49
  59. package/lib/dialects/sqlite3/schema/ddl.js +525 -525
  60. package/lib/dialects/sqlite3/schema/tablecompiler.js +238 -238
  61. package/lib/formatter.js +295 -295
  62. package/lib/functionhelper.js +14 -14
  63. package/lib/helpers.js +92 -92
  64. package/lib/index.js +3 -3
  65. package/lib/interface.js +115 -115
  66. package/lib/knex.js +42 -42
  67. package/lib/logger.js +76 -76
  68. package/lib/migrate/MigrationGenerator.js +82 -82
  69. package/lib/migrate/Migrator.js +611 -611
  70. package/lib/migrate/configuration-merger.js +60 -60
  71. package/lib/migrate/migrate-stub.js +17 -17
  72. package/lib/migrate/migration-list-resolver.js +36 -36
  73. package/lib/migrate/sources/fs-migrations.js +99 -99
  74. package/lib/migrate/stub/cjs.stub +15 -15
  75. package/lib/migrate/stub/coffee.stub +13 -13
  76. package/lib/migrate/stub/eg.stub +14 -14
  77. package/lib/migrate/stub/js.stub +15 -15
  78. package/lib/migrate/stub/knexfile-coffee.stub +34 -34
  79. package/lib/migrate/stub/knexfile-eg.stub +43 -43
  80. package/lib/migrate/stub/knexfile-js.stub +44 -44
  81. package/lib/migrate/stub/knexfile-ls.stub +35 -35
  82. package/lib/migrate/stub/knexfile-ts.stub +44 -44
  83. package/lib/migrate/stub/ls.stub +14 -14
  84. package/lib/migrate/stub/ts.stub +21 -21
  85. package/lib/migrate/table-creator.js +67 -67
  86. package/lib/migrate/table-resolver.js +27 -27
  87. package/lib/query/builder.js +1372 -1372
  88. package/lib/query/compiler.js +889 -889
  89. package/lib/query/constants.js +13 -13
  90. package/lib/query/joinclause.js +263 -263
  91. package/lib/query/methods.js +92 -92
  92. package/lib/query/string.js +190 -190
  93. package/lib/raw.js +188 -188
  94. package/lib/ref.js +39 -39
  95. package/lib/runner.js +285 -285
  96. package/lib/schema/builder.js +82 -82
  97. package/lib/schema/columnbuilder.js +117 -117
  98. package/lib/schema/columncompiler.js +177 -177
  99. package/lib/schema/compiler.js +101 -101
  100. package/lib/schema/helpers.js +51 -51
  101. package/lib/schema/tablebuilder.js +288 -288
  102. package/lib/schema/tablecompiler.js +296 -296
  103. package/lib/seed/Seeder.js +203 -203
  104. package/lib/seed/seed-stub.js +13 -13
  105. package/lib/seed/stub/coffee.stub +9 -9
  106. package/lib/seed/stub/eg.stub +11 -11
  107. package/lib/seed/stub/js.stub +13 -13
  108. package/lib/seed/stub/ls.stub +11 -11
  109. package/lib/seed/stub/ts.stub +13 -13
  110. package/lib/transaction.js +363 -363
  111. package/lib/util/batchInsert.js +59 -59
  112. package/lib/util/delay.js +6 -6
  113. package/lib/util/fake-client.js +9 -9
  114. package/lib/util/finally-mixin.js +13 -13
  115. package/lib/util/fs.js +76 -76
  116. package/lib/util/import-file.js +13 -13
  117. package/lib/util/is-module-type.js +14 -14
  118. package/lib/util/is.js +32 -32
  119. package/lib/util/make-knex.js +338 -338
  120. package/lib/util/nanoid.js +29 -29
  121. package/lib/util/noop.js +1 -1
  122. package/lib/util/parse-connection.js +66 -66
  123. package/lib/util/save-async-stack.js +14 -14
  124. package/lib/util/template.js +52 -52
  125. package/lib/util/timeout.js +29 -29
  126. package/lib/util/timestamp.js +16 -16
  127. package/package.json +1 -1
  128. package/scripts/build.js +125 -125
  129. package/scripts/docker-compose.yml +111 -111
  130. package/scripts/next-release-howto.md +24 -24
  131. package/scripts/release.sh +34 -34
  132. package/scripts/runkit-example.js +34 -34
  133. package/scripts/stress-test/README.txt +18 -18
  134. package/scripts/stress-test/docker-compose.yml +47 -47
  135. package/scripts/stress-test/knex-stress-test.js +196 -196
  136. package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
  137. package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
  138. package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
  139. package/types/index.d.ts +2249 -2249
  140. package/types/result.d.ts +27 -27
  141. package/types/tables.d.ts +4 -4
@@ -1,97 +1,97 @@
1
- const Transaction = require('../../transaction');
2
- const debug = require('debug')('knex:tx');
3
-
4
- module.exports = class Transaction_MSSQL extends Transaction {
5
- begin(conn) {
6
- debug('%s: begin', this.txid);
7
- return conn.tx_.begin().then(this._resolver, this._rejecter);
8
- }
9
-
10
- async savepoint(conn) {
11
- debug('%s: savepoint at', this.txid);
12
- return this.query(conn, `SAVE TRANSACTION ${this.txid}`);
13
- }
14
-
15
- commit(conn, value) {
16
- this._completed = true;
17
- debug('%s: commit', this.txid);
18
- return conn.tx_.commit().then(() => this._resolver(value), this._rejecter);
19
- }
20
-
21
- release(conn, value) {
22
- return this._resolver(value);
23
- }
24
-
25
- rollback(conn, error) {
26
- this._completed = true;
27
- debug('%s: rolling back', this.txid);
28
- return conn.tx_.rollback().then(
29
- () => {
30
- let err = error;
31
- if (error === undefined) {
32
- if (this.doNotRejectOnRollback) {
33
- this._resolver();
34
- return;
35
- }
36
- err = new Error(`Transaction rejected with non-error: ${error}`);
37
- }
38
- this._rejecter(err);
39
- },
40
- (err) => {
41
- if (error) {
42
- try {
43
- err.originalError = error;
44
- } catch (_err) {
45
- // This is to handle https://github.com/knex/knex/issues/4128
46
- }
47
- }
48
- return this._rejecter(err);
49
- }
50
- );
51
- }
52
-
53
- async rollbackTo(conn, error) {
54
- debug('%s: rolling backTo', this.txid);
55
- await this.query(conn, `ROLLBACK TRANSACTION ${this.txid}`, 2, error);
56
-
57
- this._rejecter(error);
58
- }
59
-
60
- // Acquire a connection and create a disposer - either using the one passed
61
- // via config or getting one off the client. The disposer will be called once
62
- // the original promise is marked completed.
63
- async acquireConnection(config, cb) {
64
- const configConnection = config && config.connection;
65
- const conn =
66
- (this.outerTx && this.outerTx.conn) ||
67
- configConnection ||
68
- (await this.client.acquireConnection());
69
-
70
- try {
71
- conn.__knexTxId = this.txid;
72
- if (!this.outerTx) {
73
- this.conn = conn;
74
- conn.tx_ = conn.transaction();
75
- }
76
-
77
- return await cb(conn);
78
- } finally {
79
- if (!this.outerTx) {
80
- if (conn.tx_) {
81
- if (!this._completed) {
82
- debug('%s: unreleased transaction', this.txid);
83
- conn.tx_.rollback();
84
- }
85
- conn.tx_ = null;
86
- }
87
- this.conn = null;
88
- if (!configConnection) {
89
- debug('%s: releasing connection', this.txid);
90
- this.client.releaseConnection(conn);
91
- } else {
92
- debug('%s: not releasing external connection', this.txid);
93
- }
94
- }
95
- }
96
- }
97
- };
1
+ const Transaction = require('../../transaction');
2
+ const debug = require('debug')('knex:tx');
3
+
4
+ module.exports = class Transaction_MSSQL extends Transaction {
5
+ begin(conn) {
6
+ debug('%s: begin', this.txid);
7
+ return conn.tx_.begin().then(this._resolver, this._rejecter);
8
+ }
9
+
10
+ async savepoint(conn) {
11
+ debug('%s: savepoint at', this.txid);
12
+ return this.query(conn, `SAVE TRANSACTION ${this.txid}`);
13
+ }
14
+
15
+ commit(conn, value) {
16
+ this._completed = true;
17
+ debug('%s: commit', this.txid);
18
+ return conn.tx_.commit().then(() => this._resolver(value), this._rejecter);
19
+ }
20
+
21
+ release(conn, value) {
22
+ return this._resolver(value);
23
+ }
24
+
25
+ rollback(conn, error) {
26
+ this._completed = true;
27
+ debug('%s: rolling back', this.txid);
28
+ return conn.tx_.rollback().then(
29
+ () => {
30
+ let err = error;
31
+ if (error === undefined) {
32
+ if (this.doNotRejectOnRollback) {
33
+ this._resolver();
34
+ return;
35
+ }
36
+ err = new Error(`Transaction rejected with non-error: ${error}`);
37
+ }
38
+ this._rejecter(err);
39
+ },
40
+ (err) => {
41
+ if (error) {
42
+ try {
43
+ err.originalError = error;
44
+ } catch (_err) {
45
+ // This is to handle https://github.com/knex/knex/issues/4128
46
+ }
47
+ }
48
+ return this._rejecter(err);
49
+ }
50
+ );
51
+ }
52
+
53
+ async rollbackTo(conn, error) {
54
+ debug('%s: rolling backTo', this.txid);
55
+ await this.query(conn, `ROLLBACK TRANSACTION ${this.txid}`, 2, error);
56
+
57
+ this._rejecter(error);
58
+ }
59
+
60
+ // Acquire a connection and create a disposer - either using the one passed
61
+ // via config or getting one off the client. The disposer will be called once
62
+ // the original promise is marked completed.
63
+ async acquireConnection(config, cb) {
64
+ const configConnection = config && config.connection;
65
+ const conn =
66
+ (this.outerTx && this.outerTx.conn) ||
67
+ configConnection ||
68
+ (await this.client.acquireConnection());
69
+
70
+ try {
71
+ conn.__knexTxId = this.txid;
72
+ if (!this.outerTx) {
73
+ this.conn = conn;
74
+ conn.tx_ = conn.transaction();
75
+ }
76
+
77
+ return await cb(conn);
78
+ } finally {
79
+ if (!this.outerTx) {
80
+ if (conn.tx_) {
81
+ if (!this._completed) {
82
+ debug('%s: unreleased transaction', this.txid);
83
+ conn.tx_.rollback();
84
+ }
85
+ conn.tx_ = null;
86
+ }
87
+ this.conn = null;
88
+ if (!configConnection) {
89
+ debug('%s: releasing connection', this.txid);
90
+ this.client.releaseConnection(conn);
91
+ } else {
92
+ debug('%s: not releasing external connection', this.txid);
93
+ }
94
+ }
95
+ }
96
+ }
97
+ };
@@ -1,191 +1,191 @@
1
- // MySQL Client
2
- // -------
3
- const defer = require('lodash/defer');
4
- const map = require('lodash/map');
5
- const { promisify, inherits } = require('util');
6
- const Client = require('../../client');
7
-
8
- const Transaction = require('./transaction');
9
- const QueryCompiler = require('./query/compiler');
10
- const SchemaCompiler = require('./schema/compiler');
11
- const TableCompiler = require('./schema/tablecompiler');
12
- const ColumnCompiler = require('./schema/columncompiler');
13
-
14
- const { makeEscape } = require('../../query/string');
15
-
16
- // Always initialize with the "QueryBuilder" and "QueryCompiler"
17
- // objects, which extend the base 'lib/query/builder' and
18
- // 'lib/query/compiler', respectively.
19
- function Client_MySQL(config) {
20
- Client.call(this, config);
21
- }
22
-
23
- inherits(Client_MySQL, Client);
24
-
25
- Object.assign(Client_MySQL.prototype, {
26
- dialect: 'mysql',
27
-
28
- driverName: 'mysql',
29
-
30
- _driver() {
31
- return require('mysql');
32
- },
33
-
34
- queryCompiler() {
35
- return new QueryCompiler(this, ...arguments);
36
- },
37
-
38
- schemaCompiler() {
39
- return new SchemaCompiler(this, ...arguments);
40
- },
41
-
42
- tableCompiler() {
43
- return new TableCompiler(this, ...arguments);
44
- },
45
-
46
- columnCompiler() {
47
- return new ColumnCompiler(this, ...arguments);
48
- },
49
-
50
- transaction() {
51
- return new Transaction(this, ...arguments);
52
- },
53
-
54
- _escapeBinding: makeEscape(),
55
-
56
- wrapIdentifierImpl(value) {
57
- return value !== '*' ? `\`${value.replace(/`/g, '``')}\`` : '*';
58
- },
59
-
60
- // Get a raw connection, called by the `pool` whenever a new
61
- // connection needs to be added to the pool.
62
- acquireRawConnection() {
63
- return new Promise((resolver, rejecter) => {
64
- const connection = this.driver.createConnection(this.connectionSettings);
65
- connection.on('error', (err) => {
66
- connection.__knex__disposed = err;
67
- });
68
- connection.connect((err) => {
69
- if (err) {
70
- // if connection is rejected, remove listener that was registered above...
71
- connection.removeAllListeners();
72
- return rejecter(err);
73
- }
74
- resolver(connection);
75
- });
76
- });
77
- },
78
-
79
- // Used to explicitly close a connection, called internally by the pool
80
- // when a connection times out or the pool is shutdown.
81
- async destroyRawConnection(connection) {
82
- try {
83
- const end = promisify((cb) => connection.end(cb));
84
- return await end();
85
- } catch (err) {
86
- connection.__knex__disposed = err;
87
- } finally {
88
- // see discussion https://github.com/knex/knex/pull/3483
89
- defer(() => connection.removeAllListeners());
90
- }
91
- },
92
-
93
- validateConnection(connection) {
94
- if (
95
- connection.state === 'connected' ||
96
- connection.state === 'authenticated'
97
- ) {
98
- return true;
99
- }
100
- return false;
101
- },
102
-
103
- // Grab a connection, run the query via the MySQL streaming interface,
104
- // and pass that through to the stream we've sent back to the client.
105
- _stream(connection, obj, stream, options) {
106
- options = options || {};
107
- const queryOptions = Object.assign({ sql: obj.sql }, obj.options);
108
- return new Promise((resolver, rejecter) => {
109
- stream.on('error', rejecter);
110
- stream.on('end', resolver);
111
- const queryStream = connection
112
- .query(queryOptions, obj.bindings)
113
- .stream(options);
114
-
115
- queryStream.on('error', (err) => {
116
- rejecter(err);
117
- stream.emit('error', err);
118
- });
119
-
120
- queryStream.pipe(stream);
121
- });
122
- },
123
-
124
- // Runs the query on the specified connection, providing the bindings
125
- // and any other necessary prep work.
126
- _query(connection, obj) {
127
- if (!obj || typeof obj === 'string') obj = { sql: obj };
128
- return new Promise(function (resolver, rejecter) {
129
- if (!obj.sql) {
130
- resolver();
131
- return;
132
- }
133
- const queryOptions = Object.assign({ sql: obj.sql }, obj.options);
134
- connection.query(queryOptions, obj.bindings, function (
135
- err,
136
- rows,
137
- fields
138
- ) {
139
- if (err) return rejecter(err);
140
- obj.response = [rows, fields];
141
- resolver(obj);
142
- });
143
- });
144
- },
145
-
146
- // Process the response as returned from the query.
147
- processResponse(obj, runner) {
148
- if (obj == null) return;
149
- const { response } = obj;
150
- const { method } = obj;
151
- const rows = response[0];
152
- const fields = response[1];
153
- if (obj.output) return obj.output.call(runner, rows, fields);
154
- switch (method) {
155
- case 'select':
156
- case 'pluck':
157
- case 'first': {
158
- if (method === 'pluck') {
159
- return map(rows, obj.pluck);
160
- }
161
- return method === 'first' ? rows[0] : rows;
162
- }
163
- case 'insert':
164
- return [rows.insertId];
165
- case 'del':
166
- case 'update':
167
- case 'counter':
168
- return rows.affectedRows;
169
- default:
170
- return response;
171
- }
172
- },
173
-
174
- canCancelQuery: true,
175
-
176
- async cancelQuery(connectionToKill) {
177
- const conn = await this.acquireConnection();
178
- try {
179
- return await this.query(conn, {
180
- method: 'raw',
181
- sql: 'KILL QUERY ?',
182
- bindings: [connectionToKill.threadId],
183
- options: {},
184
- });
185
- } finally {
186
- await this.releaseConnection(conn);
187
- }
188
- },
189
- });
190
-
191
- module.exports = Client_MySQL;
1
+ // MySQL Client
2
+ // -------
3
+ const defer = require('lodash/defer');
4
+ const map = require('lodash/map');
5
+ const { promisify, inherits } = require('util');
6
+ const Client = require('../../client');
7
+
8
+ const Transaction = require('./transaction');
9
+ const QueryCompiler = require('./query/compiler');
10
+ const SchemaCompiler = require('./schema/compiler');
11
+ const TableCompiler = require('./schema/tablecompiler');
12
+ const ColumnCompiler = require('./schema/columncompiler');
13
+
14
+ const { makeEscape } = require('../../query/string');
15
+
16
+ // Always initialize with the "QueryBuilder" and "QueryCompiler"
17
+ // objects, which extend the base 'lib/query/builder' and
18
+ // 'lib/query/compiler', respectively.
19
+ function Client_MySQL(config) {
20
+ Client.call(this, config);
21
+ }
22
+
23
+ inherits(Client_MySQL, Client);
24
+
25
+ Object.assign(Client_MySQL.prototype, {
26
+ dialect: 'mysql',
27
+
28
+ driverName: 'mysql',
29
+
30
+ _driver() {
31
+ return require('mysql');
32
+ },
33
+
34
+ queryCompiler() {
35
+ return new QueryCompiler(this, ...arguments);
36
+ },
37
+
38
+ schemaCompiler() {
39
+ return new SchemaCompiler(this, ...arguments);
40
+ },
41
+
42
+ tableCompiler() {
43
+ return new TableCompiler(this, ...arguments);
44
+ },
45
+
46
+ columnCompiler() {
47
+ return new ColumnCompiler(this, ...arguments);
48
+ },
49
+
50
+ transaction() {
51
+ return new Transaction(this, ...arguments);
52
+ },
53
+
54
+ _escapeBinding: makeEscape(),
55
+
56
+ wrapIdentifierImpl(value) {
57
+ return value !== '*' ? `\`${value.replace(/`/g, '``')}\`` : '*';
58
+ },
59
+
60
+ // Get a raw connection, called by the `pool` whenever a new
61
+ // connection needs to be added to the pool.
62
+ acquireRawConnection() {
63
+ return new Promise((resolver, rejecter) => {
64
+ const connection = this.driver.createConnection(this.connectionSettings);
65
+ connection.on('error', (err) => {
66
+ connection.__knex__disposed = err;
67
+ });
68
+ connection.connect((err) => {
69
+ if (err) {
70
+ // if connection is rejected, remove listener that was registered above...
71
+ connection.removeAllListeners();
72
+ return rejecter(err);
73
+ }
74
+ resolver(connection);
75
+ });
76
+ });
77
+ },
78
+
79
+ // Used to explicitly close a connection, called internally by the pool
80
+ // when a connection times out or the pool is shutdown.
81
+ async destroyRawConnection(connection) {
82
+ try {
83
+ const end = promisify((cb) => connection.end(cb));
84
+ return await end();
85
+ } catch (err) {
86
+ connection.__knex__disposed = err;
87
+ } finally {
88
+ // see discussion https://github.com/knex/knex/pull/3483
89
+ defer(() => connection.removeAllListeners());
90
+ }
91
+ },
92
+
93
+ validateConnection(connection) {
94
+ if (
95
+ connection.state === 'connected' ||
96
+ connection.state === 'authenticated'
97
+ ) {
98
+ return true;
99
+ }
100
+ return false;
101
+ },
102
+
103
+ // Grab a connection, run the query via the MySQL streaming interface,
104
+ // and pass that through to the stream we've sent back to the client.
105
+ _stream(connection, obj, stream, options) {
106
+ options = options || {};
107
+ const queryOptions = Object.assign({ sql: obj.sql }, obj.options);
108
+ return new Promise((resolver, rejecter) => {
109
+ stream.on('error', rejecter);
110
+ stream.on('end', resolver);
111
+ const queryStream = connection
112
+ .query(queryOptions, obj.bindings)
113
+ .stream(options);
114
+
115
+ queryStream.on('error', (err) => {
116
+ rejecter(err);
117
+ stream.emit('error', err);
118
+ });
119
+
120
+ queryStream.pipe(stream);
121
+ });
122
+ },
123
+
124
+ // Runs the query on the specified connection, providing the bindings
125
+ // and any other necessary prep work.
126
+ _query(connection, obj) {
127
+ if (!obj || typeof obj === 'string') obj = { sql: obj };
128
+ return new Promise(function (resolver, rejecter) {
129
+ if (!obj.sql) {
130
+ resolver();
131
+ return;
132
+ }
133
+ const queryOptions = Object.assign({ sql: obj.sql }, obj.options);
134
+ connection.query(queryOptions, obj.bindings, function (
135
+ err,
136
+ rows,
137
+ fields
138
+ ) {
139
+ if (err) return rejecter(err);
140
+ obj.response = [rows, fields];
141
+ resolver(obj);
142
+ });
143
+ });
144
+ },
145
+
146
+ // Process the response as returned from the query.
147
+ processResponse(obj, runner) {
148
+ if (obj == null) return;
149
+ const { response } = obj;
150
+ const { method } = obj;
151
+ const rows = response[0];
152
+ const fields = response[1];
153
+ if (obj.output) return obj.output.call(runner, rows, fields);
154
+ switch (method) {
155
+ case 'select':
156
+ case 'pluck':
157
+ case 'first': {
158
+ if (method === 'pluck') {
159
+ return map(rows, obj.pluck);
160
+ }
161
+ return method === 'first' ? rows[0] : rows;
162
+ }
163
+ case 'insert':
164
+ return [rows.insertId];
165
+ case 'del':
166
+ case 'update':
167
+ case 'counter':
168
+ return rows.affectedRows;
169
+ default:
170
+ return response;
171
+ }
172
+ },
173
+
174
+ canCancelQuery: true,
175
+
176
+ async cancelQuery(connectionToKill) {
177
+ const conn = await this.acquireConnection();
178
+ try {
179
+ return await this.query(conn, {
180
+ method: 'raw',
181
+ sql: 'KILL QUERY ?',
182
+ bindings: [connectionToKill.threadId],
183
+ options: {},
184
+ });
185
+ } finally {
186
+ await this.releaseConnection(conn);
187
+ }
188
+ },
189
+ });
190
+
191
+ module.exports = Client_MySQL;