2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// MySQL Query Compiler
|
|
|
|
// ------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import QueryCompiler from '../../../query/compiler';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
import {assign} from 'lodash'
|
|
|
|
|
|
|
|
function QueryCompiler_MySQL(client, builder) {
|
|
|
|
QueryCompiler.call(this, client, builder)
|
|
|
|
}
|
|
|
|
inherits(QueryCompiler_MySQL, QueryCompiler)
|
|
|
|
|
|
|
|
assign(QueryCompiler_MySQL.prototype, {
|
|
|
|
|
|
|
|
_emptyInsertValue: '() values ()',
|
|
|
|
|
|
|
|
// 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();
|
2016-05-17 01:01:34 +10:00
|
|
|
return `update ${this.tableName}` +
|
|
|
|
(join ? ` ${join}` : '') +
|
2016-03-02 17:07:05 +01:00
|
|
|
' set ' + updates.join(', ') +
|
2016-05-17 01:01:34 +10:00
|
|
|
(where ? ` ${where}` : '') +
|
|
|
|
(order ? ` ${order}` : '') +
|
|
|
|
(limit ? ` ${limit}` : '');
|
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';
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
forShare() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return 'lock in share mode';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compiles a `columnInfo` query.
|
2016-05-17 01:01:34 +10:00
|
|
|
columnInfo() {
|
|
|
|
const column = this.single.columnInfo;
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
|
|
|
sql: 'select * from information_schema.columns where table_name = ? and table_schema = ?',
|
|
|
|
bindings: [this.single.table, this.client.database()],
|
2016-05-17 01:01:34 +10:00
|
|
|
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')
|
|
|
|
};
|
|
|
|
return columns
|
|
|
|
}, {})
|
|
|
|
return column && out[column] || out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
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.
|
2016-05-17 01:01:34 +10:00
|
|
|
export default QueryCompiler_MySQL;
|