~ubuntu-branches/debian/sid/pgadmin3/sid

« back to all changes in this revision

Viewing changes to pgadmin/dlg/dlgTextSearchConfiguration.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gerfried Fuchs
  • Date: 2009-07-30 12:27:16 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730122716-fddbh42on721bbs2
Tags: 1.10.0-1
* New upstream release.
* Adjusted watch file to match release candidates.
* Updated to Standards-Version 3.8.2:
  - Moved to Section: database.
  - Add DEB_BUILD_OPTIONS support for parallel building.
  - Move from findstring to filter suggestion for DEB_BUILD_OPTIONS parsing.
* pgagent got split into its own separate source package by upstream.
* Exclude Docs.vcproj from installation.
* Move doc-base.enus from pgadmin3 to pgadmin3-data package, the files are
  in there too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////
 
2
//
 
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
 
7
//
 
8
// dlgTextSearchConfiguration.cpp - PostgreSQL Text Search Configuration Property
 
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 "utils/pgDefs.h"
 
19
 
 
20
#include "dlg/dlgTextSearchConfiguration.h"
 
21
#include "schema/pgSchema.h"
 
22
#include "schema/pgTextSearchConfiguration.h"
 
23
#include "schema/pgDatatype.h"
 
24
 
 
25
 
 
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")
 
35
 
 
36
 
 
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)
 
49
#ifdef __WXMAC__
 
50
    EVT_SIZE(                                   dlgTextSearchConfiguration::OnChangeSize)
 
51
#endif
 
52
END_EVENT_TABLE();
 
53
 
 
54
 
 
55
 
 
56
dlgProperty *pgTextSearchConfigurationFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
 
57
{
 
58
    return new dlgTextSearchConfiguration(this, frame, (pgTextSearchConfiguration*)node, (pgSchema*)parent);
 
59
}
 
60
 
 
61
dlgTextSearchConfiguration::dlgTextSearchConfiguration(pgaFactory *f, frmMain *frame, pgTextSearchConfiguration *node, pgSchema *sch)
 
62
: dlgTypeProperty(f, frame, wxT("dlgTextSearchConfiguration"))
 
63
{
 
64
    schema=sch;
 
65
    config=node;
 
66
    dirtyTokens = false;
 
67
 
 
68
    lstTokens->CreateColumns(0, _("Token"), _("Dictionaries"));
 
69
 
 
70
    cbCopy->Disable();
 
71
}
 
72
 
 
73
 
 
74
pgObject *dlgTextSearchConfiguration::GetObject()
 
75
{
 
76
    return config;
 
77
}
 
78
 
 
79
 
 
80
int dlgTextSearchConfiguration::Go(bool modal)
 
81
{
 
82
    wxString qry;
 
83
    pgSet *set;
 
84
 
 
85
    cbParser->Append(wxT(""));
 
86
 
 
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");
 
91
 
 
92
    set = connection->ExecuteSet(qry);
 
93
    if (set)
 
94
    {
 
95
        while (!set->Eof())
 
96
        {
 
97
            wxString procname = database->GetSchemaPrefix(set->GetVal(wxT("nspname"))) + set->GetVal(wxT("prsname"));
 
98
            cbParser->Append(procname);
 
99
            set->MoveNext();
 
100
        }
 
101
        delete set;
 
102
    }
 
103
 
 
104
    cbCopy->Append(wxT(""));
 
105
 
 
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");
 
110
 
 
111
    set = connection->ExecuteSet(qry);
 
112
    if (set)
 
113
    {
 
114
        cbCopy->Enable();
 
115
        while (!set->Eof())
 
116
        {
 
117
            wxString configname = database->GetSchemaPrefix(set->GetVal(wxT("nspname"))) + set->GetVal(wxT("cfgname"));
 
118
            cbCopy->Append(configname);
 
119
            set->MoveNext();
 
120
        }
 
121
        delete set;
 
122
    }
 
123
 
 
124
    if (config)
 
125
    {
 
126
        // edit mode
 
127
        cbParser->SetValue(config->GetParser());
 
128
        cbCopy->Disable();
 
129
 
 
130
        // second tab handling
 
131
        size_t i;
 
132
        for (i=0 ; i < config->GetTokens().GetCount() ; i++)
 
133
        {
 
134
            wxString token=config->GetTokens().Item(i);
 
135
            lstTokens->AppendItem(token.BeforeFirst('/'), token.AfterFirst('/'));
 
136
        }
 
137
 
 
138
        pgSet *tokens;
 
139
        tokens = connection->ExecuteSet(
 
140
            wxT("SELECT alias FROM ts_token_type(")
 
141
            + config->GetParserOidStr()
 
142
            + wxT(") ORDER BY alias"));
 
143
 
 
144
        if (tokens)
 
145
        {
 
146
            while (!tokens->Eof())
 
147
            {
 
148
                cbToken->Append(tokens->GetVal(wxT("alias")));
 
149
                tokens->MoveNext();
 
150
            }
 
151
            delete tokens;
 
152
        }
 
153
 
 
154
        pgSet *dictionaries;
 
155
        dictionaries = connection->ExecuteSet(
 
156
          wxT("SELECT dictname FROM pg_ts_dict ORDER BY dictname"));
 
157
 
 
158
        if (dictionaries)
 
159
        {
 
160
            while (!dictionaries->Eof())
 
161
            {
 
162
                cbDictionary->Append(dictionaries->GetVal(wxT("dictname")));
 
163
                dictionaries->MoveNext();
 
164
            }
 
165
            delete dictionaries;
 
166
        }
 
167
 
 
168
        if (!connection->BackendMinimumVersion(8, 0))
 
169
            cbOwner->Disable();
 
170
    }
 
171
    else
 
172
    {
 
173
        // create mode
 
174
    }
 
175
 
 
176
    btnAdd->Disable();
 
177
    btnRemove->Disable();
 
178
 
 
179
    return dlgProperty::Go(modal);
 
180
}
 
181
 
 
182
 
 
183
pgObject *dlgTextSearchConfiguration::CreateObject(pgCollection *collection)
 
184
{
 
185
    pgObject *obj=textSearchConfigurationFactory.CreateObjects(collection, 0,
 
186
         wxT("\n   AND cfg.cfgname=") + qtDbString(GetName()) +
 
187
         wxT("\n   AND cfg.cfgnamespace=") + schema->GetOidStr());
 
188
 
 
189
    return obj;
 
190
}
 
191
 
 
192
 
 
193
#ifdef __WXMAC__
 
194
void dlgTextSearchConfiguration::OnChangeSize(wxSizeEvent &ev)
 
195
{
 
196
        lstTokens->SetSize(wxDefaultCoord, wxDefaultCoord,
 
197
            ev.GetSize().GetWidth(), ev.GetSize().GetHeight() - 350);
 
198
    if (GetAutoLayout())
 
199
    {
 
200
        Layout();
 
201
    }
 
202
}
 
203
#endif
 
204
 
 
205
 
 
206
void dlgTextSearchConfiguration::CheckChange()
 
207
{
 
208
    if (config)
 
209
    {
 
210
        EnableOK(txtName->GetValue() != config->GetName()
 
211
            || txtComment->GetValue() != config->GetComment()
 
212
            || cbOwner->GetValue() != config->GetOwner()
 
213
            || dirtyTokens);
 
214
    }
 
215
    else
 
216
    {
 
217
        wxString name=GetName();
 
218
        bool enable=true;
 
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."));
 
221
 
 
222
        EnableOK(enable);
 
223
    }
 
224
}
 
225
 
 
226
 
 
227
void dlgTextSearchConfiguration::OnChange(wxCommandEvent &ev)
 
228
{
 
229
    cbParser->Enable(cbCopy->GetValue().Length() == 0);
 
230
    cbCopy->Enable(cbParser->GetValue().Length() == 0);
 
231
 
 
232
    CheckChange();
 
233
}
 
234
 
 
235
void dlgTextSearchConfiguration::OnChangeCbToken(wxCommandEvent &ev)
 
236
{
 
237
    bool found = false;
 
238
 
 
239
    for (int pos = 0 ; pos < lstTokens->GetItemCount() ; pos++)
 
240
    {
 
241
        if (lstTokens->GetText(pos).IsSameAs(cbToken->GetValue(), false))
 
242
        {
 
243
            lstTokens->Select(pos);
 
244
            found = true;
 
245
            break;
 
246
        }
 
247
    }
 
248
 
 
249
    btnAdd->Enable(cbToken->GetValue().Length() > 0);
 
250
}
 
251
 
 
252
 
 
253
void dlgTextSearchConfiguration::OnChangeCbDictionary(wxCommandEvent &ev)
 
254
{
 
255
    if (!txtDictionary->GetValue().Matches(wxT("*") + cbDictionary->GetStringSelection() + wxT("*")))
 
256
    {
 
257
        wxString dicts = txtDictionary->GetValue();
 
258
        if (dicts.Length() > 0)
 
259
            dicts += wxT(",");
 
260
        dicts += cbDictionary->GetStringSelection();
 
261
 
 
262
        txtDictionary->SetValue(dicts);
 
263
    }
 
264
    btnAdd->Enable(cbToken->GetValue().Length() > 0);
 
265
}
 
266
 
 
267
 
 
268
void dlgTextSearchConfiguration::OnChangeTxtDictionary(wxCommandEvent &ev)
 
269
{
 
270
    btnAdd->Enable(cbToken->GetValue().Length() > 0);
 
271
}
 
272
 
 
273
 
 
274
void dlgTextSearchConfiguration::OnSelChangeToken(wxListEvent &ev)
 
275
{
 
276
    int row=lstTokens->GetSelection();
 
277
    if (row >= 0)
 
278
    {
 
279
        cbToken->SetValue(lstTokens->GetText(row, 0));
 
280
        txtDictionary->SetValue(lstTokens->GetText(row, 1));
 
281
    }
 
282
 
 
283
    btnAdd->Enable(cbToken->GetValue().Length() > 0);
 
284
    btnRemove->Enable(row >= 0);
 
285
}
 
286
 
 
287
 
 
288
void dlgTextSearchConfiguration::OnAddToken(wxCommandEvent &ev)
 
289
{
 
290
    bool found = false;
 
291
 
 
292
    for (int pos = 0 ; pos < lstTokens->GetItemCount() ; pos++)
 
293
    {
 
294
        if (lstTokens->GetText(pos).IsSameAs(cbToken->GetValue(), false))
 
295
        {
 
296
            lstTokens->SetItem(pos, 1, txtDictionary->GetValue());
 
297
            found = true;
 
298
            break;
 
299
        }
 
300
    }
 
301
 
 
302
    if (!found)
 
303
    {
 
304
        lstTokens->AppendItem(cbToken->GetValue(), txtDictionary->GetValue());
 
305
    }
 
306
 
 
307
    btnAdd->Disable();
 
308
 
 
309
    dirtyTokens = true;
 
310
 
 
311
    CheckChange();
 
312
}
 
313
 
 
314
 
 
315
void dlgTextSearchConfiguration::OnRemoveToken(wxCommandEvent &ev)
 
316
{
 
317
    for (int pos = 0 ; pos < lstTokens->GetItemCount() ; pos++)
 
318
    {
 
319
        if (lstTokens->GetText(pos).IsSameAs(cbToken->GetValue(), false))
 
320
        {
 
321
            lstTokens->DeleteItem(pos);
 
322
            break;
 
323
        }
 
324
    }
 
325
 
 
326
    cbToken->SetValue(wxT(""));
 
327
    txtDictionary->SetValue(wxT(""));
 
328
 
 
329
    btnRemove->Disable();
 
330
 
 
331
    dirtyTokens = true;
 
332
 
 
333
    CheckChange();
 
334
}
 
335
 
 
336
 
 
337
wxString dlgTextSearchConfiguration::GetSql()
 
338
{
 
339
    wxString sql;
 
340
    wxString objname=schema->GetQuotedPrefix() + qtIdent(GetName());
 
341
 
 
342
    if (config)
 
343
    {
 
344
        // edit mode
 
345
        AppendNameChange(sql);
 
346
        AppendOwnerChange(sql, wxT("TEXT SEARCH CONFIGURATION ") + objname);
 
347
    }
 
348
    else
 
349
    {
 
350
        // create mode
 
351
        sql = wxT("CREATE TEXT SEARCH CONFIGURATION ")
 
352
            + schema->GetQuotedPrefix() + GetName()
 
353
            + wxT(" (");
 
354
        
 
355
        AppendIfFilled(sql, wxT("\n   PARSER="), cbParser->GetValue());
 
356
        AppendIfFilled(sql, wxT("\n   COPY="), cbCopy->GetValue());
 
357
        
 
358
        sql += wxT("\n);\n");
 
359
 
 
360
    }
 
361
 
 
362
    if (cbParser->GetValue().Length() > 0)
 
363
    {
 
364
        wxArrayString toks;
 
365
        size_t index;
 
366
 
 
367
        if (config)
 
368
        {
 
369
            for (index = 0 ; index < config->GetTokens().GetCount() ; index++)
 
370
                toks.Add(config->GetTokens().Item(index));
 
371
        }
 
372
 
 
373
        int cnt=lstTokens->GetItemCount();
 
374
        int pos;
 
375
 
 
376
        // check for changed or added tokens
 
377
        for (pos=0 ; pos < cnt ; pos++)
 
378
        {
 
379
            wxString newTok=lstTokens->GetText(pos);
 
380
            wxString newVal=lstTokens->GetText(pos, 1);
 
381
 
 
382
            wxString oldVal;
 
383
 
 
384
            for (index=0 ; index < toks.GetCount() ; index++)
 
385
            {
 
386
                wxString tok=toks.Item(index);
 
387
                if (tok.BeforeFirst('/').IsSameAs(newTok, false))
 
388
                {
 
389
                    oldVal = tok.Mid(newTok.Length()+1);
 
390
                    toks.RemoveAt(index);
 
391
                    break;
 
392
                }
 
393
            }
 
394
            if (oldVal != newVal)
 
395
            {
 
396
                if (oldVal.Length() == 0)
 
397
                {
 
398
                    sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
 
399
                        +  wxT(" ADD MAPPING FOR ") + newTok
 
400
                        +  wxT(" WITH ") + newVal
 
401
                        +  wxT(";\n");
 
402
                }
 
403
                else
 
404
                {
 
405
                    sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
 
406
                        +  wxT(" ALTER MAPPING FOR ") + newTok
 
407
                        + wxT(" WITH ") + newVal
 
408
                        +  wxT(";\n");
 
409
                }
 
410
            }
 
411
        }
 
412
        
 
413
        // check for removed tokens
 
414
        wxString oldTok;
 
415
        for (pos=0 ; pos < (int)toks.GetCount() ; pos++)
 
416
        {
 
417
            if (!toks.Item(pos).BeforeFirst('/').IsSameAs(oldTok, false))
 
418
            {
 
419
                oldTok = toks.Item(pos).BeforeFirst('/');
 
420
                sql += wxT("ALTER TEXT SEARCH CONFIGURATION ") + objname
 
421
                    +  wxT(" DROP MAPPING FOR ") + oldTok
 
422
                    + wxT(";\n");
 
423
            }
 
424
        }
 
425
    }
 
426
 
 
427
    AppendComment(sql, wxT("TEXT SEARCH CONFIGURATION ") + objname, config);
 
428
 
 
429
    return sql;
 
430
}