Skip to content

Commit

Permalink
feat(PostgreSQL): procedures management
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Apr 10, 2021
1 parent d0b3e1b commit 3dde1c1
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 48 deletions.
3 changes: 2 additions & 1 deletion src/common/customizations/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ module.exports = {
viewUpdateOption: false,
procedureDeterministic: false,
procedureDataAccess: false,
procedureSql: false
procedureSql: false,
parametersLength: false
};
6 changes: 5 additions & 1 deletion src/common/customizations/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ module.exports = {
onUpdate: true,
viewAlgorithm: true,
viewSqlSecurity: true,
viewUpdateOption: true
viewUpdateOption: true,
procedureDeterministic: true,
procedureDataAccess: true,
procedureSql: 'BEGIN\r\n\r\nEND',
parametersLength: true
};
64 changes: 37 additions & 27 deletions src/main/libs/clients/PostgreSQLClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ export class PostgreSQLClient extends AntaresCore {
const sql = `SELECT pg_get_functiondef((SELECT oid FROM pg_proc WHERE proname = '${routine}'));`;
const results = await this.raw(sql);

return results.rows.map(row => {
return results.rows.map(async row => {
if (!row.pg_get_functiondef) {
return {
definer: null,
Expand All @@ -570,40 +570,47 @@ export class PostgreSQLClient extends AntaresCore {
};
}

const parameters = row.pg_get_functiondef
.match(/(\([^()]*(?:(?:\([^()]*\))[^()]*)*\)\s*)/s)[0]
.replaceAll('\r', '')
.replaceAll('\t', '')
.slice(1, -1)
.split(',')
.map(el => {
const param = el.split(' ');
const type = param[2] ? param[2].replace(')', '').split('(') : ['', null];
return {
name: param[1] ? param[1].replaceAll('`', '') : '',
type: type[0].replaceAll('\n', ''),
length: +type[1] ? +type[1].replace(/\D/g, '') : '',
context: param[0] ? param[0].replace('\n', '') : ''
};
}).filter(el => el.name);

let dataAccess = 'CONTAINS SQL';
if (row.pg_get_functiondef.includes('NO SQL'))
dataAccess = 'NO SQL';
if (row.pg_get_functiondef.includes('READS SQL DATA'))
dataAccess = 'READS SQL DATA';
if (row.pg_get_functiondef.includes('MODIFIES SQL DATA'))
dataAccess = 'MODIFIES SQL DATA';
const sql = `SELECT proc.specific_schema AS procedure_schema,
proc.specific_name,
proc.routine_name AS procedure_name,
proc.external_language,
args.parameter_name,
args.parameter_mode,
args.data_type
FROM information_schema.routines proc
LEFT JOIN information_schema.parameters args
ON proc.specific_schema = args.specific_schema
AND proc.specific_name = args.specific_name
WHERE proc.routine_schema not in ('pg_catalog', 'information_schema')
AND proc.routine_type = 'PROCEDURE'
AND proc.routine_name = '${routine}'
AND proc.specific_schema = '${schema}'
ORDER BY procedure_schema,
specific_name,
procedure_name,
args.ordinal_position
`;

const results = await this.raw(sql);

const parameters = results.rows.map(row => {
return {
name: row.parameter_name,
type: row.data_type.toUpperCase(),
length: '',
context: row.parameter_mode
};
});

return {
definer: '',
sql: row.pg_get_functiondef.match(/(\$(.*)\$)(.*)(\$(.*)\$)/gs)[0],
parameters: parameters || [],
name: routine,
comment: '',
security: row.pg_get_functiondef.includes('SECURITY INVOKER') ? 'INVOKER' : 'DEFINER',
security: row.pg_get_functiondef.includes('SECURITY DEFINER') ? 'DEFINER' : 'INVOKER',
deterministic: null,
dataAccess,
dataAccess: null,
language: row.pg_get_functiondef.match(/(?<=LANGUAGE )(.*)(?<=[\S+\n\r\s])/gm)[0]
};
})[0];
Expand Down Expand Up @@ -656,6 +663,9 @@ export class PostgreSQLClient extends AntaresCore {
}, []).join(',')
: '';

if (this._schema !== 'public')
this.use(this._schema);

const sql = `CREATE PROCEDURE ${this._schema}.${routine.name}(${parameters})
LANGUAGE SQL
SECURITY ${routine.security}
Expand Down
21 changes: 19 additions & 2 deletions src/renderer/components/ModalAskParameters.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
</template>

<script>
import { NUMBER, FLOAT } from 'common/fieldTypes';
import ConfirmModal from '@/components/BaseConfirmModal';
export default {
Expand All @@ -57,7 +58,8 @@ export default {
}
},
props: {
localRoutine: Object
localRoutine: Object,
client: String
},
data () {
return {
Expand All @@ -82,7 +84,22 @@ export default {
},
runRoutine () {
const valArr = Object.keys(this.values).reduce((acc, curr) => {
const value = isNaN(this.values[curr]) ? `"${this.values[curr]}"` : this.values[curr];
let qc;
switch (this.client) {
case 'maria':
case 'mysql':
qc = '"';
break;
case 'pg':
qc = '\'';
break;
default:
qc = '"';
}
const param = this.localRoutine.parameters.find(param => param.name === curr);
const value = [...NUMBER, ...FLOAT].includes(param.type) ? this.values[curr] : `${qc}${this.values[curr]}${qc}`;
acc.push(value);
return acc;
}, []);
Expand Down
15 changes: 10 additions & 5 deletions src/renderer/components/ModalNewRoutine.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
>
</div>
</div>
<div class="form-group">
<div v-if="customizations.definer" class="form-group">
<label class="form-label col-4">
{{ $t('word.definer') }}
</label>
Expand Down Expand Up @@ -53,7 +53,7 @@
</select>
</div>
</div>
<div class="form-group">
<div v-if="customizations.comment" class="form-group">
<label class="form-label col-4">
{{ $t('word.comment') }}
</label>
Expand All @@ -76,7 +76,7 @@
</select>
</div>
</div>
<div class="form-group">
<div v-if="customizations.comment" class="form-group">
<label class="form-label col-4">
{{ $t('message.dataAccess') }}
</label>
Expand All @@ -89,7 +89,7 @@
</select>
</div>
</div>
<div class="form-group">
<div v-if="customizations.procedureDeterministic" class="form-group">
<div class="col-4" />
<div class="column">
<label class="form-checkbox form-inline">
Expand Down Expand Up @@ -117,7 +117,7 @@ export default {
return {
localRoutine: {
definer: '',
sql: 'BEGIN\r\n\r\nEND',
sql: '',
parameters: [],
name: '',
comment: '',
Expand All @@ -131,9 +131,14 @@ export default {
computed: {
schema () {
return this.workspace.breadcrumbs.schema;
},
customizations () {
return this.workspace.customizations;
}
},
mounted () {
if (this.customizations.procedureSql)
this.localRoutine.sql = this.customizations.procedureSql;
setTimeout(() => {
this.$refs.firstInput.focus();
}, 20);
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/components/WorkspaceExploreBarMiscContext.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ModalAskParameters
v-if="isAskingParameters"
:local-routine="localElement"
:client="workspace.client"
@confirm="runElement"
@close="hideAskParamsModal"
/>
Expand Down Expand Up @@ -205,13 +206,13 @@ export default {
case 'maria':
case 'mysql':
case 'pg':
sql = `CALL \`${this.localElement.name}\` (${params.join(',')})`;
sql = `CALL ${this.localElement.name}(${params.join(',')})`;
break;
case 'mssql':
sql = `EXEC ${this.localElement.name} ${params.join(',')}`;
break;
default:
sql = `CALL \`${this.localElement.name}\` (${params.join(',')})`;
sql = `CALL \`${this.localElement.name}\`(${params.join(',')})`;
}
this.newTab({ uid: this.workspace.uid, content: sql, autorun: true });
Expand Down
11 changes: 7 additions & 4 deletions src/renderer/components/WorkspacePropsRoutineOptionsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
>
</div>
</div>
<div class="form-group">
<div v-if="customizations.definer" class="form-group">
<label class="form-label col-4">
{{ $t('word.definer') }}
</label>
Expand Down Expand Up @@ -54,7 +54,7 @@
</select>
</div>
</div>
<div class="form-group">
<div v-if="customizations.comment" class="form-group">
<label class="form-label col-4">
{{ $t('word.comment') }}
</label>
Expand All @@ -77,7 +77,7 @@
</select>
</div>
</div>
<div class="form-group">
<div v-if="customizations.procedureDataAccess" class="form-group">
<label class="form-label col-4">
{{ $t('message.dataAccess') }}
</label>
Expand All @@ -90,7 +90,7 @@
</select>
</div>
</div>
<div class="form-group">
<div v-if="customizations.procedureDeterministic" class="form-group">
<div class="col-4" />
<div class="column">
<label class="form-checkbox form-inline">
Expand Down Expand Up @@ -124,6 +124,9 @@ export default {
computed: {
isTableNameValid () {
return this.optionsProxy.name !== '';
},
customizations () {
return this.workspace.customizations;
}
},
created () {
Expand Down
11 changes: 7 additions & 4 deletions src/renderer/components/WorkspacePropsRoutineParamsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
</select>
</div>
</div>
<div class="form-group">
<div v-if="customizations.parametersLength" class="form-group">
<label class="form-label col-3">
{{ $t('word.length') }}
</label>
Expand Down Expand Up @@ -199,6 +199,9 @@ export default {
},
isChanged () {
return JSON.stringify(this.localParameters) !== JSON.stringify(this.parametersProxy);
},
customizations () {
return this.workspace.customizations;
}
},
mounted () {
Expand Down Expand Up @@ -235,10 +238,10 @@ export default {
addParameter () {
this.parametersProxy = [...this.parametersProxy, {
_id: uidGen(),
name: `Param${this.i++}`,
type: 'INT',
name: `param${this.i++}`,
type: this.workspace.dataTypes[0].types[0].name,
context: 'IN',
length: 10
length: ''
}];
if (this.parametersProxy.length === 1)
Expand Down
1 change: 1 addition & 0 deletions src/renderer/components/WorkspacePropsTabFunction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<ModalAskParameters
v-if="isAskingParameters"
:local-routine="localFunction"
:client="workspace.client"
@confirm="runFunction"
@close="hideAskParamsModal"
/>
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/components/WorkspacePropsTabRoutine.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<ModalAskParameters
v-if="isAskingParameters"
:local-routine="localRoutine"
:client="workspace.client"
@confirm="runRoutine"
@close="hideAskParamsModal"
/>
Expand Down Expand Up @@ -281,13 +282,13 @@ export default {
case 'maria':
case 'mysql':
case 'pg':
sql = `CALL \`${this.originalRoutine.name}\` (${params.join(',')})`;
sql = `CALL ${this.originalRoutine.name}(${params.join(',')})`;
break;
case 'mssql':
sql = `EXEC ${this.originalRoutine.name} ${params.join(',')}`;
break;
default:
sql = `CALL \`${this.originalRoutine.name}\` (${params.join(',')})`;
sql = `CALL \`${this.originalRoutine.name}\`(${params.join(',')})`;
}
this.newTab({ uid: this.connection.uid, content: sql, autorun: true });
Expand Down

0 comments on commit 3dde1c1

Please sign in to comment.