1
//////////////////////////////////////////////////////////////////////////
3
// pgAdmin III - PostgreSQL Tools
4
// RCS-ID: $Id: dlgTextSearchConfiguration.cpp 7758 2009-03-26 20:49:59Z dpage $
5
// Copyright (C) 2002 - 2009, The pgAdmin Development Team
6
// This software is released under the BSD Licence
8
// dlgTextSearchConfiguration.cpp - PostgreSQL Text Search Configuration Property
10
//////////////////////////////////////////////////////////////////////////
17
#include "utils/misc.h"
18
#include "utils/pgDefs.h"
20
#include "dlg/dlgTextSearchConfiguration.h"
21
#include "schema/pgSchema.h"
22
#include "schema/pgTextSearchConfiguration.h"
23
#include "schema/pgDatatype.h"
26
// pointer to controls
27
#define cbParser CTRL_COMBOBOX2("cbParser")
28
#define cbCopy CTRL_COMBOBOX2("cbCopy")
29
#define lstTokens CTRL_LISTVIEW("lstTokens")
30
#define cbToken CTRL_COMBOBOX2("cbToken")
31
#define txtDictionary CTRL_TEXT("txtDictionary")
32
#define cbDictionary CTRL_CHOICE("cbDictionary")
33
#define btnAdd CTRL_BUTTON("wxID_ADD")
34
#define btnRemove CTRL_BUTTON("wxID_REMOVE")
37
BEGIN_EVENT_TABLE(dlgTextSearchConfiguration, dlgTypeProperty)
38
EVT_TEXT(XRCID("cbParser"), dlgTextSearchConfiguration::OnChange)
39
EVT_COMBOBOX(XRCID("cbParser"), dlgTextSearchConfiguration::OnChange)
40
EVT_TEXT(XRCID("cbCopy"), dlgTextSearchConfiguration::OnChange)
41
EVT_COMBOBOX(XRCID("cbCopy"), dlgTextSearchConfiguration::OnChange)
42
EVT_LIST_ITEM_SELECTED(XRCID("lstTokens"), dlgTextSearchConfiguration::OnSelChangeToken)
43
EVT_TEXT(XRCID("cbToken"), dlgTextSearchConfiguration::OnChangeCbToken)
44
EVT_COMBOBOX(XRCID("cbToken"), dlgTextSearchConfiguration::OnChangeCbToken)
45
EVT_TEXT(XRCID("txtDictionary"), dlgTextSearchConfiguration::OnChangeTxtDictionary)
46
EVT_CHOICE(XRCID("cbDictionary"), dlgTextSearchConfiguration::OnChangeCbDictionary)
47
EVT_BUTTON(wxID_ADD, dlgTextSearchConfiguration::OnAddToken)
48
EVT_BUTTON(wxID_REMOVE, dlgTextSearchConfiguration::OnRemoveToken)
50
EVT_SIZE( dlgTextSearchConfiguration::OnChangeSize)
56
dlgProperty *pgTextSearchConfigurationFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
58
return new dlgTextSearchConfiguration(this, frame, (pgTextSearchConfiguration*)node, (pgSchema*)parent);
61
dlgTextSearchConfiguration::dlgTextSearchConfiguration(pgaFactory *f, frmMain *frame, pgTextSearchConfiguration *node, pgSchema *sch)
62
: dlgTypeProperty(f, frame, wxT("dlgTextSearchConfiguration"))
68
lstTokens->CreateColumns(0, _("Token"), _("Dictionaries"));
74
pgObject *dlgTextSearchConfiguration::GetObject()
80
int dlgTextSearchConfiguration::Go(bool modal)
85
cbParser->Append(wxT(""));
87
qry = wxT("SELECT prsname, nspname\n")
88
wxT(" FROM pg_ts_parser\n")
89
wxT(" JOIN pg_namespace n ON n.oid=prsnamespace\n")
90
wxT(" ORDER BY prsname\n");
92
set = connection->ExecuteSet(qry);
97
wxString procname = database->GetSchemaPrefix(set->GetVal(wxT("nspname"))) + set->GetVal(wxT("prsname"));
98
cbParser->Append(procname);
104
cbCopy->Append(wxT(""));
106
qry = wxT("SELECT cfgname, nspname\n")
107
wxT(" FROM pg_ts_config\n")
108
wxT(" JOIN pg_namespace n ON n.oid=cfgnamespace\n")
109
wxT(" ORDER BY nspname, cfgname\n");
111
set = connection->ExecuteSet(qry);
117
wxString configname = database->GetSchemaPrefix(set->GetVal(wxT("nspname"))) + set->GetVal(wxT("cfgname"));
118
cbCopy->Append(configname);
127
cbParser->SetValue(config->GetParser());
130
// second tab handling
132
for (i=0 ; i < config->GetTokens().GetCount() ; i++)
134
wxString token=config->GetTokens().Item(i);
135
lstTokens->AppendItem(token.BeforeFirst('/'), token.AfterFirst('/'));
139
tokens = connection->ExecuteSet(
140
wxT("SELECT alias FROM ts_token_type(")
141
+ config->GetParserOidStr()
142
+ wxT(") ORDER BY alias"));
146
while (!tokens->Eof())
148
cbToken->Append(tokens->GetVal(wxT("alias")));
155
dictionaries = connection->ExecuteSet(
156
wxT("SELECT dictname FROM pg_ts_dict ORDER BY dictname"));
160
while (!dictionaries->Eof())
162
cbDictionary->Append(dictionaries->GetVal(wxT("dictname")));
163
dictionaries->MoveNext();
168
if (!connection->BackendMinimumVersion(8, 0))
177
btnRemove->Disable();
179
return dlgProperty::Go(modal);
183
pgObject *dlgTextSearchConfiguration::CreateObject(pgCollection *collection)
185
pgObject *obj=textSearchConfigurationFactory.CreateObjects(collection, 0,
186
wxT("\n AND cfg.cfgname=") + qtDbString(GetName()) +
187
wxT("\n AND cfg.cfgnamespace=") + schema->GetOidStr());
194
void dlgTextSearchConfiguration::OnChangeSize(wxSizeEvent &ev)
196
lstTokens->SetSize(wxDefaultCoord, wxDefaultCoord,
197
ev.GetSize().GetWidth(), ev.GetSize().GetHeight() - 350);
206
void dlgTextSearchConfiguration::CheckChange()
210
EnableOK(txtName->GetValue() != config->GetName()
211
|| txtComment->GetValue() != config->GetComment()
212
|| cbOwner->GetValue() != config->GetOwner()
217
wxString name=GetName();
219
CheckValid(enable, !name.IsEmpty(), _("Please specify name."));
220
CheckValid(enable, cbParser->GetGuessedSelection()>0 || cbCopy->GetGuessedSelection() > 0 , _("Please select a parser or a configuration to copy."));
227
void dlgTextSearchConfiguration::OnChange(wxCommandEvent &ev)
229
cbParser->Enable(cbCopy->GetValue().Length() == 0);
230
cbCopy->Enable(cbParser->GetValue().Length() == 0);
235
void dlgTextSearchConfiguration::OnChangeCbToken(wxCommandEvent &ev)
239
for (int pos = 0 ; pos < lstTokens->GetItemCount() ; pos++)
241
if (lstTokens->GetText(pos).IsSameAs(cbToken->GetValue(), false))
243
lstTokens->Select(pos);
249
btnAdd->Enable(cbToken->GetValue().Length() > 0);
253
void dlgTextSearchConfiguration::OnChangeCbDictionary(wxCommandEvent &ev)
255
if (!txtDictionary->GetValue().Matches(wxT("*") + cbDictionary->GetStringSelection() + wxT("*")))
257
wxString dicts = txtDictionary->GetValue();
258
if (dicts.Length() > 0)
260
dicts += cbDictionary->GetStringSelection();
262
txtDictionary->SetValue(dicts);
264
btnAdd->Enable(cbToken->GetValue().Length() > 0);
268
void dlgTextSearchConfiguration::OnChangeTxtDictionary(wxCommandEvent &ev)
270
btnAdd->Enable(cbToken->GetValue().Length() > 0);
274
void dlgTextSearchConfiguration::OnSelChangeToken(wxListEvent &ev)
276
int row=lstTokens->GetSelection();
279
cbToken->SetValue(lstTokens->GetText(row, 0));
280
txtDictionary->SetValue(lstTokens->GetText(row, 1));
283
btnAdd->Enable(cbToken->GetValue().Length() > 0);
284
btnRemove->Enable(row >= 0);
288
void dlgTextSearchConfiguration::OnAddToken(wxCommandEvent &ev)
292
for (int pos = 0 ; pos < lstTokens->GetItemCount() ; pos++)
294
if (lstTokens->GetText(pos).IsSameAs(cbToken->GetValue(), false))
296
lstTokens->SetItem(pos, 1, txtDictionary->GetValue());
304
lstTokens->AppendItem(cbToken->GetValue(), txtDictionary->GetValue());
315
void dlgTextSearchConfiguration::OnRemoveToken(wxCommandEvent &ev)
317
for (int pos = 0 ; pos < lstTokens->GetItemCount() ; pos++)
319
if (lstTokens->GetText(pos).IsSameAs(cbToken->GetValue(), false))
321
lstTokens->DeleteItem(pos);
326
cbToken->SetValue(wxT(""));
327
txtDictionary->SetValue(wxT(""));
329
btnRemove->Disable();
337
wxString dlgTextSearchConfiguration::GetSql()
340
wxString objname=schema->GetQuotedPrefix() + qtIdent(GetName());
345
AppendNameChange(sql);
346
AppendOwnerChange(sql, wxT("TEXT SEARCH CONFIGURATION ") + objname);
351
sql = wxT("CREATE TEXT SEARCH CONFIGURATION ")
352
+ schema->GetQuotedPrefix() + GetName()
355
AppendIfFilled(sql, wxT("\n PARSER="), cbParser->GetValue());
356
AppendIfFilled(sql, wxT("\n COPY="), cbCopy->GetValue());
358
sql += wxT("\n);\n");
362
if (cbParser->GetValue().Length() > 0)
369
for (index = 0 ; index < config->GetTokens().GetCount() ; index++)
370
toks.Add(config->GetTokens().Item(index));
373
int cnt=lstTokens->GetItemCount();
376
// check for changed or added tokens
377
for (pos=0 ; pos < cnt ; pos++)
379
wxString newTok=lstTokens->GetText(pos);
380
wxString newVal=lstTokens->GetText(pos, 1);
384
for (index=0 ; index < toks.GetCount() ; index++)
386
wxString tok=toks.Item(index);
387
if (tok.BeforeFirst('/').IsSameAs(newTok, false))
389
oldVal = tok.Mid(newTok.Length()+1);
390
toks.RemoveAt(index);
394
if (oldVal != newVal)
396
if (oldVal.Length() == 0)
398
sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
399
+ wxT(" ADD MAPPING FOR ") + newTok
400
+ wxT(" WITH ") + newVal
405
sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
406
+ wxT(" ALTER MAPPING FOR ") + newTok
407
+ wxT(" WITH ") + newVal
413
// check for removed tokens
415
for (pos=0 ; pos < (int)toks.GetCount() ; pos++)
417
if (!toks.Item(pos).BeforeFirst('/').IsSameAs(oldTok, false))
419
oldTok = toks.Item(pos).BeforeFirst('/');
420
sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
421
+ wxT(" DROP MAPPING FOR ") + oldTok
427
AppendComment(sql, wxT("TEXT SEARCH CONFIGURATION ") + objname, config);