1
1
mirror of https://github.com/Fabio286/antares.git synced 2025-02-18 20:50:48 +01:00

refactor: improved the way how schema is passed to client classes

This commit is contained in:
Fabio Di Stasio 2021-07-21 14:40:29 +02:00
parent d67d122270
commit e6ef5ffa56
14 changed files with 134 additions and 116 deletions

View File

@ -127,7 +127,7 @@
"standard-version": "^9.3.0", "standard-version": "^9.3.0",
"stylelint": "^13.13.1", "stylelint": "^13.13.1",
"stylelint-config-standard": "^22.0.0", "stylelint-config-standard": "^22.0.0",
"stylelint-scss": "^3.19.0", "stylelint-scss": "^3.20.1",
"vue": "^2.6.14", "vue": "^2.6.14",
"vue-template-compiler": "^2.6.14", "vue-template-compiler": "^2.6.14",
"webpack": "^4.46.0" "webpack": "^4.46.0"

View File

@ -128,7 +128,12 @@ export default connections => {
if (!query) return; if (!query) return;
try { try {
const result = await connections[uid].raw(query, { nest: true, details: true, schema }); const result = await connections[uid].raw(query, {
nest: true,
details: true,
schema,
comments: false
});
return { status: 'success', response: result }; return { status: 'success', response: result };
} }

View File

@ -573,7 +573,7 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async dropView (params) { async dropView (params) {
const sql = `DROP VIEW \`${this._schema}\`.\`${params.view}\``; const sql = `DROP VIEW \`${params.schema}\`.\`${params.view}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -585,10 +585,10 @@ export class MySQLClient extends AntaresCore {
*/ */
async alterView (params) { async alterView (params) {
const { view } = params; const { view } = params;
let sql = `ALTER ALGORITHM = ${view.algorithm}${view.definer ? ` DEFINER=${view.definer}` : ''} SQL SECURITY ${view.security} VIEW \`${this._schema}\`.\`${view.oldName}\` AS ${view.sql} ${view.updateOption ? `WITH ${view.updateOption} CHECK OPTION` : ''}`; let sql = `ALTER ALGORITHM = ${view.algorithm}${view.definer ? ` DEFINER=${view.definer}` : ''} SQL SECURITY ${view.security} VIEW \`${view.schema}\`.\`${view.oldName}\` AS ${view.sql} ${view.updateOption ? `WITH ${view.updateOption} CHECK OPTION` : ''}`;
if (view.name !== view.oldName) if (view.name !== view.oldName)
sql += `; RENAME TABLE \`${this._schema}\`.\`${view.oldName}\` TO \`${this._schema}\`.\`${view.name}\``; sql += `; RENAME TABLE \`${view.schema}\`.\`${view.oldName}\` TO \`${view.schema}\`.\`${view.name}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -599,8 +599,8 @@ export class MySQLClient extends AntaresCore {
* @returns {Array.<Object>} parameters * @returns {Array.<Object>} parameters
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async createView (view) { async createView (params) {
const sql = `CREATE ALGORITHM = ${view.algorithm} ${view.definer ? `DEFINER=${view.definer} ` : ''}SQL SECURITY ${view.security} VIEW \`${this._schema}\`.\`${view.name}\` AS ${view.sql} ${view.updateOption ? `WITH ${view.updateOption} CHECK OPTION` : ''}`; const sql = `CREATE ALGORITHM = ${params.algorithm} ${params.definer ? `DEFINER=${params.definer} ` : ''}SQL SECURITY ${params.security} VIEW \`${params.schema}\`.\`${params.name}\` AS ${params.sql} ${params.updateOption ? `WITH ${params.updateOption} CHECK OPTION` : ''}`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -633,7 +633,7 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async dropTrigger (params) { async dropTrigger (params) {
const sql = `DROP TRIGGER \`${this._schema}\`.\`${params.trigger}\``; const sql = `DROP TRIGGER \`${params.schema}\`.\`${params.trigger}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -650,8 +650,8 @@ export class MySQLClient extends AntaresCore {
try { try {
await this.createTrigger(tempTrigger); await this.createTrigger(tempTrigger);
await this.dropTrigger({ trigger: tempTrigger.name }); await this.dropTrigger({ schema: trigger.schema, trigger: tempTrigger.name });
await this.dropTrigger({ trigger: trigger.oldName }); await this.dropTrigger({ schema: trigger.schema, trigger: trigger.oldName });
await this.createTrigger(trigger); await this.createTrigger(trigger);
} }
catch (err) { catch (err) {
@ -665,8 +665,8 @@ export class MySQLClient extends AntaresCore {
* @returns {Array.<Object>} parameters * @returns {Array.<Object>} parameters
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async createTrigger (trigger) { async createTrigger (params) {
const sql = `CREATE ${trigger.definer ? `DEFINER=${trigger.definer} ` : ''}TRIGGER \`${this._schema}\`.\`${trigger.name}\` ${trigger.activation} ${trigger.event} ON \`${trigger.table}\` FOR EACH ROW ${trigger.sql}`; const sql = `CREATE ${params.definer ? `DEFINER=${params.definer} ` : ''}TRIGGER \`${params.schema}\`.\`${params.name}\` ${params.activation} ${params.event} ON \`${params.table}\` FOR EACH ROW ${params.sql}`;
return await this.raw(sql, { split: false }); return await this.raw(sql, { split: false });
} }
@ -740,7 +740,7 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async dropRoutine (params) { async dropRoutine (params) {
const sql = `DROP PROCEDURE \`${this._schema}\`.\`${params.routine}\``; const sql = `DROP PROCEDURE \`${params.schema}\`.\`${params.routine}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -757,8 +757,8 @@ export class MySQLClient extends AntaresCore {
try { try {
await this.createRoutine(tempProcedure); await this.createRoutine(tempProcedure);
await this.dropRoutine({ routine: tempProcedure.name }); await this.dropRoutine({ schema: routine.schema, routine: tempProcedure.name });
await this.dropRoutine({ routine: routine.oldName }); await this.dropRoutine({ schema: routine.schema, routine: routine.oldName });
await this.createRoutine(routine); await this.createRoutine(routine);
} }
catch (err) { catch (err) {
@ -772,21 +772,21 @@ export class MySQLClient extends AntaresCore {
* @returns {Array.<Object>} parameters * @returns {Array.<Object>} parameters
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async createRoutine (routine) { async createRoutine (params) {
const parameters = 'parameters' in routine const parameters = 'parameters' in params
? routine.parameters.reduce((acc, curr) => { ? params.parameters.reduce((acc, curr) => {
acc.push(`${curr.context} \`${curr.name}\` ${curr.type}${curr.length ? `(${curr.length})` : ''}`); acc.push(`${curr.context} \`${curr.name}\` ${curr.type}${curr.length ? `(${curr.length})` : ''}`);
return acc; return acc;
}, []).join(',') }, []).join(',')
: ''; : '';
const sql = `CREATE ${routine.definer ? `DEFINER=${routine.definer} ` : ''}PROCEDURE \`${this._schema}\`.\`${routine.name}\`(${parameters}) const sql = `CREATE ${params.definer ? `DEFINER=${params.definer} ` : ''}PROCEDURE \`${params.schema}\`.\`${params.name}\`(${parameters})
LANGUAGE SQL LANGUAGE SQL
${routine.deterministic ? 'DETERMINISTIC' : 'NOT DETERMINISTIC'} ${params.deterministic ? 'DETERMINISTIC' : 'NOT DETERMINISTIC'}
${routine.dataAccess} ${params.dataAccess}
SQL SECURITY ${routine.security} SQL SECURITY ${params.security}
COMMENT '${routine.comment}' COMMENT '${params.comment}'
${routine.sql}`; ${params.sql}`;
return await this.raw(sql, { split: false }); return await this.raw(sql, { split: false });
} }
@ -867,7 +867,7 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async dropFunction (params) { async dropFunction (params) {
const sql = `DROP FUNCTION \`${this._schema}\`.\`${params.func}\``; const sql = `DROP FUNCTION \`${params.schema}\`.\`${params.func}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -884,8 +884,8 @@ export class MySQLClient extends AntaresCore {
try { try {
await this.createFunction(tempProcedure); await this.createFunction(tempProcedure);
await this.dropFunction({ func: tempProcedure.name }); await this.dropFunction({ schema: func.schema, func: tempProcedure.name });
await this.dropFunction({ func: func.oldName }); await this.dropFunction({ schema: func.schema, func: func.oldName });
await this.createFunction(func); await this.createFunction(func);
} }
catch (err) { catch (err) {
@ -899,20 +899,20 @@ export class MySQLClient extends AntaresCore {
* @returns {Array.<Object>} parameters * @returns {Array.<Object>} parameters
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async createFunction (func) { async createFunction (params) {
const parameters = func.parameters.reduce((acc, curr) => { const parameters = params.parameters.reduce((acc, curr) => {
acc.push(`\`${curr.name}\` ${curr.type}${curr.length ? `(${curr.length})` : ''}`); acc.push(`\`${curr.name}\` ${curr.type}${curr.length ? `(${curr.length})` : ''}`);
return acc; return acc;
}, []).join(','); }, []).join(',');
const body = func.returns ? func.sql : 'BEGIN\n RETURN 0;\nEND'; const body = params.returns ? params.sql : 'BEGIN\n RETURN 0;\nEND';
const sql = `CREATE ${func.definer ? `DEFINER=${func.definer} ` : ''}FUNCTION \`${this._schema}\`.\`${func.name}\`(${parameters}) RETURNS ${func.returns || 'SMALLINT'}${func.returnsLength ? `(${func.returnsLength})` : ''} const sql = `CREATE ${params.definer ? `DEFINER=${params.definer} ` : ''}FUNCTION \`${params.schema}\`.\`${params.name}\`(${parameters}) RETURNS ${params.returns || 'SMALLINT'}${params.returnsLength ? `(${params.returnsLength})` : ''}
LANGUAGE SQL LANGUAGE SQL
${func.deterministic ? 'DETERMINISTIC' : 'NOT DETERMINISTIC'} ${params.deterministic ? 'DETERMINISTIC' : 'NOT DETERMINISTIC'}
${func.dataAccess} ${params.dataAccess}
SQL SECURITY ${func.security} SQL SECURITY ${params.security}
COMMENT '${func.comment}' COMMENT '${params.comment}'
${body}`; ${body}`;
return await this.raw(sql, { split: false }); return await this.raw(sql, { split: false });
@ -959,7 +959,7 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async dropEvent (params) { async dropEvent (params) {
const sql = `DROP EVENT \`${this._schema}\`.\`${params.scheduler}\``; const sql = `DROP EVENT \`${params.schema}\`.\`${params.scheduler}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -975,13 +975,13 @@ export class MySQLClient extends AntaresCore {
if (scheduler.execution === 'EVERY' && scheduler.every[0].includes('-')) if (scheduler.execution === 'EVERY' && scheduler.every[0].includes('-'))
scheduler.every[0] = `'${scheduler.every[0]}'`; scheduler.every[0] = `'${scheduler.every[0]}'`;
const sql = `ALTER ${scheduler.definer ? ` DEFINER=${scheduler.definer}` : ''} EVENT \`${this._schema}\`.\`${scheduler.oldName}\` const sql = `ALTER ${scheduler.definer ? ` DEFINER=${scheduler.definer}` : ''} EVENT \`${scheduler.schema}\`.\`${scheduler.oldName}\`
ON SCHEDULE ON SCHEDULE
${scheduler.execution === 'EVERY' ${scheduler.execution === 'EVERY'
? `EVERY ${scheduler.every.join(' ')}${scheduler.starts ? ` STARTS '${scheduler.starts}'` : ''}${scheduler.ends ? ` ENDS '${scheduler.ends}'` : ''}` ? `EVERY ${scheduler.every.join(' ')}${scheduler.starts ? ` STARTS '${scheduler.starts}'` : ''}${scheduler.ends ? ` ENDS '${scheduler.ends}'` : ''}`
: `AT '${scheduler.at}'`} : `AT '${scheduler.at}'`}
ON COMPLETION${!scheduler.preserve ? ' NOT' : ''} PRESERVE ON COMPLETION${!scheduler.preserve ? ' NOT' : ''} PRESERVE
${scheduler.name !== scheduler.oldName ? `RENAME TO \`${this._schema}\`.\`${scheduler.name}\`` : ''} ${scheduler.name !== scheduler.oldName ? `RENAME TO \`${scheduler.schema}\`.\`${scheduler.name}\`` : ''}
${scheduler.state} ${scheduler.state}
COMMENT '${scheduler.comment}' COMMENT '${scheduler.comment}'
DO ${scheduler.sql}`; DO ${scheduler.sql}`;
@ -995,16 +995,16 @@ export class MySQLClient extends AntaresCore {
* @returns {Array.<Object>} parameters * @returns {Array.<Object>} parameters
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async createEvent (scheduler) { async createEvent (params) {
const sql = `CREATE ${scheduler.definer ? ` DEFINER=${scheduler.definer}` : ''} EVENT \`${this._schema}\`.\`${scheduler.name}\` const sql = `CREATE ${params.definer ? ` DEFINER=${params.definer}` : ''} EVENT \`${params.schema}\`.\`${params.name}\`
ON SCHEDULE ON SCHEDULE
${scheduler.execution === 'EVERY' ${params.execution === 'EVERY'
? `EVERY ${scheduler.every.join(' ')}${scheduler.starts ? ` STARTS '${scheduler.starts}'` : ''}${scheduler.ends ? ` ENDS '${scheduler.ends}'` : ''}` ? `EVERY ${params.every.join(' ')}${params.starts ? ` STARTS '${params.starts}'` : ''}${params.ends ? ` ENDS '${params.ends}'` : ''}`
: `AT '${scheduler.at}'`} : `AT '${params.at}'`}
ON COMPLETION${!scheduler.preserve ? ' NOT' : ''} PRESERVE ON COMPLETION${!params.preserve ? ' NOT' : ''} PRESERVE
${scheduler.state} ${params.state}
COMMENT '${scheduler.comment}' COMMENT '${params.comment}'
DO ${scheduler.sql}`; DO ${params.sql}`;
return await this.raw(sql, { split: false }); return await this.raw(sql, { split: false });
} }
@ -1126,14 +1126,7 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async createTable (params) { async createTable (params) {
const { const sql = `CREATE TABLE \`${params.schema}\`.\`${params.name}\` (\`${params.name}_ID\` INT NULL) COMMENT='${params.comment}', COLLATE='${params.collation}', ENGINE=${params.engine}`;
name,
collation,
comment,
engine
} = params;
const sql = `CREATE TABLE \`${this._schema}\`.\`${name}\` (\`${name}_ID\` INT NULL) COMMENT='${comment}', COLLATE='${collation}', ENGINE=${engine}`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -1147,6 +1140,7 @@ export class MySQLClient extends AntaresCore {
async alterTable (params) { async alterTable (params) {
const { const {
table, table,
schema,
additions, additions,
deletions, deletions,
changes, changes,
@ -1155,7 +1149,7 @@ export class MySQLClient extends AntaresCore {
options options
} = params; } = params;
let sql = `ALTER TABLE \`${this._schema || params.options.schema}\`.\`${table}\` `; let sql = `ALTER TABLE \`${schema}\`.\`${table}\` `;
const alterColumns = []; const alterColumns = [];
// OPTIONS // OPTIONS
@ -1267,7 +1261,7 @@ export class MySQLClient extends AntaresCore {
sql += alterColumns.join(', '); sql += alterColumns.join(', ');
// RENAME // RENAME
if (options.name) sql += `; RENAME TABLE \`${this._schema}\`.\`${table}\` TO \`${this._schema}\`.\`${options.name}\``; if (options.name) sql += `; RENAME TABLE \`${schema}\`.\`${table}\` TO \`${schema}\`.\`${options.name}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -1372,16 +1366,19 @@ export class MySQLClient extends AntaresCore {
* @memberof MySQLClient * @memberof MySQLClient
*/ */
async raw (sql, args) { async raw (sql, args) {
sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '');
if (process.env.NODE_ENV === 'development') this._logger(sql);// TODO: replace BLOB content with a placeholder if (process.env.NODE_ENV === 'development') this._logger(sql);// TODO: replace BLOB content with a placeholder
args = { args = {
nest: false, nest: false,
details: false, details: false,
split: true, split: true,
comments: true,
...args ...args
}; };
if (!args.comments)
sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '');// Remove comments
const nestTables = args.nest ? '.' : false; const nestTables = args.nest ? '.' : false;
const resultsArr = []; const resultsArr = [];
let paramsArr = []; let paramsArr = [];

View File

@ -483,7 +483,7 @@ export class PostgreSQLClient extends AntaresCore {
* @memberof PostgreSQLClient * @memberof PostgreSQLClient
*/ */
async dropView (params) { async dropView (params) {
const sql = `DROP VIEW ${this._schema}.${params.view}`; const sql = `DROP VIEW ${params.schema}.${params.view}`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -495,10 +495,10 @@ export class PostgreSQLClient extends AntaresCore {
*/ */
async alterView (params) { async alterView (params) {
const { view } = params; const { view } = params;
let sql = `CREATE OR REPLACE VIEW ${this._schema}.${view.oldName} AS ${view.sql}`; let sql = `CREATE OR REPLACE VIEW ${view.schema}.${view.oldName} AS ${view.sql}`;
if (view.name !== view.oldName) if (view.name !== view.oldName)
sql += `; ALTER VIEW ${this._schema}.${view.oldName} RENAME TO ${view.name}`; sql += `; ALTER VIEW ${view.schema}.${view.oldName} RENAME TO ${view.name}`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -509,8 +509,8 @@ export class PostgreSQLClient extends AntaresCore {
* @returns {Array.<Object>} parameters * @returns {Array.<Object>} parameters
* @memberof PostgreSQLClient * @memberof PostgreSQLClient
*/ */
async createView (view) { async createView (params) {
const sql = `CREATE VIEW ${this._schema}.${view.name} AS ${view.sql}`; const sql = `CREATE VIEW ${params.schema}.${params.name} AS ${params.sql}`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -560,7 +560,7 @@ export class PostgreSQLClient extends AntaresCore {
*/ */
async dropTrigger (params) { async dropTrigger (params) {
const triggerParts = params.trigger.split('.'); const triggerParts = params.trigger.split('.');
const sql = `DROP TRIGGER "${triggerParts[1]}" ON "${triggerParts[0]}"`; const sql = `DROP TRIGGER "${triggerParts[1]}" ON "${params.schema}"."${triggerParts[0]}"`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -577,8 +577,8 @@ export class PostgreSQLClient extends AntaresCore {
try { try {
await this.createTrigger(tempTrigger); await this.createTrigger(tempTrigger);
await this.dropTrigger({ trigger: `${tempTrigger.table}.${tempTrigger.name}` }); await this.dropTrigger({ schema: trigger.schema, trigger: `${tempTrigger.table}.${tempTrigger.name}` });
await this.dropTrigger({ trigger: `${trigger.table}.${trigger.oldName}` }); await this.dropTrigger({ schema: trigger.schema, trigger: `${trigger.table}.${trigger.oldName}` });
await this.createTrigger(trigger); await this.createTrigger(trigger);
} }
catch (err) { catch (err) {
@ -592,9 +592,9 @@ export class PostgreSQLClient extends AntaresCore {
* @returns {Array.<Object>} parameters * @returns {Array.<Object>} parameters
* @memberof PostgreSQLClient * @memberof PostgreSQLClient
*/ */
async createTrigger (trigger) { async createTrigger (params) {
const eventsString = Array.isArray(trigger.event) ? trigger.event.join(' OR ') : trigger.event; const eventsString = Array.isArray(params.event) ? params.event.join(' OR ') : params.event;
const sql = `CREATE TRIGGER "${trigger.name}" ${trigger.activation} ${eventsString} ON "${trigger.table}" FOR EACH ROW ${trigger.sql}`; const sql = `CREATE TRIGGER "${params.name}" ${params.activation} ${eventsString} ON "${params.schema}"."${params.table}" FOR EACH ROW ${params.sql}`;
return await this.raw(sql, { split: false }); return await this.raw(sql, { split: false });
} }
@ -637,6 +637,7 @@ export class PostgreSQLClient extends AntaresCore {
AND proc.routine_type = 'PROCEDURE' AND proc.routine_type = 'PROCEDURE'
AND proc.routine_name = '${routine}' AND proc.routine_name = '${routine}'
AND proc.specific_schema = '${schema}' AND proc.specific_schema = '${schema}'
AND args.data_type != NULL
ORDER BY procedure_schema, ORDER BY procedure_schema,
specific_name, specific_name,
procedure_name, procedure_name,
@ -675,7 +676,7 @@ export class PostgreSQLClient extends AntaresCore {
* @memberof PostgreSQLClient * @memberof PostgreSQLClient
*/ */
async dropRoutine (params) { async dropRoutine (params) {
const sql = `DROP PROCEDURE ${this._schema}.${params.routine}`; const sql = `DROP PROCEDURE "${params.schema}"."${params.routine}"`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -692,8 +693,8 @@ export class PostgreSQLClient extends AntaresCore {
try { try {
await this.createRoutine(tempProcedure); await this.createRoutine(tempProcedure);
await this.dropRoutine({ routine: tempProcedure.name }); await this.dropRoutine({ schema: routine.schema, routine: tempProcedure.name });
await this.dropRoutine({ routine: routine.oldName }); await this.dropRoutine({ schema: routine.schema, routine: routine.oldName });
await this.createRoutine(routine); await this.createRoutine(routine);
} }
catch (err) { catch (err) {
@ -715,10 +716,10 @@ export class PostgreSQLClient extends AntaresCore {
}, []).join(',') }, []).join(',')
: ''; : '';
if (this._schema !== 'public') if (routine.schema !== 'public')
await this.use(this._schema); await this.use(routine.schema);
const sql = `CREATE PROCEDURE ${this._schema}.${routine.name}(${parameters}) const sql = `CREATE PROCEDURE "${routine.schema}"."${routine.name}"(${parameters})
LANGUAGE ${routine.language} LANGUAGE ${routine.language}
SECURITY ${routine.security} SECURITY ${routine.security}
AS ${routine.sql}`; AS ${routine.sql}`;
@ -804,7 +805,7 @@ export class PostgreSQLClient extends AntaresCore {
* @memberof PostgreSQLClient * @memberof PostgreSQLClient
*/ */
async dropFunction (params) { async dropFunction (params) {
const sql = `DROP FUNCTION ${this._schema}.${params.func}`; const sql = `DROP FUNCTION "${params.schema}"."${params.func}"`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -821,8 +822,8 @@ export class PostgreSQLClient extends AntaresCore {
try { try {
await this.createFunction(tempProcedure); await this.createFunction(tempProcedure);
await this.dropFunction({ func: tempProcedure.name }); await this.dropFunction({ schema: func.schema, func: tempProcedure.name });
await this.dropFunction({ func: func.oldName }); await this.dropFunction({ schema: func.schema, func: func.oldName });
await this.createFunction(func); await this.createFunction(func);
} }
catch (err) { catch (err) {
@ -839,17 +840,17 @@ export class PostgreSQLClient extends AntaresCore {
async createFunction (func) { async createFunction (func) {
const parameters = 'parameters' in func const parameters = 'parameters' in func
? func.parameters.reduce((acc, curr) => { ? func.parameters.reduce((acc, curr) => {
acc.push(`${curr.context} ${curr.name} ${curr.type}${curr.length ? `(${curr.length})` : ''}`); acc.push(`${curr.context} ${curr.name || ''} ${curr.type}${curr.length ? `(${curr.length})` : ''}`);
return acc; return acc;
}, []).join(',') }, []).join(',')
: ''; : '';
if (this._schema !== 'public') if (func.schema !== 'public')
await this.use(this._schema); await this.use(func.schema);
const body = func.returns ? func.sql : '$function$\n$function$'; const body = func.returns ? func.sql : '$function$\n$function$';
const sql = `CREATE FUNCTION ${this._schema}.${func.name}(${parameters}) const sql = `CREATE FUNCTION "${func.schema}"."${func.name}" (${parameters})
RETURNS ${func.returns || 'void'} RETURNS ${func.returns || 'void'}
LANGUAGE ${func.language} LANGUAGE ${func.language}
SECURITY ${func.security} SECURITY ${func.security}
@ -867,12 +868,12 @@ export class PostgreSQLClient extends AntaresCore {
async alterTriggerFunction (params) { async alterTriggerFunction (params) {
const { func } = params; const { func } = params;
if (this._schema !== 'public') if (func.schema !== 'public')
await this.use(this._schema); await this.use(func.schema);
const body = func.returns ? func.sql : '$function$\n$function$'; const body = func.returns ? func.sql : '$function$\n$function$';
const sql = `CREATE OR REPLACE FUNCTION ${this._schema}.${func.name}() const sql = `CREATE OR REPLACE FUNCTION "${func.schema}"."${func.name}" ()
RETURNS TRIGGER RETURNS TRIGGER
LANGUAGE ${func.language} LANGUAGE ${func.language}
AS ${body}`; AS ${body}`;
@ -887,12 +888,12 @@ export class PostgreSQLClient extends AntaresCore {
* @memberof PostgreSQLClient * @memberof PostgreSQLClient
*/ */
async createTriggerFunction (func) { async createTriggerFunction (func) {
if (this._schema !== 'public') if (func.schema !== 'public')
await this.use(this._schema); await this.use(func.schema);
const body = func.returns ? func.sql : '$function$\r\nBEGIN\r\n\r\nEND\r\n$function$'; const body = func.returns ? func.sql : '$function$\r\nBEGIN\r\n\r\nEND\r\n$function$';
const sql = `CREATE FUNCTION ${this._schema}.${func.name}() const sql = `CREATE FUNCTION "${func.schema}"."${func.name}" ()
RETURNS TRIGGER RETURNS TRIGGER
LANGUAGE ${func.language} LANGUAGE ${func.language}
AS ${body}`; AS ${body}`;
@ -988,11 +989,7 @@ export class PostgreSQLClient extends AntaresCore {
* @memberof PostgreSQLClient * @memberof PostgreSQLClient
*/ */
async createTable (params) { async createTable (params) {
const { const sql = `CREATE TABLE "${params.schema}"."${params.name}" (${params.name}_id INTEGER NULL); ALTER TABLE "${params.schema}"."${params.name}" DROP COLUMN ${params.name}_id`;
name
} = params;
const sql = `CREATE TABLE ${this._schema}.${name} (${name}_id INTEGER NULL); ALTER TABLE ${this._schema}.${name} DROP COLUMN ${name}_id`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -1006,6 +1003,7 @@ export class PostgreSQLClient extends AntaresCore {
async alterTable (params) { async alterTable (params) {
const { const {
table, table,
schema,
additions, additions,
deletions, deletions,
changes, changes,
@ -1014,8 +1012,8 @@ export class PostgreSQLClient extends AntaresCore {
options options
} = params; } = params;
if (this._schema !== 'public') if (schema !== 'public')
await this.use(this._schema); await this.use(schema);
let sql = ''; let sql = '';
const alterColumns = []; const alterColumns = [];
@ -1056,7 +1054,7 @@ export class PostgreSQLClient extends AntaresCore {
else if (type === 'UNIQUE') else if (type === 'UNIQUE')
alterColumns.push(`ADD CONSTRAINT ${addition.name} UNIQUE (${fields})`); alterColumns.push(`ADD CONSTRAINT ${addition.name} UNIQUE (${fields})`);
else else
manageIndexes.push(`CREATE INDEX ${addition.name} ON ${this._schema}.${table}(${fields})`); manageIndexes.push(`CREATE INDEX ${addition.name} ON "${schema}"."${table}" (${fields})`);
}); });
// ADD FOREIGN KEYS // ADD FOREIGN KEYS
@ -1094,7 +1092,7 @@ export class PostgreSQLClient extends AntaresCore {
} }
if (change.orgName !== change.name) if (change.orgName !== change.name)
renameColumns.push(`ALTER TABLE "${this._schema}"."${table}" RENAME COLUMN "${change.orgName}" TO "${change.name}"`); renameColumns.push(`ALTER TABLE "${schema}"."${table}" RENAME COLUMN "${change.orgName}" TO "${change.name}"`);
}); });
// CHANGE INDEX // CHANGE INDEX
@ -1112,7 +1110,7 @@ export class PostgreSQLClient extends AntaresCore {
else if (type === 'UNIQUE') else if (type === 'UNIQUE')
alterColumns.push(`ADD CONSTRAINT ${change.name} UNIQUE (${fields})`); alterColumns.push(`ADD CONSTRAINT ${change.name} UNIQUE (${fields})`);
else else
manageIndexes.push(`CREATE INDEX ${change.name} ON ${this._schema}.${table}(${fields})`); manageIndexes.push(`CREATE INDEX ${change.name} ON "${schema}"."${table}" (${fields})`);
}); });
// CHANGE FOREIGN KEYS // CHANGE FOREIGN KEYS
@ -1139,10 +1137,10 @@ export class PostgreSQLClient extends AntaresCore {
alterColumns.push(`DROP CONSTRAINT ${deletion.constraintName}`); alterColumns.push(`DROP CONSTRAINT ${deletion.constraintName}`);
}); });
if (alterColumns.length) sql += `ALTER TABLE "${this._schema}"."${table}" ${alterColumns.join(', ')}; `; if (alterColumns.length) sql += `ALTER TABLE "${schema}"."${table}" ${alterColumns.join(', ')}; `;
if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`; if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`;
if (manageIndexes.length) sql = `${manageIndexes.join(';')}; ${sql}`; if (manageIndexes.length) sql = `${manageIndexes.join(';')}; ${sql}`;
if (options.name) sql += `ALTER TABLE "${this._schema}"."${table}" RENAME TO "${options.name}"; `; if (options.name) sql += `ALTER TABLE "${schema}"."${table}" RENAME TO "${options.name}"; `;
// RENAME // RENAME
if (renameColumns.length) sql = `${renameColumns.join(';')}; ${sql}`; if (renameColumns.length) sql = `${renameColumns.join(';')}; ${sql}`;
@ -1250,18 +1248,20 @@ export class PostgreSQLClient extends AntaresCore {
* @memberof PostgreSQLClient * @memberof PostgreSQLClient
*/ */
async raw (sql, args) { async raw (sql, args) {
sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '');
args = { args = {
nest: false, nest: false,
details: false, details: false,
split: true, split: true,
comments: true,
...args ...args
}; };
if (args.schema && args.schema !== 'public') if (args.schema && args.schema !== 'public')
await this.use(args.schema); await this.use(args.schema);
if (!args.comments)
sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '');// Remove comments
const resultsArr = []; const resultsArr = [];
let paramsArr = []; let paramsArr = [];
const queries = args.split const queries = args.split

View File

@ -421,7 +421,10 @@ export default {
table: 'data', table: 'data',
view: 'data', view: 'data',
trigger: 'trigger-props', trigger: 'trigger-props',
triggerFunction: 'trigger-function-props' triggerFunction: 'trigger-function-props',
function: 'function-props',
routine: 'routine-props',
scheduler: 'scheduler-props'
}; };
this.newTab({ this.newTab({

View File

@ -312,6 +312,7 @@ export default {
async openCreateTableEditor (payload) { async openCreateTableEditor (payload) {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.selectedSchema,
...payload ...payload
}; };
@ -376,6 +377,7 @@ export default {
async openCreateViewEditor (payload) { async openCreateViewEditor (payload) {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.selectedSchema,
...payload ...payload
}; };
@ -407,6 +409,7 @@ export default {
async openCreateTriggerEditor (payload) { async openCreateTriggerEditor (payload) {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.selectedSchema,
...payload ...payload
}; };
@ -439,6 +442,7 @@ export default {
async openCreateRoutineEditor (payload) { async openCreateRoutineEditor (payload) {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.selectedSchema,
...payload ...payload
}; };
@ -452,8 +456,8 @@ export default {
uid: this.workspace.uid, uid: this.workspace.uid,
schema: this.selectedSchema, schema: this.selectedSchema,
elementName: payload.name, elementName: payload.name,
elementType: 'procedure', elementType: 'routine',
type: 'procedure-props' type: 'routine-props'
}); });
} }
else else
@ -486,6 +490,7 @@ export default {
async openCreateFunctionEditor (payload) { async openCreateFunctionEditor (payload) {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.selectedSchema,
...payload ...payload
}; };
@ -509,6 +514,7 @@ export default {
async openCreateTriggerFunctionEditor (payload) { async openCreateTriggerFunctionEditor (payload) {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.selectedSchema,
...payload ...payload
}; };
@ -532,6 +538,7 @@ export default {
async openCreateSchedulerEditor (payload) { async openCreateSchedulerEditor (payload) {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.selectedSchema,
...payload ...payload
}; };

View File

@ -128,12 +128,14 @@ export default {
case 'trigger': case 'trigger':
res = await Triggers.dropTrigger({ res = await Triggers.dropTrigger({
uid: this.selectedWorkspace, uid: this.selectedWorkspace,
schema: this.selectedSchema,
trigger: this.selectedMisc.name trigger: this.selectedMisc.name
}); });
break; break;
case 'procedure': case 'procedure':
res = await Routines.dropRoutine({ res = await Routines.dropRoutine({
uid: this.selectedWorkspace, uid: this.selectedWorkspace,
schema: this.selectedSchema,
routine: this.selectedMisc.name routine: this.selectedMisc.name
}); });
break; break;
@ -141,12 +143,14 @@ export default {
case 'triggerFunction': case 'triggerFunction':
res = await Functions.dropFunction({ res = await Functions.dropFunction({
uid: this.selectedWorkspace, uid: this.selectedWorkspace,
schema: this.selectedSchema,
func: this.selectedMisc.name func: this.selectedMisc.name
}); });
break; break;
case 'scheduler': case 'scheduler':
res = await Schedulers.dropScheduler({ res = await Schedulers.dropScheduler({
uid: this.selectedWorkspace, uid: this.selectedWorkspace,
schema: this.selectedSchema,
scheduler: this.selectedMisc.name scheduler: this.selectedMisc.name
}); });
break; break;
@ -187,8 +191,8 @@ export default {
async runRoutineCheck () { async runRoutineCheck () {
const params = { const params = {
uid: this.selectedWorkspace, uid: this.selectedWorkspace,
schema: this.workspace.breadcrumbs.schema, schema: this.selectedSchema,
routine: this.workspace.breadcrumbs.procedure routine: this.selectedMisc.name
}; };
try { try {
@ -231,8 +235,8 @@ export default {
async runFunctionCheck () { async runFunctionCheck () {
const params = { const params = {
uid: this.selectedWorkspace, uid: this.selectedWorkspace,
schema: this.workspace.breadcrumbs.schema, schema: this.selectedSchema,
func: this.workspace.breadcrumbs.function func: this.selectedMisc.name
}; };
try { try {
@ -270,7 +274,7 @@ export default {
sql = `SELECT \`${this.localElement.name}\` (${params.join(',')})`; sql = `SELECT \`${this.localElement.name}\` (${params.join(',')})`;
} }
this.newTab({ uid: this.workspace.uid, content: sql, autorun: true }); this.newTab({ uid: this.workspace.uid, content: sql, type: 'query', autorun: true });
this.closeContext(); this.closeContext();
} }
} }

View File

@ -243,9 +243,9 @@ export default {
this.isSaving = true; this.isSaving = true;
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.schema,
func: { func: {
...this.localFunction, ...this.localFunction,
schema: this.schema,
oldName: this.originalFunction.name oldName: this.originalFunction.name
} }
}; };

View File

@ -242,9 +242,9 @@ export default {
this.isSaving = true; this.isSaving = true;
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.schema,
routine: { routine: {
...this.localRoutine, ...this.localRoutine,
schema: this.schema,
oldName: this.originalRoutine.name oldName: this.originalRoutine.name
} }
}; };

View File

@ -283,9 +283,9 @@ export default {
this.isSaving = true; this.isSaving = true;
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.schema,
scheduler: { scheduler: {
...this.localScheduler, ...this.localScheduler,
schema: this.schema,
oldName: this.originalScheduler.name oldName: this.originalScheduler.name
} }
}; };

View File

@ -293,9 +293,9 @@ export default {
this.isSaving = true; this.isSaving = true;
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.schema,
trigger: { trigger: {
...this.localTrigger, ...this.localTrigger,
schema: this.schema,
oldName: this.originalTrigger.name oldName: this.originalTrigger.name
} }
}; };

View File

@ -216,9 +216,9 @@ export default {
this.isSaving = true; this.isSaving = true;
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.schema,
func: { func: {
...this.localFunction, ...this.localFunction,
schema: this.schema,
oldName: this.originalFunction.name oldName: this.originalFunction.name
} }
}; };

View File

@ -307,9 +307,9 @@ export default {
this.isSaving = true; this.isSaving = true;
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.schema,
view: { view: {
...this.localView, ...this.localView,
schema: this.schema,
oldName: this.originalView.name oldName: this.originalView.name
} }
}; };

View File

@ -212,6 +212,8 @@ export default {
}); });
}, },
REMOVE_TABS (state, { uid, schema, elementName, elementType }) { // Multiple tabs based on schema and element name REMOVE_TABS (state, { uid, schema, elementName, elementType }) { // Multiple tabs based on schema and element name
if (elementType === 'procedure') elementType = 'routine'; // TODO: pass directly "routine"
state.workspaces = state.workspaces.map(workspace => { state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) { if (workspace.uid === uid) {
return { return {