forked from Kitware/VTK
/
vtkQtSQLDatabase.cxx
339 lines (281 loc) · 9.1 KB
/
vtkQtSQLDatabase.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkQtSQLDatabase.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
// Check for Qt SQL module before defining this class.
#include <qglobal.h>
#if (QT_EDITION & QT_MODULE_SQL)
#include "vtkQtSQLDatabase.h"
#include "vtkObjectFactory.h"
#include "vtkQtSQLQuery.h"
#include "vtkStringArray.h"
#include "vtkVariant.h"
#include <QtSql/QtSql>
#include <QtSql/QSqlError>
#include <vtksys/SystemTools.hxx>
#include <vtksys/ios/sstream>
vtkStandardNewMacro(vtkQtSQLDatabase);
int vtkQtSQLDatabase::id = 0;
vtkSQLDatabase* vtkQtSQLDatabaseCreateFromURLCallback(const char* URL)
{
return vtkQtSQLDatabase::CreateFromURL(URL);
}
class vtkQtSQLDatabaseInitializer
{
public:
inline void Use()
{
}
vtkQtSQLDatabaseInitializer()
{
vtkSQLDatabase::RegisterCreateFromURLCallback(
vtkQtSQLDatabaseCreateFromURLCallback);
}
};
static vtkQtSQLDatabaseInitializer vtkQtSQLDatabaseInitializerGlobal;
vtkQtSQLDatabase::vtkQtSQLDatabase()
{
vtkQtSQLDatabaseInitializerGlobal.Use();
this->DatabaseType = NULL;
this->HostName = NULL;
this->UserName = NULL;
this->DatabaseName = NULL;
this->Port = -1;
this->ConnectOptions = NULL;
this->myTables = vtkStringArray::New();
this->currentRecord = vtkStringArray::New();
}
vtkQtSQLDatabase::~vtkQtSQLDatabase()
{
this->SetDatabaseType(NULL);
this->SetHostName(NULL);
this->SetUserName(NULL);
this->SetDatabaseName(NULL);
this->SetConnectOptions(NULL);
this->myTables->Delete();
this->currentRecord->Delete();
}
bool vtkQtSQLDatabase::Open(const char* password)
{
if (!QCoreApplication::instance())
{
vtkErrorMacro("Qt isn't initialized, you must create an instance of QCoreApplication before using this class.");
return false;
}
if (this->DatabaseType == NULL)
{
vtkErrorMacro("Qt database type must be non-null.");
return false;
}
// We have to assign a unique ID to each database connection, so
// Qt doesn't blow-away existing connections
const QString connection_name = QString::number(this->id++);
this->QtDatabase = QSqlDatabase::addDatabase(this->DatabaseType, connection_name);
if (this->HostName != NULL)
{
this->QtDatabase.setHostName(this->HostName);
}
if (this->DatabaseName != NULL)
{
this->QtDatabase.setDatabaseName(this->DatabaseName);
}
if (this->ConnectOptions != NULL)
{
this->QtDatabase.setConnectOptions(this->ConnectOptions);
}
if (this->Port >= 0)
{
this->QtDatabase.setPort(this->Port);
}
if (this->QtDatabase.open(this->UserName, password))
{
return true;
}
return false;
}
void vtkQtSQLDatabase::Close()
{
this->QtDatabase.close();
}
bool vtkQtSQLDatabase::IsOpen()
{
return this->QtDatabase.isOpen();
}
vtkSQLQuery* vtkQtSQLDatabase::GetQueryInstance()
{
vtkQtSQLQuery* query = vtkQtSQLQuery::New();
query->SetDatabase(this);
return query;
}
bool vtkQtSQLDatabase::HasError()
{
return (this->QtDatabase.lastError().number() != QSqlError::NoError);
}
const char* vtkQtSQLDatabase::GetLastErrorText()
{
return this->QtDatabase.lastError().text().toLatin1();
}
vtkStringArray* vtkQtSQLDatabase::GetTables()
{
// Clear out any exiting stuff
this->myTables->Initialize();
// Yea... do different things depending on database type
// Get tables on oracle is different
if (this->QtDatabase.driverName() == "QOCI")
{
vtkSQLQuery *query = this->GetQueryInstance();
query->SetQuery("select table_name from user_tables");
query->Execute();
while(query->NextRow())
this->myTables->InsertNextValue(query->DataValue(0).ToString());
// Okay done with query so delete
query->Delete();
}
else
{
// Copy the table list from Qt database
QStringList tables = this->QtDatabase.tables(QSql::Tables);
for (int i = 0; i < tables.size(); ++i)
{
this->myTables->InsertNextValue(tables.at(i).toLatin1());
}
}
return this->myTables;
}
vtkStringArray* vtkQtSQLDatabase::GetRecord(const char *table)
{
// Clear any existing records
currentRecord->Resize(0);
QSqlRecord columns = this->QtDatabase.record(table);
for (int i = 0; i < columns.count(); i++)
{
this->currentRecord->InsertNextValue(columns.fieldName(i).toLatin1());
}
return currentRecord;
}
vtkStringArray* vtkQtSQLDatabase::GetColumns()
{
return this->currentRecord;
}
void vtkQtSQLDatabase::SetColumnsTable(const char* table)
{
this->GetRecord(table);
}
bool vtkQtSQLDatabase::IsSupported(int feature)
{
switch (feature)
{
case VTK_SQL_FEATURE_TRANSACTIONS:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::Transactions);
case VTK_SQL_FEATURE_QUERY_SIZE:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::QuerySize);
case VTK_SQL_FEATURE_BLOB:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::BLOB);
case VTK_SQL_FEATURE_UNICODE:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::Unicode);
case VTK_SQL_FEATURE_PREPARED_QUERIES:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::PreparedQueries);
case VTK_SQL_FEATURE_NAMED_PLACEHOLDERS:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::NamedPlaceholders);
case VTK_SQL_FEATURE_POSITIONAL_PLACEHOLDERS:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::PositionalPlaceholders);
case VTK_SQL_FEATURE_LAST_INSERT_ID:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::LastInsertId);
case VTK_SQL_FEATURE_BATCH_OPERATIONS:
return this->QtDatabase.driver()->hasFeature(QSqlDriver::BatchOperations);
default:
{
vtkErrorMacro(<< "Unknown SQL feature code " << feature << "! See "
<< "vtkSQLDatabase.h for a list of possible features.");
return false;
}
}
}
void vtkQtSQLDatabase::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "DatabaseType: " << (this->DatabaseType ? this->DatabaseType : "NULL") << endl;
os << indent << "HostName: " << (this->HostName ? this->HostName : "NULL") << endl;
os << indent << "UserName: " << (this->UserName ? this->UserName : "NULL") << endl;
os << indent << "DatabaseName: " << (this->DatabaseName ? this->DatabaseName : "NULL") << endl;
os << indent << "Port: " << this->Port << endl;
os << indent << "ConnectOptions: " << (this->ConnectOptions ? this->ConnectOptions : "NULL") << endl;
}
// ----------------------------------------------------------------------
bool vtkQtSQLDatabase::ParseURL(const char* URL)
{
std::string protocol;
std::string username;
std::string unused;
std::string hostname;
std::string dataport;
std::string database;
std::string dataglom;
// SQLite is a bit special so lets get that out of the way :)
if ( ! vtksys::SystemTools::ParseURLProtocol( URL, protocol, dataglom))
{
vtkGenericWarningMacro( "Invalid URL: " << URL );
return false;
}
if ( protocol == "sqlite" )
{
this->SetDatabaseType("QSQLITE");
this->SetDatabaseName(dataglom.c_str());
return true;
}
// Okay now for all the other database types get more detailed info
if ( ! vtksys::SystemTools::ParseURL( URL, protocol, username,
unused, hostname, dataport, database) )
{
vtkGenericWarningMacro( "Invalid URL: " << URL );
return false;
}
// Create Qt 'version' of database prototcol type
QString qtType;
qtType = protocol.c_str();
qtType = "Q" + qtType.toUpper();
this->SetDatabaseType(qtType.toLatin1());
this->SetUserName(username.c_str());
this->SetHostName(hostname.c_str());
this->SetPort(atoi(dataport.c_str()));
this->SetDatabaseName(database.c_str());
return true;
}
// ----------------------------------------------------------------------
vtkSQLDatabase* vtkQtSQLDatabase::CreateFromURL( const char* URL )
{
vtkQtSQLDatabase* qt_db = vtkQtSQLDatabase::New();
if (qt_db->ParseURL(URL))
{
return qt_db;
}
qt_db->Delete();
return NULL;
}
// ----------------------------------------------------------------------
vtkStdString vtkQtSQLDatabase::GetURL()
{
vtkStdString url;
url = this->GetDatabaseType();
url += "://";
url += this->GetUserName();
url += "@";
url += this->GetHostName();
url += ":";
url += this->GetPort();
url += "/";
url += this->GetDatabaseName();
return url;
}
#endif // (QT_EDITION & QT_MODULE_SQL)