File: filterdlg.cpp

package info (click to toggle)
wxsqlite3 3.0.0.1~dfsg0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,740 kB
  • sloc: cpp: 20,130; sh: 2,837; makefile: 302; ansic: 197; php: 9
file content (365 lines) | stat: -rwxr-xr-x 13,288 bytes parent folder | download | duplicates (2)
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
/*---------------------------------------------------------------------------*/
/* Logiciel de gestion de fichier de bases de donnes SQLite                 */
/*---------------------------------------------------------------------------*/
/* Projet  : wxSQLitePlus                              Version  : 0.2.3.0    */
/* Fichier : filterdlg.cpp                                                   */
/* Auteur  : Fred Cailleau-Lepetit                     Date     : 24/08/2008 */
/* email   : softinthebox@free.fr                      Rvision : 09/11/2008 */
/*---------------------------------------------------------------------------*/
/* Copyright (C) Fred Cailleau-Lepetit 2007-2008                             */
/* Licence GNU General Public License  http://www.fsf.org/copyleft/gpl.html  */
/*---------------------------------------------------------------------------*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation (version 3);

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Ce programme est libre, vous pouvez le redistribuer et/ou le modifier
selon les termes de la Licence Publique Gnrale GNU publie par la
Free Software Foundation (version 3).

Ce programme est distribu car potentiellement utile, mais
SANS AUCUNE GARANTIE, ni explicite ni implicite, y compris
les garanties de commercialisation ou d'adaptation dans un but
spcifique. Reportez-vous  la Licence Publique Gnrale GNU
pour plus de dtails.
*/
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "filterdlg.h"
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "wxsqliteplusapp.h"
#include "filterdlg.h"
/*---------------------------------------------------------------------------*/
#define ID_FILTER_PANEL 15001
#define ID_FIELDS       15002
#define ID_OPERATORS    15003
#define ID_VALUE        15004
#define ID_BTN_AND      15005
#define ID_BTN_OR       15006
#define ID_BTN_OPAR     15007
#define ID_BTN_CPAR     15008
#define ID_BTN_ADD      15009
#define ID_STATEMENT    15010
/*---------------------------------------------------------------------------*/
IMPLEMENT_DYNAMIC_CLASS(wxFilterDialog, wxDialog)

BEGIN_EVENT_TABLE(wxFilterDialog, wxDialog)
   EVT_CHOICE(ID_FIELDS, wxFilterDialog::OnFieldsSelected)
   EVT_UPDATE_UI(ID_VALUE, wxFilterDialog::OnValueUpdate)
   EVT_BUTTON(ID_BTN_AND, wxFilterDialog::OnBtnAndClick)
   EVT_BUTTON(ID_BTN_OR, wxFilterDialog::OnBtnOrClick)
   EVT_BUTTON(ID_BTN_OPAR, wxFilterDialog::OnBtnOparClick)
   EVT_BUTTON(ID_BTN_CPAR, wxFilterDialog::OnBtnCparClick)
   EVT_BUTTON(wxID_CLEAR, wxFilterDialog::OnClearClick)
   EVT_UPDATE_UI(wxID_CLEAR, wxFilterDialog::OnClearUpdate)
   EVT_BUTTON(ID_BTN_ADD, wxFilterDialog::OnBtnAddClick)
   EVT_BUTTON(wxID_OK, wxFilterDialog::OnOkClick)
END_EVENT_TABLE()
/*---------------------------------------------------------------------------*/
wxFilterDialog::wxFilterDialog()
{
   Init();
}
/*---------------------------------------------------------------------------*/
wxFilterDialog::wxFilterDialog(wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style)
{
   Init();
   Create(parent, id, caption, pos, size, style);
}
/*---------------------------------------------------------------------------*/
bool wxFilterDialog::Create(wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style)
{
   SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
   wxDialog::Create(parent, id, caption, pos, size, style);

   CreateControls();
   SetIcon(wxGetApp().GetIcon(ID_ICO_FILTER));
   if (GetSizer())
   {
      GetSizer()->SetSizeHints(this);
   }
   Centre();
   return true;
}
/*---------------------------------------------------------------------------*/
wxFilterDialog::~wxFilterDialog()
{
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::Init()
{
   m_fields = NULL;
   m_operators = NULL;
   m_Value = NULL;
   m_filter = NULL;
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::CreateControls()
{
   wxBoxSizer* bSizer1 = new wxBoxSizer(wxVERTICAL);
   SetSizer(bSizer1);

   wxPanel* iPanel1 = new wxPanel(this, ID_FILTER_PANEL, wxDefaultPosition,
                                  wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL);
   bSizer1->Add(iPanel1, 0, wxGROW, 0);

   wxBoxSizer* bSizer2 = new wxBoxSizer(wxVERTICAL);
   iPanel1->SetSizer(bSizer2);

   wxBoxSizer* bSizer3 = new wxBoxSizer(wxHORIZONTAL);
   bSizer2->Add(bSizer3, 0, wxGROW, 0);

   m_fields = new wxChoice(iPanel1, ID_FIELDS, wxDefaultPosition,
                           wxDefaultSize, 0, NULL, 0);
   bSizer3->Add(m_fields, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxArrayString m_operatorsStrings;
   m_operatorsStrings.Add(_(">"));
   m_operatorsStrings.Add(_(">="));
   m_operatorsStrings.Add(_("<"));
   m_operatorsStrings.Add(_("<="));
   m_operatorsStrings.Add(_("="));
   m_operatorsStrings.Add(_("<>"));
   m_operatorsStrings.Add(_("LIKE"));
   m_operatorsStrings.Add(_("NOT LIKE"));
   m_operatorsStrings.Add(_("GLOB"));
   m_operatorsStrings.Add(_("NOT GLOB"));
   m_operatorsStrings.Add(_("ISNULL"));
   m_operatorsStrings.Add(_("NOTNULL"));
   m_operators = new wxChoice(iPanel1, ID_OPERATORS, wxDefaultPosition,
                              wxDefaultSize, m_operatorsStrings, 0);
   m_operators->SetStringSelection(_(">"));
   bSizer3->Add(m_operators, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   m_Value = new wxTextCtrl(iPanel1, ID_VALUE, _T(""), wxDefaultPosition,
                            wxDefaultSize, 0);
   bSizer3->Add(m_Value, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxBoxSizer* bSizer4 = new wxBoxSizer(wxHORIZONTAL);
   bSizer2->Add(bSizer4, 0, wxALIGN_LEFT, 0);

   wxButton* btnAnd = new wxButton(iPanel1, ID_BTN_AND, _("AND"),
                                   wxDefaultPosition, wxSize(50, -1), 0);
   bSizer4->Add(btnAnd, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxButton* btnOr = new wxButton(iPanel1, ID_BTN_OR, _("OR"),
                                  wxDefaultPosition, wxSize(50, -1), 0);
   bSizer4->Add(btnOr, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxButton* btnOpenPar = new wxButton(iPanel1, ID_BTN_OPAR, _("("),
                                       wxDefaultPosition, wxSize(50, -1), 0);
   bSizer4->Add(btnOpenPar, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxButton* btnClosePar = new wxButton(iPanel1, ID_BTN_CPAR, _(")"),
                                        wxDefaultPosition, wxSize(50, -1), 0);
   bSizer4->Add(btnClosePar, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxButton* btnClear = new wxButton(iPanel1, wxID_CLEAR, _("&Clear"),
                                     wxDefaultPosition, wxDefaultSize, 0);
   bSizer4->Add(btnClear, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   bSizer4->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxButton* btnAdd = new wxButton(iPanel1, ID_BTN_ADD, _("Add"),
                                   wxDefaultPosition, wxDefaultSize, 0);
   bSizer4->Add(btnAdd, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   m_filter = new wxSQLEditor(this, ID_STATEMENT, wxDefaultPosition, wxSize(-1, 100));
   m_filter->SetLineNumberMarginStyle(false);

   bSizer1->Add(m_filter, 1, wxGROW|wxLEFT, 0);

   wxStdDialogButtonSizer* dlgButtonSizer1 = new wxStdDialogButtonSizer;

   bSizer1->Add(dlgButtonSizer1, 0, wxALIGN_RIGHT|wxALL, 5);
   wxButton* btnOk = new wxButton(this, wxID_OK, _("&OK"),
                                  wxDefaultPosition, wxDefaultSize, 0);
   btnOk->SetDefault();
   dlgButtonSizer1->AddButton(btnOk);

   wxButton* btnCancel = new wxButton(this, wxID_CANCEL, _("&Cancel"),
                                      wxDefaultPosition, wxDefaultSize, 0);
   dlgButtonSizer1->AddButton(btnCancel);

   dlgButtonSizer1->Realize();
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnBtnAddClick(wxCommandEvent& event)
{
  wxUnusedVar(event);
   wxString tmp1, tmp2;
   int idxfield, idxoper;

   idxfield = m_fields->GetSelection();
   idxoper = m_operators->GetSelection();

   tmp1 = wxString::Format(_T("\"%s\" %s"),
                           m_fields->GetString(idxfield).c_str(),
                           m_operators->GetString(idxoper).c_str());
   if (idxoper < 10)
   {
      tmp2 = m_Value-> GetValue();
      if (m_fields->GetClientData(idxfield) == (void*)1)
         tmp2 = wxString::Format(_T("'%s'"), tmp2.c_str());
      tmp1 = wxString::Format(_T("%s %s"), tmp1.c_str(), tmp2.c_str());
   }
   m_filter->AddText(tmp1);
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnBtnAndClick(wxCommandEvent& event)
{
  wxUnusedVar(event);
   m_filter->AddText(_T("\nAND "));
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnBtnOrClick(wxCommandEvent& event)
{
  wxUnusedVar(event);
   m_filter->AddText(_T("\nOR "));
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnBtnOparClick(wxCommandEvent& event)
{
  wxUnusedVar(event);
   m_filter->AddText(_T("("));
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnBtnCparClick(wxCommandEvent& event)
{
  wxUnusedVar(event);
   m_filter->AddText(_T(")"));
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnClearClick(wxCommandEvent& event)
{
  wxUnusedVar(event);
   m_filter->ClearAll();
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnClearUpdate(wxUpdateUIEvent& event)
{
   event.Enable(m_filter->GetLength() > 0);
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnOkClick(wxCommandEvent& event)
{
   wxString sql, base;

   try
   {
      m_Statement = m_filter->GetText();
      if (!m_Statement.IsEmpty())
      {
         if (m_Base.IsEmpty())
            base = _T("main");
         else
            base = m_Base;
         sql = _T("EXPLAIN SELECT *\r");
         sql += _T("FROM ") + base + _T(".") + m_Table + _T("\r");
         sql += _T("WHERE ") + m_Statement + _T(";");
         wxSQLite3ResultSet resultSet = m_Db->ExecuteQuery(ToUTF8(sql));
         resultSet.Finalize();
      }
      event.Skip();
   }
   catch(wxSQLite3Exception& ex)
   {
      wxMessageBox(ex.GetMessage(), _("Error"));
   }
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnFieldsSelected(wxCommandEvent& event)
{
   m_Value->Clear();
   event.Skip();
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::OnValueUpdate(wxUpdateUIEvent& event)
{
   event.Enable(m_operators->GetSelection() < 10);
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::SetDatabase(wxSQLite3Database* db, const wxString& base)
{
   m_Db = db;
   m_Base = base;
   if (!m_Db||!m_Db->IsOpen())
      return;
   m_fields->Clear();
   m_Table = wxEmptyString;
}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::SetTable(const wxString& table)
{
   if (!m_Db||!m_Db->IsOpen())
      return;

   m_Table = table;
   m_fields->Clear();

   try
   {
      wxSQLite3ResultSet clmnQRY;
      wxString sql, basename;
      int numberCols;

      if (m_Base != wxEmptyString)
         basename = m_Base;
      else
         basename = _T("main");

      // limiter  1 car si limite  0 a ne fonctionne pas
      // les informations sur le type de colonnes n'est pas retourn
      sql = wxString::Format(_T("SELECT * FROM %s.%s LIMIT 1;"),
                             basename.c_str(), m_Table.c_str());
      clmnQRY = m_Db->ExecuteQuery(ToUTF8(sql));

      numberCols = clmnQRY.GetColumnCount();
      int colt;
      for (int i = 0; i < numberCols; i++)
      {
         colt = clmnQRY.GetColumnType(i);
         switch (colt)
         {
            case WXSQLITE_INTEGER :
            case WXSQLITE_FLOAT   :
               m_fields->Append(clmnQRY.GetColumnName(i), (void*)0);
               break;
            default : ; // WXSQLITE_TEXT WXSQLITE_BLOB WXSQLITE_NULL
               m_fields->Append(clmnQRY.GetColumnName(i), (void*)1);
         }
      }

      clmnQRY.Finalize();
      m_fields->SetSelection(0);
   }
   catch(wxSQLite3Exception& ex)
   {
      wxGetApp().ShowError(_T("SetTable"), ex);
   }

}
/*---------------------------------------------------------------------------*/
void wxFilterDialog::SetStatement(const wxString& value)
{
   m_Statement = value;
   m_filter->SetText(m_Statement);
}
/*---------------------------------------------------------------------------*/