Skip to content

Commit

Permalink
[CLEAN] Remove duplicate code (#4813)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCavadenti committed Nov 9, 2021
1 parent 9a497b7 commit 49e597d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 146 deletions.
42 changes: 3 additions & 39 deletions lib/dialects/cockroachdb/crdb-querycompiler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const QueryCompiler_PG = require('../postgres/query/pg-querycompiler');
const isEmpty = require('lodash/isEmpty');
const { columnize: columnize_ } = require('../../formatter/wrappingFormatter');

class QueryCompiler_CRDB extends QueryCompiler_PG {
truncate() {
Expand All @@ -20,43 +18,9 @@ class QueryCompiler_CRDB extends QueryCompiler_PG {

_upsert() {
const upsertValues = this.single.upsert || [];
let sql = this.with() + `upsert into ${this.tableName} `;
if (Array.isArray(upsertValues)) {
if (upsertValues.length === 0) return '';
} else if (typeof upsertValues === 'object' && isEmpty(upsertValues)) {
return sql + this._emptyInsertValue;
}

const upsertData = this._prepInsert(upsertValues);
if (typeof upsertData === 'string') {
sql += upsertData;
} else {
if (upsertData.columns.length) {
sql += `(${columnize_(
upsertData.columns,
this.builder,
this.client,
this.bindingsHolder
)}`;
sql += ') values (';
let i = -1;
while (++i < upsertData.values.length) {
if (i !== 0) sql += '), (';
sql += this.client.parameterize(
upsertData.values[i],
this.client.valueForUndefined,
this.builder,
this.bindingsHolder
);
}
sql += ')';
} else if (upsertValues.length === 1 && upsertValues[0]) {
sql += this._emptyInsertValue;
} else {
sql = '';
}
}
return sql;
const sql = this.with() + `upsert into ${this.tableName} `;
const body = this._insertBody(upsertValues);
return body === '' ? '' : sql + body;
}
}

Expand Down
66 changes: 20 additions & 46 deletions lib/dialects/mssql/query/mssql-querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,40 +100,37 @@ class QueryCompiler_MSSQL extends QueryCompiler {
returning,
};
}
sql += this._buildInsertData(insertValues, returningSql);

if (returning) {
sql += this._buildReturningSelect(returning);
}

return {
sql,
returning,
};
}

_buildInsertData(insertValues, returningSql) {
let sql = '';
const insertData = this._prepInsert(insertValues);
if (typeof insertData === 'string') {
sql += insertData;
} else {
if (insertData.columns.length) {
sql += `(${this.formatter.columnize(insertData.columns)}`;
sql += `) ${returningSql}values (`;
let i = -1;
while (++i < insertData.values.length) {
if (i !== 0) sql += '), (';
sql += this.client.parameterize(
insertData.values[i],
this.client.valueForUndefined,
this.builder,
this.bindingsHolder
);
}
sql += ')';
sql +=
`) ${returningSql}values (` +
this._buildInsertValues(insertData) +
')';
} else if (insertValues.length === 1 && insertValues[0]) {
sql += returningSql + this._emptyInsertValue;
} else {
sql = '';
return '';
}
}

if (returning) {
sql += this._buildReturningSelect(returning);
}

return {
sql,
returning,
};
return sql;
}

standardInsert() {
Expand All @@ -155,30 +152,7 @@ class QueryCompiler_MSSQL extends QueryCompiler {
};
}

const insertData = this._prepInsert(insertValues);
if (typeof insertData === 'string') {
sql += insertData;
} else {
if (insertData.columns.length) {
sql += `(${this.formatter.columnize(insertData.columns)}`;
sql += `) ${returningSql}values (`;
let i = -1;
while (++i < insertData.values.length) {
if (i !== 0) sql += '), (';
sql += this.client.parameterize(
insertData.values[i],
this.client.valueForUndefined,
this.builder,
this.bindingsHolder
);
}
sql += ')';
} else if (insertValues.length === 1 && insertValues[0]) {
sql += returningSql + this._emptyInsertValue;
} else {
sql = '';
}
}
sql += this._buildInsertData(insertValues, returningSql);

return {
sql,
Expand Down
6 changes: 5 additions & 1 deletion lib/dialects/postgres/query/pg-querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,14 @@ class QueryCompiler_PG extends QueryCompiler {
schema = this.client.customWrapIdentifier(schema, identity);
}

let sql =
const sql =
'select * from information_schema.columns where table_name = ? and table_catalog = current_database()';
const bindings = [table];

return this._buildColumnInfoQuery(schema, sql, bindings, column);
}

_buildColumnInfoQuery(schema, sql, bindings, column) {
if (schema) {
sql += ' and table_schema = ?';
bindings.push(schema);
Expand Down
31 changes: 2 additions & 29 deletions lib/dialects/redshift/query/redshift-querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const QueryCompiler = require('../../../query/querycompiler');
const QueryCompiler_PG = require('../../postgres/query/pg-querycompiler');

const identity = require('lodash/identity');
const reduce = require('lodash/reduce');

class QueryCompiler_Redshift extends QueryCompiler_PG {
truncate() {
Expand Down Expand Up @@ -87,40 +86,14 @@ class QueryCompiler_Redshift extends QueryCompiler_PG {
schema = this.client.customWrapIdentifier(schema, identity);
}

let sql =
const sql =
'select * from information_schema.columns where table_name = ? and table_catalog = ?';
const bindings = [
table.toLowerCase(),
this.client.database().toLowerCase(),
];

if (schema) {
sql += ' and table_schema = ?';
bindings.push(schema);
} else {
sql += ' and table_schema = current_schema()';
}

return {
sql,
bindings,
output(resp) {
const out = reduce(
resp.rows,
function (columns, val) {
columns[val.column_name] = {
type: val.data_type,
maxLength: val.character_maximum_length,
nullable: val.is_nullable === 'YES',
defaultValue: val.column_default,
};
return columns;
},
{}
);
return (column && out[column]) || out;
},
};
return this._buildColumnInfoQuery(schema, sql, bindings, column);
}
}

Expand Down
55 changes: 24 additions & 31 deletions lib/query/querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,28 @@ class QueryCompiler {
// inserts using a single query statement.
insert() {
const insertValues = this.single.insert || [];
let sql = this.with() + `insert into ${this.tableName} `;
const sql = this.with() + `insert into ${this.tableName} `;
const body = this._insertBody(insertValues);
return body === '' ? '' : sql + body;
}

_buildInsertValues(insertData) {
let sql = '';
let i = -1;
while (++i < insertData.values.length) {
if (i !== 0) sql += '), (';
sql += this.client.parameterize(
insertData.values[i],
this.client.valueForUndefined,
this.builder,
this.bindingsHolder
);
}
return sql;
}

_insertBody(insertValues) {
let sql = '';
if (Array.isArray(insertValues)) {
if (insertValues.length === 0) {
return '';
Expand All @@ -164,18 +185,7 @@ class QueryCompiler {
this.client,
this.bindingsHolder
)}`;
sql += ') values (';
let i = -1;
while (++i < insertData.values.length) {
if (i !== 0) sql += '), (';
sql += this.client.parameterize(
insertData.values[i],
this.client.valueForUndefined,
this.builder,
this.bindingsHolder
);
}
sql += ')';
sql += ') values (' + this._buildInsertValues(insertData) + ')';
} else if (insertValues.length === 1 && insertValues[0]) {
sql += this._emptyInsertValue;
} else {
Expand Down Expand Up @@ -656,24 +666,7 @@ class QueryCompiler {
}

multiHavingIn(statement) {
let i = -1,
sql = `(${columnize_(
statement.column,
this.builder,
this.client,
this.bindingsHolder
)}) `;
sql += this._not(statement, 'in ') + '((';
while (++i < statement.value.length) {
if (i !== 0) sql += '),(';
sql += this.client.parameterize(
statement.value[i],
undefined,
this.builder,
this.bindingsHolder
);
}
return sql + '))';
return this.multiOnIn(statement);
}

// Compile the "union" queries attached to the main query.
Expand Down

0 comments on commit 49e597d

Please sign in to comment.