File: dlgEventTrigger.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (247 lines) | stat: -rw-r--r-- 8,098 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgEventTrigger.cpp - PostgreSQL Trigger Property
//
//////////////////////////////////////////////////////////////////////////

// wxWindows headers
#include <wx/wx.h>

// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "frm/frmMain.h"
#include "utils/pgDefs.h"

#include "dlg/dlgEventTrigger.h"
#include "schema/pgEventTrigger.h"
#include "ctl/ctlSeclabelPanel.h"

// pointer to controls
#define chkEnable		CTRL_CHECKBOX("chkEnable")
#define rdbEnableStatus CTRL_RADIOBOX("rdbEnableStatus")
#define cbFunction		CTRL_COMBOBOX2("cbFunction")
#define rdbEvents		CTRL_RADIOBOX("rdbEvents")
#define txtWhen			CTRL_TEXT("txtWhen")
#define cbOwner			CTRL_COMBOBOX2("cbOwner")

BEGIN_EVENT_TABLE(dlgEventTrigger, dlgProperty)
	EVT_CHECKBOX(XRCID("chkEnable"),				dlgEventTrigger::OnChangeEnable)
	EVT_RADIOBOX(XRCID("rdbEnableStatus"),			dlgProperty::OnChange)
	EVT_COMBOBOX(XRCID("cbFunction"),				dlgProperty::OnChange)
	EVT_RADIOBOX(XRCID("rdbEvents"),				dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtWhen"),						dlgProperty::OnChange)
END_EVENT_TABLE();

dlgEventTrigger::dlgEventTrigger(pgaFactory *factory, frmMain *frame, pgEventTrigger *node, pgObject *parent)
	: dlgProperty(factory, frame, wxT("dlgEventTrigger"))
{
	seclabelPage = new ctlSeclabelPanel(nbNotebook);
	eventTrigger = node;
}

dlgProperty *pgEventTriggerFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgEventTrigger(this, frame, (pgEventTrigger *)node, parent);
}

wxString dlgEventTrigger::GetSql()
{
	wxString sql = wxEmptyString;
	wxString name = GetName();

	if (eventTrigger)
	{
		if (!GetName().IsEmpty() && GetName() != eventTrigger->GetName())
			sql = wxT("ALTER EVENT TRIGGER ") + eventTrigger->GetQuotedFullIdentifier() + wxT("\nRENAME TO ") + qtIdent(GetName()) + wxT(";\n\n");

		if (!cbOwner->GetValue().IsEmpty() && cbOwner->GetValue() != eventTrigger->GetOwner())
			sql += wxT("ALTER EVENT TRIGGER ") + eventTrigger->GetQuotedFullIdentifier() + wxT("\nOWNER TO ") + cbOwner->GetValue() + wxT(";\n\n");

		if (rdbEnableStatus->GetSelection() != 0 && chkEnable->GetValue())
			sql += wxT("ALTER EVENT TRIGGER ") + qtIdent(name) + ((rdbEnableStatus->GetSelection() == 1) ? wxT(" ENABLE REPLICA ;\n\n") : wxT(" ENABLE ALWAYS ;\n\n"));
		else if (!chkEnable->GetValue())
			sql += wxT("ALTER EVENT TRIGGER ") + qtIdent(name) + wxT(" DISABLE ;\n\n");
	}

	if (!eventTrigger ||
	        (
	            cbFunction->GetValue() != (eventTrigger->GetFunction()) ||
	            rdbEvents->GetStringSelection().Lower() != (eventTrigger->GetEventName().Lower()) ||
	            txtWhen->GetValue() != (eventTrigger->GetWhen())
	        )
	   )
	{
		if (eventTrigger)
			sql  = wxT("DROP EVENT TRIGGER IF EXISTS ") + ((eventTrigger) ? qtIdent(eventTrigger->GetName()) : qtIdent(GetName())) + wxT(";\n\n");

		sql += wxT("CREATE EVENT TRIGGER ") + qtIdent(name) + wxT(" ON ");

		if (rdbEvents->GetSelection() == 0)
			sql += wxT(" DDL_COMMAND_START ");
		else if (rdbEvents->GetSelection() == 1)
			sql += wxT(" DDL_COMMAND_END ");
		else
			sql += wxT(" SQL_DROP ");

		if (!txtWhen->IsEmpty())
			sql += wxT("\nWHEN TAG IN (") + txtWhen->GetValue() + wxT(")");

		if (!cbFunction->GetValue().IsEmpty())
			sql += wxT("\nEXECUTE PROCEDURE ") + (cbFunction->GetValue()) + wxT("();\n\n");

		if (rdbEnableStatus->GetSelection() != 0 && chkEnable->GetValue())
			sql += wxT("ALTER EVENT TRIGGER ") + qtIdent(name) + ((rdbEnableStatus->GetSelection() == 1) ? wxT(" ENABLE REPLICA ;\n\n") : wxT(" ENABLE ALWAYS ;\n\n"));
		else if (!chkEnable->GetValue())
			sql += wxT("ALTER EVENT TRIGGER ") + qtIdent(name) + wxT(" DISABLE ;\n\n");

		if (!eventTrigger && !cbOwner->GetValue().IsEmpty())
			sql += wxT("ALTER EVENT TRIGGER ") + qtIdent(GetName()) + wxT("\nOWNER TO ") + cbOwner->GetValue() + wxT(";\n\n");
	}

	AppendComment(sql, wxT("EVENT TRIGGER ") + qtIdent(GetName()), eventTrigger);

	if (seclabelPage)
		sql += seclabelPage->GetSqlForSecLabels(wxT("EVENT TRIGGER"), qtIdent(name));

	return sql;
}

pgObject *dlgEventTrigger::CreateObject(pgCollection *pgcol)
{
	pgObject *obj = eventTriggerFactory.CreateObjects(pgcol, 0,
	                wxT(" \n AND e.evtname = ") + qtDbString(GetName()));
	return obj;
}

pgObject *dlgEventTrigger::GetObject()
{
	return eventTrigger;
}

int dlgEventTrigger::Go(bool modal)
{
	seclabelPage->SetConnection(connection);
	seclabelPage->SetObject(eventTrigger);
	this->Connect(EVT_SECLABELPANEL_CHANGE, wxCommandEventHandler(dlgEventTrigger::OnChange));

	if (eventTrigger)
	{
		// Edit mode
		chkEnable->SetValue(eventTrigger->GetEnabled());

		if (eventTrigger->GetEnableStatus() == wxT("enabled"))
			rdbEnableStatus->SetSelection(0);
		else if(eventTrigger->GetEnableStatus() == wxT("replica"))
			rdbEnableStatus->SetSelection(1);
		else if(eventTrigger->GetEnableStatus() == wxT("always"))
			rdbEnableStatus->SetSelection(2);
		else
			rdbEnableStatus->Disable();

		if(eventTrigger->GetEventName().Lower() == wxT("ddl command start"))
			rdbEvents->SetSelection(0);
		else if(eventTrigger->GetEventName().Lower() == wxT("ddl command end"))
			rdbEvents->SetSelection(1);
		else
			rdbEvents->SetSelection(2);

		cbFunction->SetValue(eventTrigger->GetFunction());
		cbOwner->SetValue(eventTrigger->GetOwner());
		(!eventTrigger->GetWhen().IsEmpty()) ? txtWhen->SetValue(eventTrigger->GetWhen()) : txtWhen->SetValue(wxEmptyString);
	}
	else
	{
		// Create mode
		chkEnable->SetValue(true);
		rdbEnableStatus->Disable();
	}

	pgSet *funcSet = connection->ExecuteSet(
	                     wxT("SELECT quote_ident(nspname) || '.' || quote_ident(proname)\n")
	                     wxT(" FROM pg_proc p, pg_namespace n, pg_language l\n")
	                     wxT(" WHERE p.pronamespace = n.oid AND p.prolang = l.oid AND p.pronargs = 0 AND l.lanname != 'sql' AND prorettype::regtype::text = 'event_trigger'\n")
	                     wxT(" ORDER BY nspname ASC, proname ASC "));
	if (funcSet)
	{
		while (!funcSet->Eof())
		{
			cbFunction->Append(funcSet->GetVal(0));
			funcSet->MoveNext();
		}
		delete funcSet;
	}

	pgSet *userSet = connection->ExecuteSet(
	                     wxT("SELECT usename ")
	                     wxT("FROM	pg_user ")
	                     wxT("WHERE  usesuper IS TRUE"));
	if (userSet)
	{
		while (!userSet->Eof())
		{
			cbOwner->Append(userSet->GetVal(0));
			userSet->MoveNext();
		}
		delete userSet;
	}

	return  dlgProperty::Go(modal);
}

void dlgEventTrigger::CheckChange()
{
	bool enable = true;

	wxString function = cbFunction->GetValue();
	wxString name = GetName();
	wxString owner = cbOwner->GetValue();

	(chkEnable->GetValue()) ? rdbEnableStatus->Enable() : rdbEnableStatus->Disable();

	CheckValid(enable, !name.IsEmpty(), _("Please specify event trigger name."));
	CheckValid(enable, !owner.IsEmpty(), _("Please specify owner of event trigger."));
	CheckValid(enable, !function.IsEmpty(), _("Please specify event trigger function."));

	if (eventTrigger)
	{
		EnableOK(enable &&
		         (txtComment->GetValue() != eventTrigger->GetComment() ||
		          txtName->GetValue() != eventTrigger->GetName() ||
		          txtWhen->GetValue() != eventTrigger->GetWhen() ||
		          chkEnable->GetValue() != eventTrigger->GetEnabled() ||
		          rdbEvents->GetStringSelection().Lower() != eventTrigger->GetEventName().Lower() ||
		          rdbEnableStatus->GetStringSelection().Lower() != eventTrigger->GetEnableStatus().Lower() ||
		          !function.IsEmpty() ||
		          !owner.IsEmpty()
		         )
		        );
	}
	else
	{
		EnableOK(enable);
	}
}

bool dlgEventTrigger::IsUpToDate()
{
	if (eventTrigger && !eventTrigger->IsUpToDate())
		return false;
	else
		return true;
}

void dlgEventTrigger::OnChange(wxCommandEvent &ev)
{
	CheckChange();
}

void dlgEventTrigger::OnChangeEnable(wxCommandEvent &ev)
{
	CheckChange();
}