不要怂,就是干,撸起袖子干!

Commit 0769aea3 by Sushant Committed by GitHub

refactor: cleanup query generators (#12304)

1 parent 4d9165b6
...@@ -336,8 +336,6 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator { ...@@ -336,8 +336,6 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
const allAttributes = []; const allAttributes = [];
const allQueries = []; const allQueries = [];
let needIdentityInsertWrapper = false, let needIdentityInsertWrapper = false,
outputFragment = ''; outputFragment = '';
......
...@@ -147,7 +147,6 @@ class MySQLQueryGenerator extends AbstractQueryGenerator { ...@@ -147,7 +147,6 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
]); ]);
} }
describeTableQuery(tableName, schema, schemaDelimiter) { describeTableQuery(tableName, schema, schemaDelimiter) {
const table = this.quoteTable( const table = this.quoteTable(
this.addSchema({ this.addSchema({
......
...@@ -335,14 +335,6 @@ class PostgresQueryGenerator extends AbstractQueryGenerator { ...@@ -335,14 +335,6 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
return `CREATE OR REPLACE FUNCTION pg_temp.${fnName}(${parameters}) ${returns} AS $func$ BEGIN ${body} END; $func$ LANGUAGE ${language}; SELECT * FROM pg_temp.${fnName}();`; return `CREATE OR REPLACE FUNCTION pg_temp.${fnName}(${parameters}) ${returns} AS $func$ BEGIN ${body} END; $func$ LANGUAGE ${language}; SELECT * FROM pg_temp.${fnName}();`;
} }
exceptionFn(fnName, tableName, parameters, main, then, when, returns, language) {
when = when || 'unique_violation';
const body = `${main} EXCEPTION WHEN ${when} THEN ${then};`;
return this.fn(fnName, tableName, parameters, body, returns, language);
}
truncateTableQuery(tableName, options = {}) { truncateTableQuery(tableName, options = {}) {
return [ return [
`TRUNCATE ${this.quoteTable(tableName)}`, `TRUNCATE ${this.quoteTable(tableName)}`,
...@@ -593,7 +585,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator { ...@@ -593,7 +585,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
const decodedEventType = this.decodeTriggerEventType(eventType); const decodedEventType = this.decodeTriggerEventType(eventType);
const eventSpec = this.expandTriggerEventSpec(fireOnSpec); const eventSpec = this.expandTriggerEventSpec(fireOnSpec);
const expandedOptions = this.expandOptions(optionsArray); const expandedOptions = this.expandOptions(optionsArray);
const paramList = this.expandFunctionParamList(functionParams); const paramList = this._expandFunctionParamList(functionParams);
return `CREATE ${this.triggerEventTypeIsConstraint(eventType)}TRIGGER ${this.quoteIdentifier(triggerName)} ${decodedEventType} ${ return `CREATE ${this.triggerEventTypeIsConstraint(eventType)}TRIGGER ${this.quoteIdentifier(triggerName)} ${decodedEventType} ${
eventSpec} ON ${this.quoteTable(tableName)}${expandedOptions ? ` ${expandedOptions}` : ''} EXECUTE PROCEDURE ${functionName}(${paramList});`; eventSpec} ON ${this.quoteTable(tableName)}${expandedOptions ? ` ${expandedOptions}` : ''} EXECUTE PROCEDURE ${functionName}(${paramList});`;
...@@ -610,8 +602,8 @@ class PostgresQueryGenerator extends AbstractQueryGenerator { ...@@ -610,8 +602,8 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
createFunction(functionName, params, returnType, language, body, optionsArray, options) { createFunction(functionName, params, returnType, language, body, optionsArray, options) {
if (!functionName || !returnType || !language || !body) throw new Error('createFunction missing some parameters. Did you pass functionName, returnType, language and body?'); if (!functionName || !returnType || !language || !body) throw new Error('createFunction missing some parameters. Did you pass functionName, returnType, language and body?');
const paramList = this.expandFunctionParamList(params); const paramList = this._expandFunctionParamList(params);
const variableList = options && options.variables ? this.expandFunctionVariableList(options.variables) : ''; const variableList = options && options.variables ? this._expandFunctionVariableList(options.variables) : '';
const expandedOptionsArray = this.expandOptions(optionsArray); const expandedOptionsArray = this.expandOptions(optionsArray);
const statement = options && options.force ? 'CREATE OR REPLACE FUNCTION' : 'CREATE FUNCTION'; const statement = options && options.force ? 'CREATE OR REPLACE FUNCTION' : 'CREATE FUNCTION';
...@@ -622,34 +614,22 @@ class PostgresQueryGenerator extends AbstractQueryGenerator { ...@@ -622,34 +614,22 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
dropFunction(functionName, params) { dropFunction(functionName, params) {
if (!functionName) throw new Error('requires functionName'); if (!functionName) throw new Error('requires functionName');
// RESTRICT is (currently, as of 9.2) default but we'll be explicit // RESTRICT is (currently, as of 9.2) default but we'll be explicit
const paramList = this.expandFunctionParamList(params); const paramList = this._expandFunctionParamList(params);
return `DROP FUNCTION ${functionName}(${paramList}) RESTRICT;`; return `DROP FUNCTION ${functionName}(${paramList}) RESTRICT;`;
} }
renameFunction(oldFunctionName, params, newFunctionName) { renameFunction(oldFunctionName, params, newFunctionName) {
const paramList = this.expandFunctionParamList(params); const paramList = this._expandFunctionParamList(params);
return `ALTER FUNCTION ${oldFunctionName}(${paramList}) RENAME TO ${newFunctionName};`; return `ALTER FUNCTION ${oldFunctionName}(${paramList}) RENAME TO ${newFunctionName};`;
} }
databaseConnectionUri(config) {
let uri = `${config.protocol}://${config.user}:${config.password}@${config.host}`;
if (config.port) {
uri += `:${config.port}`;
}
uri += `/${config.database}`;
if (config.ssl) {
uri += `?ssl=${config.ssl}`;
}
return uri;
}
pgEscapeAndQuote(val) { pgEscapeAndQuote(val) {
return this.quoteIdentifier(Utils.removeTicks(this.escape(val), "'")); return this.quoteIdentifier(Utils.removeTicks(this.escape(val), "'"));
} }
expandFunctionParamList(params) { _expandFunctionParamList(params) {
if (params === undefined || !Array.isArray(params)) { if (params === undefined || !Array.isArray(params)) {
throw new Error('expandFunctionParamList: function parameters array required, including an empty one for no arguments'); throw new Error('_expandFunctionParamList: function parameters array required, including an empty one for no arguments');
} }
const paramList = []; const paramList = [];
...@@ -671,9 +651,9 @@ class PostgresQueryGenerator extends AbstractQueryGenerator { ...@@ -671,9 +651,9 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
return paramList.join(', '); return paramList.join(', ');
} }
expandFunctionVariableList(variables) { _expandFunctionVariableList(variables) {
if (!Array.isArray(variables)) { if (!Array.isArray(variables)) {
throw new Error('expandFunctionVariableList: function variables must be an array'); throw new Error('_expandFunctionVariableList: function variables must be an array');
} }
const variableDefinitions = []; const variableDefinitions = [];
variables.forEach(variable => { variables.forEach(variable => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!