More formatter cleanup (#4218)

This commit is contained in:
Igor Savin 2021-01-07 23:34:46 +02:00 committed by GitHub
parent 0bef361ffd
commit cd6479bf0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 46 additions and 45 deletions

View File

@ -87,8 +87,8 @@ Object.assign(Client.prototype, {
return new QueryBuilder(this);
},
queryCompiler(builder) {
return new QueryCompiler(this, builder);
queryCompiler(builder, formatter) {
return new QueryCompiler(this, builder, formatter);
},
schemaBuilder() {

View File

@ -8,7 +8,7 @@ const { inherits } = require('util');
const Client = require('../../client');
const Transaction = require('./transaction');
const QueryCompiler = require('./query/compiler');
const QueryCompiler = require('./query/mssql-querycompiler');
const SchemaCompiler = require('./schema/mssql-compiler');
const TableCompiler = require('./schema/mssql-tablecompiler');
const ColumnCompiler = require('./schema/mssql-columncompiler');
@ -191,8 +191,8 @@ Object.assign(Client_MSSQL.prototype, {
return new Transaction(this, ...arguments);
},
queryCompiler() {
return new QueryCompiler(this, ...arguments);
queryCompiler(builder, formatter) {
return new QueryCompiler(this, builder, formatter);
},
schemaCompiler() {

View File

@ -20,8 +20,8 @@ const components = [
];
class QueryCompiler_MSSQL extends QueryCompiler {
constructor(client, builder) {
super(client, builder);
constructor(client, builder, formatter) {
super(client, builder, formatter);
const { onConflict } = this.single;
if (onConflict) {

View File

@ -31,8 +31,8 @@ Object.assign(Client_MySQL.prototype, {
return require('mysql');
},
queryCompiler() {
return new QueryCompiler(this, ...arguments);
queryCompiler(builder, formatter) {
return new QueryCompiler(this, builder, formatter);
},
schemaCompiler() {

View File

@ -5,8 +5,8 @@ const QueryCompiler = require('../../../query/compiler');
const { wrapAsIdentifier } = require('../../../formatter/formatterUtils');
class QueryCompiler_MySQL extends QueryCompiler {
constructor(client, builder) {
super(client, builder);
constructor(client, builder, formatter) {
super(client, builder, formatter);
const { returning } = this.single;
if (returning) {

View File

@ -29,8 +29,8 @@ const components = [
// ensuring that all parameterized values (even across sub-queries)
// are properly built into the same query.
class QueryCompiler_Oracle extends QueryCompiler {
constructor(client, builder) {
super(client, builder);
constructor(client, builder, formatter) {
super(client, builder, formatter);
const { onConflict } = this.single;
if (onConflict) {

View File

@ -8,7 +8,7 @@ const isEmpty = require('lodash/isEmpty');
const map = require('lodash/map');
const values = require('lodash/values');
const QueryCompiler = require('./query/compiler');
const QueryCompiler = require('./query/oracledb-querycompiler');
const ColumnCompiler = require('./schema/oracledb-columncompiler');
const { BlobHelper, ReturningHelper, isConnectionError } = require('./utils');
const Transaction = require('./transaction');
@ -55,8 +55,8 @@ Client_Oracledb.prototype._driver = function () {
return oracledb;
};
Client_Oracledb.prototype.queryCompiler = function () {
return new QueryCompiler(this, ...arguments);
Client_Oracledb.prototype.queryCompiler = function (builder, formatter) {
return new QueryCompiler(this, builder, formatter);
};
Client_Oracledb.prototype.columnCompiler = function () {
return new ColumnCompiler(this, ...arguments);

View File

@ -2,16 +2,12 @@ const clone = require('lodash/clone');
const each = require('lodash/each');
const isEmpty = require('lodash/isEmpty');
const isPlainObject = require('lodash/isPlainObject');
const Oracle_Compiler = require('../../oracle/query/compiler');
const Oracle_Compiler = require('../../oracle/query/oracle-querycompiler');
const ReturningHelper = require('../utils').ReturningHelper;
const BlobHelper = require('../utils').BlobHelper;
const { isString } = require('../../../util/is');
class Oracledb_Compiler extends Oracle_Compiler {
constructor(client, builder) {
super(client, builder);
}
// Compiles an "insert" query, allowing for multiple
// inserts using a single query statement.
insert() {

View File

@ -30,8 +30,8 @@ Object.assign(Client_PG.prototype, {
return new Transaction(this, ...arguments);
},
queryCompiler() {
return new QueryCompiler(this, ...arguments);
queryCompiler(builder, formatter) {
return new QueryCompiler(this, builder, formatter);
},
columnCompiler() {

View File

@ -7,8 +7,8 @@ const QueryCompiler = require('../../../query/compiler');
const { wrapString } = require('../../../formatter/wrappingFormatter');
class QueryCompiler_PG extends QueryCompiler {
constructor(client, builder) {
super(client, builder);
constructor(client, builder, formatter) {
super(client, builder, formatter);
this._defaultInsertValue = 'default';
}

View File

@ -5,7 +5,7 @@ const Client_PG = require('../postgres');
const map = require('lodash/map');
const Transaction = require('./transaction');
const QueryCompiler = require('./query/reshift-querycompiler');
const QueryCompiler = require('./query/redshift-querycompiler');
const ColumnBuilder = require('./schema/redshift-columnbuilder');
const ColumnCompiler = require('./schema/redshift-columncompiler');
const TableCompiler = require('./schema/redshift-tablecompiler');
@ -21,8 +21,8 @@ Object.assign(Client_Redshift.prototype, {
return new Transaction(this, ...arguments);
},
queryCompiler() {
return new QueryCompiler(this, ...arguments);
queryCompiler(builder, formatter) {
return new QueryCompiler(this, builder, formatter);
},
columnBuilder() {

View File

@ -7,10 +7,6 @@ const identity = require('lodash/identity');
const reduce = require('lodash/reduce');
class QueryCompiler_Redshift extends QueryCompiler_PG {
constructor(client, builder) {
super(client, builder);
}
truncate() {
return `truncate ${this.tableName.toLowerCase()}`;
}

View File

@ -7,7 +7,7 @@ const { promisify, inherits } = require('util');
const Client = require('../../client');
const Transaction = require('./execution/sqlite-transaction');
const QueryCompiler = require('./query/sqlite-query-compiler');
const SqliteQueryCompiler = require('./query/sqlite-query-compiler');
const SchemaCompiler = require('./schema/sqlite-compiler');
const ColumnCompiler = require('./schema/sqlite-columncompiler');
const TableCompiler = require('./schema/sqlite-tablecompiler');
@ -44,8 +44,8 @@ Object.assign(Client_SQLite3.prototype, {
return new Transaction(this, ...arguments);
},
queryCompiler() {
return new QueryCompiler(this, ...arguments);
queryCompiler(builder, formatter) {
return new SqliteQueryCompiler(this, builder, formatter);
},
columnCompiler() {

View File

@ -14,8 +14,8 @@ const { wrapString } = require('../../../formatter/wrappingFormatter');
const emptyStr = constant('');
class QueryCompiler_SQLite3 extends QueryCompiler {
constructor(client, builder) {
super(client, builder);
constructor(client, builder, formatter) {
super(client, builder, formatter);
const { returning } = this.single;

View File

@ -5,8 +5,7 @@ function compileCallback(callback, method, client, formatter) {
callback.call(builder, builder);
// Compile the callback, using the current formatter (to track all bindings).
const compiler = client.queryCompiler(builder);
compiler.formatter = formatter;
const compiler = client.queryCompiler(builder, formatter);
// Return the compiled & parameterized sql.
return compiler.toSQL(method || builder._method || 'select');

View File

@ -100,7 +100,7 @@ function unwrapRaw(value, isParameter, builder, client, formatter) {
if (value instanceof QueryBuilder) {
query = client.queryCompiler(value).toSQL();
if (query.bindings) {
formatter.bindings = formatter.bindings.concat(query.bindings);
formatter.bindings.push(...query.bindings);
}
return outputQuery(query, isParameter, builder, client);
}
@ -114,7 +114,7 @@ function unwrapRaw(value, isParameter, builder, client, formatter) {
query = value.toSQL();
if (query.bindings) {
formatter.bindings = formatter.bindings.concat(query.bindings);
formatter.bindings.push(...query.bindings);
}
return query.sql;
}

View File

@ -17,7 +17,10 @@ const omitBy = require('lodash/omitBy');
const reduce = require('lodash/reduce');
const { nanoid } = require('../util/nanoid');
const { isString, isUndefined } = require('../util/is');
const { direction: direction_ } = require('../formatter/wrappingFormatter');
const {
direction: direction_,
columnize: columnize_,
} = require('../formatter/wrappingFormatter');
const debugBindings = debug('knex:bindings');
@ -39,7 +42,7 @@ const components = [
// have been gathered in the "QueryBuilder" and turns them into a
// properly formatted / bound query string.
class QueryCompiler {
constructor(client, builder) {
constructor(client, builder, formatter) {
this.client = client;
this.method = builder._method || 'select';
this.options = builder._options;
@ -47,10 +50,12 @@ class QueryCompiler {
this.timeout = builder._timeout || false;
this.cancelOnTimeout = builder._cancelOnTimeout || false;
this.grouped = groupBy(builder._statements, 'grouping');
this.formatter = client.formatter(builder);
this.formatter = formatter || client.formatter(builder);
// Used when the insert call is empty.
this._emptyInsertValue = 'default values';
this.first = this.select;
this.builder = this.formatter.builder;
}
// Collapse the builder into a single object
@ -147,7 +152,12 @@ class QueryCompiler {
sql += insertData;
} else {
if (insertData.columns.length) {
sql += `(${this.formatter.columnize(insertData.columns)}`;
sql += `(${columnize_(
insertData.columns,
this.builder,
this.client,
this.formatter
)}`;
sql += ') values (';
let i = -1;
while (++i < insertData.values.length) {