This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
/
dlgTrigger.cpp
327 lines (276 loc) · 10.6 KB
/
dlgTrigger.cpp
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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
// RCS-ID: $Id$
// Copyright (C) 2002 - 2009, The pgAdmin Development Team
// This software is released under the BSD 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 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 txtBody CTRL_SQLBOX("txtBody")
BEGIN_EVENT_TABLE(dlgTrigger, dlgProperty)
EVT_RADIOBOX(XRCID("rdbFires"), dlgProperty::OnChange)
EVT_CHECKBOX(XRCID("chkRow"), dlgProperty::OnChange)
EVT_CHECKBOX(XRCID("chkInsert"), dlgProperty::OnChange)
EVT_CHECKBOX(XRCID("chkUpdate"), dlgProperty::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_STC_MODIFIED(XRCID("txtBody"), dlgProperty::OnChangeStc)
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)
: dlgProperty(f, frame, wxT("dlgTrigger"))
{
trigger=node;
table=parentNode;
wxASSERT(!table || table->GetMetaType() == PGM_TABLE || table->GetMetaType() == GP_PARTITION);
txtBody->SetMarginType(1, wxSTC_MARGIN_NUMBER);
txtBody->SetMarginWidth(1, ConvertDialogToPixels(wxPoint(16, 0)).x);
}
pgObject *dlgTrigger::GetObject()
{
return trigger;
}
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);
rdbFires->SetSelection(trigger->GetTriggerType() & TRIGGER_TYPE_BEFORE ? 0 : 1);
txtArguments->SetValue(trigger->GetArguments());
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
{
// 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();
}
return dlgProperty::Go(modal);
}
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(" 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 : 1))
{
if (cbFunction->GetValue() == wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")))
sql += wxT("CREATE OR REPLACE TRIGGER ") + qtIdent(name);
else
sql += wxT("CREATE TRIGGER ") + qtIdent(name);
if (rdbFires->GetSelection())
sql += wxT(" AFTER");
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 (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()
+ wxT(" FOR EACH ");
if (chkRow->GetValue())
sql += wxT("ROW");
else
sql += wxT("STATEMENT");
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::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::CheckChange()
{
bool enable=true;
wxString function=cbFunction->GetValue();
wxString name=GetName();
// We can only have per-statement TRUNCATE triggers
if (connection->BackendMinimumVersion(8, 4))
{
if (chkRow->GetValue())
{
chkTruncate->Disable();
chkTruncate->SetValue(false);
}
else
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"))) ||
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 : 1)));
}
else
{
EnableOK(enable);
}
}
bool dlgTrigger::IsUpToDate()
{
if (trigger && !trigger->IsUpToDate())
return false;
else
return true;
}