/
TreeBuilderSql.cpp
221 lines (184 loc) · 6.67 KB
/
TreeBuilderSql.cpp
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
/////////////////////////////////////////////////////////////////////////////
// //
// NppTags - CTags plugin for Notepad++ //
// Copyright (C) 2013-2015 Frank Fesevur //
// //
// 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; either version 2 of the License, or //
// (at your option) any later version. //
// //
// 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. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
// //
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <commctrl.h>
#include <assert.h>
#include "TagsDatabase.h"
#include "TreeBuilder.h"
#include "TreeBuilderSql.h"
#include "NppTags.h"
#include "Tag.h"
#include "WaitCursor.h"
using namespace std;
using namespace std::placeholders;
/////////////////////////////////////////////////////////////////////////////
//
TreeBuilderSql::TreeBuilderSql() : TreeBuilder("SQL")
{
_table = false;
_tableHasIndexes = false;
}
TreeBuilderSql::TreeBuilderSql(Tag* tag) : TreeBuilder(tag)
{
_table = false;
_tableHasIndexes = false;
}
/////////////////////////////////////////////////////////////////////////////
//
bool TreeBuilderSql::Expand()
{
bool opened = false;
if (g_DB->GetDB() == NULL)
{
g_DB->Open();
opened = true;
}
WaitCursor wait;
bool added = false;
if (_depth == 1)
added = AddTypes();
else
{
// Within a table?
if (_table)
{
// level 2 = the names of the tables
if (_depth == 2)
added = AddTables();
else
{
//
if (_depth == 3)
{
// Has the table indexes, add an extra level with "field" and "index"
if (_tableHasIndexes)
added = AddTableSubTypes();
else
{
// Just add the members of this table
added = AddMembers();
}
}
else
added = AddTableMembers();
}
}
else
added = AddTypeMembers();
}
if (opened)
g_DB->Close();
return added;
}
/////////////////////////////////////////////////////////////////////////////
// This first method is not used in this class
TreeBuilder* TreeBuilderSql::New()
{
return new TreeBuilderSql(NULL);
}
TreeBuilder* TreeBuilderSql::New(Tag* tag)
{
return new TreeBuilderSql(tag);
}
/////////////////////////////////////////////////////////////////////////////
// Add the various types to the tree. "field" and "index" belong to a table
// so they are excluded here and added as member of a table later
bool TreeBuilderSql::AddTypes()
{
SqliteStatement stmt(g_DB, "SELECT DISTINCT Type FROM Tags WHERE Language = @lang AND Type NOT IN ('field', 'index') ORDER BY Type");
stmt.Bind("@lang", _lang.c_str());
bool added = false;
while (stmt.GetNextRecord())
{
wstring type = stmt.GetWTextColumn("Type");
TreeBuilderSql* builder = (TreeBuilderSql*) New();
builder->_table = (type == L"table");
if (InsertItem(builder, type.c_str(), true) != NULL)
added = true;
}
stmt.Finalize();
return added;
}
/////////////////////////////////////////////////////////////////////////////
//
void TreeBuilderSql::AddTablesCallback(TreeBuilder* builder)
{
TreeBuilderSql* buildersql = (TreeBuilderSql*) builder;
buildersql->_table = true;
// Are there any indexes for this table?
// If so, we need to add an extra level later
SqliteStatement stmt(g_DB, "SELECT COUNT(*) FROM Tags WHERE Type = 'index' AND Language = @lang AND MemberOf = @member");
stmt.Bind("@member", buildersql->_tag->getTag().c_str());
stmt.Bind("@lang", _lang.c_str());
buildersql->_tableHasIndexes = false;
if (stmt.GetNextRecord())
{
int count = stmt.GetIntColumn(0);
buildersql->_tableHasIndexes = (count > 0);
}
stmt.Finalize();
}
/////////////////////////////////////////////////////////////////////////////
// Add all the tables to the tree
bool TreeBuilderSql::AddTables()
{
SqliteStatement stmt(g_DB, "SELECT * FROM Tags WHERE Language = @lang AND Type = 'table' ORDER BY Tag");
stmt.Bind("@lang", _lang.c_str());
return AddTagsFromStmt(&stmt, true, bind(&TreeBuilderSql::AddTablesCallback, this, _1));
}
/////////////////////////////////////////////////////////////////////////////
//
void TreeBuilderSql::AddTablesSubTypesCallback(TreeBuilder* builder)
{
TreeBuilderSql* buildersql = (TreeBuilderSql*) builder;
Tag* tag = new Tag(*_tag);
buildersql->_tag = tag;
buildersql->_table = true;
}
/////////////////////////////////////////////////////////////////////////////
// Add the types (normally "field" and "index") that need to be added
// below this table.
bool TreeBuilderSql::AddTableSubTypes()
{
SqliteStatement stmt(g_DB, "SELECT DISTINCT Type FROM Tags WHERE Language = @lang AND MemberOf = @memberof ORDER BY Type");
stmt.Bind("@lang", _lang.c_str());
stmt.Bind("@memberof", _tag->getTag().c_str());
return AddTextsFromStmt(&stmt, true, bind(&TreeBuilderSql::AddTablesSubTypesCallback, this, _1));
}
/////////////////////////////////////////////////////////////////////////////
// Add the fields or indexes to the table
bool TreeBuilderSql::AddTableMembers()
{
wstring type = GetItemText();
SqliteStatement stmt(g_DB);
stmt.Prepare("SELECT * FROM Tags WHERE Language = @lang AND MemberOf = @memberof AND Type = @type ORDER BY Tag");
stmt.Bind("@lang", _lang.c_str());
stmt.Bind("@memberof", _tag->getTag().c_str());
stmt.Bind("@type", type.c_str());
return AddTagsFromStmt(&stmt, false);
}
/////////////////////////////////////////////////////////////////////////////
// Non of non-table types have members
bool TreeBuilderSql::TypeHasMembers(LPCWSTR type)
{
UNREFERENCED_PARAMETER(type);
return false;
}