File: dlgTrigger.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 (551 lines) | stat: -rw-r--r-- 15,866 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgTrigger.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/dlgTrigger.h"
#include "schema/pgTrigger.h"
#include "schema/pgTable.h"
#include "schema/pgSchema.h"


// pointer to controls
#define chkRow          CTRL_CHECKBOX("chkRow")
#define chkConstraint   CTRL_CHECKBOX("chkConstraint")
#define chkDeferrable   CTRL_CHECKBOX("chkDeferrable")
#define chkDeferred     CTRL_CHECKBOX("chkDeferred")
#define cbFunction      CTRL_COMBOBOX2("cbFunction")
#define txtArguments    CTRL_TEXT("txtArguments")
#define rdbFires        CTRL_RADIOBOX("rdbFires")
#define chkInsert       CTRL_CHECKBOX("chkInsert")
#define chkUpdate       CTRL_CHECKBOX("chkUpdate")
#define chkDelete       CTRL_CHECKBOX("chkDelete")
#define chkTruncate     CTRL_CHECKBOX("chkTruncate")
#define txtWhen         CTRL_TEXT("txtWhen")
#define txtBody         CTRL_SQLBOX("txtBody")
#define btnAddCol       CTRL_BUTTON("btnAddCol")
#define btnRemoveCol    CTRL_BUTTON("btnRemoveCol")
#define lstColumns      CTRL_LISTVIEW("lstColumns")


BEGIN_EVENT_TABLE(dlgTrigger, dlgCollistProperty)
	EVT_RADIOBOX(XRCID("rdbFires"),                 dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkConstraint"),            dlgTrigger::OnChangeConstraint)
	EVT_CHECKBOX(XRCID("chkDeferrable"),            dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkRow"),                   dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkInsert"),                dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkUpdate"),                dlgTrigger::OnChange)
	EVT_CHECKBOX(XRCID("chkDelete"),                dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkTruncate"),              dlgProperty::OnChange)
	EVT_TEXT(XRCID("cbFunction"),                   dlgTrigger::OnChangeFunc)
	EVT_COMBOBOX(XRCID("cbFunction"),               dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtArguments"),                 dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtWhen"),                      dlgProperty::OnChange)
	EVT_STC_MODIFIED(XRCID("txtBody"),              dlgProperty::OnChangeStc)
	EVT_LIST_ITEM_SELECTED(XRCID("lstColumns"),     dlgTrigger::OnSelectListCol)
	EVT_COMBOBOX(XRCID("cbColumns"),                dlgTrigger::OnSelectComboCol)
	EVT_BUTTON(XRCID("btnAddCol"),                  dlgTrigger::OnAddCol)
	EVT_BUTTON(XRCID("btnRemoveCol"),               dlgTrigger::OnRemoveCol)
END_EVENT_TABLE();


dlgProperty *pgTriggerFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgTrigger(this, frame, (pgTrigger *)node, (pgTable *)parent);
}


dlgTrigger::dlgTrigger(pgaFactory *f, frmMain *frame, pgTrigger *node, pgTable *parentNode)
	: dlgCollistProperty(f, frame, wxT("dlgTrigger"), parentNode)
{
	trigger = node;
	table = parentNode;
	wxASSERT(!table || table->GetMetaType() == PGM_TABLE || table->GetMetaType() == PGM_VIEW
	         || table->GetMetaType() == GP_PARTITION);

	bool bVal;
	settings->Read(wxT("frmQuery/ShowLineNumber"), &bVal, false);
	if (!bVal)
	{
		txtBody->SetMarginType(1, wxSTC_MARGIN_NUMBER);
		txtBody->SetMarginWidth(1, ConvertDialogToPixels(wxPoint(16, 0)).x);
	}

	lstColumns->AddColumn(_("Column name"));
}


pgObject *dlgTrigger::GetObject()
{
	return trigger;
}


wxString dlgTrigger::GetColumns()
{
	wxString sql;

	int pos;
	// iterate cols
	for (pos = 0 ; pos < lstColumns->GetItemCount() ; pos++)
	{
		if (pos)
			sql += wxT(", ");

		sql += qtIdent(lstColumns->GetItemText(pos));
	}
	return sql;
}


int dlgTrigger::Go(bool modal)
{
	if (trigger)
	{
		// edit mode
		chkRow->SetValue((trigger->GetTriggerType() & TRIGGER_TYPE_ROW) != 0);
		chkInsert->SetValue((trigger->GetTriggerType() & TRIGGER_TYPE_INSERT) != 0);
		chkUpdate->SetValue((trigger->GetTriggerType() & TRIGGER_TYPE_UPDATE) != 0);
		chkDelete->SetValue((trigger->GetTriggerType() & TRIGGER_TYPE_DELETE) != 0);
		chkTruncate->SetValue((trigger->GetTriggerType() & TRIGGER_TYPE_TRUNCATE) != 0);
		if (trigger->GetTriggerType() & TRIGGER_TYPE_BEFORE)
			rdbFires->SetSelection(0);
		else if (trigger->GetTriggerType() & TRIGGER_TYPE_INSTEAD)
			rdbFires->SetSelection(2);
		else
			rdbFires->SetSelection(1);
		txtArguments->SetValue(trigger->GetArguments());
		txtWhen->SetValue(trigger->GetWhen());
		if (!connection->BackendMinimumVersion(7, 4))
			txtName->Disable();

		if (trigger->GetLanguage() == wxT("edbspl"))
		{
			cbFunction->Append(wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")));
			txtBody->SetText(trigger->GetSource());
		}
		else
		{
			cbFunction->Append(trigger->GetFunction());
			txtBody->Disable();
		}

		cbFunction->SetSelection(0);
		txtArguments->Disable();
		cbFunction->Disable();

		if (!connection->EdbMinimumVersion(8, 0))
		{
			chkRow->Disable();
			rdbFires->Disable();
			chkInsert->Disable();
			chkUpdate->Disable();
			chkDelete->Disable();
			chkTruncate->Disable();
		}
		else if (!connection->BackendMinimumVersion(8, 4))
			chkTruncate->Disable();
		else if (!connection->BackendMinimumVersion(8, 5))
			txtWhen->Disable();

		wxArrayString colsArr = trigger->GetColumnList();
		for (int colIdx = 0, colsCount = colsArr.Count(); colIdx < colsCount; colIdx++)
		{
			lstColumns->InsertItem(colIdx, colsArr.Item(colIdx));
		}

		if (connection->BackendMinimumVersion(8, 2))
		{
			chkConstraint->SetValue(trigger->GetIsConstraint());
			chkDeferrable->SetValue(trigger->GetDeferrable());
			chkDeferred->SetValue(trigger->GetDeferred());
		}
		chkConstraint->Enable(false);
		chkDeferrable->Enable(false);
		chkDeferred->Enable(false);
	}
	else
	{
		// create mode
		if (connection->EdbMinimumVersion(8, 0))
			cbFunction->Append(wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")));

		wxString sysRestr;
		if (!settings->GetShowSystemObjects())
			sysRestr = wxT("   AND ") + connection->SystemNamespaceRestriction(wxT("nspname"));

		pgSet *set = 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 l.lanname != 'edbspl' AND prorettype=") + NumToStr(PGOID_TYPE_TRIGGER) + sysRestr +
		                 wxT(" ORDER BY nspname ASC, proname ASC "));
		if (set)
		{
			while (!set->Eof())
			{
				cbFunction->Append(set->GetVal(0));
				set->MoveNext();
			}
			delete set;
		}
		if (!connection->BackendMinimumVersion(7, 4))
		{
			chkRow->SetValue(true);
			chkRow->Disable();
		}

		txtBody->Disable();

		if (!connection->BackendMinimumVersion(8, 4))
			chkTruncate->Disable();

		if (!connection->BackendMinimumVersion(9, 1) || table->GetMetaType() != PGM_VIEW)
			rdbFires->Enable(2, false);

		chkConstraint->Enable(connection->BackendMinimumVersion(8, 2));
		chkDeferrable->Disable();
		chkDeferred->Disable();
	}

	cbColumns->Disable();
	btnAddCol->Disable();
	btnRemoveCol->Disable();

	// Reset the radio box rbxEvent item values with the keywords to resolve the locale issues.
	rdbFires->SetString(0, wxT("BEFORE"));
	rdbFires->SetString(1, wxT("AFTER"));
	rdbFires->SetString(2, wxT("INSTEAD OF"));

	chkTruncate->SetLabel(wxT("TRUNCATE"));
	chkDelete->SetLabel(wxT("DELETE"));
	chkInsert->SetLabel(wxT("INSERT"));
	chkUpdate->SetLabel(wxT("UPDATE"));

	return dlgCollistProperty::Go(modal);
}


void dlgTrigger::OnAddCol(wxCommandEvent &ev)
{
	wxString colName = cbColumns->GetValue();

	if (!colName.IsEmpty())
	{
		lstColumns->InsertItem(lstColumns->GetItemCount(), colName);

		cbColumns->Delete(cbColumns->GetCurrentSelection());
		if (cbColumns->GetCount())
			cbColumns->SetSelection(0);

		CheckChange();
		if (!cbColumns->GetCount())
			btnAddCol->Disable();
	}
}


void dlgTrigger::OnRemoveCol(wxCommandEvent &ev)
{
	long pos = lstColumns->GetSelection();
	if (pos >= 0)
	{
		wxString colName = lstColumns->GetItemText(pos);

		lstColumns->DeleteItem(pos);
		cbColumns->Append(colName);

		CheckChange();
		btnRemoveCol->Disable();
	}
}

wxString dlgTrigger::GetSql()
{
	wxString sql;
	wxString name = GetName();

	if (trigger)
	{
		if (name != trigger->GetName())
			sql = wxT("ALTER TRIGGER ") + trigger->GetQuotedIdentifier() + wxT(" ON ") + table->GetQuotedFullIdentifier()
			      + wxT("\n  RENAME TO ") + qtIdent(name) + wxT(";\n\n");
	}

	if (!trigger ||
	        (cbFunction->GetValue() == wxString::Format(wxT("<%s>"), _("Inline EDB-SPL"))
	         && (
	             txtBody->GetText() != trigger->GetSource() ||
	             chkRow->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_ROW) ||
	             chkInsert->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_INSERT ? true : false) ||
	             chkUpdate->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_UPDATE ? true : false) ||
	             chkDelete->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_DELETE ? true : false) ||
	             chkTruncate->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_TRUNCATE ? true : false) ||
	             rdbFires->GetSelection() != (trigger->GetTriggerType() & TRIGGER_TYPE_BEFORE ? 0 : TRIGGER_TYPE_INSTEAD ? 2 : 1)
	         )
	        )
	   )
	{
		if (cbFunction->GetValue() == wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")))
			sql += wxT("CREATE OR REPLACE TRIGGER ");
		else if (chkConstraint->GetValue())
			sql += wxT("CREATE CONSTRAINT TRIGGER ");
		else
			sql += wxT("CREATE TRIGGER ");
		sql += qtIdent(name);

		if (rdbFires->GetSelection() == 1)
			sql += wxT(" AFTER");
		else if (rdbFires->GetSelection() == 2)
			sql += wxT(" INSTEAD OF");
		else
			sql += wxT(" BEFORE");
		int actionCount = 0;
		if (chkInsert->GetValue())
		{
			if (actionCount++)
				sql += wxT(" OR");
			sql += wxT(" INSERT");
		}
		if (chkUpdate->GetValue())
		{
			if (actionCount++)
				sql += wxT(" OR");
			sql += wxT(" UPDATE");
			if (lstColumns->GetItemCount() > 0)
				sql += wxT(" OF ") + GetColumns();
		}
		if (chkDelete->GetValue())
		{
			if (actionCount++)
				sql += wxT(" OR");
			sql += wxT(" DELETE");
		}
		if (chkTruncate->GetValue())
		{
			if (actionCount++)
				sql += wxT(" OR");
			sql += wxT(" TRUNCATE");
		}
		sql += wxT("\n   ON ") + table->GetQuotedFullIdentifier();
		if (chkDeferrable->GetValue())
		{
			sql += wxT(" DEFERRABLE");
			if (chkDeferred->GetValue())
				sql += wxT(" INITIALLY DEFERRED");
		}
		sql += wxT(" FOR EACH ");
		if (chkRow->GetValue())
			sql += wxT("ROW");
		else
			sql += wxT("STATEMENT");

		if (connection->BackendMinimumVersion(8, 5) && !txtWhen->GetValue().IsEmpty())
			sql += wxT("\n   WHEN (") + txtWhen->GetValue() + wxT(")");

		if (cbFunction->GetValue() != wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")))
		{
			sql += wxT("\n   EXECUTE PROCEDURE ") + cbFunction->GetValue()
			       + wxT("(") + txtArguments->GetValue()
			       + wxT(");\n");
		}
		else
		{
			sql += wxT("\n") + txtBody->GetText();
			if (!sql.Trim().EndsWith(wxT(";")))
				sql = sql.Trim() + wxT(";");
			sql += wxT("\n");
		}
	}
	AppendComment(sql, wxT("TRIGGER ") + qtIdent(GetName())
	              + wxT(" ON ") + table->GetQuotedFullIdentifier(), trigger);

	return sql;
}


pgObject *dlgTrigger::CreateObject(pgCollection *collection)
{
	pgObject *obj = triggerFactory.CreateObjects(collection, 0,
	                wxT("\n   AND tgname=") + qtDbString(GetName()) +
	                wxT("\n   AND tgrelid=") + table->GetOidStr() +
	                wxT("\n   AND relnamespace=") + table->GetSchema()->GetOidStr());
	return obj;
}


void dlgTrigger::OnChange(wxCommandEvent &ev)
{
	if (chkUpdate->GetValue())
	{
		cbColumns->Enable();
	}
	else
	{
		if (lstColumns->GetItemCount() > 0)
		{
			if (wxMessageBox(_("Removing the UPDATE event will cause the column list to be cleared. Do you wish to continue?"), _("Remove UPDATE event?"), wxYES_NO) != wxYES)
			{
				chkUpdate->SetValue(true);
				return;
			}

			// Move all the columns back to the combo
			for (int pos = lstColumns->GetItemCount(); pos > 0; pos--)
			{
				wxString colName = lstColumns->GetItemText(pos - 1);

				lstColumns->DeleteItem(pos - 1);
				cbColumns->Append(colName);
			}
		}

		cbColumns->Disable();
		btnAddCol->Disable();
		btnRemoveCol->Disable();
	}

	CheckChange();
}


void dlgTrigger::OnChangeFunc(wxCommandEvent &ev)
{
	cbFunction->GuessSelection(ev);

	if (cbFunction->GetValue() == wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")))
	{
		txtArguments->Disable();
		txtBody->Enable();
	}
	else
	{
		txtArguments->Enable();
		txtBody->Disable();
	}

	CheckChange();
}


void dlgTrigger::OnChangeConstraint(wxCommandEvent &ev)
{
	if (chkConstraint->GetValue())
	{
		rdbFires->SetSelection(1);
		rdbFires->Disable();
		chkRow->SetValue(true);
		chkRow->Disable();
		chkDeferrable->Enable();
		chkDeferred->Enable();
	}
	else
	{
		rdbFires->Enable();
		chkRow->Enable();
		chkDeferrable->Disable();
		chkDeferred->Disable();
	}

	CheckChange();
}


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

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

	// We can only have per-statement TRUNCATE triggers
	if (trigger || connection->BackendMinimumVersion(8, 4))
	{
		if (chkRow->GetValue())
		{
			chkTruncate->Disable();
			chkTruncate->SetValue(false);
		}
		else if (connection->EdbMinimumVersion(8, 0))
			chkTruncate->Enable();
	}

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

	CheckValid(enable, chkInsert->GetValue() || chkUpdate->GetValue() || chkDelete->GetValue() || chkTruncate->GetValue(),
	           _("Please specify at least one action."));

	if (cbFunction->GetValue() == wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")))
		CheckValid(enable, !txtBody->GetText().IsEmpty(), _("Please specify trigger body."));

	if (trigger)
	{
		EnableOK(enable &&
		         (txtComment->GetValue() != trigger->GetComment() ||
		          txtName->GetValue() != trigger->GetName() ||
		          (txtBody->GetText() != trigger->GetSource() && cbFunction->GetValue() == wxString::Format(wxT("<%s>"), _("Inline EDB-SPL"))) ||
		          txtWhen->GetValue() != trigger->GetWhen() ||
		          chkRow->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_ROW ? true : false) ||
		          chkInsert->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_INSERT ? true : false) ||
		          chkUpdate->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_UPDATE ? true : false) ||
		          chkDelete->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_DELETE ? true : false) ||
		          chkTruncate->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_TRUNCATE ? true : false) ||
		          rdbFires->GetSelection() != (trigger->GetTriggerType() & TRIGGER_TYPE_BEFORE ? 0 : (trigger->GetTriggerType() & TRIGGER_TYPE_INSTEAD ? 2 : 1))));
	}
	else
	{
		EnableOK(enable);
	}
}

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

void dlgTrigger::OnSelectListCol(wxListEvent &ev)
{
	OnSelectCol();
}

void dlgTrigger::OnSelectComboCol(wxCommandEvent &ev)
{
	OnSelectCol();
}

void dlgTrigger::OnSelectCol()
{
	// Can't change the columns on an existing index.
	if (trigger)
		return;

	if (lstColumns->GetSelection() != wxNOT_FOUND)
		btnRemoveCol->Enable(true);
	else
		btnRemoveCol->Enable(false);

	if (cbColumns->GetSelection() != wxNOT_FOUND && !cbColumns->GetValue().IsEmpty())
		btnAddCol->Enable(true);
	else
		btnAddCol->Enable(false);
}