File: dlgRule.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 (197 lines) | stat: -rw-r--r-- 4,983 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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgRule.cpp - PostgreSQL View Property
//
//////////////////////////////////////////////////////////////////////////

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

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

#include "ctl/ctlSQLBox.h"
#include "dlg/dlgRule.h"
#include "schema/pgRule.h"
#include "schema/pgTable.h"
#include "schema/pgCollection.h"



// pointer to controls
#define rbxEvent        CTRL_RADIOBOX("rbxEvent")
#define chkDoInstead    CTRL_CHECKBOX("chkDoInstead")
#define txtCondition    CTRL_TEXT("txtCondition")

#define pnlDefinition   CTRL_PANEL("pnlDefinition")
#define txtSqlBox       CTRL_SQLBOX("txtSqlBox")


BEGIN_EVENT_TABLE(dlgRule, dlgProperty)
	EVT_TEXT(XRCID("txtCondition"),                 dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkSelect"),                dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkInsert"),                dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkUpdate"),                dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkDelete"),                dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkDoInstead"),             dlgProperty::OnChange)
	EVT_RADIOBOX(XRCID("rbxEvent"),                 dlgProperty::OnChange)
	EVT_STC_MODIFIED(XRCID("txtSqlBox"),            dlgProperty::OnChangeStc)
END_EVENT_TABLE();


dlgProperty *pgRuleFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgRule(this, frame, (pgRule *)node, (pgTable *)parent);
}



dlgRule::dlgRule(pgaFactory *f, frmMain *frame, pgRule *node, pgTable *tab)
	: dlgProperty(f, frame, wxT("dlgRule"))
{
	table = tab;
	rule = node;
}


pgObject *dlgRule::GetObject()
{
	return rule;
}


int dlgRule::Go(bool modal)
{
	if (rule)
	{
		// edit mode

		oldDefinition = rule->GetFormattedDefinition();
		if (!oldDefinition.IsEmpty())
		{
			int doPos = oldDefinition.Find(wxT(" DO INSTEAD "));
			if (doPos > 0)
				oldDefinition = oldDefinition.Mid(doPos + 12).Strip(wxString::both);
			else
			{
				doPos = oldDefinition.Find(wxT(" DO "));
				if (doPos > 0)
					oldDefinition = oldDefinition.Mid(doPos + 4).Strip(wxString::both);
			}
		}
		chkDoInstead->SetValue(rule->GetDoInstead());
		rbxEvent->SetStringSelection(rule->GetEvent());
		txtCondition->SetValue(rule->GetCondition());
		txtSqlBox->SetText(oldDefinition);

		txtName->Disable();
	}
	else
	{
		// create mode
	}

	// Reset the labels as they XRC values will have been localised :-(
	rbxEvent->SetString(0, wxT("SELECT"));
	rbxEvent->SetString(1, wxT("INSERT"));
	rbxEvent->SetString(2, wxT("UPDATE"));
	rbxEvent->SetString(3, wxT("DELETE"));

	return dlgProperty::Go(modal);
}


pgObject *dlgRule::CreateObject(pgCollection *collection)
{
	pgObject *obj = ruleFactory.CreateObjects(collection, 0,
	                wxT("\n   AND rulename=") + qtDbString(GetName()) +
	                wxT("\n   AND rw.ev_class=") + table->GetOidStr());
	return obj;
}


bool dlgRule::didChange()
{
	if (!rule)
		return true;

	if (GetName() != rule->GetName())
		return true;
	if (txtSqlBox->GetText().Strip(wxString::both) != oldDefinition)
		return true;
	if (chkDoInstead->GetValue() != rule->GetDoInstead())
		return true;
	if (rbxEvent->GetStringSelection() != rule->GetEvent())
		return true;
	if (txtCondition->GetValue() != rule->GetCondition())
		return true;
	if (txtSqlBox->GetText() != oldDefinition)
		return true;

	return false;
}


void dlgRule::CheckChange()
{
	wxString name = GetName();
	if (rule)
	{
		EnableOK(didChange() || txtSqlBox->GetText() != oldDefinition || txtComment->GetValue() != rule->GetComment());
	}
	else
	{
		wxString name = GetName();

		bool enable = true;

		CheckValid(enable, !name.IsEmpty(), _("Please specify name."));
		CheckValid(enable, rbxEvent->GetSelection() >= 0,
		           _("Please select at an event."));
		CheckValid(enable, !txtSqlBox->GetTextLength() || txtSqlBox->GetTextLength() > 6 , _("Please enter function definition."));

		EnableOK(enable);
	}
}


wxString dlgRule::GetSql()
{
	wxString sql, name = GetName();


	if (!rule || didChange())
	{
		sql += wxT("CREATE OR REPLACE RULE ") + qtIdent(name)
		       + wxT(" AS\n   ON ") + rbxEvent->GetStringSelection()
		       + wxT(" TO ") + table->GetQuotedFullIdentifier();
		AppendIfFilled(sql, wxT("\n   WHERE ") , txtCondition->GetValue());

		sql += wxT("\n   DO ");

		if (chkDoInstead->GetValue())
			sql += wxT("INSTEAD ");

		if (txtSqlBox->GetTextLength())
		{
			sql += wxT("\n") + txtSqlBox->GetText().Strip(wxString::both);
			if (sql.Right(1) != wxT(";"))
				sql += wxT(";");
		}
		else
			sql += wxT("NOTHING;");

		sql += wxT("\n");
	}
	AppendComment(sql, wxT("RULE ") + qtIdent(name)
	              + wxT(" ON ") + table->GetQuotedFullIdentifier(), rule);
	return sql;
}