File: pgDomain.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 (425 lines) | stat: -rw-r--r-- 12,829 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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// pgDomain.cpp - Domain class
//
//////////////////////////////////////////////////////////////////////////

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

// App headers
#include "pgAdmin3.h"
#include "frm/frmMain.h"
#include "utils/misc.h"
#include "schema/pgDomain.h"
#include "schema/pgDatatype.h"

#include "schema/pgTable.h"
#include "schema/pgColumn.h"
#include "schema/pgIndexConstraint.h"
#include "schema/pgForeignKey.h"
#include "schema/pgCheck.h"
#include "utils/sysSettings.h"
#include "utils/pgfeatures.h"
#include "schema/pgRule.h"
#include "schema/pgTrigger.h"
#include "schema/pgConstraints.h"
#include "schema/gpPartition.h"


pgDomain::pgDomain(pgSchema *newSchema, const wxString &newName)
	: pgSchemaObject(newSchema, domainFactory, newName)
{
}

pgDomain::~pgDomain()
{
}

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

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

	return message;
}


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

wxString pgDomain::GetSql(ctlTree *browser)
{
	if (sql.IsNull())
	{
		sql = wxT("-- Domain: ") + GetQuotedFullIdentifier() + wxT("\n\n")
		      + wxT("-- DROP DOMAIN ") + GetQuotedFullIdentifier() + wxT(";")
		      + wxT("\n\nCREATE DOMAIN ") + GetQuotedFullIdentifier()
		      + wxT("\n  AS ") + GetQuotedBasetype();
		if (GetCollationOid() > 0)
			sql += wxT("\n  COLLATE ") + GetQuotedCollation();
		AppendIfFilled(sql, wxT("\n  DEFAULT "), GetDefault());
		// CONSTRAINT Name Dont know where it's stored, may be omitted anyway
		if (notNull)
			sql += wxT("\n  NOT NULL");

		// Get a count of the constraints.
		int consCount = 0;
		pgCollection *constraints = browser->FindCollection(checkFactory, GetId());
		if (constraints)
		{
			constraints->ShowTreeDetail(browser);
			treeObjectIterator consIt(browser, constraints);

			pgObject *data;

			while ((data = consIt.GetNextObject()) != 0)
			{
				data->ShowTreeDetail(browser);

				sql += wxT("\n  CONSTRAINT ") + data->GetQuotedIdentifier()
				       + wxT(" ") + data->GetTypeName().Upper()
				       + wxT(" ") ;

				switch (data->GetMetaType())
				{
					case PGM_CHECK:
						sql += wxT("(") + ((pgCheck *)data)->GetDefinition() + wxT(")");
						if (GetDatabase()->BackendMinimumVersion(9, 2) && !((pgCheck *)data)->GetValid())
							sql += wxT(" NOT VALID");
						break;
				}
			}
		}

		sql += wxT(";\n")
		       + GetOwnerSql(7, 4)
		       + GetCommentSql();

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

	return sql;
}


wxMenu *pgDomain::GetNewMenu()
{
	wxMenu *menu = pgObject::GetNewMenu();
	if (schema->GetCreatePrivilege())
	{
		checkFactory.AppendMenu(menu);
	}
	return menu;
}


void pgDomain::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
	if (!expandedKids)
	{
		expandedKids = true;

		browser->RemoveDummyChild(this);

		// Log
		wxLogInfo(wxT("Adding child object to domain %s"), GetIdentifier().c_str());
		if (GetConnection()->BackendMinimumVersion(7, 4))
			browser->AppendCollection(this, constraintFactory);
	}

	if (properties)
	{
		CreateListColumns(properties);

		properties->AppendItem(_("Name"), GetName());
		properties->AppendItem(_("OID"), GetOid());
		properties->AppendItem(_("Owner"), GetOwner());
		properties->AppendItem(_("Base type"), GetBasetype());
		if (GetDimensions())
			properties->AppendItem(_("Dimensions"), GetDimensions());
		if (GetCollationOid() > 0)
			properties->AppendItem(_("Collation"), GetQuotedCollation());
		properties->AppendItem(_("Default"), GetDefault());
		properties->AppendYesNoItem(_("Not NULL?"), GetNotNull());
		properties->AppendYesNoItem(_("System domain?"), 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));
				}
			}
		}
	}
}



pgObject *pgDomain::Refresh(ctlTree *browser, const wxTreeItemId item)
{
	pgObject *domain = 0;

	pgCollection *coll = browser->GetParentCollection(item);
	if (coll)
		domain = domainFactory.CreateObjects(coll, 0, wxT("   AND d.oid=") + GetOidStr() + wxT("\n"));

	return domain;
}


void pgDomain::Validate(frmMain *form)
{
	wxString sql = wxT("ALTER DOMAIN ") + GetQuotedFullIdentifier()
	               + wxT("\n  VALIDATE CONSTRAINT ") + GetCheckConstraintName();
	GetDatabase()->ExecuteVoid(sql);

	iSetValid(true);
	UpdateIcon(form->GetBrowser());
}


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



pgObject *pgDomainFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
{
	wxString sql;
	pgDomain *domain = 0;

	pgDatabase *db = collection->GetDatabase();

	sql = wxT("SELECT d.oid, d.typname as domname, d.typbasetype, format_type(b.oid,NULL) as basetype, pg_get_userbyid(d.typowner) as domainowner, \n");
	if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
		sql += wxT("c.oid AS colloid, c.collname, cn.nspname as collnspname, \n");
	sql += wxT("       d.typlen, d.typtypmod, d.typnotnull, d.typdefault, d.typndims, d.typdelim, bn.nspname as basensp,\n")
	       wxT("       description, (SELECT COUNT(1) FROM pg_type t2 WHERE t2.typname=d.typname) > 1 AS domisdup,\n")
	       wxT("       (SELECT COUNT(1) FROM pg_type t3 WHERE t3.typname=b.typname) > 1 AS baseisdup");
	if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
	{
		sql += wxT(",\n(SELECT array_agg(label) FROM pg_seclabels sl1 WHERE sl1.objoid=d.oid) AS labels");
		sql += wxT(",\n(SELECT array_agg(provider) FROM pg_seclabels sl2 WHERE sl2.objoid=d.oid) AS providers");
	}
	sql += wxT("\n   FROM pg_type d\n")
	       wxT("  JOIN pg_type b ON b.oid = d.typbasetype\n")
	       wxT("  JOIN pg_namespace bn ON bn.oid=b.typnamespace\n")
	       wxT("  LEFT OUTER JOIN pg_description des ON (des.objoid=d.oid AND des.classoid='pg_type'::regclass)\n");
	if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
		sql += wxT("  LEFT OUTER JOIN pg_collation c ON d.typcollation=c.oid\n")
		       wxT("  LEFT OUTER JOIN pg_namespace cn ON c.collnamespace=cn.oid\n");
	sql += wxT(" WHERE d.typtype = 'd' AND d.typnamespace = ") + NumToStr(collection->GetSchema()->GetOid()) + wxT("::oid\n")
	       + restriction +
	       wxT(" ORDER BY d.typname");
	pgSet *domains = db->ExecuteSet(sql);

	if (domains)
	{
		while (!domains->Eof())
		{
			domain = new pgDomain(collection->GetSchema(), domains->GetVal(wxT("domname")));

			domain->iSetOid(domains->GetOid(wxT("oid")));
			domain->iSetOwner(domains->GetVal(wxT("domainowner")));
			domain->iSetBasetype(domains->GetVal(wxT("basetype")));
			domain->iSetBasetypeOid(domains->GetOid(wxT("typbasetype")));
			domain->iSetComment(domains->GetVal(wxT("description")));
			long typmod = domains->GetLong(wxT("typtypmod"));

			pgDatatype dt(domains->GetVal(wxT("basensp")), domains->GetVal(wxT("basetype")),
			              domains->GetBool(wxT("baseisdup")), domains->GetLong(wxT("typndims")), typmod);

			domain->iSetTyplen(domains->GetLong(wxT("typlen")));
			domain->iSetTypmod(typmod);
			domain->iSetLength(dt.Length());
			domain->iSetPrecision(dt.Precision());
			domain->iSetBasetype(dt.GetSchemaPrefix(db) + dt.FullName());
			domain->iSetQuotedBasetype(dt.GetQuotedSchemaPrefix(db) + dt.QuotedFullName());
			domain->iSetDefault(domains->GetVal(wxT("typdefault")));
			domain->iSetNotNull(domains->GetBool(wxT("typnotnull")));
			domain->iSetDimensions(domains->GetLong(wxT("typndims")));
			domain->iSetDelimiter(domains->GetVal(wxT("typdelim")));
			domain->iSetIsDup(domains->GetBool(wxT("domisdup")));
			if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
			{
				domain->iSetCollation(domains->GetVal(wxT("collname")));
				domain->iSetQuotedCollation(qtIdent(domains->GetVal(wxT("collnspname"))) + wxT(".") + qtIdent(domains->GetVal(wxT("collname"))));
				domain->iSetCollationOid(domains->GetOid(wxT("colloid")));
			}
			else
				domain->iSetCollationOid(0);

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

			// we suppose the constraint valid now
			// this is checked in ShowTreeDetail for each domain
			domain->iSetValid(true);

			if (browser)
			{
				browser->AppendObject(collection, domain);
				domains->MoveNext();
			}
			else
				break;
		}

		delete domains;
	}
	return domain;
}

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

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


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

	switch (kindOfMessage)
	{
		case RETRIEVINGDETAILS:
			message = _("Retrieving details on domains");
			break;
		case REFRESHINGDETAILS:
			message = _("Refreshing domains");
			break;
		case OBJECTSLISTREPORT:
			message = _("Domains list report");
			break;
	}

	return message;
}

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

#include "images/domain.pngc"
#include "images/domain-sm.pngc"
#include "images/domains.pngc"

pgDomainFactory::pgDomainFactory()
	: pgSchemaObjFactory(__("Domain"), __("New Domain..."), __("Create a new Domain."), domain_png_img, domain_sm_png_img)
{
	metaType = PGM_DOMAIN;
}


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

pgDomainFactory domainFactory;
static pgaCollectionFactory cf(&domainFactory, __("Domains"), domains_png_img);

validateDomainCheckFactory::validateDomainCheckFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
{
	mnu->Append(id, _("Validate domain check constraint"), _("Validate the selected domain check constraint."));
}


wxWindow *validateDomainCheckFactory::StartDialog(frmMain *form, pgObject *obj)
{
	((pgDomain *)obj)->Validate(form);
	((pgDomain *)obj)->SetDirty();

	wxTreeItemId item = form->GetBrowser()->GetSelection();
	if (obj == form->GetBrowser()->GetObject(item))
	{
		obj->ShowTreeDetail(form->GetBrowser(), 0, form->GetProperties());
		form->GetSqlPane()->SetReadOnly(false);
		form->GetSqlPane()->SetText(((pgDomain *)obj)->GetSql(form->GetBrowser()));
		form->GetSqlPane()->SetReadOnly(true);
	}
	form->GetMenuFactories()->CheckMenu(obj, form->GetMenuBar(), (ctlMenuToolbar *)form->GetToolBar());

	return 0;
}


bool validateDomainCheckFactory::CheckEnable(pgObject *obj)
{
	return obj && obj->IsCreatedBy(domainFactory) && obj->CanEdit()
	       && ((pgDomain *)obj)->GetConnection()->BackendMinimumVersion(9, 2)
	       && !((pgDomain *)obj)->GetValid();
}