File: pgSequence.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 (348 lines) | stat: -rw-r--r-- 10,048 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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// pgSequence.cpp - Sequence class
//
//////////////////////////////////////////////////////////////////////////

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

// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "schema/pgSequence.h"


pgSequence::pgSequence(pgSchema *newSchema, const wxString &newName)
	: pgSchemaObject(newSchema, sequenceFactory, newName)
{
	isReplicated = false;
}

pgSequence::~pgSequence()
{
}

wxString pgSequence::GetTranslatedMessage(int kindOfMessage) const
{
	wxString message = wxEmptyString;

	switch (kindOfMessage)
	{
		case RETRIEVINGDETAILS:
			message = _("Retrieving details on sequence");
			message += wxT(" ") + GetName();
			break;
		case REFRESHINGDETAILS:
			message = _("Refreshing sequence");
			message += wxT(" ") + GetName();
			break;
		case DROPINCLUDINGDEPS:
			message = wxString::Format(_("Are you sure you wish to drop sequence \"%s\" including all objects that depend on it?"),
			                           GetFullIdentifier().c_str());
			break;
		case DROPEXCLUDINGDEPS:
			message = wxString::Format(_("Are you sure you wish to drop sequence \"%s\"?"),
			                           GetFullIdentifier().c_str());
			break;
		case DROPCASCADETITLE:
			message = _("Drop sequence cascaded?");
			break;
		case DROPTITLE:
			message = _("Drop sequence?");
			break;
		case PROPERTIESREPORT:
			message = _("Sequence properties report");
			message += wxT(" - ") + GetName();
			break;
		case PROPERTIES:
			message = _("Sequence properties");
			break;
		case DDLREPORT:
			message = _("Sequence DDL report");
			message += wxT(" - ") + GetName();
			break;
		case DDL:
			message = _("Sequence DDL");
			break;
		case STATISTICSREPORT:
			message = _("Sequence statistics report");
			message += wxT(" - ") + GetName();
			break;
		case OBJSTATISTICS:
			message = _("Sequence statistics");
			break;
		case DEPENDENCIESREPORT:
			message = _("Sequence dependencies report");
			message += wxT(" - ") + GetName();
			break;
		case DEPENDENCIES:
			message = _("Sequence dependencies");
			break;
		case DEPENDENTSREPORT:
			message = _("Sequence dependents report");
			message += wxT(" - ") + GetName();
			break;
		case DEPENDENTS:
			message = _("Sequence dependents");
			break;
	}

	return message;
}


int pgSequence::GetIconId()
{
	if (isReplicated)
		return sequenceFactory.GetReplicatedIconId();
	else
		return sequenceFactory.GetIconId();
}

bool pgSequence::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
	wxString sql = wxT("DROP SEQUENCE ") + this->GetSchema()->GetQuotedIdentifier() + wxT(".") + this->GetQuotedIdentifier();
	if (cascaded)
		sql += wxT(" CASCADE");
	return GetDatabase()->ExecuteVoid(sql);
}


void pgSequence::UpdateValues()
{
	pgSet *sequence = ExecuteSet(
	                      wxT("SELECT last_value, min_value, max_value, cache_value, is_cycled, increment_by, is_called\n")
	                      wxT("  FROM ") + GetQuotedFullIdentifier());
	if (sequence)
	{
		lastValue = sequence->GetLongLong(wxT("last_value"));
		minValue = sequence->GetLongLong(wxT("min_value"));
		maxValue = sequence->GetLongLong(wxT("max_value"));
		cacheValue = sequence->GetLongLong(wxT("cache_value"));
		increment = sequence->GetLongLong(wxT("increment_by"));
		cycled = sequence->GetBool(wxT("is_cycled"));
		called = sequence->GetBool(wxT("is_called"));
		if (called)
			nextValue = lastValue + increment;
		else
			nextValue = lastValue;

		delete sequence;
	}
}


wxString pgSequence::GetSql(ctlTree *browser)
{
	if (sql.IsNull())
	{
		UpdateValues();
		sql = wxT("-- Sequence: ") + GetQuotedFullIdentifier() + wxT("\n\n")
		      + wxT("-- DROP SEQUENCE ") + GetQuotedFullIdentifier() + wxT(";")
		      + wxT("\n\nCREATE SEQUENCE ") + GetQuotedFullIdentifier()
		      + wxT("\n  INCREMENT ") + GetIncrement().ToString()
		      + wxT("\n  MINVALUE ") + GetMinValue().ToString()
		      + wxT("\n  MAXVALUE ") + GetMaxValue().ToString()
		      + wxT("\n  START ") + GetLastValue().ToString()
		      + wxT("\n  CACHE ") + GetCacheValue().ToString();
		if (GetCycled())
			sql += wxT("\n  CYCLE");
		sql += wxT(";\n")
		       + GetOwnerSql(7, 3, wxT("TABLE ") + GetQuotedFullIdentifier());

		if (!GetConnection()->BackendMinimumVersion(8, 2))
			sql += GetGrant(wxT("arwdRxt"), wxT("TABLE ") + GetQuotedFullIdentifier());
		else
			sql += GetGrant(wxT("rwU"), wxT("SEQUENCE ") + GetQuotedFullIdentifier());

		sql += GetCommentSql();

		if (GetConnection()->BackendMinimumVersion(9, 1))
			sql += GetSeqLabelsSql();
	}

	return sql;
}

void pgSequence::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
	UpdateValues();
	if (properties)
	{
		CreateListColumns(properties);

		properties->AppendItem(_("Name"), GetName());
		properties->AppendItem(_("OID"), GetOid());
		properties->AppendItem(_("Owner"), GetOwner());
		properties->AppendItem(_("ACL"), GetAcl());
		properties->AppendItem(_("Current value"), GetLastValue());
		properties->AppendItem(_("Next value"), GetNextValue());
		properties->AppendItem(_("Minimum"), GetMinValue());
		properties->AppendItem(_("Maximum"), GetMaxValue());
		properties->AppendItem(_("Increment"), GetIncrement());
		properties->AppendItem(_("Cache"), GetCacheValue());
		properties->AppendYesNoItem(_("Cycled?"), GetCycled());
		properties->AppendYesNoItem(_("Called?"), GetCalled());
		properties->AppendYesNoItem(_("System sequence?"), GetSystemObject());
		properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));

		if (!GetLabels().IsEmpty())
		{
			wxArrayString seclabels = GetProviderLabelArray();
			if (seclabels.GetCount() > 0)
			{
				for (unsigned int index = 0 ; index < seclabels.GetCount() - 1 ; index += 2)
				{
					properties->AppendItem(seclabels.Item(index), seclabels.Item(index + 1));
				}
			}
		}
	}
}



void pgSequence::ShowStatistics(frmMain *form, ctlListView *statistics)
{
	wxLogInfo(wxT("Displaying statistics for sequence on ") + GetSchema()->GetIdentifier());

	// Add the statistics view columns
	CreateListColumns(statistics, _("Statistic"), _("Value"));

	pgSet *stats = GetSchema()->GetDatabase()->ExecuteSet(wxT(
	                   "SELECT blks_read, blks_hit FROM pg_statio_all_sequences WHERE relid = ") + GetOidStr());

	if (stats)
	{
		statistics->InsertItem(0, _("Blocks Read"), PGICON_STATISTICS);
		statistics->SetItem(0l, 1, stats->GetVal(wxT("blks_read")));
		statistics->InsertItem(1, _("Blocks Hit"), PGICON_STATISTICS);
		statistics->SetItem(1, 1, stats->GetVal(wxT("blks_hit")));

		delete stats;
	}
}


pgObject *pgSequence::Refresh(ctlTree *browser, const wxTreeItemId item)
{
	pgObject *sequence = 0;
	pgCollection *coll = browser->GetParentCollection(item);
	if (coll)
		sequence = sequenceFactory.CreateObjects(coll, 0, wxT("\n   AND cl.oid=") + GetOidStr());

	return sequence;
}


///////////////////////////////////////////////////


pgSequenceCollection::pgSequenceCollection(pgaFactory *factory, pgSchema *sch)
	: pgSchemaObjCollection(factory, sch)
{
}


wxString pgSequenceCollection::GetTranslatedMessage(int kindOfMessage) const
{
	wxString message = wxEmptyString;

	switch (kindOfMessage)
	{
		case RETRIEVINGDETAILS:
			message = _("Retrieving details on sequences");
			break;
		case REFRESHINGDETAILS:
			message = _("Refreshing sequences");
			break;
		case GRANTWIZARDTITLE:
			message = _("Privileges for sequences");
			break;
		case OBJECTSLISTREPORT:
			message = _("Sequences list report");
			break;
	}

	return message;
}


///////////////////////////////////////////////////////////////////////////////


pgObject *pgSequenceFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
{
	pgSet *sequences;
	pgSequence *sequence = 0;
	wxString sql;

	sql = wxT("SELECT cl.oid, relname, pg_get_userbyid(relowner) AS seqowner, relacl, description");
	if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
	{
		sql += wxT(",\n(SELECT array_agg(label) FROM pg_seclabels sl1 WHERE sl1.objoid=cl.oid) AS labels");
		sql += wxT(",\n(SELECT array_agg(provider) FROM pg_seclabels sl2 WHERE sl2.objoid=cl.oid) AS providers");
	}
	sql += wxT("\n  FROM pg_class cl\n")
	       wxT("  LEFT OUTER JOIN pg_description des ON (des.objoid=cl.oid AND des.classoid='pg_class'::regclass)\n")
	       wxT(" WHERE relkind = 'S' AND relnamespace  = ") + collection->GetSchema()->GetOidStr()
	       + restriction + wxT("\n")
	       wxT(" ORDER BY relname");

	sequences = collection->GetDatabase()->ExecuteSet(sql);

	if (sequences)
	{
		while (!sequences->Eof())
		{
			sequence = new pgSequence(collection->GetSchema(),
			                          sequences->GetVal(wxT("relname")));

			sequence->iSetOid(sequences->GetOid(wxT("oid")));
			sequence->iSetComment(sequences->GetVal(wxT("description")));
			sequence->iSetOwner(sequences->GetVal(wxT("seqowner")));
			sequence->iSetAcl(sequences->GetVal(wxT("relacl")));

			if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
			{
				sequence->iSetProviders(sequences->GetVal(wxT("providers")));
				sequence->iSetLabels(sequences->GetVal(wxT("labels")));
			}

			if (browser)
			{
				browser->AppendObject(collection, sequence);
				sequences->MoveNext();
			}
			else
				break;
		}
		delete sequences;
	}
	return sequence;
}


#include "images/sequence.pngc"
#include "images/sequences.pngc"

pgSequenceFactory::pgSequenceFactory()
	: pgSchemaObjFactory(__("Sequence"), __("New Sequence..."), __("Create a new Sequence."), sequence_png_img)
{
	metaType = PGM_SEQUENCE;
}


pgCollection *pgSequenceFactory::CreateCollection(pgObject *obj)
{
	return new pgSequenceCollection(GetCollectionFactory(), (pgSchema *)obj);
}

pgSequenceFactory sequenceFactory;
static pgaCollectionFactory cf(&sequenceFactory, __("Sequences"), sequences_png_img);