knex/src/dialects/mysql/query/compiler.js

96 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
// MySQL Query Compiler
// ------
const inherits = require('inherits');
const QueryCompiler = require('../../../query/compiler');
2016-03-02 17:07:05 +01:00
const { assign, identity } = require('lodash');
2016-03-02 17:07:05 +01:00
function QueryCompiler_MySQL(client, builder) {
QueryCompiler.call(this, client, builder);
const { returning } = this.single;
if (returning) {
this.client.logger.warn(
'.returning() is not supported by mysql and will not have any effect.'
);
}
2016-03-02 17:07:05 +01:00
}
inherits(QueryCompiler_MySQL, QueryCompiler);
2016-03-02 17:07:05 +01:00
assign(QueryCompiler_MySQL.prototype, {
_emptyInsertValue: '() values ()',
// 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';
},
forShare() {
2016-03-02 17:07:05 +01:00
return 'lock in share mode';
},
// 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) {
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
};
},
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.formatter.parameter(this.single.limit);
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;