File: edbPackage.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 (355 lines) | stat: -rw-r--r-- 9,582 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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// edbPackage.cpp - EnterpriseDB Package class
//
//////////////////////////////////////////////////////////////////////////

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

// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "schema/edbPackage.h"
#include "schema/edbPackageFunction.h"
#include "schema/edbPackageVariable.h"

edbPackage::edbPackage(pgSchema *newSchema, const wxString &newName)
	: pgSchemaObject(newSchema, packageFactory, newName)
{
}

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

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

	return message;
}


bool edbPackage::IsUpToDate()
{
	pgConn *conn = GetDatabase()->GetConnection();
	if (!conn)
		return false;

	wxString sql;
	if(conn->EdbMinimumVersion(8, 2))
		sql = wxT("SELECT xmin FROM pg_namespace WHERE oid = ") + this->GetOidStr();
	else
		sql = wxT("SELECT xmin FROM edb_package WHERE oid = ") + this->GetOidStr();

	if (conn->ExecuteScalar(sql) != NumToStr(GetXid()))
		return false;
	else
		return true;
}

bool edbPackage::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
	wxString sql = wxT("DROP PACKAGE ") + GetQuotedFullIdentifier();

	return GetDatabase()->ExecuteVoid(sql);
}

wxString edbPackage::GetSql(ctlTree *browser)
{
	wxString qtName = GetQuotedFullIdentifier();

	if (sql.IsNull())
	{
		sql = wxT("-- Package: ") + qtName + wxT("\n\n")
		      wxT("-- DROP PACKAGE ") + qtName;

		sql += wxT(";\n\n");
		sql += wxT("CREATE OR REPLACE PACKAGE ") + qtName + wxT("\nIS\n");
		sql += GetHeaderInner();
		sql += wxT("\nEND ") + qtIdent(GetName()) + wxT(";\n\n");

		if (!GetBodyInner().Trim().IsEmpty())
		{
			sql += wxT("CREATE OR REPLACE PACKAGE BODY ") + qtName + wxT("\nIS\n");
			sql += GetBodyInner();
			sql += wxT("\nEND ") + qtIdent(GetName()) + wxT(";\n\n");
		}

		sql += GetGrant(wxT("X"), wxT("PACKAGE ") + qtName);

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

	return sql;
}

wxString edbPackage::GetHeaderInner()
{
	return GetInner(GetHeader());
}

wxString edbPackage::GetBodyInner()
{
	return GetInner(GetBody());
}

wxString edbPackage::GetInner(const wxString &def)
{
	long start = 0, end = 0;

	wxStringTokenizer tkz(def, wxT("\t\r\n "));

	// Find the opening AS/IF keyword
	while ( tkz.HasMoreTokens() )
	{
		wxString token = tkz.GetNextToken();
		if (token.Lower() == wxT("as") || token.Lower() == wxT("is"))
		{
			start = tkz.GetPosition();
			break;
		}
	}

	// Find the closing END keyword
	wxString tmp = def;
	tmp.Replace(wxT("\n"), wxT(" "));
	tmp.Replace(wxT("\r"), wxT(" "));
	tmp.Replace(wxT("\t"), wxT(" "));

	int e1 = tmp.Lower().rfind(wxT(" end;"));
	int e2 = tmp.Lower().rfind(wxT(" end "));

	end = (e1 > e2 ? e1 : e2);

	return def.Mid(start, end - start);
}

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

		browser->RemoveDummyChild(this);

		// Log
		wxLogInfo(wxT("Adding child object to package %s"), GetIdentifier().c_str());

		browser->AppendCollection(this, packageFunctionFactory);
		browser->AppendCollection(this, packageProcedureFactory);
		browser->AppendCollection(this, packageVariableFactory);
	}


	if (properties)
	{
		CreateListColumns(properties);

		properties->AppendItem(_("Name"), GetName());
		properties->AppendItem(_("OID"), GetOid());
		properties->AppendItem(_("Owner"), GetOwner());
		properties->AppendItem(_("Header"), firstLineOnly(GetHeader()));
		properties->AppendItem(_("Body"), firstLineOnly(GetBody()));
		properties->AppendItem(_("ACL"), GetAcl());
		properties->AppendYesNoItem(_("System package?"), GetSystemObject());
		if (GetConnection()->EdbMinimumVersion(8, 2))
			properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
	}
}



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

	pgCollection *coll = browser->GetParentCollection(item);
	if (coll)
	{
		if (coll->GetConnection()->EdbMinimumVersion(8, 2))
			package = packageFactory.CreateObjects(coll, 0, wxT(" AND nspname=") + qtDbString(GetName()));
		else
			package = packageFactory.CreateObjects(coll, 0, wxT(" AND pkgname=") + qtDbString(GetName()));
	}

	return package;
}



pgObject *edbPackageFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
{
	edbPackage *package = 0;

	wxString sql, pkgsrc;

	wxString whereclause;

	if (collection->GetConnection()->EdbMinimumVersion(9, 0))
		pkgsrc = wxT("pg_catalog.edb_get_packagebodydef(nsp.oid) AS pkgbodysrc, ")
		         wxT("pg_catalog.edb_get_packageheaddef(nsp.oid) AS pkgheadsrc,\n");
	else if (collection->GetConnection()->EdbMinimumVersion(8, 2))
		pkgsrc = wxT("nspbodysrc AS pkgbodysrc, nspheadsrc AS pkgheadsrc,\n");

	if (collection->GetConnection()->EdbMinimumVersion(8, 2))
	{
		whereclause = wxT("  WHERE nspparent = ") + NumToStr(collection->GetSchema()->GetOid()) + wxT("::oid\n");
		if (collection->GetConnection()->EdbMinimumVersion(9, 2))
			whereclause += wxT(" AND nspobjecttype = 0 ");

		sql = wxT("SELECT nsp.oid, nsp.xmin, nspname AS pkgname,\n") + pkgsrc +
		      wxT("       nspacl AS pkgacl, pg_get_userbyid(nspowner) AS owner, description\n")
		      wxT("  FROM pg_namespace nsp")
		      wxT("  LEFT OUTER JOIN pg_description des ON (des.objoid=nsp.oid AND des.classoid='pg_namespace'::regclass)\n")
		      + whereclause
		      + restriction +
		      wxT("  ORDER BY nspname;");
	}
	else
	{

		sql = wxT("SELECT oid, xmin, *, pg_get_userbyid(pkgowner) AS owner\n")
		      wxT("  FROM edb_package")
		      wxT("  WHERE pkgnamespace = ") + NumToStr(collection->GetSchema()->GetOid()) + wxT("::oid\n")
		      + restriction +
		      wxT("  ORDER BY pkgname;");
	}

	pgSet *packages = collection->GetDatabase()->ExecuteSet(sql);

	if (packages)
	{
		while (!packages->Eof())
		{
			wxString name = packages->GetVal(wxT("pkgname"));
			package = new edbPackage(collection->GetSchema(), name);

			package->iSetOid(packages->GetOid(wxT("oid")));
			package->iSetXid(packages->GetOid(wxT("xmin")));
			package->iSetDatabase(collection->GetDatabase());
			package->iSetOwner(packages->GetVal(wxT("owner")));
			if (collection->GetConnection()->EdbMinimumVersion(8, 2))
				package->iSetComment(packages->GetVal(wxT("description")));

			// EnterpriseDB's CVS code has some new parser code
			// which is stricter about the formatting of body &
			// header code and leaves off trailing ;'s
			wxString tmp = packages->GetVal(wxT("pkgheadsrc")).Strip(wxString::both);
			if (!tmp.EndsWith(wxT(";")))
				package->iSetHeader(tmp + wxT(";"));
			else
				package->iSetHeader(tmp);

			tmp = packages->GetVal(wxT("pkgbodysrc")).Strip(wxString::both);
			if (!tmp.EndsWith(wxT(";")) && !tmp.IsEmpty())
				package->iSetBody(tmp + wxT(";"));
			else
				package->iSetBody(tmp);

			package->iSetAcl(packages->GetVal(wxT("pkgacl")));

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

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

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

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

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

	return message;
}

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

#include "images/package.pngc"
#include "images/packages.pngc"

edbPackageFactory::edbPackageFactory()
	: pgSchemaObjFactory(__("Package"), __("New Package..."), __("Create a new package."), package_png_img)
{
	metaType = EDB_PACKAGE;
}


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

edbPackageFactory packageFactory;
static pgaCollectionFactory cf(&packageFactory, __("Packages"), packages_png_img);