File: dlgTextSearchDictionary.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 (350 lines) | stat: -rw-r--r-- 8,820 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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgTextSearchDictionary.cpp - PostgreSQL Text Search Dictionary Property
//
//////////////////////////////////////////////////////////////////////////

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

// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "utils/pgDefs.h"

#include "dlg/dlgTextSearchDictionary.h"
#include "schema/pgSchema.h"
#include "schema/pgTextSearchDictionary.h"
#include "schema/pgDatatype.h"


// pointer to controls
#define cbTemplate          CTRL_COMBOBOX2("cbTemplate")
#define lstOptions          CTRL_LISTVIEW("lstOptions")
#define txtOption           CTRL_TEXT("txtOption")
#define txtValue            CTRL_TEXT("txtValue")
#define btnAdd              CTRL_BUTTON("wxID_ADD")
#define btnRemove           CTRL_BUTTON("wxID_REMOVE")


BEGIN_EVENT_TABLE(dlgTextSearchDictionary, dlgTypeProperty)
	EVT_TEXT(XRCID("cbTemplate"),               dlgTextSearchDictionary::OnChange)
	EVT_COMBOBOX(XRCID("cbTemplate"),           dlgTextSearchDictionary::OnChange)
	EVT_LIST_ITEM_SELECTED(XRCID("lstOptions"), dlgTextSearchDictionary::OnSelChangeOption)
	EVT_TEXT(XRCID("txtOption"),                dlgTextSearchDictionary::OnChangeOptionName)
	EVT_BUTTON(wxID_ADD,                        dlgTextSearchDictionary::OnAddOption)
	EVT_BUTTON(wxID_REMOVE,                     dlgTextSearchDictionary::OnRemoveOption)
#ifdef __WXMAC__
	EVT_SIZE(                                   dlgTextSearchDictionary::OnChangeSize)
#endif
END_EVENT_TABLE();



dlgProperty *pgTextSearchDictionaryFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgTextSearchDictionary(this, frame, (pgTextSearchDictionary *)node, (pgSchema *)parent);
}

dlgTextSearchDictionary::dlgTextSearchDictionary(pgaFactory *f, frmMain *frame, pgTextSearchDictionary *node, pgSchema *sch)
	: dlgTypeProperty(f, frame, wxT("dlgTextSearchDictionary"))
{
	schema = sch;
	dict = node;
}


pgObject *dlgTextSearchDictionary::GetObject()
{
	return dict;
}


int dlgTextSearchDictionary::Go(bool modal)
{
	wxString qry;
	pgSet *set;

	qry = wxT("SELECT tmplname, nspname\n")
	      wxT("  FROM pg_ts_template\n")
	      wxT("  JOIN pg_namespace n ON n.oid=tmplnamespace\n")
	      wxT("  ORDER BY tmplname\n");

	set = connection->ExecuteSet(qry);
	if (set)
	{
		while (!set->Eof())
		{
			wxString procname = database->GetSchemaPrefix(set->GetVal(wxT("nspname"))) + set->GetVal(wxT("tmplname"));
			cbTemplate->Append(procname);
			set->MoveNext();
		}
		delete set;
	}

	lstOptions->AddColumn(_("Option"), 80);
	lstOptions->AddColumn(_("Value"), 40);

	if (dict)
	{
		// edit mode
		cbSchema->Enable(connection->BackendMinimumVersion(9, 1));
		cbTemplate->SetValue(dict->GetTemplate());
		cbTemplate->Disable();

		wxString options = dict->GetOptions();
		wxString option, optionname, optionvalue;
		while (options.Length() > 0)
		{
			option = options.BeforeFirst(',');
			optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
			optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();
			lstOptions->AppendItem(optionname, optionvalue);
			options = options.AfterFirst(',');
		}

		if (!connection->BackendMinimumVersion(8, 0))
			cbOwner->Disable();
	}
	else
	{
		// create mode
	}

	txtOption->SetValue(wxT(""));
	txtValue->SetValue(wxT(""));
	btnAdd->Disable();
	btnRemove->Disable();

	return dlgProperty::Go(modal);
}


pgObject *dlgTextSearchDictionary::CreateObject(pgCollection *collection)
{
	pgObject *obj = textSearchDictionaryFactory.CreateObjects(collection, 0,
	                wxT("\n   AND dict.dictname=") + qtDbString(GetName()) +
	                wxT("\n   AND dict.dictnamespace=") + schema->GetOidStr());

	return obj;
}


#ifdef __WXMAC__
void dlgTextSearchDictionary::OnChangeSize(wxSizeEvent &ev)
{
	lstOptions->SetSize(wxDefaultCoord, wxDefaultCoord,
	                    ev.GetSize().GetWidth(), ev.GetSize().GetHeight() - 350);
	if (GetAutoLayout())
	{
		Layout();
	}
}
#endif


void dlgTextSearchDictionary::CheckChange()
{
	if (dict)
	{
		EnableOK(txtName->GetValue() != dict->GetName()
		         || cbSchema->GetValue() != dict->GetSchema()->GetName()
		         || txtComment->GetValue() != dict->GetComment()
		         || cbOwner->GetValue() != dict->GetOwner()
		         || GetOptionsSql().Length() > 0);
	}
	else
	{
		wxString name = GetName();
		bool enable = true;
		CheckValid(enable, !name.IsEmpty(), _("Please specify name."));
		CheckValid(enable, cbTemplate->GetValue().Length() > 0 , _("Please select a template."));

		EnableOK(enable);
	}
}


void dlgTextSearchDictionary::OnChange(wxCommandEvent &ev)
{
	CheckChange();
}


void dlgTextSearchDictionary::OnChangeOptionName(wxCommandEvent &ev)
{
	btnAdd->Enable(txtOption->GetValue().Length() > 0);
}


void dlgTextSearchDictionary::OnSelChangeOption(wxListEvent &ev)
{
	int row = lstOptions->GetSelection();
	if (row >= 0)
	{
		txtOption->SetValue(lstOptions->GetText(row, 0));
		txtValue->SetValue(lstOptions->GetText(row, 1));
	}

	btnRemove->Enable(row >= 0);
}


void dlgTextSearchDictionary::OnAddOption(wxCommandEvent &ev)
{
	bool found = false;

	for (int pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
	{
		if (lstOptions->GetText(pos).IsSameAs(txtOption->GetValue(), false))
		{
			lstOptions->SetItem(pos, 1, txtValue->GetValue());
			found = true;
			break;
		}
	}

	if (!found)
	{
		lstOptions->AppendItem(txtOption->GetValue(), txtValue->GetValue());
	}

	txtOption->SetValue(wxT(""));
	txtValue->SetValue(wxT(""));
	btnAdd->Disable();

	CheckChange();
}


void dlgTextSearchDictionary::OnRemoveOption(wxCommandEvent &ev)
{
	int sel = lstOptions->GetSelection();
	lstOptions->DeleteItem(sel);

	txtOption->SetValue(wxT(""));
	txtValue->SetValue(wxT(""));
	btnRemove->Disable();

	CheckChange();
}


wxString dlgTextSearchDictionary::GetOptionsSql()
{
	wxString options = dict->GetOptions();
	wxString option, optionname, optionvalue, sqloptions;
	bool found;
	int pos;

	while (options.Length() > 0)
	{
		option = options.BeforeFirst(',');
		optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
		optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();

		// check for options
		found = false;
		for (pos = 0 ; pos < lstOptions->GetItemCount() && !found; pos++)
		{
			found = lstOptions->GetText(pos, 0).Cmp(optionname) == 0;
			if (found) break;
		}

		if (found)
		{
			if (lstOptions->GetText(pos, 1).Cmp(optionvalue) != 0)
			{
				if (sqloptions.Length() > 0)
					sqloptions += wxT(", ");
				sqloptions += optionname + wxT("=") + lstOptions->GetText(pos, 1);
			}
		}
		else
		{
			if (sqloptions.Length() > 0)
				sqloptions += wxT(", ");
			sqloptions += optionname;
		}

		options = options.AfterFirst(',');
	}

	for (pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
	{
		options = dict->GetOptions();
		found = false;

		while (options.Length() > 0 && !found)
		{
			option = options.BeforeFirst(',');
			optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
			found = lstOptions->GetText(pos, 0).Cmp(optionname) == 0;
			options = options.AfterFirst(',');
		}

		if (!found)
		{
			optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();

			if (sqloptions.Length() > 0)
				sqloptions += wxT(", ");
			sqloptions += lstOptions->GetText(pos, 0) + wxT("=") + lstOptions->GetText(pos, 1);
		}
	}

	return sqloptions;
}


wxString dlgTextSearchDictionary::GetSql()
{
	wxString sql;
	wxString objname;

	if (dict)
	{
		// edit mode
		objname = schema->GetQuotedPrefix() + qtIdent(GetName());
		AppendNameChange(sql, wxT("TEXT SEARCH DICTIONARY ") + dict->GetQuotedFullIdentifier());

		wxString sqloptions = GetOptionsSql();
		if (sqloptions.Length() > 0)
		{
			sql += wxT("ALTER TEXT SEARCH DICTIONARY ") + objname
			       + wxT("\n  (") + sqloptions + wxT(");\n");
		}
		AppendOwnerChange(sql, wxT("TEXT SEARCH DICTIONARY ") + objname);

		AppendSchemaChange(sql, wxT("TEXT SEARCH DICTIONARY ") + objname);
	}
	else
	{
		// create mode
		objname = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());
		sql = wxT("CREATE TEXT SEARCH DICTIONARY ")
		      + objname
		      + wxT("\n  (")
		      + wxT("\n  TEMPLATE = ") + cbTemplate->GetValue();

		// check for options
		for (int pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
		{
			sql += wxT(", ") + lstOptions->GetText(pos, 0)
			       + wxT("=") + lstOptions->GetText(pos, 1);
		}

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

		AppendOwnerNew(sql, wxT("TEXT SEARCH DICTIONARY ") + objname);
	}

	AppendComment(sql, wxT("TEXT SEARCH DICTIONARY ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), dict);

	return sql;
}