Skip to content

Commit

Permalink
Update QueryCompiler implementation to use classes (#3647)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorefnon committed Feb 1, 2020
1 parent c14252d commit 589ea74
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 264 deletions.
41 changes: 19 additions & 22 deletions lib/dialects/mssql/query/compiler.js
@@ -1,15 +1,9 @@
// MSSQL Query Compiler
// ------
const inherits = require('inherits');
const QueryCompiler = require('../../../query/compiler');

const { isEmpty, compact, identity } = require('lodash');

function QueryCompiler_MSSQL(client, builder) {
QueryCompiler.call(this, client, builder);
}
inherits(QueryCompiler_MSSQL, QueryCompiler);

const components = [
'columns',
'join',
Expand All @@ -23,14 +17,17 @@ const components = [
'offset',
];

Object.assign(QueryCompiler_MSSQL.prototype, {
_emptyInsertValue: 'default values',
class QueryCompiler_MSSQL extends QueryCompiler {
constructor(client, builder) {
super(client, builder);
this._emptyInsertValue = 'default values';
}

select() {
const sql = this.with();
const statements = components.map((component) => this[component](this));
return sql + compact(statements).join(' ');
},
}

// Compiles an "insert" query, allowing for multiple
// inserts using a single query statement.
Expand Down Expand Up @@ -79,7 +76,7 @@ Object.assign(QueryCompiler_MSSQL.prototype, {
sql,
returning,
};
},
}

// Compiles an `update` query, allowing for a return value.
update() {
Expand All @@ -103,7 +100,7 @@ Object.assign(QueryCompiler_MSSQL.prototype, {
(!returning ? this._returning('rowcount', '@@rowcount') : ''),
returning: returning || '@@rowcount',
};
},
}

// Compiles a `delete` query.
del() {
Expand All @@ -121,7 +118,7 @@ Object.assign(QueryCompiler_MSSQL.prototype, {
(!returning ? this._returning('rowcount', '@@rowcount') : ''),
returning: returning || '@@rowcount',
};
},
}

// Compiles the columns in the query, specifying if an item was distinct.
columns() {
Expand Down Expand Up @@ -156,7 +153,7 @@ Object.assign(QueryCompiler_MSSQL.prototype, {
sql.join(', ') +
(this.tableName ? ` from ${this.tableName}` : '')
);
},
}

_returning(method, value) {
switch (method) {
Expand All @@ -172,23 +169,23 @@ Object.assign(QueryCompiler_MSSQL.prototype, {
case 'rowcount':
return value ? ';select @@rowcount' : '';
}
},
}

// Compiles a `truncate` query.
truncate() {
return `truncate table ${this.tableName}`;
},
}

forUpdate() {
// this doesn't work exacltly as it should, one should also mention index while locking
// https://stackoverflow.com/a/9818448/360060
return 'with (UPDLOCK)';
},
}

forShare() {
// http://www.sqlteam.com/article/introduction-to-locking-in-sql-server
return 'with (HOLDLOCK)';
},
}

// Compiles a `columnInfo` query.
columnInfo() {
Expand Down Expand Up @@ -230,18 +227,18 @@ Object.assign(QueryCompiler_MSSQL.prototype, {
return (column && out[column]) || out;
},
};
},
}

top() {
const noLimit = !this.single.limit && this.single.limit !== 0;
const noOffset = !this.single.offset;
if (noLimit || !noOffset) return '';
return `top (${this.formatter.parameter(this.single.limit)})`;
},
}

limit() {
return '';
},
}

offset() {
const noLimit = !this.single.limit && this.single.limit !== 0;
Expand All @@ -256,8 +253,8 @@ Object.assign(QueryCompiler_MSSQL.prototype, {
)} rows only`;
}
return offset;
},
});
}
}

// Set the QueryBuilder & QueryCompiler on the client object,
// in case anyone wants to modify things to suit their own purposes.
Expand Down
42 changes: 19 additions & 23 deletions lib/dialects/mysql/query/compiler.js
@@ -1,26 +1,22 @@
// MySQL Query Compiler
// ------
const inherits = require('inherits');
const QueryCompiler = require('../../../query/compiler');

const { identity } = require('lodash');

function QueryCompiler_MySQL(client, builder) {
QueryCompiler.call(this, client, builder);
class QueryCompiler_MySQL extends QueryCompiler {
constructor(client, builder) {
super(client, builder);

const { returning } = this.single;
const { returning } = this.single;

if (returning) {
this.client.logger.warn(
'.returning() is not supported by mysql and will not have any effect.'
);
if (returning) {
this.client.logger.warn(
'.returning() is not supported by mysql and will not have any effect.'
);
}
this._emptyInsertValue = '() values ()';
}
}

inherits(QueryCompiler_MySQL, QueryCompiler);

Object.assign(QueryCompiler_MySQL.prototype, {
_emptyInsertValue: '() values ()',

// Update method, including joins, wheres, order & limits.
update() {
Expand All @@ -38,25 +34,25 @@ Object.assign(QueryCompiler_MySQL.prototype, {
(order ? ` ${order}` : '') +
(limit ? ` ${limit}` : '')
);
},
}

forUpdate() {
return 'for update';
},
}

forShare() {
return 'lock in share mode';
},
}

// Only supported on MySQL 8.0+
skipLocked() {
return 'skip locked';
},
}

// Supported on MySQL 8.0+ and MariaDB 10.3.0+
noWait() {
return 'nowait';
},
}

// Compiles a `columnInfo` query.
columnInfo() {
Expand All @@ -72,7 +68,7 @@ Object.assign(QueryCompiler_MySQL.prototype, {
'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) {
const out = resp.reduce(function (columns, val) {
columns[val.COLUMN_NAME] = {
defaultValue: val.COLUMN_DEFAULT,
type: val.DATA_TYPE,
Expand All @@ -84,7 +80,7 @@ Object.assign(QueryCompiler_MySQL.prototype, {
return (column && out[column]) || out;
},
};
},
}

limit() {
const noLimit = !this.single.limit && this.single.limit !== 0;
Expand All @@ -97,8 +93,8 @@ Object.assign(QueryCompiler_MySQL.prototype, {
? '18446744073709551615'
: this.formatter.parameter(this.single.limit);
return `limit ${limit}`;
},
});
}
}

// Set the QueryBuilder & QueryCompiler on the client object,
// in case anyone wants to modify things to suit their own purposes.
Expand Down
42 changes: 19 additions & 23 deletions lib/dialects/oracle/query/compiler.js
Expand Up @@ -3,7 +3,6 @@
// Oracle Query Builder & Compiler
// ------
const {
assign,
isPlainObject,
isEmpty,
isString,
Expand All @@ -12,7 +11,6 @@ const {
compact,
identity,
} = require('lodash');
const inherits = require('inherits');
const QueryCompiler = require('../../../query/compiler');
const { ReturningHelper } = require('../utils');

Expand All @@ -33,13 +31,16 @@ const components = [
// Set the "Formatter" to use for the queries,
// ensuring that all parameterized values (even across sub-queries)
// are properly built into the same query.
function QueryCompiler_Oracle(client, builder) {
QueryCompiler.call(this, client, builder);
}
class QueryCompiler_Oracle extends QueryCompiler {
constructor(client, builder) {
super(client, builder);

inherits(QueryCompiler_Oracle, QueryCompiler);
// Compiles the `select` statement, or nested sub-selects
// by calling each of the component compilers, trimming out
// the empties, and returning a generated query string.
this.first = this.select;
}

assign(QueryCompiler_Oracle.prototype, {
// Compiles an "insert" query, allowing for multiple
// inserts using a single query statement.
insert() {
Expand Down Expand Up @@ -167,7 +168,7 @@ assign(QueryCompiler_Oracle.prototype, {
}

return sql;
},
}

// Update method, including joins, wheres, order & limits.
update() {
Expand All @@ -190,16 +191,16 @@ assign(QueryCompiler_Oracle.prototype, {
}

return this._addReturningToSqlAndConvert(sql, returning, this.tableName);
},
}

// Compiles a `truncate` query.
truncate() {
return `truncate table ${this.tableName}`;
},
}

forUpdate() {
return 'for update';
},
}

forShare() {
// lock for share is not directly supported by oracle
Expand All @@ -208,7 +209,7 @@ assign(QueryCompiler_Oracle.prototype, {
'lock for share is not supported by oracle dialect'
);
return '';
},
}

// Compiles a `columnInfo` query.
columnInfo() {
Expand Down Expand Up @@ -247,7 +248,7 @@ assign(QueryCompiler_Oracle.prototype, {
return (column && out[column]) || out;
},
};
},
}

select() {
let query = this.with();
Expand All @@ -256,11 +257,11 @@ assign(QueryCompiler_Oracle.prototype, {
});
query += compact(statements).join(' ');
return this._surroundQueryWithLimitAndOffset(query);
},
}

aggregate(stmt) {
return this._aggregate(stmt, { aliasSeparator: ' ' });
},
}

// for single commands only
_addReturningToSqlAndConvert(sql, returning, tableName) {
Expand All @@ -284,7 +285,7 @@ assign(QueryCompiler_Oracle.prototype, {
res.outParams = [returningHelper];
res.returning = returning;
return res;
},
}

_surroundQueryWithLimitAndOffset(query) {
let { limit } = this.single;
Expand Down Expand Up @@ -314,12 +315,7 @@ assign(QueryCompiler_Oracle.prototype, {
'where rownum_ > ' +
this.formatter.parameter(offset)
);
},
});

// Compiles the `select` statement, or nested sub-selects
// by calling each of the component compilers, trimming out
// the empties, and returning a generated query string.
QueryCompiler_Oracle.prototype.first = QueryCompiler_Oracle.prototype.select;
}
}

module.exports = QueryCompiler_Oracle;

0 comments on commit 589ea74

Please sign in to comment.