File: dlgSearchObject.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 (971 lines) | stat: -rw-r--r-- 46,156 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgSearchObject.h - Search dialogue
//
//////////////////////////////////////////////////////////////////////////


//AVANT DE LE COMMITER, IL FAUT AJOUTER LA MODIF DU PROJET VS2008

// App headers
#include "pgAdmin3.h"

#include "frm/frmMain.h"
#include "dlg/dlgSearchObject.h"
#include "utils/sysSettings.h"
#include "utils/misc.h"
#include "ctl/ctlListView.h"

#define txtPattern        CTRL_TEXT("txtPattern")
#define cbType            CTRL_COMBOBOX("cbType")
#define cbSchema          CTRL_COMBOBOX("cbSchema")
#define lcResults         CTRL_LISTCTRL("lcResults")
#define btnSearch         CTRL_BUTTON("btnSearch")
#define chkNames          CTRL_CHECKBOX("chkNames")
#define chkDefinitions    CTRL_CHECKBOX("chkDefinitions")
#define chkComments       CTRL_CHECKBOX("chkComments")

BEGIN_EVENT_TABLE(dlgSearchObject, pgDialog)
	EVT_BUTTON(wxID_HELP,                      dlgSearchObject::OnHelp)
	EVT_BUTTON(XRCID("btnSearch"),             dlgSearchObject::OnSearch)
	EVT_BUTTON(wxID_CANCEL,                    dlgSearchObject::OnCancel)
	EVT_TEXT(XRCID("txtPattern"),              dlgSearchObject::OnChange)
	EVT_COMBOBOX(XRCID("cbType"),              dlgSearchObject::OnChange)
	EVT_LIST_ITEM_SELECTED(XRCID("lcResults"), dlgSearchObject::OnSelSearchResult)
	EVT_CHECKBOX(XRCID("chkNames"),            dlgSearchObject::OnChange)
	EVT_CHECKBOX(XRCID("chkDefinitions"),      dlgSearchObject::OnChange)
	EVT_CHECKBOX(XRCID("chkComments"),         dlgSearchObject::OnChange)
END_EVENT_TABLE()

dlgSearchObject::dlgSearchObject(frmMain *p, pgDatabase *db, pgObject *obj)
{
	parent = p;
	header = wxT("");
	currentdb = db;

	SetFont(settings->GetSystemFont());
	LoadResource(p, wxT("dlgSearchObject"));
	statusBar = XRCCTRL(*this, "unkStatusBar", wxStatusBar);

	// Icon
	appearanceFactory->SetIcons(this);
	RestorePosition();

	ToggleBtnSearch(false);

	lcResults->InsertColumn(0, _("Type"));
	lcResults->InsertColumn(1, _("Name"));
	lcResults->InsertColumn(2, _("Path"));

	// Mapping table between local language and english,
	// because in SQL we're using only english and translate it later
	// to the local language.

	aMap[_("All types")] = wxT("All types");
	aMap[_("Schemas")] = wxT("Schemas");
	aMap[_("Tables")] = wxT("Tables");
	aMap[_("Columns")] = wxT("Columns");
	aMap[_("Triggers")] = wxT("Triggers");
	aMap[_("Views")] = wxT("Views");
	aMap[_("Rules")] = wxT("Rules");
	aMap[_("Indexes")] = wxT("Indexes");
	aMap[_("Functions")] = wxT("Functions");
	aMap[_("Aggregates")] = wxT("Aggregates");
	aMap[_("Trigger Functions")] = wxT("Trigger Functions");
	aMap[_("Constraints")] = wxT("Constraints");
	aMap[_("Sequences")] = wxT("Sequences");
	aMap[_("Types")] = wxT("Types");
	aMap[_("Domains")] = wxT("Domains");
	aMap[_("Languages")] = wxT("Languages");
	aMap[_("Conversions")] = wxT("Conversions");
	aMap[_("Casts")] = wxT("Casts");
	aMap[_("Login Roles")] = wxT("Login Roles");
	aMap[_("Group Roles")] = wxT("Group Roles");
	aMap[_("FTS Configurations")] = wxT("FTS Configurations");
	aMap[_("FTS Dictionaries")] = wxT("FTS Dictionaries");
	aMap[_("FTS Parsers")] = wxT("FTS Parsers");
	aMap[_("FTS Templates")] = wxT("FTS Templates");
	aMap[_("Foreign Data Wrappers")] = wxT("Foreign Data Wrappers");
	aMap[_("Foreign Servers")] = wxT("Foreign Servers");
	aMap[_("Foreign Tables")] = wxT("Foreign Tables");
	aMap[_("User Mappings")] = wxT("User Mappings");
	aMap[_("Operators")] = wxT("Operators");
	aMap[_("Operator Classes")] = wxT("Operator Classes");
	aMap[_("Operator Families")] = wxT("Operator Families");
	aMap[_("Extensions")] = wxT("Extensions");
	aMap[_("Collations")] = wxT("Collations");

	cbType->Clear();
	cbType->Append(_("All types"));
	cbType->Append(_("Schemas"));
	cbType->Append(_("Tables"));
	cbType->Append(_("Columns"));
	cbType->Append(_("Triggers"));
	cbType->Append(_("Views"));
	cbType->Append(_("Rules"));
	cbType->Append(_("Indexes"));
	cbType->Append(_("Functions"));
	cbType->Append(_("Aggregates"));
	cbType->Append(_("Trigger Functions"));
	cbType->Append(_("Constraints"));
	cbType->Append(_("Sequences"));
	cbType->Append(_("Types"));
	cbType->Append(_("Languages"));
	cbType->Append(_("Domains"));
	cbType->Append(_("Conversions"));
	cbType->Append(_("Casts"));
	cbType->Append(_("Login Roles"));
	cbType->Append(_("Group Roles"));
	cbType->Append(_("FTS Configurations"));
	cbType->Append(_("FTS Dictionaries"));
	cbType->Append(_("FTS Parsers"));
	cbType->Append(_("FTS Templates"));
	if(currentdb->BackendMinimumVersion(8, 4))
	{
		cbType->Append(_("Foreign Data Wrappers"));
		cbType->Append(_("Foreign Servers"));
		cbType->Append(_("User Mappings"));
	}
	if(currentdb->BackendMinimumVersion(9, 1))
	{
		cbType->Append(_("Foreign Tables"));
	}
	cbType->Append(_("Operators"));
	cbType->Append(_("Operator Classes"));
	cbType->Append(_("Operator Families"));

	if(currentdb->BackendMinimumVersion(9, 1))
	{
		cbType->Append(_("Extensions"));
		cbType->Append(_("Collations"));
	}

	cbSchema->Clear();
	cbSchema->Append(_("All schemas"));
	cbSchema->Append(_("My schemas"));

	if (obj->GetSchema()) {
		if (obj->GetSchema()->GetSchema())
			currentSchema = obj->GetSchema()->GetSchema()->GetName();
		else
			currentSchema = obj->GetSchema()->GetName();
	}
	else if (obj->GetMetaType() == PGM_SCHEMA && !obj->IsCollection())
		currentSchema = obj->GetName();
	else
		currentSchema = wxEmptyString;

	if (!currentSchema.IsEmpty())
		cbSchemaIdxCurrent = cbSchema->Append(wxString::Format(_("Current schema (%s)"), currentSchema.c_str()));

	wxString sql;

	sql = wxT("SELECT nsp.nspname")
	      wxT("  FROM pg_namespace nsp\n");
	if (!settings->GetShowSystemObjects())
	{
		if (currentdb->BackendMinimumVersion(8, 1))
			sql += wxT(" WHERE nspname NOT LIKE E'pg\\\\_temp\\\\_%' AND nspname NOT LIKE E'pg\\\\_toast%'");
		else
			sql += wxT(" WHERE nspname NOT LIKE 'pg\\\\_temp\\\\_%' AND nspname NOT LIKE 'pg\\\\_toast%'");
	}
	sql += wxT(" ORDER BY nspname");

	pgSet *set = currentdb->GetConnection()->ExecuteSet(sql);
	if(set)
	{
		while(!set->Eof())
		{
			cbSchema->Append(set->GetVal(wxT("nspname")));
			set->MoveNext();
		}
		delete set;
	}

	RestoreSettings();
	txtPattern->SetFocus();
}


dlgSearchObject::~dlgSearchObject()
{
	SaveSettings();
	SavePosition();
}

void dlgSearchObject::SaveSettings()
{
	settings->Write(wxT("SearchObject/Pattern"), txtPattern->GetValue());
	settings->Write(wxT("SearchObject/Type"), aMap[cbType->GetValue()]);
	settings->Write(wxT("SearchObject/Schema"), cbSchema->GetValue());
	settings->WriteBool(wxT("SearchObject/Names"), chkNames->GetValue());
	settings->WriteBool(wxT("SearchObject/Definitions"), chkDefinitions->GetValue());
	settings->WriteBool(wxT("SearchObject/Comments"), chkComments->GetValue());
}

wxString dlgSearchObject::getMapKeyByValue(wxString search_value)
{
	wxString key = wxEmptyString;
	LngMapping::iterator it;

	for (it = aMap.begin(); it != aMap.end(); ++it)
	{
		if (search_value.IsSameAs(it->second))
		{
			key = it->first;
			break;
		}
	}
	return key;
}

void dlgSearchObject::RestoreSettings()
{
	wxString val, mapkey;
	bool bVal;
	
	// Pattern
	settings->Read(wxT("SearchObject/Pattern"), &val, wxEmptyString);
	txtPattern->SetValue(val);
	
	// Type
	settings->Read(wxT("SearchObject/Type"), &val, wxT("All types"));
	mapkey = getMapKeyByValue(val);
	if (cbType->FindString(mapkey, true) == wxNOT_FOUND)
		cbType->SetValue(getMapKeyByValue(wxT("All types")));
	else
		cbType->SetValue(mapkey);

	// Schema
	settings->Read(wxT("SearchObject/Schema"), &val, wxT("All schemas"));
	if (cbSchema->FindString(val, true) == wxNOT_FOUND)
		cbSchema->SetValue(wxT("All schemas"));
	else
		cbSchema->SetValue(val);

	// names
	settings->Read(wxT("SearchObject/Names"), &bVal, true);
	chkNames->SetValue(bVal);
	
	// definitions
	settings->Read(wxT("SearchObject/Definitions"), &bVal, false);
	chkDefinitions->SetValue(bVal);
	
	// comments
	settings->Read(wxT("SearchObject/Comments"), &bVal, false);
	chkComments->SetValue(bVal);
}

void dlgSearchObject::OnHelp(wxCommandEvent &ev)
{
	DisplayHelp(wxT("search_object"), HELP_PGADMIN);
}

void dlgSearchObject::OnSelSearchResult(wxListEvent &ev)
{

	long row_number = ev.GetIndex();

	if(lcResults->GetItemTextColour(row_number) == wxColour(128, 128, 128))
	{
		/* Result type is not enabled in settings, so we don't search for it in the tree */
		return;
	}

	//Taken from: http://wiki.wxwidgets.org/WxListCtrl#Get_the_String_Contents_of_a_.22cell.22_in_a_LC_REPORT_wxListCtrl
	wxListItem     row_info;
	wxString       path;

	// Set what row it is (m_itemId is a member of the regular wxListCtrl class)
	row_info.m_itemId = row_number;
	// Set what column of that row we want to query for information.
	row_info.m_col = 2;
	// Set text mask
	row_info.m_mask = wxLIST_MASK_TEXT;

	// Get the info and store it in row_info variable.
	lcResults->GetItem(row_info);

	// Extract the text out that cell
	path = row_info.m_text;


	if(!parent->SetCurrentNode(parent->GetBrowser()->GetRootItem(), path))
	{
		wxMessageBox(_("The specified object couldn't be found in the tree."));
	}

}

void dlgSearchObject::OnChange(wxCommandEvent &ev)
{
	ToggleBtnSearch(true);
}

void dlgSearchObject::ToggleBtnSearch(bool enable)
{
	if(enable &&
	   /* When someone searches for operators, the limit of 3 characters is ignored */
	   (aMap[cbType->GetValue()] == wxT("Operators") || txtPattern->GetValue().Length() >= 3) &&
	   // At least one search mode enabled
	   (chkNames->GetValue() || chkDefinitions->GetValue() || chkComments->GetValue()))
	{
		btnSearch->Enable();
	}
	else
		btnSearch->Disable();
}

void dlgSearchObject::OnSearch(wxCommandEvent &ev)
{
	if (!(chkNames->GetValue() || chkDefinitions->GetValue() || chkComments->GetValue()))
		return; // should not happen

	wxBusyCursor wait;
	ToggleBtnSearch(false);
	if (statusBar)
		statusBar->SetStatusText(_("Searching..."));

	wxString txtPatternStr;
	if (txtPattern->GetValue().Contains(wxT("%")))
		txtPatternStr = currentdb->GetConnection()->qtDbString(txtPattern->GetValue().Lower());
	else
		txtPatternStr = currentdb->GetConnection()->qtDbString(wxT("%") + txtPattern->GetValue().Lower() + wxT("%"));

	/*
	Adding objects:

	Create a sql statement which lists all objects of the specified type and add it to the inner statement with an union.
	We need four columns: type, objectname, path and nspname (schema name). If object is schemaless, set nspname to NULL.
	Parts of the path which has to be translated to the local langauge (because of tree path) must begin with a colon.
	Append the type to the combobox and the mapping table in the constructor. */

	wxString databasePath = parent->GetNodePath(currentdb->GetDatabase()->GetId());
	wxString searchSQL = wxT("SELECT * FROM ( ");

	bool nextMode = false;
	// search names
	if (chkNames->GetValue())
	{
		if (nextMode)
			searchSQL += wxT("UNION \n");
		nextMode = true;
		searchSQL += wxT("SELECT * FROM (  ")
		             wxT("	SELECT  ")
		             wxT("	CASE   ")
		             wxT("		WHEN c.relkind = 'r' THEN 'Tables'   ")
		             wxT("		WHEN c.relkind = 'S' THEN 'Sequences'   ")
		             wxT("		WHEN c.relkind = 'v' THEN 'Views'   ")
		             wxT("		ELSE 'should not happen'   ")
		             wxT("	END AS type, c.relname AS objectname,  ")
		             wxT("	':Schemas/' || n.nspname || '/' ||  ")
		             wxT("	CASE   ")
		             wxT("		WHEN c.relkind = 'r' THEN ':Tables'   ")
		             wxT("		WHEN c.relkind = 'S' THEN ':Sequences'   ")
		             wxT("		WHEN c.relkind = 'v' THEN ':Views'   ")
		             wxT("		ELSE 'should not happen'   ")
		             wxT("	END || '/' || c.relname AS path, n.nspname  ")
		             wxT("	FROM pg_class c  ")
		             wxT("	LEFT JOIN pg_namespace n ON n.oid = c.relnamespace     ")
		             wxT("	WHERE c.relkind in ('r','S','v')  ")
		             wxT("	UNION  ")
		             wxT("	SELECT 'Indexes', cls.relname, ':Schemas/' || n.nspname || '/:Tables/' || tab.relname || '/:Indexes/' || cls.relname, n.nspname ")
		             wxT("	FROM pg_index idx ")
		             wxT("	JOIN pg_class cls ON cls.oid=indexrelid ")
		             wxT("	JOIN pg_class tab ON tab.oid=indrelid ")
		             wxT("	JOIN pg_namespace n ON n.oid=tab.relnamespace ")
		             wxT("	LEFT JOIN pg_depend dep ON (dep.classid = cls.tableoid AND dep.objid = cls.oid AND dep.refobjsubid = '0' AND dep.refclassid=(SELECT oid FROM pg_class WHERE relname='pg_constraint') AND dep.deptype='i') ")
		             wxT("	LEFT OUTER JOIN pg_constraint con ON (con.tableoid = dep.refclassid AND con.oid = dep.refobjid) ")
		             wxT("	LEFT OUTER JOIN pg_description des ON des.objoid=cls.oid ")
		             wxT("	LEFT OUTER JOIN pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0) ")
		             wxT("	WHERE contype IS NULL ")
		             wxT("	UNION  ")
		             wxT("	SELECT CASE WHEN t.typname = 'trigger' THEN 'Trigger Functions' ELSE 'Functions' END AS type, p.proname,  ")
		             wxT("	':Schemas/' || n.nspname || '/' || case when t.typname = 'trigger' then ':Trigger Functions' else ':Functions' end || '/' || p.proname, n.nspname ")
		             wxT("	from pg_proc p  ")
		             wxT("	left join pg_namespace n on p.pronamespace = n.oid  ")
		             wxT("	left join pg_type t on p.prorettype = t.oid  ")
		             wxT("	union  ")
		             wxT("	select 'Schemas', nspname, ':Schemas/' || nspname, nspname from pg_namespace  ")
		             wxT("	union  ")
		             wxT("	select 'Columns', a.attname,  ")
		             wxT("	':Schemas/' || n.nspname || '/' ||  ")
		             wxT("	case   ")
		             wxT("		when t.relkind = 'r' then ':Tables'   ")
		             wxT("		when t.relkind = 'S' then ':Sequences'   ")
		             wxT("		when t.relkind = 'v' then ':Views'   ")
		             wxT("		else 'should not happen'   ")
		             wxT("	end || '/' || t.relname || '/:Columns/' || a.attname AS path, n.nspname  ")
		             wxT("	from pg_attribute a  ")
		             wxT("	inner join pg_class t on a.attrelid = t.oid and t.relkind in ('r','v')  ")
		             wxT("	left join pg_namespace n on t.relnamespace = n.oid where a.attnum > 0  ")
		             wxT("	union  ")
		             wxT("	select 'Constraints', case when tf.relname is null then c.conname else c.conname || ' -> ' || tf.relname end, ':Schemas/' || n.nspname||'/:Tables/'||t.relname||'/:Constraints/'||case when tf.relname is null then c.conname else c.conname || ' -> ' || tf.relname end, n.nspname from pg_constraint c    ")
		             wxT("	left join pg_class t on c.conrelid = t.oid  ")
		             wxT("	left join pg_class tf on c.confrelid = tf.oid  ")
		             wxT("	left join pg_namespace n on t.relnamespace = n.oid 						 ")
		             wxT("	union  ")
		             wxT("	select 'Rules', r.rulename, ':Schemas/' || n.nspname||case when t.relkind = 'v' then '/:Views/' else '/:Tables/' end||t.relname||'/:Rules/'|| r.rulename, n.nspname from pg_rewrite r  ")
		             wxT("	left join pg_class t on r.ev_class = t.oid  ")
		             wxT("	left join pg_namespace n on t.relnamespace = n.oid 						 ")
		             wxT("	union  ")
		             wxT("	select 'Triggers', tr.tgname, ':Schemas/' || n.nspname||case when t.relkind = 'v' then '/:Views/' else '/:Tables/' end||t.relname || '/:Triggers/' || tr.tgname, n.nspname from pg_trigger tr  ")
		             wxT("	left join pg_class t on tr.tgrelid = t.oid  ")
		             wxT("	left join pg_namespace n on t.relnamespace = n.oid  ")
		             wxT("	where ");
		if(currentdb->BackendMinimumVersion(9, 0))
			searchSQL += wxT(" tr.tgisinternal = false ");
		else
			searchSQL += wxT(" tr.tgisconstraint = false ");
		searchSQL += wxT("	union ")
		             wxT("	SELECT 'Types', t.typname, ':Schemas/' || n.nspname || '/:Types/' || t.typname, n.nspname ")
		             wxT("	FROM pg_type t ")
		             wxT("	LEFT OUTER JOIN pg_type e ON e.oid=t.typelem ")
		             wxT("	LEFT OUTER JOIN pg_class ct ON ct.oid=t.typrelid AND ct.relkind <> 'c' ")
		             wxT("	LEFT OUTER JOIN pg_namespace n on t.typnamespace = n.oid ")
		             wxT("	WHERE t.typtype != 'd' AND t.typname NOT LIKE E'\\\\_%' 	 ");
		if (!settings->GetShowSystemObjects())
			searchSQL += wxT("   AND ct.oid IS NULL\n");
		searchSQL += wxT("	union ")
		             wxT("	SELECT 'Conversions', co.conname, ':Schemas/' || n.nspname || '/:Conversions/' || co.conname, n.nspname ")
		             wxT("	FROM pg_conversion co ")
		             wxT("	JOIN pg_namespace n ON n.oid=co.connamespace ")
		             wxT("	LEFT OUTER JOIN pg_description des ON des.objoid=co.oid AND des.objsubid=0	 ")
		             wxT("	union ")
		             wxT("	SELECT 'Casts', format_type(st.oid,NULL) ||'->'|| format_type(tt.oid,tt.typtypmod), ':Casts/' || format_type(st.oid,NULL) ||'->'|| format_type(tt.oid,tt.typtypmod), NULL as nspname ")
		             wxT("	FROM pg_cast ca ")
		             wxT("	JOIN pg_type st ON st.oid=castsource ")
		             wxT("	JOIN pg_type tt ON tt.oid=casttarget ")
		             wxT("	union ")
		             wxT("	SELECT 'Languages', lanname, ':Languages/' || lanname, NULL as nspname ")
		             wxT("	FROM pg_language lan ")
		             wxT("	WHERE lanispl IS TRUE ")
		             wxT("	union ")
		             wxT("	SELECT 'FTS Configurations', cfg.cfgname, ':Schemas/' || n.nspname || '/:FTS Configurations/' || cfg.cfgname, n.nspname ")
		             wxT("	FROM pg_ts_config cfg ")
		             wxT("	left join pg_namespace n on cfg.cfgnamespace = n.oid	 ")
		             wxT("	union ")
		             wxT("	SELECT 'FTS Dictionaries', dict.dictname, ':Schemas/' || ns.nspname || '/:FTS Dictionaries/' || dict.dictname, ns.nspname ")
		             wxT("	FROM pg_ts_dict dict ")
		             wxT("	left join pg_namespace ns on dict.dictnamespace = ns.oid ")
		             wxT("	union ")
		             wxT("	SELECT 'FTS Parsers', prs.prsname, ':Schemas/' || ns.nspname || '/:FTS Parsers/' || prs.prsname, ns.nspname ")
		             wxT("	FROM pg_ts_parser prs ")
		             wxT("	left join pg_namespace ns on prs.prsnamespace = ns.oid ")
		             wxT("	union ")
		             wxT("	SELECT 'FTS Templates', tmpl.tmplname, ':Schemas/' || ns.nspname || '/:FTS Templates/' || tmpl.tmplname, ns.nspname ")
		             wxT("	FROM pg_ts_template tmpl ")
		             wxT("	left join pg_namespace ns on tmpl.tmplnamespace = ns.oid ")
		             wxT("	union ")
		             wxT("	select 'Domains', t.typname, ':Schemas/' || n.nspname || '/:Domains/' || t.typname, n.nspname from pg_type t  ")
		             wxT("	inner join pg_namespace n on t.typnamespace = n.oid ")
		             wxT("	where t.typtype = 'd' ")
		             wxT("	union ")
		             wxT("	select 'Aggregates', pr.proname, ':Schemas/' || ns.nspname || '/:Aggregates/' || pr.proname , ns.nspname from pg_catalog.pg_aggregate ag ")
		             wxT("	inner join pg_proc pr on ag.aggfnoid = pr.oid ")
		             wxT("	left join pg_namespace ns on  pr.pronamespace = ns.oid ")
		             wxT("	union ")
		             wxT("	select case when rolcanlogin = true then 'Login Roles' else 'Group Roles' end, rolname, case when rolcanlogin = true then ':Login Roles' else ':Group Roles' end || '/' || rolname, NULL as nspname ")
		             wxT("	from pg_roles ")
		             wxT("	union ")
		             wxT("	select 'Tablespaces', spcname, ':Tablespaces/'||spcname, NULL as nspname from pg_tablespace ")
		             wxT("	union ")
		             wxT("	SELECT 'Operators', op.oprname, ':Schemas/' || ns.nspname || '/:Operators/' || op.oprname, ns.nspname ")
		             wxT("	FROM pg_operator op ")
		             wxT("	left join pg_namespace ns on op.oprnamespace = ns.oid ")
		             wxT("	union ")
		             wxT("	SELECT 'Operator Classes', op.opcname, ':Schemas/' || ns.nspname || '/:Operator Classes/' || op.opcname, ns.nspname ")
		             wxT("	FROM pg_opclass op ")
		             wxT("	left join pg_namespace ns on op.opcnamespace = ns.oid ")
		             wxT("	union ")
		             wxT("	SELECT 'Operator Families', opf.opfname, ':Schemas/' || ns.nspname || '/:Operator Families/' || opf.opfname, ns.nspname ")
		             wxT("	FROM pg_opfamily opf ")
		             wxT("	left join pg_namespace ns on opf.opfnamespace = ns.oid ");

		if(currentdb->BackendMinimumVersion(8, 4) && currentdb->GetConnection()->IsSuperuser())
		{
			searchSQL += wxT("	union ")
			             wxT("	select 'Foreign Data Wrappers', fdwname, ':Foreign Data Wrappers/' || fdwname, NULL as nspname from pg_foreign_data_wrapper ")
			             wxT("	union ")
			             wxT("	select 'Foreign Server', sr.srvname, ':Foreign Data Wrappers/' || fdw.fdwname || '/:Foreign Servers/' || sr.srvname, NULL as nspname from pg_foreign_server sr ")
			             wxT("	inner join pg_foreign_data_wrapper fdw on sr.srvfdw = fdw.oid ")
			             wxT("	union ")
			             wxT("	select 'User Mappings', ro.rolname, ':Foreign Data Wrappers/' || fdw.fdwname || '/:Foreign Servers/' || sr.srvname || '/:User Mappings/' || ro.rolname, NULL as nspname from pg_user_mapping um ")
			             wxT("	inner join pg_roles ro on um.umuser = ro.oid ")
			             wxT("	inner join pg_foreign_server sr on um.umserver = sr.oid ")
			             wxT("	inner join pg_foreign_data_wrapper fdw on sr.srvfdw = fdw.oid ");
		}

		if(currentdb->BackendMinimumVersion(9, 1))
		{
			searchSQL += wxT("	union ")
			             wxT("	select 'Foreign Tables', c.relname, ':Schemas/' || ns.nspname || '/:Foreign Tables/' || c.relname, ns.nspname from pg_foreign_table ft ")
			             wxT("	inner join pg_class c on ft.ftrelid = c.oid ")
			             wxT("	inner join pg_namespace ns on c.relnamespace = ns.oid ")
			             wxT("	union ")
			             wxT("	select 'Extensions', x.extname, ':Extensions/' || x.extname, NULL as nspname ")
			             wxT("	FROM pg_extension x	")
			             wxT("	JOIN pg_namespace n on x.extnamespace=n.oid ")
			             wxT("	join pg_available_extensions() e(name, default_version, comment) ON x.extname=e.name ")
			             wxT("	union ")
			             wxT("	SELECT 'Collations', c.collname, ':Schemas/' || n.nspname || '/:Collations/' || c.collname, n.nspname ")
			             wxT("	FROM pg_collation c ")
			             wxT("	JOIN pg_namespace n ON n.oid=c.collnamespace ");
		}

		searchSQL += wxT(") sn \n")
		             wxT("where lower(sn.objectname) like ") + txtPatternStr + wxT(" \n");
	} // search names

		// search definitions
	if (chkDefinitions->GetValue())
	{
		if (nextMode)
			searchSQL += wxT("UNION \n");
		nextMode = true;
		searchSQL += wxT("SELECT * FROM (  ") // Function's source code
		             wxT("	SELECT CASE WHEN t.typname = 'trigger' THEN 'Trigger Functions' ELSE 'Functions' END AS type, p.proname as objectname,  ")
		             wxT("	':Schemas/' || n.nspname || '/' || case when t.typname = 'trigger' then ':Trigger Functions' else ':Functions' end || '/' || p.proname as path, n.nspname ")
		             wxT("	from pg_proc p  ")
		             wxT("	left join pg_namespace n on p.pronamespace = n.oid  ")
		             wxT("	left join pg_type t on p.prorettype = t.oid  ")
		             wxT("WHERE p.prosrc ILIKE ") + txtPatternStr + wxT(" ")
		             wxT("UNION ") // Column's type name and default value
		             wxT("select 'Columns', a.attname, ")
		             wxT("':Schemas/' || n.nspname || '/' || ")
		             wxT("case   ")
		             wxT("	when t.relkind = 'r' then ':Tables' ")
		             wxT("	when t.relkind = 'S' then ':Sequences' ")
		             wxT("	when t.relkind = 'v' then ':Views' ")
		             wxT("	else 'should not happen' ")
		             wxT("end || '/' || t.relname || '/:Columns/' || a.attname AS path, n.nspname ")
		             wxT("from pg_attribute a ")
		             wxT("inner join pg_type ty on a.atttypid = ty.oid ")
		             wxT("left join pg_attrdef ad on a.attrelid = ad.adrelid and a.attnum = ad.adnum ")
		             wxT("inner join pg_class t on a.attrelid = t.oid and t.relkind in ('r','v') ")
		             wxT("left join pg_namespace n on t.relnamespace = n.oid ")
		             wxT("where a.attnum > 0 ")
		             wxT("  and (ty.typname ilike ") + txtPatternStr + wxT(" or ad.adsrc ilike ") + txtPatternStr + wxT(") ")
		             wxT("UNION ") // View's definition
		             wxT("SELECT 'Views', c.relname, ")
		             wxT("':Schemas/' || n.nspname || '/:Views/' || c.relname, n.nspname ")
		             wxT(" FROM pg_class c ")
		             wxT(" LEFT JOIN pg_namespace n ON n.oid = c.relnamespace ")
		             wxT(" WHERE c.relkind = 'v' ")
		             wxT("  and pg_get_viewdef(c.oid) ilike ") + txtPatternStr + wxT(" ")
		             wxT("UNION ") // Relation's column names except for Views (searched earlier)
		             wxT("SELECT CASE ")
		             wxT("  WHEN c.relkind = 'c' THEN 'Types' ")
		             wxT("	WHEN c.relkind = 'r' THEN 'Tables' ")
		             wxT("	WHEN c.relkind = 'f' THEN 'Foreign Tables' ")
		             wxT("	ELSE 'should not happen' ")
		             wxT("	END AS type, c.relname AS objectname, ")
		             wxT("	':Schemas/' || n.nspname || '/' || ")
		             wxT("	CASE ")
		             wxT("	WHEN c.relkind = 'c' THEN ':Types' ")
		             wxT("	WHEN c.relkind = 'r' THEN ':Tables' ")
		             wxT("	WHEN c.relkind = 'f' THEN ':Foreign Tables' ")
		             wxT("	ELSE 'should not happen' ")
		             wxT("	END || '/' || c.relname AS path, n.nspname ")
		             wxT(" from pg_attribute a ")
		             wxT(" inner join pg_class c on a.attrelid = c.oid and c.relkind in ('c','r','f') ")
		             wxT(" left join pg_namespace n on c.relnamespace = n.oid ")
		             wxT(" where a.attname ilike ") + txtPatternStr + wxT(" ");
		             // TODO: search for other object's definitions (indexes, constraints and so on)
		searchSQL += wxT(") sd \n");
	} // search definitions

	// search comments
	if (chkComments->GetValue())
	{
		if (nextMode)
			searchSQL += wxT("UNION \n");
		nextMode = true;

		wxString pd = wxT("(select pd.objoid, pd.classoid, pd.objsubid, c.relname")
		              wxT("  from pg_description pd")
		              wxT("  join pg_class c on pd.classoid = c.oid")
		              wxT(" where pd.description ilike ") + txtPatternStr + wxT(" UNION ")
		              wxT("select psd.objoid, psd.classoid, NULL as objsubid, c.relname")
		              wxT("  from pg_shdescription psd")
		              wxT("  join pg_class c on psd.classoid = c.oid")
		              wxT(" where psd.description ilike ") + txtPatternStr + wxT(") ");

		searchSQL += wxT("SELECT * FROM (  ");
		if(currentdb->BackendMinimumVersion(8, 4)) // Common Table Expressions are available
		{
			searchSQL += wxT("with pd as ") + pd;
			pd = wxT("pd ");
		}
		else // use pd as a subquery
			pd += wxT(" pd ");

		searchSQL += wxT("SELECT CASE")
		             wxT("	WHEN c.relkind = 'r' THEN 'Tables'")
		             wxT("	WHEN c.relkind = 'S' THEN 'Sequences'")
		             wxT("	WHEN c.relkind = 'v' THEN 'Views'")
		             wxT("	ELSE 'should not happen'")
		             wxT("	END AS type, c.relname AS objectname,")
		             wxT("	':Schemas/' || n.nspname || '/' ||")
		             wxT("	CASE")
		             wxT("	WHEN c.relkind = 'r' THEN ':Tables'")
		             wxT("	WHEN c.relkind = 'S' THEN ':Sequences'")
		             wxT("	WHEN c.relkind = 'v' THEN ':Views'")
		             wxT("	ELSE 'should not happen'")
		             wxT("	END || '/' || c.relname AS path, n.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_class c on pd.relname = 'pg_class' and pd.objoid = c.oid")
		             wxT("	LEFT JOIN pg_namespace n ON n.oid = c.relnamespace")
		             wxT("	WHERE c.relkind in ('r','S','v')")
		             wxT("	UNION")
		             wxT("	SELECT 'Indexes', cls.relname, ':Schemas/' || n.nspname || '/:Tables/' || tab.relname || '/:Indexes/' || cls.relname, n.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_class cls ON pd.relname = 'pg_class' and pd.objoid = cls.oid")
		             wxT("	JOIN pg_index idx ON cls.oid=indexrelid")
		             wxT("	JOIN pg_class tab ON tab.oid=indrelid")
		             wxT("	JOIN pg_namespace n ON n.oid=tab.relnamespace")
		             wxT("	LEFT JOIN pg_depend dep ON (dep.classid = cls.tableoid AND dep.objid = cls.oid AND dep.refobjsubid = '0' AND dep.refclassid=(SELECT oid FROM pg_class WHERE relname='pg_constraint') AND dep.deptype='i')")
		             wxT("	LEFT OUTER JOIN pg_constraint con ON (con.tableoid = dep.refclassid AND con.oid = dep.refobjid)")
		             wxT("	LEFT OUTER JOIN pg_description des ON des.objoid=cls.oid")
		             wxT("	LEFT OUTER JOIN pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0)")
		             wxT("	WHERE contype IS NULL")
		             wxT("	UNION")
		             wxT("  select case when p_t.typname = 'trigger' THEN 'Trigger Functions' ELSE 'Functions' end as type,")
		             wxT("       p_.proname AS objectname,")
		             wxT("       ':Schemas/' || n.nspname || '/' ||")
		             wxT("         case when p_t.typname = 'trigger' then ':Trigger Functions/' else ':Functions/' end || p_.proname AS path, n.nspname")
		             wxT("  from ") + pd +
		             wxT("  join pg_proc p_  on pd.relname = 'pg_proc' and pd.objoid = p_.oid and p_.proisagg = false")
		             wxT("	left join pg_type p_t on p_.prorettype = p_t.oid")
		             wxT("	left join pg_namespace n on p_.pronamespace = n.oid")
		             wxT("	union")
		             wxT("	select 'Schemas', n_.nspname, ':Schemas/' || n_.nspname, n_.nspname")
		             wxT("	  from ") + pd +
		             wxT("  join pg_namespace n_  on pd.relname = 'pg_namespace' and pd.objoid = n_.oid")
		             wxT("	union")
		             wxT("  select 'Columns', a.attname,")
		             wxT("	':Schemas/' || n.nspname || '/' ||")
		             wxT("	case")
		             wxT("	when t.relkind = 'r' then ':Tables'")
		             wxT("	when t.relkind = 'S' then ':Sequences'")
		             wxT("	when t.relkind = 'v' then ':Views'")
		             wxT("	else 'should not happen'")
		             wxT("	end || '/' || t.relname || '/:Columns/' || a.attname AS path, n.nspname")
		             wxT("	from ") + pd +
		             wxT("	join pg_class t on pd.relname = 'pg_class' and pd.objoid = t.oid and t.relkind in ('r','v')")
		             wxT("  join pg_attribute a on a.attrelid = t.oid and pd.objsubid = a.attnum")
		             wxT("	left join pg_namespace n on t.relnamespace = n.oid where a.attnum > 0")
		             wxT("	union")
		             wxT("	select 'Constraints',")
		             wxT("	  case when tf.relname is null then c.conname else c.conname || ' -> ' || tf.relname end,")
		             wxT("	  ':Schemas/' || n.nspname||'/:Tables/'||t.relname||'/:Constraints/'")
		             wxT("	    ||case when tf.relname is null then c.conname else c.conname || ' -> ' || tf.relname end, n.nspname")
		             wxT("  from ") + pd +
		             wxT("  join pg_constraint c on pd.relname = 'pg_constraint' and pd.objoid = c.oid")
		             wxT("	left join pg_class t on c.conrelid = t.oid")
		             wxT("	left join pg_class tf on c.confrelid = tf.oid")
		             wxT("	left join pg_namespace n on t.relnamespace = n.oid")
		             wxT("	union")
		             wxT("  select 'Rules', r.rulename, ':Schemas/' || n.nspname||case when t.relkind = 'v' then '/:Views/' else '/:Tables/' end||t.relname||'/:Rules/'|| r.rulename, n.nspname")
		             wxT("	from ") + pd +
		             wxT("	join pg_rewrite r on pd.relname = 'pg_rewrite' and pd.objoid = r.oid")
		             wxT("	left join pg_class t on r.ev_class = t.oid")
		             wxT("	left join pg_namespace n on t.relnamespace = n.oid")
		             wxT("	union")
		             wxT("	select 'Triggers', tr.tgname, ':Schemas/' || n.nspname||case when t.relkind = 'v' then '/:Views/' else '/:Tables/' end||t.relname || '/:Triggers/' || tr.tgname, n.nspname")
		             wxT("	from ") + pd +
		             wxT("	join pg_trigger tr on pd.relname = 'pg_trigger' and pd.objoid = tr.oid")
		             wxT("	left join pg_class t on tr.tgrelid = t.oid")
		             wxT("	left join pg_namespace n on t.relnamespace = n.oid WHERE ");
		if(currentdb->BackendMinimumVersion(9, 0))
			searchSQL += wxT(" tr.tgisinternal = false ");
		else
			searchSQL += wxT(" tr.tgisconstraint = false ");
		searchSQL += wxT("	union")
		             wxT("	SELECT 'Types', t.typname, ':Schemas/' || n.nspname || '/:Types/' || t.typname, n.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_type t on pd.relname = 'pg_type' and pd.objoid = t.oid")
		             wxT("	LEFT OUTER JOIN pg_type e ON e.oid=t.typelem")
		             wxT("	LEFT OUTER JOIN pg_class ct ON ct.oid=t.typrelid AND ct.relkind <> 'c'")
		             wxT("	LEFT OUTER JOIN pg_namespace n on t.typnamespace = n.oid")
		             wxT("	WHERE t.typtype != 'd' AND t.typname NOT LIKE E'\\\\_%'")
		             wxT("	union")
		             wxT("	SELECT 'Conversions', co.conname, ':Schemas/' || n.nspname || '/:Conversions/' || co.conname, n.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_conversion co on pd.relname = 'pg_conversion' and pd.objoid = co.oid")
		             wxT("	JOIN pg_namespace n ON n.oid=co.connamespace")
		             wxT("	LEFT OUTER JOIN pg_description des ON des.objoid=co.oid AND des.objsubid=0")
		             wxT("	union")
		             wxT("	SELECT 'Casts', format_type(st.oid,NULL) ||'->'|| format_type(tt.oid,tt.typtypmod), ':Casts/' || format_type(st.oid,NULL) ||'->'|| format_type(tt.oid,tt.typtypmod), NULL as nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_cast ca on pd.relname = 'pg_cast' and pd.objoid = ca.oid")
		             wxT("	JOIN pg_type st ON st.oid=castsource")
		             wxT("	JOIN pg_type tt ON tt.oid=casttarget")
		             wxT("	union")
		             wxT("	SELECT 'Languages', lanname, ':Languages/' || lanname, NULL as nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_language lan on pd.relname = 'pg_language' and pd.objoid = lan.oid")
		             wxT("	WHERE lanispl IS TRUE")
		             wxT("	union")
		             wxT("	SELECT 'FTS Configurations', cfg.cfgname, ':Schemas/' || n.nspname || '/:FTS Configurations/' || cfg.cfgname, n.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_ts_config cfg on pd.relname = 'pg_ts_config' and pd.objoid = cfg.oid")
		             wxT("	left join pg_namespace n on cfg.cfgnamespace = n.oid")
		             wxT("	union")
		             wxT("	SELECT 'FTS Dictionaries', dict.dictname, ':Schemas/' || ns.nspname || '/:FTS Dictionaries/' || dict.dictname, ns.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_ts_dict dict on pd.relname = 'pg_ts_dict' and pd.objoid = dict.oid")
		             wxT("	left join pg_namespace ns on dict.dictnamespace = ns.oid")
		             wxT("	union")
		             wxT("	SELECT 'FTS Parsers', prs.prsname, ':Schemas/' || ns.nspname || '/:FTS Parsers/' || prs.prsname, ns.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_ts_parser prs on pd.relname = 'pg_ts_parser' and pd.objoid = prs.oid")
		             wxT("	left join pg_namespace ns on prs.prsnamespace = ns.oid")
		             wxT("	union")
		             wxT("	SELECT 'FTS Templates', tmpl.tmplname, ':Schemas/' || ns.nspname || '/:FTS Templates/' || tmpl.tmplname, ns.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_ts_template tmpl on pd.relname = 'pg_ts_template' and pd.objoid = tmpl.oid")
		             wxT("	left join pg_namespace ns on tmpl.tmplnamespace = ns.oid")
		             wxT("	union")
		             wxT("	select 'Domains', t.typname, ':Schemas/' || n.nspname || '/:Domains/' || t.typname, n.nspname")
		             wxT("  FROM ") + pd +
		             wxT("  JOIN pg_type t on pd.relname = 'pg_type' and pd.objoid = t.oid")
		             wxT("	inner join pg_namespace n on t.typnamespace = n.oid")
		             wxT("	where t.typtype = 'd'")
		             wxT("	union")
		             wxT("	select 'Aggregates', pr.proname, ':Schemas/' || ns.nspname || '/:Aggregates/' || pr.proname, ns.nspname")
		             wxT("	from ") + pd +
		             wxT("	join pg_proc pr on pd.relname = 'pg_proc' and pd.objoid = pr.oid")
		             wxT("	JOIN pg_catalog.pg_aggregate ag on ag.aggfnoid = pr.oid")
		             wxT("	left join pg_namespace ns on  pr.pronamespace = ns.oid")
		             wxT("	union")
		             wxT("	select case when r_.rolcanlogin = true then 'Login Roles' else 'Group Roles' end, r_.rolname,")
		             wxT("	       case when r_.rolcanlogin = true then ':Login Roles' else ':Group Roles' end || '/' || rolname, NULL as nspname")
		             wxT("	from ") + pd +
		             wxT("	join pg_roles r_ on pd.relname = 'pg_authid' and pd.objoid = r_.oid")
		             wxT("	union")
		             wxT("	select 'Tablespaces', ts_.spcname, ':Tablespaces/'||ts_.spcname, NULL as nspname")
		             wxT("	  from ") + pd +
		             wxT("	  JOIN pg_tablespace ts_ on pd.relname = 'pg_tablespace' and pd.objoid = ts_.oid")
		             wxT("	union")
		             wxT("	SELECT 'Operators', op.oprname, ':Schemas/' || ns.nspname || '/:Operators/' || op.oprname, ns.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_operator op ON pd.relname = 'pg_operator' and pd.objoid = op.oid")
		             wxT("	left join pg_namespace ns on op.oprnamespace = ns.oid")
		             wxT("	union")
		             wxT("	SELECT 'Operator Classes', op.opcname, ':Schemas/' || ns.nspname || '/:Operator Classes/' || op.opcname, ns.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_opclass op ON pd.relname = 'pg_opclass' and pd.objoid = op.oid")
		             wxT("	left join pg_namespace ns on op.opcnamespace = ns.oid")
		             wxT("	union")
		             wxT("	SELECT 'Operator Families', opf.opfname, ':Schemas/' || ns.nspname || '/:Operator Families/' || opf.opfname, ns.nspname")
		             wxT("	FROM ") + pd +
		             wxT("	JOIN pg_opfamily opf ON pd.relname = 'pg_opfamily' and pd.objoid = opf.oid")
		             wxT("	left join pg_namespace ns on opf.opfnamespace = ns.oid");

		if(currentdb->BackendMinimumVersion(8, 4) && currentdb->GetConnection()->IsSuperuser())
		{
			searchSQL += wxT("	union")
		                 wxT("	select 'Foreign Data Wrappers', fdw.fdwname, ':Foreign Data Wrappers/' || fdw.fdwname, NULL as nspname ")
		                 wxT("	  from ") + pd +
		                 wxT("	  JOIN pg_foreign_data_wrapper fdw ON pd.relname = 'pg_foreign_data_wrapper' and pd.objoid = fdw.oid")
		                 wxT("	union ")
		                 wxT("	select 'Foreign Server', sr.srvname, ':Foreign Data Wrappers/' || fdw.fdwname || '/:Foreign Servers/' || sr.srvname, NULL as nspname")
		                 wxT("	  from ") + pd +
		                 wxT("	  JOIN pg_foreign_server sr ON pd.relname = 'pg_foreign_server' and pd.objoid = sr.oid")
		                 wxT("	inner join pg_foreign_data_wrapper fdw on sr.srvfdw = fdw.oid ");
		}

		if(currentdb->BackendMinimumVersion(9, 1))
		{
			searchSQL += wxT("	union")
		                 wxT("	select 'Foreign Tables', c.relname, ':Schemas/' || ns.nspname || '/:Foreign Tables/' || c.relname, ns.nspname")
		                 wxT("  from ") + pd +
		                 wxT("  JOIN pg_class c ON pd.relname = 'pg_class' and pd.objoid = c.oid")
		                 wxT("  join pg_foreign_table ft on ft.ftrelid = c.oid")
		                 wxT("	inner join pg_namespace ns on c.relnamespace = ns.oid")
		                 wxT("  union")
		                 wxT("	select 'Extensions', x.extname, ':Extensions/' || x.extname, NULL AS nspname")
		                 wxT("	FROM ") + pd +
		                 wxT("	JOIN pg_extension x ON pd.relname = 'pg_extension' and pd.objoid = x.oid")
		                 wxT("	JOIN pg_namespace n on x.extnamespace=n.oid")
		                 wxT("	join pg_available_extensions() e(name, default_version, comment) ON x.extname=e.name")
		                 wxT("	union")
		                 wxT("	SELECT 'Collations', c.collname, ':Schemas/' || n.nspname || '/:Collations/' || c.collname, n.nspname")
		                 wxT("	FROM ") + pd +
		                 wxT("	JOIN pg_collation c ON pd.relname = 'pg_collation' and pd.objoid = c.oid")
		                 wxT("	JOIN pg_namespace n ON n.oid=c.collnamespace");
		}
		searchSQL += wxT(") sc \n");
	} // search comments

	searchSQL += wxT(") ii \n");

	bool nextPredicate = false;
	if (cbType->GetValue() != _("All types"))
	{
		searchSQL += (nextPredicate) ? wxT("AND ") : wxT("WHERE ");
		nextPredicate = true;
		searchSQL += wxT("ii.type = ") + currentdb->GetConnection()->qtDbString(aMap[cbType->GetValue()]) + wxT(" ");
	}

	if (cbSchema->GetSelection() == cbSchemaIdxCurrent && !currentSchema.IsEmpty())
	{
		searchSQL += (nextPredicate) ? wxT("AND ") : wxT("WHERE ");
		nextPredicate = true;
		searchSQL += wxT("ii.nspname = ") + currentdb->GetConnection()->qtDbString(currentSchema) + wxT(" ");
	}
	else if (cbSchema->GetValue() == _("My schemas"))
	{
		searchSQL += (nextPredicate) ? wxT("AND ") : wxT("WHERE ");
		nextPredicate = true;
		searchSQL += wxT("ii.nspname IN (SELECT n.nspname FROM pg_namespace n WHERE n.nspowner = (SELECT u.usesysid FROM pg_user u WHERE u.usename = ")
		           + currentdb->GetConnection()->qtDbString(currentdb->GetConnection()->GetUser()) + wxT(")) ");
	}
	else if (cbSchema->GetValue() != _("All schemas"))
	{
		searchSQL += (nextPredicate) ? wxT("AND ") : wxT("WHERE ");
		nextPredicate = true;
		searchSQL += wxT("ii.nspname = ") + currentdb->GetConnection()->qtDbString(cbSchema->GetValue()) + wxT(" ");
	}

	searchSQL += wxT("ORDER BY 1, 2, 3");

	pgSet *set = currentdb->GetConnection()->ExecuteSet(searchSQL);
	int i = 0;
	if(set)
	{
		lcResults->DeleteAllItems();

		while(!set->Eof())
		{
			wxString objectType = set->GetVal(wxT("type"));
			wxString objectName = set->GetVal(wxT("objectname"));
			wxString ItemPath;


			/* Login Roles, Group Roles and Tablespaces are "outside" the database, so we have to adjust the path */
			if(objectType == wxT("Login Roles") || objectType == wxT("Group Roles") || objectType == wxT("Tablespaces"))
			{
				wxStringTokenizer tkz(databasePath, wxT("/"));
				wxString newPath;
				while(tkz.HasMoreTokens())
				{
					wxString token = tkz.GetNextToken();
					if(token == _("Databases"))
						break;
					ItemPath += token + wxT("/");
				}
				ItemPath += set->GetVal(wxT("path"));
			}
			else
			{
				ItemPath = databasePath + wxT("/") + set->GetVal(wxT("path"));
			}

			if(ItemPath.Contains(wxT("Schemas/information_schema")))
			{
				/* In information Schema only views and columns are displayed, nothing else */
				if(objectType == wxT("Views") || objectType == wxT("Columns"))
				{
					ItemPath.Replace(wxT(":Schemas/information_schema"), wxT(":Catalogs/ANSI/:Catalog Objects"));
					ItemPath.Replace(wxT(":Views/"), wxT(""));
				}
				else
				{
					set->MoveNext();
					continue;
				}
			}

			if(ItemPath.Contains(wxT("Schemas/pg_catalog")))
			{
				ItemPath.Replace(wxT(":Schemas/pg_catalog"), wxT(":Catalogs/PostgreSQL"));
			}

			wxListItem item;
			item.SetId(i);
			lcResults->InsertItem(item);

			wxString locTypeStr = wxGetTranslation(set->GetVal(wxT("type")));

			/* Check if viewing of the specified object is enabled in settings */
			if(!settings->GetDisplayOption(locTypeStr))
			{
				lcResults->SetItemTextColour(i, wxColour(128, 128, 128));
			}

			lcResults->SetItem(i, 0, locTypeStr);
			lcResults->SetItem(i, 1, objectName);
			lcResults->SetItem(i, 2, TranslatePath(ItemPath));
			set->MoveNext();
			i++;
		}
		delete set;
	}

	if(lcResults->GetItemCount() > 0)
	{
		lcResults->SetColumnWidth(0, wxLIST_AUTOSIZE);
		lcResults->SetColumnWidth(1, wxLIST_AUTOSIZE);
		lcResults->SetColumnWidth(2, wxLIST_AUTOSIZE);
	}

	if (statusBar)
	{
		if (i > 0)
			statusBar->SetStatusText(wxString::Format(wxPLURAL("Found %d item", "Found %d items",i), i));
		else
			statusBar->SetStatusText(_("Nothing was found"));
	}

	ToggleBtnSearch(true);
}

wxString dlgSearchObject::TranslatePath(wxString &path)
{
	/* Translate a path, but only word that start's with a colon (:) */
	wxStringTokenizer tkz(path, wxT("/"));
	wxString newPath;
	while(tkz.HasMoreTokens())
	{
		wxString token = tkz.GetNextToken();
		if(token.StartsWith(wxT(":")))
		{
			token = wxGetTranslation(token.AfterFirst(':'));
		}
		newPath = newPath + token.Trim() + wxT("/");
	}
	return newPath.BeforeLast('/');

}

void dlgSearchObject::OnCancel(wxCommandEvent &ev)
{
	if (IsModal())
		EndModal(wxID_CANCEL);
	else
		Destroy();
}

searchObjectFactory::searchObjectFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
{
	mnu->Append(id, _("&Search objects...\tCtrl-G"), _("Search database for specific objects"));
}

wxWindow *searchObjectFactory::StartDialog(frmMain *form, pgObject *obj)
{
	dlgSearchObject *so = new dlgSearchObject(form, obj->GetDatabase(), obj);
	so->Show();
	return 0;
}

bool searchObjectFactory::CheckEnable(pgObject *obj)
{
	return obj && obj->GetDatabase() && obj->GetDatabase()->GetConnected();
}