1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // pgTextSearchDictionary.cpp - Text Search Dictionary class
9 //
10 //////////////////////////////////////////////////////////////////////////
11 
12 // wxWindows headers
13 #include <wx/wx.h>
14 
15 // App headers
16 #include "pgAdmin3.h"
17 #include "utils/misc.h"
18 #include "schema/pgTextSearchDictionary.h"
19 
20 
pgTextSearchDictionary(pgSchema * newSchema,const wxString & newName)21 pgTextSearchDictionary::pgTextSearchDictionary(pgSchema *newSchema, const wxString &newName)
22 	: pgSchemaObject(newSchema, textSearchDictionaryFactory, newName)
23 {
24 }
25 
~pgTextSearchDictionary()26 pgTextSearchDictionary::~pgTextSearchDictionary()
27 {
28 }
29 
GetTranslatedMessage(int kindOfMessage) const30 wxString pgTextSearchDictionary::GetTranslatedMessage(int kindOfMessage) const
31 {
32 	wxString message = wxEmptyString;
33 
34 	switch (kindOfMessage)
35 	{
36 		case RETRIEVINGDETAILS:
37 			message = _("Retrieving details on FTS dictionary");
38 			message += wxT(" ") + GetName();
39 			break;
40 		case REFRESHINGDETAILS:
41 			message = _("Refreshing FTS dictionary");
42 			message += wxT(" ") + GetName();
43 			break;
44 		case DROPINCLUDINGDEPS:
45 			message = wxString::Format(_("Are you sure you wish to drop FTS dictionary \"%s\" including all objects that depend on it?"),
46 			                           GetFullIdentifier().c_str());
47 			break;
48 		case DROPEXCLUDINGDEPS:
49 			message = wxString::Format(_("Are you sure you wish to drop FTS dictionary \"%s\"?"),
50 			                           GetFullIdentifier().c_str());
51 			break;
52 		case DROPCASCADETITLE:
53 			message = _("Drop FTS dictionary cascaded?");
54 			break;
55 		case DROPTITLE:
56 			message = _("Drop FTS dictionary?");
57 			break;
58 		case PROPERTIESREPORT:
59 			message = _("FTS dictionary properties report");
60 			message += wxT(" - ") + GetName();
61 			break;
62 		case PROPERTIES:
63 			message = _("FTS dictionary properties");
64 			break;
65 		case DDLREPORT:
66 			message = _("FTS dictionary DDL report");
67 			message += wxT(" - ") + GetName();
68 			break;
69 		case DDL:
70 			message = _("FTS dictionary DDL");
71 			break;
72 		case DEPENDENCIESREPORT:
73 			message = _("FTS dictionary dependencies report");
74 			message += wxT(" - ") + GetName();
75 			break;
76 		case DEPENDENCIES:
77 			message = _("FTS dictionary dependencies");
78 			break;
79 		case DEPENDENTSREPORT:
80 			message = _("FTS dictionary dependents report");
81 			message += wxT(" - ") + GetName();
82 			break;
83 		case DEPENDENTS:
84 			message = _("FTS dictionary dependents");
85 			break;
86 	}
87 
88 	return message;
89 }
90 
DropObject(wxFrame * frame,ctlTree * browser,bool cascaded)91 bool pgTextSearchDictionary::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
92 {
93 	wxString sql = wxT("DROP TEXT SEARCH DICTIONARY ") + this->GetSchema()->GetQuotedIdentifier() + wxT(".") + qtIdent(this->GetIdentifier());
94 
95 	if (cascaded)
96 		sql += wxT(" CASCADE");
97 
98 	return GetDatabase()->ExecuteVoid(sql);
99 }
100 
101 
GetSql(ctlTree * browser)102 wxString pgTextSearchDictionary::GetSql(ctlTree *browser)
103 {
104 	if (sql.IsNull())
105 	{
106 		sql = wxT("-- Text Search Dictionary: ") + GetFullIdentifier() + wxT("\n\n")
107 		      + wxT("-- DROP TEXT SEARCH DICTIONARY ") + GetFullIdentifier() + wxT("\n\n")
108 		      + wxT("CREATE TEXT SEARCH DICTIONARY ") + GetFullIdentifier() + wxT(" (")
109 		      + wxT("\n   TEMPLATE = ") + qtTypeIdent(GetTemplate());
110 
111 		if (options.Length() > 0)
112 			sql += wxT(",\n   ") + options;
113 
114 		sql += wxT("\n);\n");
115 
116 		if (!GetComment().IsNull())
117 			sql += wxT("COMMENT ON TEXT SEARCH DICTIONARY ") + GetFullIdentifier()
118 			       + wxT(" IS ") + qtDbString(GetComment()) + wxT(";\n");
119 	}
120 
121 	return sql;
122 }
123 
124 
ShowTreeDetail(ctlTree * browser,frmMain * form,ctlListView * properties,ctlSQLBox * sqlPane)125 void pgTextSearchDictionary::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
126 {
127 	if (properties)
128 	{
129 		CreateListColumns(properties);
130 
131 		properties->AppendItem(_("Name"), GetName());
132 		properties->AppendItem(_("OID"), GetOid());
133 		properties->AppendItem(_("Owner"), GetOwner());
134 		properties->AppendItem(_("Template"), GetTemplate());
135 		properties->AppendItem(_("Options"), GetOptions());
136 		properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
137 	}
138 }
139 
140 
141 
Refresh(ctlTree * browser,const wxTreeItemId item)142 pgObject *pgTextSearchDictionary::Refresh(ctlTree *browser, const wxTreeItemId item)
143 {
144 	pgObject *dict = 0;
145 	pgCollection *coll = browser->GetParentCollection(item);
146 	if (coll)
147 		dict = textSearchDictionaryFactory.CreateObjects(coll, 0, wxT("\n   AND dict.oid=") + GetOidStr());
148 
149 	return dict;
150 }
151 
152 
153 ///////////////////////////////////////////////////
154 
155 
pgTextSearchDictionaryCollection(pgaFactory * factory,pgSchema * sch)156 pgTextSearchDictionaryCollection::pgTextSearchDictionaryCollection(pgaFactory *factory, pgSchema *sch)
157 	: pgSchemaObjCollection(factory, sch)
158 {
159 }
160 
161 
GetTranslatedMessage(int kindOfMessage) const162 wxString pgTextSearchDictionaryCollection::GetTranslatedMessage(int kindOfMessage) const
163 {
164 	wxString message = wxEmptyString;
165 
166 	switch (kindOfMessage)
167 	{
168 		case RETRIEVINGDETAILS:
169 			message = _("Retrieving details on FTS dictionaries");
170 			break;
171 		case REFRESHINGDETAILS:
172 			message = _("Refreshing FTS dictionaries");
173 			break;
174 		case OBJECTSLISTREPORT:
175 			message = _("FTS dictionaries list report");
176 			break;
177 	}
178 
179 	return message;
180 }
181 
182 
183 //////////////////////////////////////////////////////
184 
185 
CreateObjects(pgCollection * collection,ctlTree * browser,const wxString & restriction)186 pgObject *pgTextSearchDictionaryFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
187 {
188 	pgTextSearchDictionary *dict = 0;
189 
190 	pgSet *dictionaries;
191 	dictionaries = collection->GetDatabase()->ExecuteSet(
192 	                   wxT("SELECT dict.oid, dict.dictname, pg_get_userbyid(dict.dictowner) as dictowner, t.tmplname, dict.dictinitoption, description\n")
193 	                   wxT("  FROM pg_ts_dict dict\n")
194 	                   wxT("  LEFT OUTER JOIN pg_ts_template t ON t.oid=dict.dicttemplate\n")
195 	                   wxT("  LEFT OUTER JOIN pg_description des ON (des.objoid=dict.oid AND des.classoid='pg_ts_dict'::regclass)\n")
196 	                   wxT(" WHERE dict.dictnamespace = ") + collection->GetSchema()->GetOidStr()
197 	                   + restriction + wxT("\n")
198 	                   wxT(" ORDER BY dict.dictname"));
199 
200 	if (dictionaries)
201 	{
202 		while (!dictionaries->Eof())
203 		{
204 			dict = new pgTextSearchDictionary(collection->GetSchema(), dictionaries->GetVal(wxT("dictname")));
205 			dict->iSetOid(dictionaries->GetOid(wxT("oid")));
206 			dict->iSetOwner(dictionaries->GetVal(wxT("dictowner")));
207 			dict->iSetComment(dictionaries->GetVal(wxT("description")));
208 			dict->iSetTemplate(dictionaries->GetVal(wxT("tmplname")));
209 			dict->iSetOptions(dictionaries->GetVal(wxT("dictinitoption")));
210 
211 			if (browser)
212 			{
213 				browser->AppendObject(collection, dict);
214 				dictionaries->MoveNext();
215 			}
216 			else
217 				break;
218 		}
219 
220 		delete dictionaries;
221 	}
222 	return dict;
223 }
224 
225 
226 #include "images/dictionary.pngc"
227 #include "images/dictionaries.pngc"
228 
pgTextSearchDictionaryFactory()229 pgTextSearchDictionaryFactory::pgTextSearchDictionaryFactory()
230 	: pgSchemaObjFactory(__("FTS Dictionary"), __("New FTS Dictionary..."), __("Create a new FTS Dictionary."), dictionary_png_img)
231 {
232 }
233 
234 
CreateCollection(pgObject * obj)235 pgCollection *pgTextSearchDictionaryFactory::CreateCollection(pgObject *obj)
236 {
237 	return new pgTextSearchDictionaryCollection(GetCollectionFactory(), (pgSchema *)obj);
238 }
239 
240 pgTextSearchDictionaryFactory textSearchDictionaryFactory;
241 static pgaCollectionFactory cf(&textSearchDictionaryFactory, __("FTS Dictionaries"), dictionaries_png_img);
242