Trigger issues through right click. Fixes #1209

This commit is contained in:
Akshay Joshi 2016-08-24 12:10:54 +05:30
parent 8d7b840e7e
commit e016869ad5
6 changed files with 123 additions and 34 deletions

View File

@ -1,6 +1,10 @@
{### Set a flag which allows us to put OR between events ###}
{% set or_flag = False %}
{% if data.lanname == 'edbspl' or data.tfunction == 'Inline EDB-SPL' %}
CREATE OR REPLACE TRIGGER {{ conn|qtIdent(data.name) }}
{% else %}
CREATE{% if data.is_constraint_trigger %} CONSTRAINT{% endif %} TRIGGER {{ conn|qtIdent(data.name) }}
{% endif %}
{{data.fires}} {% if data.evnt_insert %}INSERT{% set or_flag = True %}
{% endif %}{% if data.evnt_delete %}
{% if or_flag %} OR {% endif %}DELETE{% set or_flag = True %}
@ -19,7 +23,8 @@ CREATE{% if data.is_constraint_trigger %} CONSTRAINT{% endif %} TRIGGER {{ conn|
WHEN {{ data.whenclause }}{% endif %}
{% if data.code %}{{ data.code }}{% else %}EXECUTE PROCEDURE {{ data.tfunction }}{% if data.tgargs %}({{ data.tgargs }}){% else %}(){% endif%}{% endif%};
{% if data.prosrc is defined and
(data.lanname == 'edbspl' or data.tfunction == 'Inline EDB-SPL') %}{{ data.prosrc }}{% else %}EXECUTE PROCEDURE {{ data.tfunction }}{% if data.tgargs %}({{ data.tgargs }}){% else %}(){% endif%}{% endif%};
{% if data.description %}
COMMENT ON TRIGGER {{ conn|qtIdent(data.name) }} ON {{ conn|qtIdent(data.schema, data.table) }}

View File

@ -8,4 +8,8 @@ FROM pg_proc p, pg_namespace n, pg_language l
{% if not show_system_objects %}
AND (nspname NOT LIKE E'pg\_%' AND nspname NOT in ('information_schema'))
{% endif %}
-- Find function for specific OID
{% if tgfoid %}
AND p.oid = {{tgfoid}}::OID
{% endif %}
ORDER BY nspname ASC, proname ASC

View File

@ -1,8 +1,37 @@
{% if data.name and o_data.name != data.name %}
ALTER TRIGGER {{ conn|qtIdent(o_data.name) }} ON {{ conn|qtIdent(o_data.nspname, o_data.relname) }}
RENAME TO {{ conn|qtIdent(data.name) }};
{% endif %}
{% if data.prosrc is defined and o_data.lanname == 'edbspl' and o_data.prosrc != data.prosrc %}
{% set or_flag = False %}
CREATE OR REPLACE TRIGGER {{ conn|qtIdent(data.name) }}
{{o_data.fires}} {% if o_data.evnt_insert %}INSERT{% set or_flag = True %}
{% endif %}{% if o_data.evnt_delete %}
{% if or_flag %} OR {% endif %}DELETE{% set or_flag = True %}
{% endif %}{% if o_data.evnt_turncate %}
{% if or_flag %} OR {% endif %}TRUNCATE{% set or_flag = True %}
{% endif %}{% if o_data.evnt_update %}
{% if or_flag %} OR {% endif %}UPDATE {% if o_data.columns|length > 0 %}OF {% for c in o_data.columns %}{% if loop.index != 1 %}, {% endif %}{{ conn|qtIdent(c.column) }}{% endfor %}{% endif %}
{% endif %}
ON {{ conn|qtIdent(data.schema, data.table) }}
{% if o_data.tgdeferrable %}
DEFERRABLE{% if o_data.tginitdeferred %} INITIALLY DEFERRED{% endif %}
{% endif %}
FOR EACH{% if o_data.is_row_trigger %} ROW{% else %} STATEMENT{% endif %}
{% if o_data.whenclause %}
WHEN {{ o_data.whenclause }}
{% endif %}
{{ data.prosrc }};
{% if data.description is not defined and o_data.description %}
COMMENT ON TRIGGER {{ conn|qtIdent(data.name) }} ON {{ conn|qtIdent(o_data.nspname, o_data.relname) }}
IS {{o_data.description|qtLiteral}};
{% endif %}
{% endif %}
{% if data.description is defined and o_data.description != data.description %}
COMMENT ON TRIGGER {{ conn|qtIdent(data.name) }} ON {{ conn|qtIdent(o_data.nspname, o_data.relname) }}
IS {{data.description|qtLiteral}};
{% endif %}
{% endif %}

View File

@ -508,6 +508,26 @@ class TriggerView(PGChildNodeView):
# Making copy of output for future use
data = dict(res['rows'][0])
# If language is 'edbspl' then trigger function should be 'Inline EDB-SPL'
# else we will find the trigger function with schema name.
if data['lanname'] == 'edbspl':
data['tfunction'] = 'Inline EDB-SPL'
else:
SQL = render_template("/".join([self.template_path,
'get_triggerfunctions.sql']),
tgfoid=data['tgfoid'],
show_system_objects=self.blueprint.show_system_objects)
status, result = self.conn.execute_dict(SQL)
if not status:
return internal_server_error(errormsg=res)
# Update the trigger function which we have fetched with schema name
if 'rows' in result and len(result['rows']) > 0 and \
'tfunctions' in result['rows'][0]:
data['tfunction'] = result['rows'][0]['tfunctions']
if data['tgnargs'] > 1:
# We know that trigger has more than 1 arguments, let's join them
# and convert it as string
@ -673,21 +693,43 @@ class TriggerView(PGChildNodeView):
)
try:
data['schema'] = self.schema
data['table'] = self.table
SQL = self.get_sql(scid, tid, trid, data)
if SQL and SQL.strip('\n') and SQL.strip(' '):
status, res = self.conn.execute_scalar(SQL)
if not status:
return internal_server_error(errormsg=res)
return make_json_response(
success=1,
info="Trigger updated",
data={
'id': trid,
'tid': tid,
'scid': scid
}
)
if hasattr(self, 'lanname') and self.lanname == 'edbspl' \
and 'prosrc' in data:
data['name'] = self.trigger_name
SQL = render_template("/".join([self.template_path,
'get_oid.sql']),
tid=tid, data=data)
status, trid = self.conn.execute_scalar(SQL)
if not status:
return internal_server_error(errormsg=tid)
return jsonify(
node=self.blueprint.generate_browser_node(
trid,
scid,
data['name'],
icon="icon-trigger"
)
)
else:
return make_json_response(
success=1,
info="Trigger updated",
data={
'id': trid,
'tid': tid,
'scid': scid
}
)
else:
return make_json_response(
success=1,
@ -758,6 +800,9 @@ class TriggerView(PGChildNodeView):
if 'name' not in data:
data['name'] = old_data['name']
self.trigger_name = data['name']
self.lanname = old_data['lanname']
if old_data['tgnargs'] > 1:
# We know that trigger has more than 1 arguments, let's join them
old_data['tgargs'] = ', '.join(old_data['tgargs'])

View File

@ -41,6 +41,7 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
label: '{{ _('Trigger') }}',
hasSQL: true,
hasDepends: true,
width: '650px',
sqlAlterHelp: 'sql-altertrigger.html',
sqlCreateHelp: 'sql-createtrigger.html',
dialogHelp: '{{ url_for('help.static', filename='trigger_dialog.html') }}',
@ -328,7 +329,7 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
if(!m.inSchemaWithModelCheck.apply(this, [m])) {
if(!_.isUndefined(is_constraint_trigger) &&
is_constraint_trigger === true) {
setTimeout(function() { m.set('fires', 'AFTER', {silent: true}) }, 10);
setTimeout(function() { m.set('fires', 'AFTER') }, 10);
return true;
} else {
return false;
@ -419,7 +420,7 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
return true;
}
},{
id: 'code', label:'{{ _('Code') }}', group: '{{ _('Code') }}',
id: 'prosrc', label:'{{ _('Code') }}', group: '{{ _('Code') }}',
type: 'text', mode: ['create', 'edit'], deps: ['tfunction'],
control: 'sql-field', visible: true,
disabled: function(m) {
@ -427,19 +428,13 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
// set to Inline EDB-SPL
var tfunction = m.get('tfunction'),
server_type = m.node_info['server']['server_type'];
if(!m.inSchemaWithModelCheck.apply(this, [m])) {
if(server_type === 'ppas' &&
!_.isUndefined(tfunction) &&
tfunction === 'Inline EDB-SPL') {
return false;
// Also clear and disable Argument field
} else {
return true;
}
} else {
// Disable it
return true;
}
if(server_type === 'ppas' &&
!_.isUndefined(tfunction) &&
tfunction === 'Inline EDB-SPL')
return false;
else
return true;
}
},{
id: 'is_sys_trigger', label:'{{ _('System trigger?') }}', cell: 'string',
@ -476,13 +471,24 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
return msg;
}
if(!this.get('evnt_turncate') && !this.get('evnt_delete') && !this.get('evnt_update') && !this.get('evnt_insert')) {
msg = '{{ _('Specify atleast one event.') }}';
this.errorModel.set('evnt_turncate', " ");
this.errorModel.set('evnt_delete', " ");
this.errorModel.set('evnt_update', " ");
this.errorModel.set('evnt_insert', msg);
return msg;
if(!this.get('evnt_turncate') && !this.get('evnt_delete') &&
!this.get('evnt_update') && !this.get('evnt_insert')) {
msg = '{{ _('Specify atleast one event.') }}';
this.errorModel.set('evnt_turncate', " ");
this.errorModel.set('evnt_delete', " ");
this.errorModel.set('evnt_update', " ");
this.errorModel.set('evnt_insert', msg);
return msg;
}
if(!_.isUndefined(this.get('tfunction')) &&
this.get('tfunction') === 'Inline EDB-SPL' &&
(_.isUndefined(this.get('prosrc'))
|| String(this.get('prosrc')).replace(/^\s+|\s+$/g, '') == ''))
{
msg = '{{ _('Trigger code can not be empty.') }}';
this.errorModel.set('prosrc', msg);
return msg;
}
return null;
},

View File

@ -767,7 +767,7 @@ fieldset.inline-fieldset-without-border {
fieldset.inline-fieldset > legend {
display: block; border: 0px solid black; box-shadow: none;
box-sizing: content-box; top: auto; bottom: auto; left: auto;
font-size: 16px; font-style: normal; font-weight: 600;
font-size: 14px; font-style: normal; font-weight: 600;
height: 20px; line-height: 20px; margin: 0px; padding-bottom: 0px;
padding-left: 5px; padding-right: 5px; padding-top: 0px;
vertical-align: middle; width: auto;