knex/lib/dialects/mysql/query/mysql-querycompiler.js

162 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
// MySQL Query Compiler
// ------
const identity = require('lodash/identity');
2021-01-09 17:59:53 +02:00
const QueryCompiler = require('../../../query/querycompiler');
const { wrapAsIdentifier } = require('../../../formatter/formatterUtils');
2016-03-02 17:07:05 +01:00
class QueryCompiler_MySQL extends QueryCompiler {
2021-01-07 23:34:46 +02:00
constructor(client, builder, formatter) {
super(client, builder, formatter);
const { returning } = this.single;
if (returning) {
this.client.logger.warn(
'.returning() is not supported by mysql and will not have any effect.'
);
}
this._emptyInsertValue = '() values ()';
}
2016-03-02 17:07:05 +01: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');
if (merge) {
sql += this._merge(merge.updates, insert);
const wheres = this.where();
if (wheres) {
throw new Error(
'.onConflict().merge().where() is not supported for mysql'
);
}
}
return sql;
}
// Compiles merge for onConflict, allowing for different merge strategies
_merge(updates, insert) {
const sql = ' on duplicate key update ';
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') {
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'
);
}
return (
sql +
insertData.columns
.map((column) => wrapAsIdentifier(column, this.builder, this.client))
.map((column) => `${column} = values(${column})`)
.join(', ')
);
}
}
2016-03-02 17:07:05 +01:00
// Update method, including joins, wheres, order & limits.
update() {
const join = this.join();
const updates = this._prepUpdate(this.single.update);
const where = this.where();
const order = this.order();
const limit = this.limit();
return (
`update ${this.tableName}` +
(join ? ` ${join}` : '') +
' set ' +
updates.join(', ') +
(where ? ` ${where}` : '') +
(order ? ` ${order}` : '') +
(limit ? ` ${limit}` : '')
);
}
2016-03-02 17:07:05 +01:00
forUpdate() {
2016-03-02 17:07:05 +01:00
return 'for update';
}
2016-03-02 17:07:05 +01:00
forShare() {
2016-03-02 17:07:05 +01:00
return 'lock in share mode';
}
2016-03-02 17:07:05 +01:00
// Only supported on MySQL 8.0+
skipLocked() {
return 'skip locked';
}
// Supported on MySQL 8.0+ and MariaDB 10.3.0+
noWait() {
return 'nowait';
}
2016-03-02 17:07:05 +01:00
// Compiles a `columnInfo` query.
columnInfo() {
const column = this.single.columnInfo;
// 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 {
sql:
'select * from information_schema.columns where table_name = ? and table_schema = ?',
bindings: [table, this.client.database()],
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,
nullable: val.IS_NULLABLE === 'YES',
2016-03-02 17:07:05 +01:00
};
return columns;
}, {});
return (column && out[column]) || out;
},
2016-03-02 17:07:05 +01:00
};
}
2016-03-02 17:07:05 +01: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 '';
// Workaround for offset only.
// see: http://stackoverflow.com/questions/255517/mysql-offset-infinite-rows
const limit =
this.single.offset && noLimit
? '18446744073709551615'
: this.client.parameter(
this.single.limit,
this.builder,
this.bindingsHolder
);
return `limit ${limit}`;
}
}
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.
module.exports = QueryCompiler_MySQL;