File: pgOperatorClass.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 (426 lines) | stat: -rw-r--r-- 13,801 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
426
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// pgOperatorClass.cpp - OperatorClass class
//
//////////////////////////////////////////////////////////////////////////

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

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


pgOperatorClass::pgOperatorClass(pgSchema *newSchema, const wxString &newName)
	: pgSchemaObject(newSchema, operatorClassFactory, newName)
{
}

pgOperatorClass::~pgOperatorClass()
{
}

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

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

	return message;
}

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

wxString pgOperatorClass::GetSql(ctlTree *browser)
{
	if (sql.IsNull())
	{
		sql = wxT("-- Operator Class: ") + GetName() + wxT("\n\n")
		      + wxT("-- DROP OPERATOR CLASS ") + GetQuotedFullIdentifier() + wxT(" USING ") + GetAccessMethod() + wxT(";")
		      + wxT("\n\nCREATE OPERATOR CLASS ") + GetQuotedFullIdentifier();
		if (GetOpcDefault())
			sql += wxT(" DEFAULT");
		sql += wxT("\n   FOR TYPE ") + GetInType()
		       +  wxT(" USING ") + GetAccessMethod()
		       +  wxT(" AS");
		unsigned int i;
		bool needComma = false;

		for (i = 0 ; i < operators.Count() ; i++)
		{
			if (needComma)
				sql += wxT(",");

			sql += wxT("\n   OPERATOR ") + operators.Item(i);
			needComma = true;
		}
		for (i = 0 ; i < functions.Count() ; i++)
		{
			if (needComma)
				sql += wxT(",");

			sql += wxT("\n   FUNCTION ") + quotedFunctions.Item(i);
			needComma = true;
		}
		AppendIfFilled(sql, wxT("\n   STORAGE "), GetKeyType());
		sql += wxT(";\n");
	}

	return sql;
}


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

		pgSet *set;

		if (!GetConnection()->BackendMinimumVersion(8, 3))
		{
			set = ExecuteSet(
			          wxT("SELECT amopstrategy, amopreqcheck, oprname, lt.typname as lefttype, rt.typname as righttype\n")
			          wxT("  FROM pg_amop am\n")
			          wxT("  JOIN pg_operator op ON amopopr=op.oid\n")
			          wxT("  LEFT OUTER JOIN pg_type lt ON lt.oid=oprleft\n")
			          wxT("  LEFT OUTER JOIN pg_type rt ON rt.oid=oprright\n")
			          wxT(" WHERE amopclaid=") + GetOidStr() + wxT("\n")
			          wxT(" ORDER BY amopstrategy"));
		}
		else if (!GetConnection()->BackendMinimumVersion(8, 4))
		{
			set = ExecuteSet(
			          wxT("SELECT amopstrategy, amopreqcheck, oprname, lt.typname as lefttype, rt.typname as righttype\n")
			          wxT("  FROM pg_amop am\n")
			          wxT("  JOIN pg_operator op ON amopopr=op.oid\n")
			          wxT("  JOIN pg_opfamily opf ON amopfamily = opf.oid\n")
			          wxT("  JOIN pg_opclass opc ON opf.oid = opcfamily\n")
			          wxT("  LEFT OUTER JOIN pg_type lt ON lt.oid=oprleft\n")
			          wxT("  LEFT OUTER JOIN pg_type rt ON rt.oid=oprright\n")
			          wxT(" WHERE opc.oid=") + GetOidStr() + wxT("\n")
			          wxT(" AND amopmethod = opf.opfmethod\n")
			          wxT(" AND amoplefttype = op.oprleft AND amoprighttype = op.oprright\n")
			          wxT(" ORDER BY amopstrategy"));
		}
		else
		{
			set = ExecuteSet(
			          wxT("SELECT amopstrategy, oprname, lt.typname as lefttype, rt.typname as righttype\n")
			          wxT("  FROM pg_amop am\n")
			          wxT("  JOIN pg_operator op ON amopopr=op.oid\n")
			          wxT("  JOIN pg_opfamily opf ON amopfamily = opf.oid\n")
			          wxT("  JOIN pg_opclass opc ON opf.oid = opcfamily\n")
			          wxT("  LEFT OUTER JOIN pg_type lt ON lt.oid=oprleft\n")
			          wxT("  LEFT OUTER JOIN pg_type rt ON rt.oid=oprright\n")
			          wxT(" WHERE opc.oid=") + GetOidStr() + wxT("\n")
			          wxT(" AND amopmethod = opf.opfmethod\n")
			          wxT(" AND amoplefttype = op.oprleft AND amoprighttype = op.oprright\n")
			          wxT(" ORDER BY amopstrategy"));
		}

		if (set)
		{
			while (!set->Eof())
			{
				wxString str = set->GetVal(wxT("amopstrategy")) + wxT("  ") + set->GetVal(wxT("oprname"));
				wxString lt = set->GetVal(wxT("lefttype"));
				wxString rt = set->GetVal(wxT("righttype"));
				if (lt == GetInType() && (rt.IsEmpty() || rt == GetInType()))
					lt = wxEmptyString;
				if (rt == GetInType() && lt.IsEmpty())
					rt = wxEmptyString;

				if (!lt.IsEmpty() || !rt.IsEmpty())
				{
					str += wxT("(");
					if (!lt.IsEmpty())
					{
						str += lt;
						if (!rt.IsEmpty())
							str += wxT(", ");
					}
					if (!rt.IsEmpty())
						str += rt;
					str += wxT(")");
				}

				if (!GetConnection()->BackendMinimumVersion(8, 4))
				{
					if (set->GetBool(wxT("amopreqcheck")))
						str += wxT(" RECHECK");
				}

				operators.Add(str);
				set->MoveNext();
			}
			delete set;
		}

		if (!GetConnection()->BackendMinimumVersion(8, 3))
		{
			set = ExecuteSet(
			          wxT("SELECT amprocnum, amproc::oid\n")
			          wxT("  FROM pg_amproc am\n")
			          wxT(" WHERE amopclaid=") + GetOidStr() + wxT("\n")
			          wxT(" ORDER BY amprocnum"));
		}
		else
		{
			set = ExecuteSet(
			          wxT("SELECT amprocnum, amproc::oid\n")
			          wxT("  FROM pg_amproc am\n")
			          wxT("  JOIN pg_opfamily opf ON amprocfamily = opf.oid\n")
			          wxT("  JOIN pg_opclass opc ON opf.oid = opcfamily\n")
			          wxT(" WHERE opc.oid=") + GetOidStr() + wxT("\n")
			          wxT(" AND amproclefttype = opc.opcintype AND amprocrighttype = opc.opcintype\n")
			          wxT(" ORDER BY amprocnum"));
		}

		if (set)
		{
			while (!set->Eof())
			{
				wxString amproc = set->GetVal(wxT("amproc"));
				functionOids.Add(amproc);

				// We won't build a PG_FUNCTIONS collection under OperatorClass, so we create
				// temporary function items
				pgFunction *function = functionFactory.AppendFunctions(this, GetSchema(), 0, wxT(" WHERE pr.oid=") + amproc);
				if (function)
				{
					functions.Add(set->GetVal(wxT("amprocnum")) + wxT("  ") + function->GetFullName());
					quotedFunctions.Add(set->GetVal(wxT("amprocnum")) + wxT("  ")
					                    + function->GetQuotedFullIdentifier() + wxT("(") + function->GetArgSigList() + wxT(")"));
					delete function;
				}

				set->MoveNext();
			}
			delete set;
		}
	}
	if (properties)
	{
		CreateListColumns(properties);

		properties->AppendItem(_("Name"), GetName());
		properties->AppendItem(_("OID"), GetOid());
		properties->AppendItem(_("Owner"), GetOwner());
		properties->AppendYesNoItem(_("Default?"), GetOpcDefault());
		properties->AppendItem(_("For type"), GetInType());
		properties->AppendItem(_("Access method"), GetAccessMethod());
		if (GetConnection()->BackendMinimumVersion(8, 3))
			properties->AppendItem(_("Family"), GetFamily());

		if (!GetKeyType().IsEmpty())
			properties->AppendItem(_("Storage"), GetKeyType());
		unsigned int i;
		for (i = 0 ; i < operators.Count() ; i++)
			properties->AppendItem(wxT("OPERATOR"), operators.Item(i));
		for (i = 0 ; i < functions.Count() ; i++)
			properties->AppendItem(wxT("FUNCTION"), functions.Item(i));
		properties->AppendYesNoItem(_("System operator class?"), GetSystemObject());
		if (GetConnection()->BackendMinimumVersion(7, 5))
			properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
	}
}



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

	return operatorClass;
}


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


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


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

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

	return message;
}


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


pgObject *pgOperatorClassFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
{
	pgOperatorClass *operatorClass = 0;

	pgSet *operatorClasses;
	if (collection->GetDatabase()->BackendMinimumVersion(8, 3))
	{
		operatorClasses = collection->GetDatabase()->ExecuteSet(
		                      wxT("SELECT op.oid, op.*, pg_get_userbyid(op.opcowner) as opowner, it.typname as intypename, dt.typname as keytypename, amname, opfname\n")
		                      wxT("  FROM pg_opclass op\n")
		                      wxT("  JOIN pg_opfamily opf ON op.opcfamily=opf.oid\n")
		                      wxT("  JOIN pg_am am ON am.oid=opf.opfmethod\n")
		                      wxT("  JOIN pg_type it ON it.oid=opcintype\n")
		                      wxT("  LEFT OUTER JOIN pg_type dt ON dt.oid=opckeytype\n")
		                      wxT(" WHERE opcnamespace = ") + collection->GetSchema()->GetOidStr()
		                      + restriction + wxT("\n")
		                      wxT(" ORDER BY opcname"));
	}
	else
	{
		operatorClasses = collection->GetDatabase()->ExecuteSet(
		                      wxT("SELECT op.oid, op.*, pg_get_userbyid(op.opcowner) as opowner, it.typname as intypename, dt.typname as keytypename, amname\n")
		                      wxT("  FROM pg_opclass op\n")
		                      wxT("  JOIN pg_am am ON am.oid=opcamid\n")
		                      wxT("  JOIN pg_type it ON it.oid=opcintype\n")
		                      wxT("  LEFT OUTER JOIN pg_type dt ON dt.oid=opckeytype\n")
		                      wxT(" WHERE opcnamespace = ") + collection->GetSchema()->GetOidStr()
		                      + restriction + wxT("\n")
		                      wxT(" ORDER BY opcname"));
	}


	if (operatorClasses)
	{
		while (!operatorClasses->Eof())
		{
			operatorClass = new pgOperatorClass(
			    collection->GetSchema(), operatorClasses->GetVal(wxT("opcname")));

			operatorClass->iSetOid(operatorClasses->GetOid(wxT("oid")));
			operatorClass->iSetOwner(operatorClasses->GetVal(wxT("opowner")));
			operatorClass->iSetAccessMethod(operatorClasses->GetVal(wxT("amname")));
			operatorClass->iSetInType(operatorClasses->GetVal(wxT("intypename")));
			operatorClass->iSetKeyType(operatorClasses->GetVal(wxT("keytypename")));
			operatorClass->iSetOpcDefault(operatorClasses->GetBool(wxT("opcdefault")));

			if (collection->GetDatabase()->BackendMinimumVersion(8, 3))
				operatorClass->iSetFamily(operatorClasses->GetVal(wxT("opfname")));

			if (browser)
			{
				browser->AppendObject(collection, operatorClass);
				operatorClasses->MoveNext();
			}
			else
				break;
		}

		delete operatorClasses;
	}
	return operatorClass;
}


#include "images/operatorclass.pngc"
#include "images/operatorclasses.pngc"

pgOperatorClassFactory::pgOperatorClassFactory()
	: pgSchemaObjFactory(__("Operator Class"), __("New Operator Class..."), __("Create a new Operator Class."), operatorclass_png_img)
{
	metaType = PGM_OPCLASS;
}

dlgProperty *pgOperatorClassFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return 0; // not implemented
}

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

pgOperatorClassFactory operatorClassFactory;
static pgaCollectionFactory cf(&operatorClassFactory, __("Operator Classes"), operatorclasses_png_img);