-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.cpp
242 lines (228 loc) · 8.14 KB
/
database.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//-------------------------------------------------------------------------------------------------
/*
Fix8logviewer is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
Fix8logviewer Open Source FIX Log Viewer.
Copyright (C) 2010-14 David N Boosalis [email protected], David L. Dight <[email protected]>
Fix8logviewer is free software: you can redistribute it and / or modify it under the terms of the
GNU Lesser General Public License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
Fix8logviewer 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.
You should have received a copy of the GNU Lesser General Public License along with Fix8.
If not, see <http://www.gnu.org/licenses/>.
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*/
//-------------------------------------------------------------------------------------------------
#include "database.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QVariant>
QString Database::tableNames[] = {"sqlinfo","windows","worksheets","tableschemas","schemafields","searchfunctions","filterfunctions"};
QString Database::arguments[] = {
// sqlinfo
"version integer",
// windows
"id INTEGER primary key,menubarStyleSheet char[256],geometry BLOB,restoreState BLOB, isVisible integer default 1,currentTab integer default 0, name char(32),tableSchemaID integer, searchAll integer default 0,searchFunction char[60], searchJavascript char[60], fix8sharedlib char[120], fontPtSize INTEGER",
//worksheets
"id INTEGER primary key, windowID integer,alias char(32), file char(120),selectedRow integer,splitterState BLOB,headerState BLOB,headerExpanded integer default 0, fieldsExpanded integer default 0,trailerExpanded integer default 0, searchFunction char[60], searchJavascript char[60], messageAreaHeaderState BLOB, fieldsExpansionType integer default 0",
// tableschemas
"id INTEGER primary key, name char(32), description char(120),locked integer default 0, xmlSchema char(36), fixSharedLibFile char(120)",
// schemafields
"id INTEGER primary key,name char(60),schemaID integer",
// searchfunctions
"id INTEGER primary key,alias char(32),function char(60), javascript char(60)",
// filterfunctions
"id INTEGER primary key,alias char(32),function char(60), javascript char(60)"
};
Database::Database(QString fileName,QObject *parent):QObject(parent),name(fileName),handle(0)
{
}
Database::~Database()
{
if (handle) {
if (handle->isOpen()) {
handle->close();
delete handle;
}
else
qWarning() << "handle not open...." __FILE__;
QSqlDatabase::removeDatabase(name);
}
}
bool Database::tableIsValid(TableType tt)
{
QString str;
QString str1;
QSqlQuery query;
QString ttname = tableNames[tt];
bool bstatus = false;
if (!handle) {
errorMessage = tr("Error Creating Database - handle is not initialized");
qWarning() << errorMessage;
goto done;
}
if (handle->isOpen() == false) {
errorMessage = "Database not open, cannot validate table";
qWarning() << errorMessage;
goto done;
}
query = QSqlQuery(*handle);
str = "select * from " + tableNames[tt];
bstatus = query.exec(str);
if (bstatus == 0) {
sqlError = query.lastError();
errorMessage = sqlError.databaseText();
qWarning() << "exec failed - " << errorMessage;
goto done;
}
done:
return bstatus;
}
QString Database::getLastError()
{
QString str;
QSqlError qsqlError;
if (handle) {
qsqlError = handle->lastError();
str = qsqlError.text();
}
return str;
}
QSqlDatabase *Database::getHandle()
{
return handle;
}
bool Database::isOpen()
{
bool status = false;
if (handle) {
status = handle->isOpen();
}
return status;
}
bool Database::open()
{
bool isopen = false;
QSqlError qsqlError;
QString str;
if ( handle) {
if (handle->isOpen()) {
errorMessage = tr("Closing local database, before openning: ") + name;
qWarning() << errorMessage;
handle->close();
}
delete handle;
}
handle = new QSqlDatabase(QSqlDatabase::addDatabase(LDB_DRIVER,name));
if (!handle) {
errorMessage = tr("Cannot open local database:" ) + name;
errorMessage.append(tr(" Handle not created"));
goto done;
}
handle->setDatabaseName(name);
isopen = handle->open();
if (!isopen) {
qsqlError = handle->lastError();
errorMessage = qsqlError.text();
}
done:
if (isopen == false) {
qWarning() << errorMessage;
qWarning() << "Openning database " + name;
}
return(isopen);
}
bool Database::createTable(TableType tt)
{
bool bstatus = false;
QSqlQuery query;
QString name;
QString args;
QString str;
QSqlError qsqlError;
name = tableNames[tt];
args = arguments[tt];
if (!handle) {
errorMessage = tr("Error Creating Database - handle is not initialized");
qWarning() << errorMessage;
goto done;
}
if (handle->isValid() == false) {
errorMessage = tr("Error Creating Database - not a valid handle");
qWarning() << errorMessage;
goto done;
}
query = QSqlQuery(*handle);
if (name.length() == 0) {
errorMessage = tr("Error create tabe: no value given for name");
qWarning() << errorMessage;
goto done;
}
str = "Create table " + name + " ( " + args + " )";
bstatus = query.exec(str);
if (!bstatus) {
qsqlError = query.lastError();
errorMessage = qsqlError.text();
qWarning() << errorMessage;
}
done:
return bstatus;
}
int Database::getVersion()
{
int version = -1;
bool bstatus;
bool ok;
QString str;
if (!handle) {
errorMessage = tr("Error in get database version - handle is not initialized");
qWarning() << errorMessage;
return -1;
}
QSqlQuery query(*handle);
str = "select * from sqlinfo";
bstatus = query.prepare(str);
if (bstatus == 0) {
qWarning("Error in get sql version in prepare statement...");
sqlError = query.lastError();
errorMessage = sqlError.databaseText();
qWarning() << errorMessage;
return -1;
}
bstatus = query.exec();
if (bstatus == false) {
sqlError = query.lastError();
errorMessage = sqlError.databaseText();
qWarning() << errorMessage;
return -1;
}
while (query.next()) {
version = query.value(0).toInt(&ok);
if (!ok) {
errorMessage = "Invalid database version found";
return -1;
}
break;
}
return version;
}
bool Database::setVersion(int nwVersion)
{
// to be done
return true;
}