File: dlgSequence.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 (386 lines) | stat: -rw-r--r-- 11,545 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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgSequence.cpp - PostgreSQL Sequence Property
//
//////////////////////////////////////////////////////////////////////////

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

// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "dlg/dlgSequence.h"

#include "schema/pgSchema.h"
#include "schema/pgSequence.h"
#include "ctl/ctlSeclabelPanel.h"


#define txtIncrement        CTRL_TEXT("txtIncrement")
#define cbOwner             CTRL_COMBOBOX2("cbOwner")
#define txtMin              CTRL_TEXT("txtMin")
#define txtMax              CTRL_TEXT("txtMax")
#define txtStart            CTRL_TEXT("txtStart")
#define txtCache            CTRL_TEXT("txtCache")
#define chkCycled           CTRL_CHECKBOX("chkCycled")
#define stStart             CTRL_STATIC("stStart")

// pointer to controls

BEGIN_EVENT_TABLE(dlgSequence, dlgSecurityProperty)
	EVT_TEXT(XRCID("txtStart"),                     dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtMin"),                       dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtMax"),                       dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtCache"),                     dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtIncrement"),                 dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkCycled"),                dlgProperty::OnChange)
END_EVENT_TABLE();


dlgProperty *pgSequenceFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgSequence(this, frame, (pgSequence *)node, (pgSchema *)parent);
}


dlgSequence::dlgSequence(pgaFactory *f, frmMain *frame, pgSequence *node, pgSchema *sch)
	: dlgSecurityProperty(f, frame, node, wxT("dlgSequence"), wxT("INSERT,SELECT,UPDATE,DELETE,RULE,REFERENCES,TRIGGER,USAGE"), "arwdRxtU")
{
	schema = sch;
	sequence = node;
	seclabelPage = new ctlSeclabelPanel(nbNotebook);
}


pgObject *dlgSequence::GetObject()
{
	return sequence;
}


int dlgSequence::Go(bool modal)
{
	if (connection->BackendMinimumVersion(9, 1))
	{
		seclabelPage->SetConnection(connection);
		seclabelPage->SetObject(sequence);
		this->Connect(EVT_SECLABELPANEL_CHANGE, wxCommandEventHandler(dlgSequence::OnChange));
	}
	else
		seclabelPage->Disable();

	if (sequence)
	{
		// edit mode
		txtIncrement->SetValue(sequence->GetIncrement().ToString());
		txtStart->SetValue(sequence->GetLastValue().ToString());
		txtMin->SetValue(sequence->GetMinValue().ToString());
		txtMax->SetValue(sequence->GetMaxValue().ToString());
		txtCache->SetValue(sequence->GetCacheValue().ToString());
		chkCycled->SetValue(sequence->GetCycled());

		stStart->SetLabel(_("Current value"));

		cbSchema->Enable(connection->BackendMinimumVersion(8, 1));

		if (!connection->BackendMinimumVersion(7, 4))
		{
			txtIncrement->Disable();
			txtMin->Disable();
			txtMax->Disable();
			txtCache->Disable();
			chkCycled->Disable();
		}
	}
	else
	{
		// create mode
		txtIncrement->SetValidator(numericValidator);
		txtMin->SetValidator(numericValidator);
		txtMax->SetValidator(numericValidator);
		txtCache->SetValidator(numericValidator);
	}

	txtStart->SetValidator(numericValidator);

	// Find, and disable the USAGE ACL option if we're on pre 8.2
	// 8.2+ only supports SELECT, UPDATE and USAGE
	if (!connection->BackendMinimumVersion(8, 2))
	{
		// Disable the checkbox
		if (!DisablePrivilege(wxT("USAGE")))
		{
			wxLogError(_("Failed to disable the USAGE privilege checkbox!"));
		}
	}
	else
	{
		if (!DisablePrivilege(wxT("INSERT")))
		{
			wxLogError(_("Failed to disable the INSERT privilege checkbox!"));
		}
		if (!DisablePrivilege(wxT("DELETE")))
		{
			wxLogError(_("Failed to disable the DELETE privilege checkbox!"));
		}
		if (!DisablePrivilege(wxT("RULE")))
		{
			wxLogError(_("Failed to disable the RULE privilege checkbox!"));
		}
		if (!DisablePrivilege(wxT("REFERENCES")))
		{
			wxLogError(_("Failed to disable the REFERENCES privilege checkbox!"));
		}
		if (!DisablePrivilege(wxT("TRIGGER")))
		{
			wxLogError(_("Failed to disable the TRIGGER privilege checkbox!"));
		}
	}

	return dlgSecurityProperty::Go(modal);
}


pgObject *dlgSequence::CreateObject(pgCollection *collection)
{
	pgObject *obj = sequenceFactory.CreateObjects(collection, 0,
	                wxT("   AND relname=") + qtDbString(GetName()) +
	                wxT("\n   AND relnamespace=") + schema->GetOidStr());

	return obj;
}


#ifdef __WXMAC__
void dlgSequence::OnChangeSize(wxSizeEvent &ev)
{
	SetPrivilegesLayout();
	if (GetAutoLayout())
	{
		Layout();
	}
}
#endif


void dlgSequence::CheckChange()
{
	bool enable = true;
	wxString name = GetName();
	bool maxOk = true;

	if (statusBar)
		statusBar->SetStatusText(wxEmptyString);

	// Check we don't overflow INT64_MAX
	if (doesOverflowBigInt(txtCache->GetValue(), !sequence))
	{
		if (statusBar)
			statusBar->SetStatusText(_("Invalid cache value"));
		maxOk = false;
	}

	if (doesOverflowBigInt(txtMax->GetValue(), !sequence))
	{
		if (statusBar)
			statusBar->SetStatusText(_("Invalid maximum value"));
		maxOk = false;
	}

	if (doesOverflowBigInt(txtMin->GetValue(), !sequence))
	{
		if (statusBar)
			statusBar->SetStatusText(_("Invalid minimum value"));
		maxOk = false;
	}

	if (doesOverflowBigInt(txtStart->GetValue(), !sequence))
	{
		if (statusBar)
			statusBar->SetStatusText(_("Invalid current value"));
		maxOk = false;
	}

	if (doesOverflowBigInt(txtIncrement->GetValue(), !sequence))
	{
		if (statusBar)
			statusBar->SetStatusText(_("Invalid increment value"));
		maxOk = false;
	}

	if (sequence)
	{
		enable = maxOk && (name != sequence->GetName()
		                   || cbSchema->GetValue() != sequence->GetSchema()->GetName()
		                   || txtComment->GetValue() != sequence->GetComment()
		                   || cbOwner->GetValue() != sequence->GetOwner()
		                   || txtStart->GetValue() != sequence->GetLastValue().ToString()
		                   || txtMin->GetValue() != sequence->GetMinValue().ToString()
		                   || txtMax->GetValue() != sequence->GetMaxValue().ToString()
		                   || txtCache->GetValue() != sequence->GetCacheValue().ToString()
		                   || txtIncrement->GetValue() != sequence->GetIncrement().ToString()
		                   || chkCycled->GetValue() != sequence->GetCycled());
		if (seclabelPage && connection->BackendMinimumVersion(9, 1))
			enable = enable || !(seclabelPage->GetSqlForSecLabels().IsEmpty());
		EnableOK(enable);
	}
	else
	{
		CheckValid(enable, !name.IsEmpty(), _("Please specify name."));
		EnableOK(enable && maxOk);
	}
}

bool dlgSequence::doesOverflowBigInt(const wxString &str, bool emptyAllowed)
{
	if (emptyAllowed && str.IsEmpty())
		return false;

	if (NumToStr(StrToLongLong(str)) != str)
		return true;

	return false;
}

wxString dlgSequence::GetSql()
{
	wxString sql, name;

	if (sequence)
	{
		// edit mode
		name = GetName();

		if (connection->BackendMinimumVersion(8, 3))
			AppendNameChange(sql, wxT("SEQUENCE ") + sequence->GetQuotedFullIdentifier());
		else
			AppendNameChange(sql, wxT("TABLE ") + sequence->GetQuotedFullIdentifier());

		if (connection->BackendMinimumVersion(8, 4))
			AppendOwnerChange(sql, wxT("SEQUENCE ") + schema->GetQuotedPrefix() + qtIdent(name));
		else
			AppendOwnerChange(sql, wxT("TABLE ") + schema->GetQuotedPrefix() + qtIdent(name));

		// This is where things get hairy. Per some thought by Horvath Gabor,
		// we need to adjust the min/max sequence values, and the the current
		// value per the rules:
		//
		// 1 Any ALTER SEQUENCE MIN/MAXVALUE statements that widen the range
		// 2 SETVAL
		// 3 Any ALTER SEQUENCE MIN/MAXVALUE statements that narrow the range.
		//
		// We'll change any other options at the end.
		wxString tmp;

		// MIN/MAX changes that widen the range.
		if (connection->BackendMinimumVersion(7, 4))
		{
			tmp = wxEmptyString;
			if (txtMin->GetValue().IsEmpty())
				tmp += wxT("\n   NO MINVALUE");
			else if (StrToLongLong(txtMin->GetValue()) < sequence->GetMinValue())
				tmp += wxT("\n   MINVALUE ") + txtMin->GetValue();

			if (txtMax->GetValue().IsEmpty())
				tmp += wxT("\n   NO MAXVALUE");
			else if (StrToLongLong(txtMax->GetValue()) > sequence->GetMaxValue())
				tmp += wxT("\n   MAXVALUE ") + txtMax->GetValue();

			if (!tmp.IsEmpty())
			{
				sql += wxT("ALTER SEQUENCE ") + schema->GetQuotedPrefix() + qtIdent(name)
				       +  tmp + wxT(";\n");
			}
		}

		// The new sequence value
		if (txtStart->GetValue() != sequence->GetLastValue().ToString())
			sql += wxT("SELECT setval('") + qtIdent(schema->GetName()) + wxT(".") + qtIdent(name)
			       +  wxT("', ") + txtStart->GetValue()
			       +  wxT(", true);\n");

		// Min/Max changes that narrow the ranges, as well as other changes.
		if (connection->BackendMinimumVersion(7, 4))
		{
			tmp = wxEmptyString;
			if (txtIncrement->GetValue() != sequence->GetIncrement().ToString())
				tmp += wxT("\n   INCREMENT ") + txtIncrement->GetValue();

			if ((!txtMin->GetValue().IsEmpty()) && StrToLongLong(txtMin->GetValue()) > sequence->GetMinValue())
				tmp += wxT("\n   MINVALUE ") + txtMin->GetValue();

			if ((!txtMax->GetValue().IsEmpty()) && StrToLongLong(txtMax->GetValue()) < sequence->GetMaxValue())
				tmp += wxT("\n   MAXVALUE ") + txtMax->GetValue();

			if (txtCache->GetValue() != sequence->GetCacheValue().ToString())
				tmp += wxT("\n   CACHE ") + txtCache->GetValue();

			if (chkCycled->GetValue() != sequence->GetCycled())
			{
				if (chkCycled->GetValue())
					tmp += wxT("\n   CYCLE");
				else
					tmp += wxT("\n   NO CYCLE");
			}

			if (!tmp.IsEmpty())
			{
				sql += wxT("ALTER SEQUENCE ") + schema->GetQuotedPrefix() + qtIdent(name)
				       +  tmp + wxT(";\n");
			}

			if (connection->BackendMinimumVersion(8, 1))
				AppendSchemaChange(sql,  wxT("SEQUENCE ") + schema->GetQuotedPrefix() + qtIdent(name));
		}
	}
	else
	{
		name = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());

		// create mode
		sql = wxT("CREATE SEQUENCE ") + name;
		if (chkCycled->GetValue())
			sql += wxT(" CYCLE");
		AppendIfFilled(sql, wxT("\n   INCREMENT "), txtIncrement->GetValue());
		AppendIfFilled(sql, wxT("\n   START "), txtStart->GetValue());
		AppendIfFilled(sql, wxT("\n   MINVALUE "), txtMin->GetValue());
		AppendIfFilled(sql, wxT("\n   MAXVALUE "), txtMax->GetValue());
		AppendIfFilled(sql, wxT("\n   CACHE "), txtCache->GetValue());
		sql += wxT(";\n");

		if (cbOwner->GetGuessedSelection() > 0)
		{
			if (connection->BackendMinimumVersion(8, 4))
			{
				AppendOwnerChange(sql, wxT("SEQUENCE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));
			}
			else
			{
				AppendOwnerChange(sql, wxT("TABLE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));
			}
		}
	}

	if (!connection->BackendMinimumVersion(8, 2))
		sql +=  GetGrant(wxT("arwdRxt"), wxT("TABLE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));
	else
		sql +=  GetGrant(wxT("rwU"), wxT("SEQUENCE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));

	AppendComment(sql, wxT("SEQUENCE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), sequence);

	if (seclabelPage && connection->BackendMinimumVersion(9, 1))
		sql += seclabelPage->GetSqlForSecLabels(wxT("SEQUENCE"), qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));

	return sql;
}

void dlgSequence::OnChange(wxCommandEvent &event)
{
	CheckChange();
}