2016-03-02 17:07:05 +01:00
|
|
|
// MySQL Query Compiler
|
|
|
|
// ------
|
2020-04-18 20:41:23 +03:00
|
|
|
const identity = require('lodash/identity');
|
2021-01-09 17:59:53 +02:00
|
|
|
const QueryCompiler = require('../../../query/querycompiler');
|
2021-01-07 17:31:56 +02:00
|
|
|
const { wrapAsIdentifier } = require('../../../formatter/formatterUtils');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2020-02-01 21:52:00 +05:30
|
|
|
class QueryCompiler_MySQL extends QueryCompiler {
|
2021-01-07 23:34:46 +02:00
|
|
|
constructor(client, builder, formatter) {
|
|
|
|
super(client, builder, formatter);
|
2019-03-03 21:53:06 +09:00
|
|
|
|
2020-02-01 21:52:00 +05:30
|
|
|
const { returning } = this.single;
|
|
|
|
if (returning) {
|
|
|
|
this.client.logger.warn(
|
|
|
|
'.returning() is not supported by mysql and will not have any effect.'
|
|
|
|
);
|
|
|
|
}
|
2020-10-31 15:22:52 +00:00
|
|
|
|
2020-02-01 21:52:00 +05:30
|
|
|
this._emptyInsertValue = '() values ()';
|
2019-03-03 21:53:06 +09:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2020-10-31 15:22:52 +00:00
|
|
|
// Compiles an `insert` query, allowing for multiple
|
|
|
|
// inserts using a single query statement.
|
|
|
|
insert() {
|
|
|
|
let sql = super.insert();
|
|
|
|
if (sql === '') return sql;
|
|
|
|
|
|
|
|
const { ignore, merge, insert } = this.single;
|
|
|
|
if (ignore) sql = sql.replace('insert into', 'insert ignore into');
|
2020-12-09 10:51:29 -05:00
|
|
|
if (merge) {
|
|
|
|
sql += this._merge(merge.updates, insert);
|
|
|
|
const wheres = this.where();
|
|
|
|
if (wheres) {
|
2020-12-31 14:38:50 +02:00
|
|
|
throw new Error(
|
|
|
|
'.onConflict().merge().where() is not supported for mysql'
|
|
|
|
);
|
2020-12-09 10:51:29 -05:00
|
|
|
}
|
|
|
|
}
|
2020-10-31 15:22:52 +00:00
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2021-02-09 21:04:43 +13:00
|
|
|
// Compiles merge for onConflict, allowing for different merge strategies
|
2020-10-31 15:22:52 +00:00
|
|
|
_merge(updates, insert) {
|
2021-01-07 02:04:10 +02:00
|
|
|
const sql = ' on duplicate key update ';
|
2021-02-09 21:04:43 +13:00
|
|
|
if (updates && Array.isArray(updates)) {
|
|
|
|
// update subset of columns
|
|
|
|
return (
|
|
|
|
sql +
|
|
|
|
updates
|
|
|
|
.map((column) =>
|
|
|
|
wrapAsIdentifier(column, this.formatter.builder, this.client)
|
|
|
|
)
|
|
|
|
.map((column) => `${column} = values(${column})`)
|
|
|
|
.join(', ')
|
|
|
|
);
|
|
|
|
} else if (updates && typeof updates === 'object') {
|
2020-10-31 15:22:52 +00:00
|
|
|
const updateData = this._prepUpdate(updates);
|
|
|
|
return sql + updateData.join(',');
|
|
|
|
} else {
|
|
|
|
const insertData = this._prepInsert(insert);
|
|
|
|
if (typeof insertData === 'string') {
|
|
|
|
throw new Error(
|
|
|
|
'If using merge with a raw insert query, then updates must be provided'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-07 02:04:10 +02:00
|
|
|
return (
|
|
|
|
sql +
|
|
|
|
insertData.columns
|
2021-02-03 21:17:20 +02:00
|
|
|
.map((column) => wrapAsIdentifier(column, this.builder, this.client))
|
2021-01-07 02:04:10 +02:00
|
|
|
.map((column) => `${column} = values(${column})`)
|
|
|
|
.join(', ')
|
|
|
|
);
|
2020-10-31 15:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// Update method, including joins, wheres, order & limits.
|
2016-05-17 01:01:34 +10:00
|
|
|
update() {
|
2016-05-18 19:59:24 +10:00
|
|
|
const join = this.join();
|
2016-05-17 01:01:34 +10:00
|
|
|
const updates = this._prepUpdate(this.single.update);
|
2016-05-18 19:59:24 +10:00
|
|
|
const where = this.where();
|
|
|
|
const order = this.order();
|
|
|
|
const limit = this.limit();
|
2018-07-09 08:10:34 -04:00
|
|
|
return (
|
|
|
|
`update ${this.tableName}` +
|
2016-05-17 01:01:34 +10:00
|
|
|
(join ? ` ${join}` : '') +
|
2018-07-09 08:10:34 -04:00
|
|
|
' set ' +
|
|
|
|
updates.join(', ') +
|
2016-05-17 01:01:34 +10:00
|
|
|
(where ? ` ${where}` : '') +
|
|
|
|
(order ? ` ${order}` : '') +
|
2018-07-09 08:10:34 -04:00
|
|
|
(limit ? ` ${limit}` : '')
|
|
|
|
);
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
forUpdate() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return 'for update';
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
forShare() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return 'lock in share mode';
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-07-06 09:05:53 -03:00
|
|
|
// Only supported on MySQL 8.0+
|
|
|
|
skipLocked() {
|
|
|
|
return 'skip locked';
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2019-07-06 09:05:53 -03:00
|
|
|
|
|
|
|
// Supported on MySQL 8.0+ and MariaDB 10.3.0+
|
|
|
|
noWait() {
|
|
|
|
return 'nowait';
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2019-07-06 09:05:53 -03:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// Compiles a `columnInfo` query.
|
2016-05-17 01:01:34 +10:00
|
|
|
columnInfo() {
|
|
|
|
const column = this.single.columnInfo;
|
2018-01-03 00:05:48 +02:00
|
|
|
|
|
|
|
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
|
|
// need to run the identifiers through that function, but not format them as
|
|
|
|
// identifiers otherwise.
|
|
|
|
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
2018-07-09 08:10:34 -04:00
|
|
|
sql:
|
|
|
|
'select * from information_schema.columns where table_name = ? and table_schema = ?',
|
2018-01-03 00:05:48 +02:00
|
|
|
bindings: [table, this.client.database()],
|
2016-05-17 01:01:34 +10:00
|
|
|
output(resp) {
|
2020-04-19 00:40:23 +02:00
|
|
|
const out = resp.reduce(function (columns, val) {
|
2016-03-02 17:07:05 +01:00
|
|
|
columns[val.COLUMN_NAME] = {
|
|
|
|
defaultValue: val.COLUMN_DEFAULT,
|
|
|
|
type: val.DATA_TYPE,
|
|
|
|
maxLength: val.CHARACTER_MAXIMUM_LENGTH,
|
2018-07-09 08:10:34 -04:00
|
|
|
nullable: val.IS_NULLABLE === 'YES',
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
2018-07-09 08:10:34 -04:00
|
|
|
return columns;
|
|
|
|
}, {});
|
|
|
|
return (column && out[column]) || out;
|
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
limit() {
|
|
|
|
const noLimit = !this.single.limit && this.single.limit !== 0;
|
2016-03-02 17:07:05 +01:00
|
|
|
if (noLimit && !this.single.offset) return '';
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
// Workaround for offset only.
|
|
|
|
// see: http://stackoverflow.com/questions/255517/mysql-offset-infinite-rows
|
2018-07-09 08:10:34 -04:00
|
|
|
const limit =
|
|
|
|
this.single.offset && noLimit
|
|
|
|
? '18446744073709551615'
|
2021-02-04 15:54:26 +02:00
|
|
|
: this.client.parameter(
|
|
|
|
this.single.limit,
|
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
|
|
|
);
|
2016-05-17 01:01:34 +10:00
|
|
|
return `limit ${limit}`;
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Set the QueryBuilder & QueryCompiler on the client object,
|
|
|
|
// in case anyone wants to modify things to suit their own purposes.
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = QueryCompiler_MySQL;
|