File: createview.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 (342 lines) | stat: -rwxr-xr-x 12,538 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
/*---------------------------------------------------------------------------*/
/* Logiciel de gestion de fichier de bases de donnes SQLite                 */
/*---------------------------------------------------------------------------*/
/* Projet  : wxSQLitePlus                              Version  : 0.2.0.0    */
/* Fichier : createview.cpp                                                  */
/* Auteur  : Fred Cailleau-Lepetit                     Date     : 23/08/2007 */
/* email   : softinthebox@free.fr                      Rvision : 01/02/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 "createview.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 <wx/notebook.h>
#include <wx/valgen.h>
/*---------------------------------------------------------------------------*/
#include "createview.h"
#include "wxsqliteplusapp.h"
#include "sqliteplusframe.h"
/*---------------------------------------------------------------------------*/
#define ID_VIEWNAME     15001
#define ID_TEMPVIEW     15002
#define ID_NOTEBOOK     15003
#define ID_PANEL        15004
#define ID_VIEWQUERY    15005
#define ID_PARSERESULTS 15006
#define ID_BTN_TEST     15007
#define ID_DDL          15008
/*---------------------------------------------------------------------------*/
IMPLEMENT_DYNAMIC_CLASS(wxCreateViewDialog, wxDialog)

BEGIN_EVENT_TABLE(wxCreateViewDialog, wxDialog)
   EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK, wxCreateViewDialog::OnNotebookPageChanging)
   EVT_BUTTON(ID_BTN_TEST, wxCreateViewDialog::OnBtnTestClick)
   EVT_UPDATE_UI(ID_BTN_TEST, wxCreateViewDialog::OnBtnTestUpdate)
   EVT_BUTTON(wxID_OK, wxCreateViewDialog::OnOkClick)
END_EVENT_TABLE()
/*---------------------------------------------------------------------------*/
wxCreateViewDialog::wxCreateViewDialog()
{
   Init();
}
/*---------------------------------------------------------------------------*/
wxCreateViewDialog::wxCreateViewDialog(wxWindow* parent, wxWindowID id,
                                       const wxString& caption,
                                       const wxPoint& pos,
                                       const wxSize& size, long style)
{
   Init();
   Create(parent, id, caption, pos, size, style);
}
/*---------------------------------------------------------------------------*/
bool wxCreateViewDialog::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_VIEW));
   if (GetSizer())
   {
      GetSizer()->SetSizeHints(this);
   }
   Centre();
   return true;
}
/*---------------------------------------------------------------------------*/
wxCreateViewDialog::~wxCreateViewDialog()
{
}
/*---------------------------------------------------------------------------*/
void wxCreateViewDialog::SetDatabase(wxSQLite3Database* db,
                                     const wxString& base)
{
   m_Db = db;
   m_Base = base;
   if (!m_Db||!m_Db->IsOpen())
      m_Db = NULL;
}
/*---------------------------------------------------------------------------*/
void wxCreateViewDialog::Init()
{
   m_ViewName = NULL;
   m_TempView = NULL;
   m_ViewQuery = NULL;
   m_ParseResults = NULL;
   m_TextDdl = NULL;
   m_Db = NULL;
   m_Temporary = false;
}
/*---------------------------------------------------------------------------*/
void wxCreateViewDialog::CreateControls()
{    
   wxBoxSizer* bSizer1 = new wxBoxSizer(wxVERTICAL);
   SetSizer(bSizer1);

   wxBoxSizer* bSizer2 = new wxBoxSizer(wxHORIZONTAL);
   bSizer1->Add(bSizer2, 0, wxALIGN_LEFT|wxALL, 5);

   wxStaticText* sText1 = new wxStaticText(this, wxID_STATIC, _("Na&me :"),
                                           wxDefaultPosition, wxDefaultSize, 0);
   bSizer2->Add(sText1, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   m_ViewName = new wxTextCtrl(this, ID_VIEWNAME, wxEmptyString,
                               wxDefaultPosition, wxSize(200, -1), 0);
   bSizer2->Add(m_ViewName, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

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

   m_TempView = new wxCheckBox(this, ID_TEMPVIEW, _("&Temporary View"),
                               wxDefaultPosition, wxDefaultSize, 0);
   m_TempView->SetValue(false);
   bSizer2->Add(m_TempView, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxNotebook* nBook1 = new wxNotebook(this, ID_NOTEBOOK, wxDefaultPosition,
                                       wxDefaultSize, wxBK_DEFAULT);

   wxPanel* panel1 = new wxPanel(nBook1, ID_PANEL, wxDefaultPosition,
                                 wxDefaultSize, wxTAB_TRAVERSAL);
   wxBoxSizer* bSizer3 = new wxBoxSizer(wxVERTICAL);
   panel1->SetSizer(bSizer3);

   wxStaticText* sText2 = new wxStaticText(panel1, wxID_STATIC,
                                           _("SQL &Query :"), wxDefaultPosition,
                                           wxDefaultSize, 0);
   bSizer3->Add(sText2, 0, wxALIGN_LEFT|wxALL, 5);

   m_ViewQuery = new wxSQLEditor(panel1, ID_VIEWQUERY, wxDefaultPosition,
                                 wxSize(600, 170));
   m_ViewQuery->SetLineNumberMarginStyle(false);
   bSizer3->Add(m_ViewQuery, 1, wxGROW|wxALL, 5);

   wxBoxSizer* bSizer4 = new wxBoxSizer(wxHORIZONTAL);
   bSizer3->Add(bSizer4, 0, wxGROW, 0);
   wxBoxSizer* bSizer5 = new wxBoxSizer(wxVERTICAL);
   bSizer4->Add(bSizer5, 1, wxGROW, 0);
   wxStaticText* sText3 = new wxStaticText(panel1, wxID_STATIC,
                                           _("SQL &Parse Results :"),
                                           wxDefaultPosition, wxDefaultSize, 0);
   bSizer5->Add(sText3, 0, wxALIGN_LEFT|wxALL, 5);

   m_ParseResults = new wxTextCtrl(panel1, ID_PARSERESULTS, wxEmptyString,
                                   wxDefaultPosition, wxSize(-1, 80),
                                   wxTE_MULTILINE|wxTE_READONLY|
                                   wxTE_WORDWRAP|wxSIMPLE_BORDER);
   bSizer5->Add(m_ParseResults, 1, wxGROW|wxALL, 5);

   wxButton* btnTextSyntax = new wxButton(panel1, ID_BTN_TEST,
                                          _("Test &Syntax"), wxDefaultPosition,
                                          wxDefaultSize, 0);
   bSizer4->Add(btnTextSyntax, 0, wxALIGN_TOP|wxALL, 5);

   nBook1->AddPage(panel1, _("SQL Query"));

   m_TextDdl = new wxDDLEditor(nBook1, ID_DDL);

   nBook1->AddPage(m_TextDdl, _("DDL"));

   bSizer1->Add(nBook1, 0, wxGROW|wxALL, 5);

   wxStdDialogButtonSizer* dlgBtnSizer1 = new wxStdDialogButtonSizer;

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

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

   dlgBtnSizer1->Realize();

   // Connect events and objects
   m_ViewName->Connect(ID_VIEWNAME, wxEVT_CHAR,
                       wxKeyEventHandler(wxCreateViewDialog::OnChar),
                       NULL, this);
   // Set validators
   m_TempView->SetValidator(wxGenericValidator(&m_Temporary));
}
/*---------------------------------------------------------------------------*/
bool wxCreateViewDialog::ShowToolTips()
{
   return true;
}
/*---------------------------------------------------------------------------*/
void wxCreateViewDialog::OnChar(wxKeyEvent& event)
{
   int c = event.GetKeyCode();
   if ((!wxIsalnum(c) && c != _T('_') && c != _T(' ') && c != WXK_BACK && c != WXK_DELETE)&&
      // Ctrl+C Ctrl+V Ctrl+X
       !(event.ControlDown() && (c == 3 || c == 22 || c == 24)))
      return;
   event.Skip();
}
/*---------------------------------------------------------------------------*/
void wxCreateViewDialog::OnNotebookPageChanging(wxNotebookEvent& event)
{
   if (event.GetOldSelection() == 0)
   {
      wxString ddl = GetCreateViewDDL();

      if (ddl.IsEmpty())
         event.Veto();
      else
         m_TextDdl->SetValue(ddl);
   }
}
/*---------------------------------------------------------------------------*/
void wxCreateViewDialog::OnOkClick(wxCommandEvent& event)
{
   if (GetCreateViewDDL().IsEmpty())
      return;
   event.Skip();
}
/*---------------------------------------------------------------------------*/
void wxCreateViewDialog::OnBtnTestClick(wxCommandEvent& event)
{
   if (!CheckViewQuery(m_ViewQuery->GetText()))
      m_ParseResults->SetValue(m_QueryError);
   else
      m_ParseResults->SetValue(_("No errors found in SQL."));
   event.Skip();
}
/*---------------------------------------------------------------------------*/
void wxCreateViewDialog::OnBtnTestUpdate(wxUpdateUIEvent& event)
{
   event.Enable(!m_ViewQuery->GetText().IsEmpty());
}
/*---------------------------------------------------------------------------*/
bool wxCreateViewDialog::CheckViewQuery(const wxString& query)
{
   if (!query.IsEmpty())
   {
      /*
      wxString tmp;
      // Ce test n'est pas bon ni performant
      tmp = query.Strip(wxString::both).BeforeFirst(_T(' ')).Upper();
      if (tmp != _T("SELECT"))
      {
         m_QueryError = _("You must enter a SELECT sql statement.");
      }
      else
      {
         return wxGetApp().CheckStatementSyntax(m_Db, query, m_QueryError);
      }
      */
      return wxGetApp().CheckStatementSyntax(m_Db, query, m_QueryError);
   }
   else
      m_QueryError = _("You must enter a SELECT sql statement.");
   return false;
}
/*---------------------------------------------------------------------------*/
wxString wxCreateViewDialog::GetCreateViewDDL()
{
   wxString ddl, query, viewname;
   wxSQLitePlusFrame* frame;

   if (!m_Db||!m_Db->IsOpen())
      return wxEmptyString;

   viewname = m_ViewName->GetValue();
   if (viewname.IsEmpty())
   {
      wxMessageBox(_("You have to enter a view name."),
                   _("Error"));
      m_ViewName->SetFocus();
      return wxEmptyString;
   }

   query = m_ViewQuery->GetText();
   if (!CheckViewQuery(query))
   {
      wxMessageBox(m_QueryError, _("Error"));
      m_ViewQuery->SetFocus();
      return wxEmptyString;
   }

   frame = (wxSQLitePlusFrame*)wxGetApp().GetTopWindow();

   if (frame->ExistDbObject(otView, viewname, m_Base))
   {
      wxMessageBox(_("View or table with this name already exist."),
                   _("Error"));
      m_ViewName->SetFocus();
      return wxEmptyString;
   }

   // Cration du DDL
   ddl = _T("CREATE ");
   if (m_TempView->IsChecked())
      ddl += _T("TEMPORARY ");
   ddl += _T("VIEW ");
   if (!m_TempView->IsChecked() && m_Base != wxEmptyString)
      ddl += m_Base + _T(".");
   ddl += _T("\"") + viewname.Lower() + _T("\" AS\n");
   ddl += query;
   if (query.Last() != _T(';'))
      ddl += _T(';');
/*
CREATE [TEMP | TEMPORARY] VIEW [IF NOT EXISTS] [database-name.] view-name AS select-statement
*/
   return ddl;
}
/*---------------------------------------------------------------------------*/