File: createtable.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 (687 lines) | stat: -rwxr-xr-x 23,730 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*---------------------------------------------------------------------------*/
/* Logiciel de gestion de fichier de bases de donnes SQLite                 */
/*---------------------------------------------------------------------------*/
/* Projet  : wxSQLitePlus                              Version  : 0.2.3.0    */
/* Fichier : createtable.cpp                                                 */
/* Auteur  : Fred Cailleau-Lepetit                     Date     : 14/07/2007 */
/* 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 "createtable.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 "createtable.h"
#include "wxsqliteplusapp.h"
#include "sqliteplusframe.h"
#include "specgrid.h"
/*---------------------------------------------------------------------------*/
#define ID_TABLENAME       15001
#define ID_CB_TEMP         15002
#define ID_BOOK            15003
#define ID_PANELBOOK       15004
#define ID_COLUMNS         15005
#define ID_BTN_ADDCOLUMN   15006
#define ID_BTN_DELCOLUMN   15007
#define ID_DDL             15008
/*---------------------------------------------------------------------------*/
wxColumnCtrTable::wxColumnCtrTable()
{
   m_Name = _T("col");
   m_Type = cttText;
   m_NotNull = false;
   m_PrimaryKey = false;
}
/*---------------------------------------------------------------------------*/
wxGridColumnsTable::wxGridColumnsTable()
{
}
/*---------------------------------------------------------------------------*/
wxGridColumnsTable::~wxGridColumnsTable()
{
   WX_CLEAR_ARRAY(m_Columns)
}
/*---------------------------------------------------------------------------*/
int wxGridColumnsTable::GetNumberRows()
{
   return (int)m_Columns.GetCount();
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::IsEmptyCell(int row, int col)
{
   if (col == 0)
      if (m_Columns[row]->GetName() == wxEmptyString)
         return true;
   return false;
}
/*---------------------------------------------------------------------------*/
wxString wxGridColumnsTable::GetValue(int row, int col)
{
   switch (col)
   {
      case 0 : return m_Columns[row]->GetName();
      case 1 :
         switch (m_Columns[row]->GetType())
         {
            case cttInteger : return _T("INTEGER");
            case cttIntegerAutoinc : return _T("INTEGER AUTOINCREMENT");
            case cttReal : return _T("REAL");
            case cttText : return _T("TEXT");
            case cttBlob : return _T("BLOB");
         }
         return wxEmptyString;
      case 2 :
         if (m_Columns[row]->GetNotNull())
            return _T("1");
         return _T("0");
      case 3 :
         if (m_Columns[row]->GetPrimaryKey())
            return _T("1");
         return _T("0");
      case 4 : return m_Columns[row]->GetDefault();
   }
   return wxEmptyString;
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::GetValueAsBool(int row, int col)
{
   if (col == 2)
      return m_Columns[row]->GetNotNull();
   else if (col == 3)
      return m_Columns[row]->GetPrimaryKey();
   else
      return false;
}
/*---------------------------------------------------------------------------*/
void wxGridColumnsTable::SetValue(int row, int col, const wxString& value)
{
   if (col == 0)
   {
      if (CheckName(value, row))
         m_Columns[row]->SetName(value.Upper());
   }
   else if (col == 1)
   {
      if (value == _T("INTEGER"))
      {
         long l_Long;
         wxString val;

         m_Columns[row]->SetType(cttInteger);
         val = m_Columns[row]->GetDefault();
         if ((val != wxEmptyString) && !val.ToLong(&l_Long))
            SetValue(row, 4, wxEmptyString);
      }
      else if (value == _T("INTEGER AUTOINCREMENT"))
      {
         if (CheckAutoInc())
            return;
         m_Columns[row]->SetType(cttIntegerAutoinc);
         SetValueAsBool(row, 3, true);
         SetValue(row, 4, wxEmptyString);
         // Oblige la grille  prendre en compte le changement
         GetView()->SetCellValue(row, 3, _T("1"));
      }
      else if (value == _T("REAL"))
      {
         double l_Double;
         wxString val;

         m_Columns[row]->SetType(cttReal);
         val = m_Columns[row]->GetDefault();
         if ((val != wxEmptyString) && !val.ToDouble(&l_Double))
            SetValue(row, 4, wxEmptyString);
      }
      else if (value == _T("TEXT"))
         m_Columns[row]->SetType(cttText);
      else if (value == _T("BLOB"))
         m_Columns[row]->SetType(cttBlob);
   }
   else if (col == 4)
   {
      if (value != wxEmptyString)
      {
         long l_Long;
         double l_Double;

         switch (m_Columns[row]->GetType())
         {
            case cttInteger :
               if (value.ToLong(&l_Long))
                  m_Columns[row]->SetDefault(wxString::Format(_T("%li"), l_Long));
               break;
            case cttIntegerAutoinc : return;
            case cttReal :
               if (value.ToDouble(&l_Double))
                  m_Columns[row]->SetDefault(wxString::Format(_T("%f"), l_Double));
               break;
            case cttText :
            case cttBlob :
               if ((value.Upper() == _T("CURRENT_TIME"))||
                  (value.Upper() == _T("CURRENT_DATE"))||
                  (value.Upper() == _T("CURRENT_TIMESTAMP")))
                  m_Columns[row]->SetDefault(value.Upper());
               else
                  m_Columns[row]->SetDefault(value);
         }
      }
      else
         m_Columns[row]->SetDefault(wxEmptyString);
   }
}
/*---------------------------------------------------------------------------*/
void wxGridColumnsTable::SetValueAsBool(int row, int col, bool value)
{

   if (col == 2)
      m_Columns[row]->SetNotNull(value);
   else if (col == 3)
   {
      if (value != 0)   // Si primary key
      {
         if (CheckAutoInc(row))
            return;

         // alors retirer le flag au autres
         for (size_t i = 0; i < m_Columns.GetCount(); i++)
         {
            m_Columns[i]->SetPrimaryKey(false);
            // Oblige la grille  prendre en compte le changement
            GetView()->SetCellValue(i, 3, _T("0"));
         }
      }
      else
      {  // Si la colonne est autoincrement ne pas supprimer le primary key
         if (m_Columns[row]->GetType() == cttIntegerAutoinc)
            return;
      }
      m_Columns[row]->SetPrimaryKey(value);
   }
}
/*---------------------------------------------------------------------------*/
wxString wxGridColumnsTable::GetColLabelValue(int col)
{
   switch (col)
   {
      case 0 : return _("Name");
      case 1 : return _("Type");
      case 2 : return _("Not Null");
      case 3 : return _("Primay Key");
      case 4 : return _("Default");
   }
   return wxEmptyString;
}
/*---------------------------------------------------------------------------*/
wxString wxGridColumnsTable::GetTypeName(int row, int col)
{
  wxUnusedVar(row);
  wxUnusedVar(col);
   if (col == 0 || col == 4)
      return wxGRID_VALUE_STRING;
   else if (col == 1)
      return wxString::Format(_T("%s:INTEGER,INTEGER AUTOINCREMENT,REAL,TEXT,BLOB"), wxGRID_VALUE_CHOICE);
   else if (col == 2 || col == 3)
      return wxGRID_VALUE_BOOL;
   return wxEmptyString;
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::CanGetValueAs(int row, int col, const wxString& typeName)
{
  wxUnusedVar(row);
   if (typeName == wxGRID_VALUE_STRING)
      return (col == 0 || col == 1 || col == 4);
   else if (typeName == wxGRID_VALUE_BOOL)
      return (col == 2 || col == 3);
   else
      return false;
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::CanSetValueAs(int row, int col, const wxString& typeName)
{
   return CanGetValueAs(row, col, typeName);
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::InsertRows(size_t pos, size_t numRows)
{
   for (size_t i = 0; i < numRows; i ++)
   {
      wxColumnCtrTable* item = new wxColumnCtrTable;
      item->SetName(GetUniqueName());
      m_Columns.Insert(item, pos + i);
   }
   if (GetView())
   {
      wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, pos, numRows);
      GetView()->ProcessTableMessage( msg );
   }
   return true;
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::AppendRows(size_t numRows)
{
   for (size_t i = 0; i < numRows; i ++)
   {
      wxColumnCtrTable* item = new wxColumnCtrTable;
      item->SetName(GetUniqueName());
      m_Columns.Add(item);
   }
   if (GetView())
   {
      wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, numRows);
      GetView()->ProcessTableMessage(msg);
   }
   return true;
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::DeleteRows(size_t pos, size_t numRows)
{
   for (size_t i = pos; i < pos + numRows && i < m_Columns.GetCount(); i++)
   {
      wxColumnCtrTable* item = m_Columns[i];
      m_Columns.Remove(item);
      delete item;
   }
   if (GetView())
   {
      wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, pos, numRows);
      GetView()->ProcessTableMessage(msg);
    }
   return true;
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::CheckName(const wxString& name, int row)
{
   wxChar c;
   // Vrification du nom
   for (size_t i = 0; i < name.Len(); i++)
   {
      c = name.GetChar(i);
      if (!wxIsalnum(c) && c != _T('_') && c != _T(' '))
         return false;
   }
   // Vrifie que le nom est unique
   for (size_t i = 0; i < m_Columns.GetCount(); i++)
      if ((int)i != row)
         if (m_Columns[i]->GetName().Upper() == name.Upper())
            return false;

   return true;
}
/*---------------------------------------------------------------------------*/
wxString wxGridColumnsTable::GetUniqueName()
{
   wxString name;
   size_t i = m_Columns.GetCount();

   do
   {
      name = wxString::Format(_T("COLUMN%u"), i++);
   }
   while (!CheckName(name));
   return name;
}
/*---------------------------------------------------------------------------*/
wxColumnCtrTable* wxGridColumnsTable::GetColumnCtrTable(size_t index)
{
   if (index < m_Columns.GetCount())
      return m_Columns[index];
   return NULL;
}
/*---------------------------------------------------------------------------*/
bool wxGridColumnsTable::CheckAutoInc(int row)
{
   // Vrifier si une ligne est AUTOINCREMENT
   for (size_t i = 0; i < m_Columns.GetCount(); i++)
      if (((int)i != row) && (m_Columns[i]->GetType() == cttIntegerAutoinc))
         return true;
   return false;
}
/*---------------------------------------------------------------------------*/
IMPLEMENT_DYNAMIC_CLASS(wxCreateTableDialog, wxDialog)

BEGIN_EVENT_TABLE(wxCreateTableDialog, wxDialog)
   EVT_NOTEBOOK_PAGE_CHANGING(ID_BOOK, wxCreateTableDialog::OnBookPageChanging)
   EVT_BUTTON(ID_BTN_ADDCOLUMN, wxCreateTableDialog::OnBtnAddcolumnClick)
   EVT_BUTTON(ID_BTN_DELCOLUMN, wxCreateTableDialog::OnBtnDelcolumnClick)
   EVT_UPDATE_UI(ID_BTN_DELCOLUMN, wxCreateTableDialog::OnBtnDelcolumnUpdate)
   EVT_BUTTON(wxID_OK, wxCreateTableDialog::OnOkClick)
END_EVENT_TABLE()
/*---------------------------------------------------------------------------*/
wxCreateTableDialog::wxCreateTableDialog()
{
   Init();
}
/*---------------------------------------------------------------------------*/
wxCreateTableDialog::wxCreateTableDialog(wxWindow* parent, wxWindowID id,
                                         const wxString& caption,
                                         const wxPoint& pos, const wxSize& size,
                                         long style)
{
   Init();
   Create(parent, id, caption, pos, size, style);
}
/*---------------------------------------------------------------------------*/
bool wxCreateTableDialog::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_TABLE));
   if (GetSizer())
   {
      GetSizer()->SetSizeHints(this);
   }
   Centre();
   return true;
}
/*---------------------------------------------------------------------------*/
wxCreateTableDialog::~wxCreateTableDialog()
{
   // if this line does not exist the program crash on GTK2
   m_GridColumns->SetTable(NULL);
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::SetDatabase(wxSQLite3Database* db,
                                      const wxString& base)
{
   m_Db = db;
   m_Base = base;
   if (!m_Db||!m_Db->IsOpen())
      m_Db = NULL;
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::Init()
{
   m_TextTableName = NULL;
   m_TempTable = NULL;
   m_GridColumns = NULL;
   m_TextDdl = NULL;
   m_Db = NULL;
   m_Temporary = false;
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::CreateControls()
{
   wxBoxSizer* bSizer1 = new wxBoxSizer(wxVERTICAL);
   SetSizer(bSizer1);

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

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

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

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

   wxNotebook* noteBook1 = new wxNotebook(this, ID_BOOK, wxDefaultPosition,
                                          wxDefaultSize, wxBK_DEFAULT);

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

   m_GridColumns = new wxSpecGrid(panel1, ID_COLUMNS, wxDefaultPosition,
                                  wxSize(700, 250), wxHSCROLL|wxVSCROLL);
   m_GridColumns->SetDefaultColSize(50);
   m_GridColumns->SetDefaultRowSize(25);
   m_GridColumns->SetColLabelSize(25);
   m_GridColumns->SetRowLabelSize(50);
   m_GridColumns->CreateGrid(0, 5, wxGrid::wxGridSelectCells);
   bSizer3->Add(m_GridColumns, 1, wxGROW|wxALL, 5);

   wxBoxSizer* bSizer4 = new wxBoxSizer(wxHORIZONTAL);
   bSizer3->Add(bSizer4, 0, wxALIGN_LEFT|wxALL, 5);
   m_BtnAddColumn = new wxButton(panel1, ID_BTN_ADDCOLUMN, _("Add Column"),
                                 wxDefaultPosition, wxDefaultSize, 0);
   bSizer4->Add(m_BtnAddColumn, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   wxButton* btnDel = new wxButton(panel1, ID_BTN_DELCOLUMN, _("Remove Column"),
                                   wxDefaultPosition, wxDefaultSize, 0);
   bSizer4->Add(btnDel, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

   noteBook1->AddPage(panel1, _("Table"));

   m_TextDdl = new wxDDLEditor(noteBook1, ID_DDL);

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

   bSizer1->Add(noteBook1, 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_TextTableName->Connect(ID_TABLENAME, wxEVT_CHAR,
                            wxKeyEventHandler(wxCreateTableDialog::OnChar),
                            NULL, this);
   m_GridColumns->Connect(ID_COLUMNS, wxEVT_CHAR,
                          wxKeyEventHandler(wxCreateTableDialog::OnGridChar),
                          NULL, this);
   m_GridColumns->SetTable(&m_TableColumns);
   m_GridColumns->SetColSize(0, 190);
   m_GridColumns->SetColSize(1, 185);
   m_GridColumns->SetColSize(2, 70);
   m_GridColumns->SetColSize(3, 90);
   m_GridColumns->SetColSize(4, 120);

   // Set validators
   m_TempTable->SetValidator(wxGenericValidator(&m_Temporary));
}
/*---------------------------------------------------------------------------*/
bool wxCreateTableDialog::ShowToolTips()
{
   return true;
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::OnBtnAddcolumnClick(wxCommandEvent& event)
{
  wxUnusedVar(event);
   m_GridColumns->AppendRows(1);
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::OnBtnDelcolumnClick(wxCommandEvent& event)
{
  wxUnusedVar(event);
   wxArrayInt rowsArray = m_GridColumns->GetSelectedRows();
   for (size_t i = rowsArray.GetCount(); i > 0; i--)
      m_GridColumns->DeleteRows(rowsArray[i - 1]);
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::OnBtnDelcolumnUpdate(wxUpdateUIEvent& event)
{
   event.Enable(m_GridColumns->GetSelectedRows().GetCount() > 0);
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::OnOkClick(wxCommandEvent& event)
{
   if (GetCreateTableDDL().IsEmpty())
      return;
   event.Skip();
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::OnGridChar(wxKeyEvent& event)
{
   event.Skip();
}
/*---------------------------------------------------------------------------*/
void wxCreateTableDialog::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 wxCreateTableDialog::OnBookPageChanging(wxNotebookEvent& event)
{
   if (event.GetOldSelection() == 0)
   {
      wxString ddl = GetCreateTableDDL();

      if (ddl.IsEmpty())
         event.Veto();
      else
         m_TextDdl->SetValue(ddl);
   }
}
/*---------------------------------------------------------------------------*/
wxString wxCreateTableDialog::GetCreateTableDDL()
{
   wxString ddl, tablename, def;
   wxColumnCtrTable* column;
   wxSQLitePlusFrame* frame;

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

   tablename = m_TextTableName->GetValue();
   if (tablename.IsEmpty())
   {
      wxMessageBox(_("You have to enter a table name."),
                   _("Error"));
      m_TextTableName->SetFocus();
      return wxEmptyString;
   }
   if (m_GridColumns->GetNumberRows() == 0)
   {
      wxMessageBox(_("You have to create column(s) for the table."),
                   _("Error"));
      m_BtnAddColumn->SetFocus();
      return wxEmptyString;
   }

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

   if (frame->ExistDbObject(otTable, tablename, m_Base))
   {
      wxMessageBox(_("Table or view with this name already exist."),
                   _("Error"));
      m_TextTableName->SetFocus();
      return wxEmptyString;
   }

   // Cration du DDL
   ddl = _T("CREATE ");
   if (m_TempTable->IsChecked())
      ddl += _T("TEMPORARY ");
   ddl += _T("TABLE ");
   if (!m_TempTable->IsChecked() && m_Base != wxEmptyString)
      ddl += m_Base + _T(".");
   ddl += _T("\"") + tablename.Lower() + _T("\"\n");
   ddl += _T("(");
   for (int i = 0; i < m_TableColumns.GetNumberRows(); i++)
   {
      column = m_TableColumns.GetColumnCtrTable(i);
      ddl += _T("\n  \"") + column->GetName() + _T("\" ");
      switch (column->GetType())
      {
         case cttIntegerAutoinc :
            if (!column->GetPrimaryKey())
            {
               wxMessageBox(_("A column with AUTOINCREMENT must be a primary key."),
                            _("Error"));
               return wxEmptyString;
            }
         case cttInteger :
            ddl += _T("INTEGER"); break;
         case cttReal :
            ddl += _T("REAL"); break;
         case cttText :
            ddl += _T("TEXT"); break;
         case cttBlob :
            ddl += _T("BLOB"); break;
      }
      if (column->GetNotNull())
         ddl += _T(" NOT NULL");
      if (column->GetPrimaryKey())
      {
         ddl += _T(" PRIMARY KEY");
         if (column->GetType() == cttIntegerAutoinc)
            ddl += _T(" AUTOINCREMENT");
      }
      if (column->GetDefault() != wxEmptyString)
      {
         ddl += _T(" DEFAULT ");
         if ((column->GetType() == cttText || column->GetType() == cttBlob)&&
             (column->GetDefault() != _T("CURRENT_TIME")||
              column->GetDefault() != _T("CURRENT_DATE")||
              column->GetDefault() != _T("CURRENT_TIMESTAMP")))
         {
            def = column->GetDefault();
            if (!def.StartsWith(_T("'"))||!def.EndsWith(_T("'")))
            {
               def.Replace(_T("'"), _T("''"));
               ddl += _T("'") + def + _T("'");
            }
         }
         else
            ddl += column->GetDefault();
      }
      if (i != m_TableColumns.GetNumberRows() - 1)
         ddl += _T(",");
   }
   ddl += _T("\n);");
   return ddl;
}
/*---------------------------------------------------------------------------*/