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

« back to all changes in this revision

Viewing changes to pgadmin/dlg/dlgTextSearchDictionary.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: dlgTextSearchDictionary.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
// dlgTextSearchDictionary.cpp - PostgreSQL Text Search Dictionary 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/dlgTextSearchDictionary.h"
 
21
#include "schema/pgSchema.h"
 
22
#include "schema/pgTextSearchDictionary.h"
 
23
#include "schema/pgDatatype.h"
 
24
 
 
25
 
 
26
// pointer to controls
 
27
#define cbTemplate          CTRL_COMBOBOX2("cbTemplate")
 
28
#define lstOptions          CTRL_LISTVIEW("lstOptions")
 
29
#define txtOption           CTRL_TEXT("txtOption")
 
30
#define txtValue            CTRL_TEXT("txtValue")
 
31
#define btnAdd              CTRL_BUTTON("wxID_ADD")
 
32
#define btnRemove           CTRL_BUTTON("wxID_REMOVE")
 
33
 
 
34
 
 
35
BEGIN_EVENT_TABLE(dlgTextSearchDictionary, dlgTypeProperty)
 
36
    EVT_TEXT(XRCID("cbTemplate"),               dlgTextSearchDictionary::OnChange)
 
37
    EVT_COMBOBOX(XRCID("cbTemplate"),           dlgTextSearchDictionary::OnChange)
 
38
    EVT_LIST_ITEM_SELECTED(XRCID("lstOptions"), dlgTextSearchDictionary::OnSelChangeOption)
 
39
    EVT_TEXT(XRCID("txtOption"),                dlgTextSearchDictionary::OnChangeOptionName)
 
40
    EVT_BUTTON(wxID_ADD,                        dlgTextSearchDictionary::OnAddOption)
 
41
    EVT_BUTTON(wxID_REMOVE,                     dlgTextSearchDictionary::OnRemoveOption)
 
42
#ifdef __WXMAC__
 
43
    EVT_SIZE(                                   dlgTextSearchDictionary::OnChangeSize)
 
44
#endif
 
45
END_EVENT_TABLE();
 
46
 
 
47
 
 
48
 
 
49
dlgProperty *pgTextSearchDictionaryFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
 
50
{
 
51
    return new dlgTextSearchDictionary(this, frame, (pgTextSearchDictionary*)node, (pgSchema*)parent);
 
52
}
 
53
 
 
54
dlgTextSearchDictionary::dlgTextSearchDictionary(pgaFactory *f, frmMain *frame, pgTextSearchDictionary *node, pgSchema *sch)
 
55
: dlgTypeProperty(f, frame, wxT("dlgTextSearchDictionary"))
 
56
{
 
57
    schema=sch;
 
58
    dict=node;
 
59
}
 
60
 
 
61
 
 
62
pgObject *dlgTextSearchDictionary::GetObject()
 
63
{
 
64
    return dict;
 
65
}
 
66
 
 
67
 
 
68
int dlgTextSearchDictionary::Go(bool modal)
 
69
{
 
70
    wxString qry;
 
71
    pgSet *set;
 
72
 
 
73
    qry = wxT("SELECT tmplname, nspname\n")
 
74
          wxT("  FROM pg_ts_template\n")
 
75
          wxT("  JOIN pg_namespace n ON n.oid=tmplnamespace\n")
 
76
          wxT("  ORDER BY tmplname\n");
 
77
 
 
78
    set = connection->ExecuteSet(qry);
 
79
    if (set)
 
80
    {
 
81
        while (!set->Eof())
 
82
        {
 
83
            wxString procname = database->GetSchemaPrefix(set->GetVal(wxT("nspname"))) + set->GetVal(wxT("tmplname"));
 
84
            cbTemplate->Append(procname);
 
85
            set->MoveNext();
 
86
        }
 
87
        delete set;
 
88
    }
 
89
 
 
90
    lstOptions->AddColumn(_("Option"), 80);
 
91
    lstOptions->AddColumn(_("Value"), 40);
 
92
 
 
93
    if (dict)
 
94
    {
 
95
        // edit mode
 
96
        cbTemplate->SetValue(dict->GetTemplate());
 
97
        cbTemplate->Disable();
 
98
 
 
99
        wxString options = dict->GetOptions();
 
100
        wxString option, optionname, optionvalue;
 
101
        while (options.Length() > 0)
 
102
        {
 
103
          option = options.BeforeFirst(',');
 
104
          optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
 
105
          optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();
 
106
          lstOptions->AppendItem(optionname, optionvalue);
 
107
          options = options.AfterFirst(',');
 
108
        }
 
109
 
 
110
        if (!connection->BackendMinimumVersion(8, 0))
 
111
            cbOwner->Disable();
 
112
    }
 
113
    else
 
114
    {
 
115
        // create mode
 
116
    }
 
117
 
 
118
    txtOption->SetValue(wxT(""));
 
119
    txtValue->SetValue(wxT(""));
 
120
    btnAdd->Disable();
 
121
    btnRemove->Disable();
 
122
 
 
123
    return dlgProperty::Go(modal);
 
124
}
 
125
 
 
126
 
 
127
pgObject *dlgTextSearchDictionary::CreateObject(pgCollection *collection)
 
128
{
 
129
    pgObject *obj=textSearchDictionaryFactory.CreateObjects(collection, 0,
 
130
         wxT("\n   AND dict.dictname=") + qtDbString(GetName()) +
 
131
         wxT("\n   AND dict.dictnamespace=") + schema->GetOidStr());
 
132
 
 
133
    return obj;
 
134
}
 
135
 
 
136
 
 
137
#ifdef __WXMAC__
 
138
void dlgTextSearchDictionary::OnChangeSize(wxSizeEvent &ev)
 
139
{
 
140
        lstOptions->SetSize(wxDefaultCoord, wxDefaultCoord,
 
141
            ev.GetSize().GetWidth(), ev.GetSize().GetHeight() - 350);
 
142
    if (GetAutoLayout())
 
143
    {
 
144
        Layout();
 
145
    }
 
146
}
 
147
#endif
 
148
 
 
149
 
 
150
void dlgTextSearchDictionary::CheckChange()
 
151
{
 
152
    if (dict)
 
153
    {
 
154
        EnableOK(txtName->GetValue() != dict->GetName()
 
155
            || txtComment->GetValue() != dict->GetComment()
 
156
            || cbOwner->GetValue() != dict->GetOwner()
 
157
            || GetOptionsSql().Length() > 0);
 
158
    }
 
159
    else
 
160
    {
 
161
        wxString name=GetName();
 
162
        bool enable=true;
 
163
        CheckValid(enable, !name.IsEmpty(), _("Please specify name."));
 
164
        CheckValid(enable, cbTemplate->GetValue().Length() > 0 , _("Please select a template."));
 
165
 
 
166
        EnableOK(enable);
 
167
    }
 
168
}
 
169
 
 
170
 
 
171
void dlgTextSearchDictionary::OnChange(wxCommandEvent &ev)
 
172
{
 
173
    CheckChange();
 
174
}
 
175
 
 
176
 
 
177
void dlgTextSearchDictionary::OnChangeOptionName(wxCommandEvent &ev)
 
178
{
 
179
    btnAdd->Enable(txtOption->GetValue().Length() > 0);
 
180
}
 
181
 
 
182
 
 
183
void dlgTextSearchDictionary::OnSelChangeOption(wxListEvent &ev)
 
184
{
 
185
    int row=lstOptions->GetSelection();
 
186
    if (row >= 0)
 
187
    {
 
188
        txtOption->SetValue(lstOptions->GetText(row, 0));
 
189
        txtValue->SetValue(lstOptions->GetText(row, 1));
 
190
    }
 
191
 
 
192
    btnRemove->Enable(row >= 0);
 
193
}
 
194
 
 
195
 
 
196
void dlgTextSearchDictionary::OnAddOption(wxCommandEvent &ev)
 
197
{
 
198
    bool found = false;
 
199
 
 
200
    for (int pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
 
201
    {
 
202
        if (lstOptions->GetText(pos).IsSameAs(txtOption->GetValue(), false))
 
203
        {
 
204
            lstOptions->SetItem(pos, 1, txtValue->GetValue());
 
205
            found = true;
 
206
            break;
 
207
        }
 
208
    }
 
209
 
 
210
    if (!found)
 
211
    {
 
212
        lstOptions->AppendItem(txtOption->GetValue(), txtValue->GetValue());
 
213
    }
 
214
 
 
215
    txtOption->SetValue(wxT(""));
 
216
    txtValue->SetValue(wxT(""));
 
217
    btnAdd->Disable();
 
218
 
 
219
    CheckChange();
 
220
}
 
221
 
 
222
 
 
223
void dlgTextSearchDictionary::OnRemoveOption(wxCommandEvent &ev)
 
224
{
 
225
    int sel=lstOptions->GetSelection();
 
226
    lstOptions->DeleteItem(sel);
 
227
 
 
228
    txtOption->SetValue(wxT(""));
 
229
    txtValue->SetValue(wxT(""));
 
230
    btnRemove->Disable();
 
231
 
 
232
    CheckChange();
 
233
}
 
234
 
 
235
 
 
236
wxString dlgTextSearchDictionary::GetOptionsSql()
 
237
{
 
238
    wxString options = dict->GetOptions();
 
239
    wxString option, optionname, optionvalue, sqloptions;
 
240
    bool found;
 
241
    int pos;
 
242
 
 
243
    while (options.Length() > 0)
 
244
    {
 
245
        option = options.BeforeFirst(',');
 
246
        optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
 
247
        optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();
 
248
 
 
249
        // check for options
 
250
        found = false;
 
251
        for (pos=0 ; pos < lstOptions->GetItemCount() && !found; pos++)
 
252
        {
 
253
            found = lstOptions->GetText(pos, 0).Cmp(optionname) == 0;
 
254
            if (found) break;
 
255
        }
 
256
 
 
257
        if (found)
 
258
        {
 
259
            if (lstOptions->GetText(pos, 1).Cmp(optionvalue) != 0)
 
260
            {
 
261
                if (sqloptions.Length() > 0)
 
262
                    sqloptions += wxT(", ");
 
263
                sqloptions += optionname + wxT("=") + lstOptions->GetText(pos, 1);
 
264
            }
 
265
        }
 
266
        else
 
267
        {
 
268
            if (sqloptions.Length() > 0)
 
269
                sqloptions += wxT(", ");
 
270
            sqloptions += optionname;
 
271
        }
 
272
 
 
273
        options = options.AfterFirst(',');
 
274
    }
 
275
 
 
276
    for (pos=0 ; pos < lstOptions->GetItemCount() ; pos++)
 
277
    {
 
278
        options = dict->GetOptions();
 
279
        found = false;
 
280
 
 
281
        while (options.Length() > 0 && !found)
 
282
        {
 
283
            option = options.BeforeFirst(',');
 
284
            optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
 
285
            found = lstOptions->GetText(pos, 0).Cmp(optionname) == 0;
 
286
            options = options.AfterFirst(',');
 
287
        }
 
288
 
 
289
        if (!found)
 
290
        {
 
291
            optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();
 
292
 
 
293
            if (sqloptions.Length() > 0)
 
294
                sqloptions += wxT(", ");
 
295
            sqloptions += lstOptions->GetText(pos, 0) + wxT("=") + lstOptions->GetText(pos, 1);
 
296
        }
 
297
    }
 
298
 
 
299
    return sqloptions;
 
300
}
 
301
 
 
302
 
 
303
wxString dlgTextSearchDictionary::GetSql()
 
304
{
 
305
    wxString sql;
 
306
    wxString objname=schema->GetQuotedPrefix() + qtIdent(GetName());
 
307
 
 
308
    if (dict)
 
309
    {
 
310
        // edit mode
 
311
        AppendNameChange(sql);
 
312
        AppendOwnerChange(sql, wxT("TEXT SEARCH DICTIONARY ") + objname);
 
313
 
 
314
        wxString sqloptions = GetOptionsSql();
 
315
        if (sqloptions.Length() > 0)
 
316
        {
 
317
            sql += wxT("ALTER TEXT SEARCH DICTIONARY ") + objname
 
318
                 + wxT(" (") + sqloptions + wxT(")");
 
319
        }
 
320
    }
 
321
    else
 
322
    {
 
323
        // create mode
 
324
        sql = wxT("CREATE TEXT SEARCH DICTIONARY ")
 
325
            + schema->GetQuotedPrefix() + GetName()
 
326
            + wxT(" (")
 
327
            + wxT("\n  TEMPLATE = ") + cbTemplate->GetValue();
 
328
 
 
329
        // check for options
 
330
        for (int pos=0 ; pos < lstOptions->GetItemCount() ; pos++)
 
331
        {
 
332
            sql += wxT(", ") + lstOptions->GetText(pos, 0)
 
333
                   + wxT("=") + lstOptions->GetText(pos, 1);
 
334
        }
 
335
        
 
336
        sql += wxT("\n);\n");
 
337
 
 
338
    }
 
339
    AppendComment(sql, wxT("TEXT SEARCH DICTIONARY ") + objname, dict);
 
340
 
 
341
    return sql;
 
342
}