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

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

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

#include "dlg/dlgTextSearchConfiguration.h"
#include "schema/pgSchema.h"
#include "schema/pgTextSearchConfiguration.h"
#include "schema/pgDatatype.h"


// pointer to controls
#define cbParser            CTRL_COMBOBOX2("cbParser")
#define cbCopy              CTRL_COMBOBOX2("cbCopy")
#define lstTokens           CTRL_LISTVIEW("lstTokens")
#define cbToken             CTRL_COMBOBOX2("cbToken")
#define txtDictionary       CTRL_TEXT("txtDictionary")
#define cbDictionary        CTRL_CHOICE("cbDictionary")
#define btnAdd              CTRL_BUTTON("wxID_ADD")
#define btnRemove           CTRL_BUTTON("wxID_REMOVE")


BEGIN_EVENT_TABLE(dlgTextSearchConfiguration, dlgTypeProperty)
	EVT_TEXT(XRCID("cbParser"),                 dlgTextSearchConfiguration::OnChange)
	EVT_COMBOBOX(XRCID("cbParser"),             dlgTextSearchConfiguration::OnChange)
	EVT_TEXT(XRCID("cbCopy"),                   dlgTextSearchConfiguration::OnChange)
	EVT_COMBOBOX(XRCID("cbCopy"),               dlgTextSearchConfiguration::OnChange)
	EVT_LIST_ITEM_SELECTED(XRCID("lstTokens"),  dlgTextSearchConfiguration::OnSelChangeToken)
	EVT_TEXT(XRCID("cbToken"),                  dlgTextSearchConfiguration::OnChangeCbToken)
	EVT_COMBOBOX(XRCID("cbToken"),              dlgTextSearchConfiguration::OnChangeCbToken)
	EVT_TEXT(XRCID("txtDictionary"),            dlgTextSearchConfiguration::OnChangeTxtDictionary)
	EVT_CHOICE(XRCID("cbDictionary"),           dlgTextSearchConfiguration::OnChangeCbDictionary)
	EVT_BUTTON(wxID_ADD,                        dlgTextSearchConfiguration::OnAddToken)
	EVT_BUTTON(wxID_REMOVE,                     dlgTextSearchConfiguration::OnRemoveToken)
#ifdef __WXMAC__
	EVT_SIZE(                                   dlgTextSearchConfiguration::OnChangeSize)
#endif
END_EVENT_TABLE();



dlgProperty *pgTextSearchConfigurationFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgTextSearchConfiguration(this, frame, (pgTextSearchConfiguration *)node, (pgSchema *)parent);
}

dlgTextSearchConfiguration::dlgTextSearchConfiguration(pgaFactory *f, frmMain *frame, pgTextSearchConfiguration *node, pgSchema *sch)
	: dlgTypeProperty(f, frame, wxT("dlgTextSearchConfiguration"))
{
	schema = sch;
	config = node;
	dirtyTokens = false;

	lstTokens->CreateColumns(0, _("Token"), _("Dictionaries"));

	cbCopy->Disable();
}


pgObject *dlgTextSearchConfiguration::GetObject()
{
	return config;
}


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

	cbParser->Append(wxT(""));

	qry = wxT("SELECT prsname, nspname\n")
	      wxT("  FROM pg_ts_parser\n")
	      wxT("  JOIN pg_namespace n ON n.oid=prsnamespace\n")
	      wxT("  ORDER BY prsname\n");

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

	cbCopy->Append(wxT(""));

	qry = wxT("SELECT cfgname, nspname\n")
	      wxT("  FROM pg_ts_config\n")
	      wxT("  JOIN pg_namespace n ON n.oid=cfgnamespace\n")
	      wxT("  ORDER BY nspname, cfgname\n");

	set = connection->ExecuteSet(qry);
	if (set)
	{
		cbCopy->Enable();
		while (!set->Eof())
		{
			wxString configname = database->GetSchemaPrefix(set->GetVal(wxT("nspname"))) + set->GetVal(wxT("cfgname"));
			cbCopy->Append(configname);
			set->MoveNext();
		}
		delete set;
	}

	if (config)
	{
		// edit mode
		cbSchema->Enable(connection->BackendMinimumVersion(9, 1));
		cbParser->SetValue(config->GetParser());
		cbCopy->Disable();

		// second tab handling
		size_t i;
		for (i = 0 ; i < config->GetTokens().GetCount() ; i++)
		{
			wxString token = config->GetTokens().Item(i);
			lstTokens->AppendItem(token.BeforeFirst('/'), token.AfterFirst('/'));
		}

		pgSet *tokens;
		tokens = connection->ExecuteSet(
		             wxT("SELECT alias FROM ts_token_type(")
		             + config->GetParserOidStr()
		             + wxT(") ORDER BY alias"));

		if (tokens)
		{
			while (!tokens->Eof())
			{
				cbToken->Append(tokens->GetVal(wxT("alias")));
				tokens->MoveNext();
			}
			delete tokens;
		}

		pgSet *dictionaries;
		dictionaries = connection->ExecuteSet(
		                   wxT("SELECT dictname FROM pg_ts_dict ORDER BY dictname"));

		if (dictionaries)
		{
			while (!dictionaries->Eof())
			{
				cbDictionary->Append(dictionaries->GetVal(wxT("dictname")));
				dictionaries->MoveNext();
			}
			delete dictionaries;
		}

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

	btnAdd->Disable();
	btnRemove->Disable();

	return dlgProperty::Go(modal);
}


pgObject *dlgTextSearchConfiguration::CreateObject(pgCollection *collection)
{
	pgObject *obj = textSearchConfigurationFactory.CreateObjects(collection, 0,
	                wxT("\n   AND cfg.cfgname=") + qtDbString(GetName()) +
	                wxT("\n   AND cfg.cfgnamespace=") + schema->GetOidStr());

	return obj;
}


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


void dlgTextSearchConfiguration::CheckChange()
{
	if (config)
	{
		EnableOK(txtName->GetValue() != config->GetName()
		         || cbSchema->GetValue() != config->GetSchema()->GetName()
		         || txtComment->GetValue() != config->GetComment()
		         || cbOwner->GetValue() != config->GetOwner()
		         || dirtyTokens);
	}
	else
	{
		wxString name = GetName();
		bool enable = true;
		CheckValid(enable, !name.IsEmpty(), _("Please specify name."));
		CheckValid(enable, cbParser->GetGuessedSelection() > 0 || cbCopy->GetGuessedSelection() > 0 , _("Please select a parser or a configuration to copy."));

		EnableOK(enable);
	}
}


void dlgTextSearchConfiguration::OnChange(wxCommandEvent &ev)
{
	cbParser->Enable(cbCopy->GetValue().Length() == 0);
	cbCopy->Enable(cbParser->GetValue().Length() == 0);

	CheckChange();
}

void dlgTextSearchConfiguration::OnChangeCbToken(wxCommandEvent &ev)
{
	bool found = false;

	for (int pos = 0 ; pos < lstTokens->GetItemCount() ; pos++)
	{
		if (lstTokens->GetText(pos).IsSameAs(cbToken->GetValue(), false))
		{
			lstTokens->Select(pos);
			found = true;
			break;
		}
	}

	btnAdd->Enable(cbToken->GetValue().Length() > 0);
}


void dlgTextSearchConfiguration::OnChangeCbDictionary(wxCommandEvent &ev)
{
	if (!txtDictionary->GetValue().Matches(wxT("*") + cbDictionary->GetStringSelection() + wxT("*")))
	{
		wxString dicts = txtDictionary->GetValue();
		if (dicts.Length() > 0)
			dicts += wxT(",");
		dicts += cbDictionary->GetStringSelection();

		txtDictionary->SetValue(dicts);
	}
	btnAdd->Enable(cbToken->GetValue().Length() > 0);
}


void dlgTextSearchConfiguration::OnChangeTxtDictionary(wxCommandEvent &ev)
{
	btnAdd->Enable(cbToken->GetValue().Length() > 0);
}


void dlgTextSearchConfiguration::OnSelChangeToken(wxListEvent &ev)
{
	int row = lstTokens->GetSelection();
	if (row >= 0)
	{
		cbToken->SetValue(lstTokens->GetText(row, 0));
		txtDictionary->SetValue(lstTokens->GetText(row, 1));
	}

	btnAdd->Enable(cbToken->GetValue().Length() > 0);
	btnRemove->Enable(row >= 0);
}


void dlgTextSearchConfiguration::OnAddToken(wxCommandEvent &ev)
{
	bool found = false;

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

	if (!found)
	{
		lstTokens->AppendItem(cbToken->GetValue(), txtDictionary->GetValue());
	}

	btnAdd->Disable();

	dirtyTokens = true;

	CheckChange();
}


void dlgTextSearchConfiguration::OnRemoveToken(wxCommandEvent &ev)
{
	for (int pos = 0 ; pos < lstTokens->GetItemCount() ; pos++)
	{
		if (lstTokens->GetText(pos).IsSameAs(cbToken->GetValue(), false))
		{
			lstTokens->DeleteItem(pos);
			break;
		}
	}

	cbToken->SetValue(wxT(""));
	txtDictionary->SetValue(wxT(""));

	btnRemove->Disable();

	dirtyTokens = true;

	CheckChange();
}


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

	if (config)
	{
		objname = schema->GetQuotedPrefix() + qtIdent(config->GetName());
	}
	else
	{
		objname = schema->GetQuotedPrefix() + qtIdent(GetName());
	}

	if (cbParser->GetValue().Length() > 0)
	{
		wxArrayString toks;
		size_t index;

		if (config)
		{
			for (index = 0 ; index < config->GetTokens().GetCount() ; index++)
				toks.Add(config->GetTokens().Item(index));
		}

		int cnt = lstTokens->GetItemCount();
		int pos;

		// check for changed or added tokens
		for (pos = 0 ; pos < cnt ; pos++)
		{
			wxString newTok = lstTokens->GetText(pos);
			wxString newVal = lstTokens->GetText(pos, 1);

			wxString oldVal;

			for (index = 0 ; index < toks.GetCount() ; index++)
			{
				wxString tok = toks.Item(index);
				if (tok.BeforeFirst('/').IsSameAs(newTok, false))
				{
					oldVal = tok.Mid(newTok.Length() + 1);
					toks.RemoveAt(index);
					break;
				}
			}
			if (oldVal != newVal)
			{
				if (oldVal.Length() == 0)
				{
					sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
					       +  wxT("\n  ADD MAPPING FOR ") + newTok
					       +  wxT("\n  WITH ") + newVal
					       +  wxT(";\n");
				}
				else
				{
					sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
					       +  wxT("\n  ALTER MAPPING FOR ") + newTok
					       +  wxT("\n  WITH ") + newVal
					       +  wxT(";\n");
				}
			}
		}

		// check for removed tokens
		wxString oldTok;
		for (pos = 0 ; pos < (int)toks.GetCount() ; pos++)
		{
			if (!toks.Item(pos).BeforeFirst('/').IsSameAs(oldTok, false))
			{
				oldTok = toks.Item(pos).BeforeFirst('/');
				sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
				       +  wxT(" DROP MAPPING FOR ") + oldTok
				       + wxT(";\n");
			}
		}
	}

	if (config)
	{
		// edit mode
		objname = schema->GetQuotedPrefix() + qtIdent(GetName());
		AppendNameChange(sql, wxT("TEXT SEARCH CONFIGURATION ") + config->GetQuotedFullIdentifier());
		AppendOwnerChange(sql, wxT("TEXT SEARCH CONFIGURATION ") + objname);
		AppendSchemaChange(sql, wxT("TEXT SEARCH CONFIGURATION ") + objname);
	}
	else
	{
		// create mode
		objname = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());
		sql = wxT("CREATE TEXT SEARCH CONFIGURATION ")
		      + objname
		      + wxT(" (");

		AppendIfFilled(sql, wxT("\n   PARSER="), cbParser->GetValue());
		AppendIfFilled(sql, wxT("\n   COPY="), cbCopy->GetValue());

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

	}

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

	return sql;
}