File: treeviewapp.cpp

package info (click to toggle)
wxsqlite3 3.4.1~dfsg-9.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,008 kB
  • sloc: cpp: 7,915; sh: 5,507; makefile: 367; ansic: 229; php: 9
file content (197 lines) | stat: -rw-r--r-- 5,289 bytes parent folder | download | duplicates (5)
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
/////////////////////////////////////////////////////////////////////////////
// Name:        treeviewapp.cpp
// Purpose:     
// Author:      Ulrich Telle
// Modified by: 
// Created:     2014-05-11
// Copyright:   (c) Ulrich Telle
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// 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

////@begin includes
////@end includes

#include "treeviewapp.h"

/*
 * Application instance implementation
 */

IMPLEMENT_APP( TreeviewSampleApp )

/*
 * TreeviewSampleApp type definition
 */

IMPLEMENT_CLASS( TreeviewSampleApp, wxApp )


/*
 * TreeviewSampleApp event table definition
 */

BEGIN_EVENT_TABLE( TreeviewSampleApp, wxApp )

END_EVENT_TABLE()


/*
 * Constructor for TreeviewSampleApp
 */

TreeviewSampleApp::TreeviewSampleApp()
{
  Init();
}


/*
 * Member initialisation
 */

void
TreeviewSampleApp::Init()
{
}

/*
 * Initialisation for TreeviewSampleApp
 */

bool
TreeviewSampleApp::OnInit()
{    
  bool ok = true;

#if wxUSE_XPM
  wxImage::AddHandler(new wxXPMHandler);
#endif
#if wxUSE_LIBPNG
  wxImage::AddHandler(new wxPNGHandler);
#endif
#if wxUSE_LIBJPEG
  wxImage::AddHandler(new wxJPEGHandler);
#endif
#if wxUSE_GIF
  wxImage::AddHandler(new wxGIFHandler);
#endif

  ok = InitializeDatabase();
  if (ok)
  {
    m_mainFrameWindow = new TreeviewSample(&m_db, NULL);
    m_mainFrameWindow->Show(true);
    SetTopWindow(m_mainFrameWindow);
  }

  return ok;
}


/*
 * Cleanup for TreeviewSampleApp
 */

int
TreeviewSampleApp::OnExit()
{    
  return wxApp::OnExit();
}

bool
TreeviewSampleApp::InitializeDatabase()
{
  bool ok = true;
  const char* sqlCommands[] = {
    "pragma foreign_keys=1;",
    "CREATE TABLE IF NOT EXISTS projects (\
       pid         integer not null,\
       prjtitle    varchar(72),\
       primary key (pid));",
    "CREATE TABLE IF NOT EXISTS folders (\
       fid     int         not null,\
       fname   varchar(64) not null,\
       fparent int         not null,\
       primary key (fid),\
       foreign key (fparent) references folders(fid) on delete cascade,\
       unique (fname, fparent));",
    "CREATE TABLE IF NOT EXISTS folderprojects (\
       fid  int not null,\
       pid  int not null,\
       foreign key (fid) references folders(fid) on delete restrict,\
       unique (fid,pid));",
    "CREATE TABLE IF NOT EXISTS folderclosure (\
       ancestor    int not null,\
       descendant  int not null,\
       distance    int not null,\
       primary key (ancestor, descendant),\
       foreign key (ancestor) references folders(fid),\
       foreign key (descendant) references folders(fid));",
    "CREATE TRIGGER IF NOT EXISTS ait_folders AFTER INSERT ON folders FOR EACH ROW\
       BEGIN\
         INSERT INTO folderclosure (ancestor, descendant, distance)\
         SELECT ancestor, NEW.fid, distance+1 FROM folderclosure\
           WHERE descendant = NEW.fparent\
           UNION ALL SELECT NEW.fid, NEW.fid, 0;\
       END;",
    "CREATE TRIGGER IF NOT EXISTS but_folders BEFORE UPDATE ON folders FOR EACH ROW\
       WHEN OLD.fparent != NEW.fparent\
       BEGIN\
         DELETE FROM folderclosure\
           WHERE descendant IN (SELECT descendant FROM folderclosure WHERE ancestor = OLD.fid)\
           AND ancestor NOT IN (SELECT descendant FROM folderclosure WHERE ancestor = OLD.fid);\
         INSERT INTO folderclosure (ancestor, descendant, distance)\
           SELECT supertree.ancestor, subtree.descendant, supertree.distance+subtree.distance+1\
             FROM folderclosure AS supertree JOIN folderclosure AS subtree\
            WHERE subtree.ancestor = OLD.fid\
              AND supertree.descendant = NEW.fparent;\
       END;",
    "CREATE TRIGGER IF NOT EXISTS bdt_folders BEFORE DELETE ON folders FOR EACH ROW\
       BEGIN\
         DELETE FROM folderclosure\
          WHERE descendant IN (SELECT descendant FROM folderclosure WHERE ancestor = OLD.fid);\
       END;",
    "INSERT OR IGNORE INTO folders (fid, fname, fparent) VALUES (1, 'root', 1);",
    NULL };

  int state = 0;
  int count;
  int j = 0;
  try
  {
    m_db.Open(wxT("tvtest.db3"));
    while (sqlCommands[j] != NULL)
    {
      m_db.ExecuteUpdate(sqlCommands[j]);
      ++j;
    }
    state = 1;
    count = m_db.ExecuteScalar("SELECT COUNT(*) FROM folderprojects;");
    if (count == 0)
    {
      m_db.ExecuteUpdate("INSERT OR IGNORE INTO projects VALUES (1,'Test project');");
      m_db.ExecuteUpdate("INSERT INTO folderprojects (fid, pid) SELECT 1, pid FROM projects;");
    }
  }
  catch (wxSQLite3Exception& e)
  {
    wxString msg;
    if (state == 0) msg = wxString(wxT("Error on creating the Treeview Sample database\n"));
    if (state == 0) msg = wxString(wxT("Error on initializing the Treeview Sample database\n"));
    msg += e.GetMessage();
    wxMessageBox(msg, wxT("Treeview Sample Database Error"), wxOK | wxCENTRE | wxICON_ERROR);
    ok = false;
  }
  return ok;
}