forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkSQLDatabase.cxx
701 lines (627 loc) · 19.8 KB
/
vtkSQLDatabase.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
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
688
689
690
691
692
693
694
695
696
697
698
699
700
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSQLDatabase.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.
-------------------------------------------------------------------------*/
#include "vtkToolkits.h"
#include "vtkInformationObjectBaseKey.h"
#include "vtkSQLDatabase.h"
#include "vtkSQLQuery.h"
#include "vtkSQLDatabaseSchema.h"
#include "vtkSQLiteDatabase.h"
#ifdef VTK_USE_POSTGRES
#include "vtkPostgreSQLDatabase.h"
#endif // VTK_USE_POSTGRES
#ifdef VTK_USE_MYSQL
#include "vtkMySQLDatabase.h"
#endif // VTK_USE_MYSQL
#ifdef VTK_USE_ODBC
#include "vtkODBCDatabase.h"
#endif // VTK_USE_ODBC
#include "vtkCriticalSection.h"
#include "vtkObjectFactory.h"
#include "vtkStdString.h"
#include <vtksys/SystemTools.hxx>
#include <vtksys/ios/sstream>
class vtkSQLDatabase::vtkCallbackVector :
public vtkstd::vector<vtkSQLDatabase::CreateFunction>
{
public:
vtkSQLDatabase* CreateFromURL(const char* URL)
{
iterator iter;
for (iter = this->begin(); iter != this->end(); ++iter)
{
vtkSQLDatabase* db =(*(*iter))(URL);
if (db)
{
return db;
}
}
return NULL;
}
};
vtkSQLDatabase::vtkCallbackVector* vtkSQLDatabase::Callbacks = 0;
// Ensures that there are no leaks when the application exits.
class vtkSQLDatabaseCleanup
{
public:
inline void Use()
{
};
~vtkSQLDatabaseCleanup()
{
vtkSQLDatabase::UnRegisterAllCreateFromURLCallbacks();
}
};
// Used to clean up the Callbacks
static vtkSQLDatabaseCleanup vtkCleanupSQLDatabaseGlobal;
vtkInformationKeyMacro(vtkSQLDatabase, DATABASE, ObjectBase);
// ----------------------------------------------------------------------
vtkSQLDatabase::vtkSQLDatabase()
{
}
// ----------------------------------------------------------------------
vtkSQLDatabase::~vtkSQLDatabase()
{
}
// ----------------------------------------------------------------------
void vtkSQLDatabase::RegisterCreateFromURLCallback(
vtkSQLDatabase::CreateFunction func)
{
if (!vtkSQLDatabase::Callbacks)
{
vtkCleanupSQLDatabaseGlobal.Use();
vtkSQLDatabase::Callbacks = new vtkCallbackVector();
}
vtkSQLDatabase::Callbacks->push_back(func);
}
// ----------------------------------------------------------------------
void vtkSQLDatabase::UnRegisterCreateFromURLCallback(
vtkSQLDatabase::CreateFunction func)
{
if (vtkSQLDatabase::Callbacks)
{
vtkSQLDatabase::vtkCallbackVector::iterator iter;
for (iter = vtkSQLDatabase::Callbacks->begin();
iter != vtkSQLDatabase::Callbacks->end(); ++iter)
{
if ((*iter) == func)
{
vtkSQLDatabase::Callbacks->erase(iter);
break;
}
}
}
}
// ----------------------------------------------------------------------
void vtkSQLDatabase::UnRegisterAllCreateFromURLCallbacks()
{
delete vtkSQLDatabase::Callbacks;
vtkSQLDatabase::Callbacks = 0;
}
// ----------------------------------------------------------------------
void vtkSQLDatabase::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
// ----------------------------------------------------------------------
vtkStdString vtkSQLDatabase::GetColumnSpecification( vtkSQLDatabaseSchema* schema,
int tblHandle,
int colHandle )
{
vtksys_ios::ostringstream queryStr;
queryStr << schema->GetColumnNameFromHandle( tblHandle, colHandle );
// Figure out column type
int colType = schema->GetColumnTypeFromHandle( tblHandle, colHandle );
vtkStdString colTypeStr;
switch ( static_cast<vtkSQLDatabaseSchema::DatabaseColumnType>( colType ) )
{
case vtkSQLDatabaseSchema::SERIAL:
colTypeStr = "INTEGER";
break;
case vtkSQLDatabaseSchema::SMALLINT:
colTypeStr = "INTEGER";
break;
case vtkSQLDatabaseSchema::INTEGER:
colTypeStr = "INTEGER";
break;
case vtkSQLDatabaseSchema::BIGINT:
colTypeStr = "INTEGER";
break;
case vtkSQLDatabaseSchema::VARCHAR:
colTypeStr = "VARCHAR";
break;
case vtkSQLDatabaseSchema::TEXT:
colTypeStr = "VARCHAR";
break;
case vtkSQLDatabaseSchema::REAL:
colTypeStr = "FLOAT";
break;
case vtkSQLDatabaseSchema::DOUBLE:
colTypeStr = "DOUBLE";
break;
case vtkSQLDatabaseSchema::BLOB:
colTypeStr = "";
break;
case vtkSQLDatabaseSchema::TIME:
colTypeStr = "TIME";
break;
case vtkSQLDatabaseSchema::DATE:
colTypeStr = "DATE";
break;
case vtkSQLDatabaseSchema::TIMESTAMP:
colTypeStr = "TIMESTAMP";
break;
}
if ( colTypeStr.size() )
{
queryStr << " " << colTypeStr;
}
else // if ( colTypeStr.size() )
{
vtkGenericWarningMacro( "Unable to get column specification: unsupported data type " << colType );
return vtkStdString();
}
// Decide whether size is allowed, required, or unused
int colSizeType = 0;
switch ( static_cast<vtkSQLDatabaseSchema::DatabaseColumnType>( colType ) )
{
case vtkSQLDatabaseSchema::SERIAL:
colSizeType = 0;
break;
case vtkSQLDatabaseSchema::SMALLINT:
colSizeType = 1;
break;
case vtkSQLDatabaseSchema::INTEGER:
colSizeType = 1;
break;
case vtkSQLDatabaseSchema::BIGINT:
colSizeType = 1;
break;
case vtkSQLDatabaseSchema::VARCHAR:
colSizeType = -1;
break;
case vtkSQLDatabaseSchema::TEXT:
colSizeType = -1;
break;
case vtkSQLDatabaseSchema::REAL:
colSizeType = 0;
break;
case vtkSQLDatabaseSchema::DOUBLE:
colSizeType = 0;
break;
case vtkSQLDatabaseSchema::BLOB:
colSizeType = 0;
break;
case vtkSQLDatabaseSchema::TIME:
colSizeType = 0;
break;
case vtkSQLDatabaseSchema::DATE:
colSizeType = 0;
break;
case vtkSQLDatabaseSchema::TIMESTAMP:
colSizeType = 0;
break;
}
// Specify size if allowed or required
if ( colSizeType )
{
int colSize = schema->GetColumnSizeFromHandle( tblHandle, colHandle );
// IF size is provided but absurd,
// OR, if size is required but not provided OR absurd,
// THEN assign the default size.
if ( ( colSize < 0 ) || ( colSizeType == -1 && colSize < 1 ) )
{
colSize = VTK_SQL_DEFAULT_COLUMN_SIZE;
}
// At this point, we have either a valid size if required, or a possibly null valid size
// if not required. Thus, skip sizing in the latter case.
if ( colSize > 0 )
{
queryStr << "(" << colSize << ")";
}
}
vtkStdString attStr = schema->GetColumnAttributesFromHandle( tblHandle, colHandle );
if ( attStr.size() )
{
queryStr << " " << attStr;
}
return queryStr.str();
}
// ----------------------------------------------------------------------
vtkStdString vtkSQLDatabase::GetIndexSpecification( vtkSQLDatabaseSchema* schema,
int tblHandle,
int idxHandle,
bool& skipped )
{
vtkStdString queryStr;
int idxType = schema->GetIndexTypeFromHandle( tblHandle, idxHandle );
switch ( idxType )
{
case vtkSQLDatabaseSchema::PRIMARY_KEY:
queryStr = ", PRIMARY KEY ";
skipped = false;
break;
case vtkSQLDatabaseSchema::UNIQUE:
queryStr = ", UNIQUE ";
skipped = false;
break;
case vtkSQLDatabaseSchema::INDEX:
// Not supported within a CREATE TABLE statement by all SQL backends:
// must be created later with a CREATE INDEX statement
queryStr = "CREATE INDEX ";
skipped = true;
break;
default:
return vtkStdString();
}
// No index_name for PRIMARY KEYs nor UNIQUEs
if ( skipped )
{
queryStr += schema->GetIndexNameFromHandle( tblHandle, idxHandle );
}
// CREATE INDEX <index name> ON <table name> syntax
if ( skipped )
{
queryStr += " ON ";
queryStr += schema->GetTableNameFromHandle( tblHandle );
}
queryStr += " (";
// Loop over all column names of the index
int numCnm = schema->GetNumberOfColumnNamesInIndex( tblHandle, idxHandle );
if ( numCnm < 0 )
{
vtkGenericWarningMacro( "Unable to get index specification: index has incorrect number of columns " << numCnm );
return vtkStdString();
}
bool firstCnm = true;
for ( int cnmHandle = 0; cnmHandle < numCnm; ++ cnmHandle )
{
if ( firstCnm )
{
firstCnm = false;
}
else
{
queryStr += ",";
}
queryStr += schema->GetIndexColumnNameFromHandle( tblHandle, idxHandle, cnmHandle );
}
queryStr += ")";
return queryStr;
}
// ----------------------------------------------------------------------
vtkStdString vtkSQLDatabase::GetTriggerSpecification( vtkSQLDatabaseSchema* schema,
int tblHandle,
int trgHandle )
{
vtkStdString queryStr = "CREATE TRIGGER ";
queryStr += schema->GetTriggerNameFromHandle( tblHandle, trgHandle );
int trgType = schema->GetTriggerTypeFromHandle( tblHandle, trgHandle );
// odd types: AFTER, even types: BEFORE
if ( trgType % 2 )
{
queryStr += " AFTER ";
}
else
{
queryStr += " BEFORE ";
}
// 0/1: INSERT, 2/3: UPDATE, 4/5: DELETE
if ( trgType > 1 )
{
if ( trgType > 3 )
{
queryStr += "DELETE ON ";
}
else // if ( trgType > 3 )
{
queryStr += "UPDATE ON ";
}
}
else // if ( trgType > 1 )
{
queryStr += "INSERT ON ";
}
queryStr += schema->GetTableNameFromHandle( tblHandle );
queryStr += " ";
queryStr += schema->GetTriggerActionFromHandle( tblHandle, trgHandle );
return queryStr;
}
// ----------------------------------------------------------------------
vtkSQLDatabase* vtkSQLDatabase::CreateFromURL( const char* URL )
{
vtkstd::string urlstr( URL ? URL : "" );
vtkstd::string protocol;
vtkstd::string username;
vtkstd::string unused;
vtkstd::string hostname;
vtkstd::string dataport;
vtkstd::string database;
vtkstd::string dataglom;
vtkSQLDatabase* db = 0;
static vtkSimpleCriticalSection dbURLCritSec;
dbURLCritSec.Lock();
// SQLite is a bit special so lets get that out of the way :)
if ( ! vtksys::SystemTools::ParseURLProtocol( urlstr, protocol, dataglom ))
{
vtkGenericWarningMacro( "Invalid URL (no protocol found): \"" << urlstr.c_str() << "\"" );
dbURLCritSec.Unlock();
return 0;
}
if ( protocol == "sqlite" )
{
db = vtkSQLiteDatabase::New();
db->ParseURL( URL );
dbURLCritSec.Unlock();
return db;
}
// Okay now for all the other database types get more detailed info
if ( ! vtksys::SystemTools::ParseURL( urlstr, protocol, username,
unused, hostname, dataport, database) )
{
vtkGenericWarningMacro( "Invalid URL (other components missing): \"" << urlstr.c_str() << "\"" );
dbURLCritSec.Unlock();
return 0;
}
#ifdef VTK_USE_POSTGRES
if ( protocol == "psql" )
{
db = vtkPostgreSQLDatabase::New();
db->ParseURL( URL );
}
#endif // VTK_USE_POSTGRES
#ifdef VTK_USE_MYSQL
if ( protocol == "mysql" )
{
db = vtkMySQLDatabase::New();
db->ParseURL( URL );
}
#endif // VTK_USE_MYSQL
#ifdef VTK_USE_ODBC
if ( protocol == "odbc" )
{
db = vtkODBCDatabase::New();
db->ParseURL( URL );
}
#endif // VTK_USE_ODBC
// Now try to look at registered callback to try and find someone who can
// provide us with the required implementation.
if (!db && vtkSQLDatabase::Callbacks)
{
db = vtkSQLDatabase::Callbacks->CreateFromURL( URL );
}
if ( ! db )
{
vtkGenericWarningMacro( "Unsupported protocol: " << protocol.c_str() );
}
dbURLCritSec.Unlock();
return db;
}
// ----------------------------------------------------------------------
bool vtkSQLDatabase::EffectSchema( vtkSQLDatabaseSchema* schema, bool dropIfExists )
{
if ( ! this->IsOpen() )
{
vtkGenericWarningMacro( "Unable to effect the schema: no database is open" );
return false;
}
// Instantiate an empty query and begin the transaction.
vtkSQLQuery* query = this->GetQueryInstance();
if ( ! query->BeginTransaction() )
{
vtkGenericWarningMacro( "Unable to effect the schema: unable to begin transaction" );
return false;
}
// Loop over preamble statements of the schema and execute them only if they are relevant
int numPre = schema->GetNumberOfPreambles();
for ( int preHandle = 0; preHandle < numPre; ++ preHandle )
{
// Don't execute if the statement is not for this backend
const char* preBackend = schema->GetPreambleBackendFromHandle( preHandle );
if ( strcmp( preBackend, VTK_SQL_ALLBACKENDS ) && strcmp( preBackend, this->GetClassName() ) )
{
continue;
}
vtkStdString preStr = schema->GetPreambleActionFromHandle( preHandle );
query->SetQuery( preStr );
if ( ! query->Execute() )
{
vtkGenericWarningMacro( "Unable to effect the schema: unable to execute query.\nDetails: "
<< query->GetLastErrorText() );
query->RollbackTransaction();
query->Delete();
return false;
}
}
// Loop over all tables of the schema and create them
int numTbl = schema->GetNumberOfTables();
for ( int tblHandle = 0; tblHandle < numTbl; ++ tblHandle )
{
// Construct the CREATE TABLE query for this table
vtkStdString queryStr( "CREATE TABLE " );
queryStr += this->GetTablePreamble( dropIfExists );
queryStr += schema->GetTableNameFromHandle( tblHandle );
queryStr += " (";
// Loop over all columns of the current table
int numCol = schema->GetNumberOfColumnsInTable( tblHandle );
if ( numCol < 0 )
{
query->RollbackTransaction();
query->Delete();
return false;
}
bool firstCol = true;
for ( int colHandle = 0; colHandle < numCol; ++ colHandle )
{
if ( ! firstCol )
{
queryStr += ", ";
}
else // ( ! firstCol )
{
firstCol = false;
}
// Get column creation syntax (backend-dependent)
vtkStdString colStr = this->GetColumnSpecification( schema, tblHandle, colHandle );
if ( colStr.size() )
{
queryStr += colStr;
}
else // if ( colStr.size() )
{
query->RollbackTransaction();
query->Delete();
return false;
}
}
// Check out number of indices
int numIdx = schema->GetNumberOfIndicesInTable( tblHandle );
if ( numIdx < 0 )
{
query->RollbackTransaction();
query->Delete();
return false;
}
// In case separate INDEX statements are needed (backend-specific)
vtkstd::vector<vtkStdString> idxStatements;
bool skipped = false;
// Loop over all indices of the current table
for ( int idxHandle = 0; idxHandle < numIdx; ++ idxHandle )
{
// Get index creation syntax (backend-dependent)
vtkStdString idxStr = this->GetIndexSpecification( schema, tblHandle, idxHandle, skipped );
if ( idxStr.size() )
{
if ( skipped )
{
// Must create this index later
idxStatements.push_back( idxStr );
continue;
}
else // if ( skipped )
{
queryStr += idxStr;
}
}
else // if ( idxStr.size() )
{
query->RollbackTransaction();
query->Delete();
return false;
}
}
queryStr += ")";
// Add options to the end of the CREATE TABLE statement
int numOpt = schema->GetNumberOfOptionsInTable( tblHandle );
if ( numOpt < 0 )
{
query->RollbackTransaction();
query->Delete();
return false;
}
for ( int optHandle = 0; optHandle < numOpt; ++ optHandle )
{
vtkStdString optBackend = schema->GetOptionBackendFromHandle( tblHandle, optHandle );
if ( strcmp( optBackend, VTK_SQL_ALLBACKENDS ) && strcmp( optBackend, this->GetClassName() ) )
{
continue;
}
queryStr += " ";
queryStr += schema->GetOptionTextFromHandle( tblHandle, optHandle );
}
// Execute the CREATE TABLE query
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
vtkGenericWarningMacro( "Unable to effect the schema: unable to execute query.\nDetails: "
<< query->GetLastErrorText() );
query->RollbackTransaction();
query->Delete();
return false;
}
// Execute separate CREATE INDEX statements if needed
for ( vtkstd::vector<vtkStdString>::iterator it = idxStatements.begin();
it != idxStatements.end(); ++ it )
{
query->SetQuery( *it );
if ( ! query->Execute() )
{
vtkGenericWarningMacro( "Unable to effect the schema: unable to execute query.\nDetails: "
<< query->GetLastErrorText() );
query->RollbackTransaction();
query->Delete();
return false;
}
}
// Check out number of triggers
int numTrg = schema->GetNumberOfTriggersInTable( tblHandle );
if ( numTrg < 0 )
{
query->RollbackTransaction();
query->Delete();
return false;
}
// Construct CREATE TRIGGER statements only if they are supported by the backend at hand
if ( numTrg && IsSupported( VTK_SQL_FEATURE_TRIGGERS ) )
{
// Loop over all triggers of the current table
for ( int trgHandle = 0; trgHandle < numTrg; ++ trgHandle )
{
// Don't execute if the trigger is not for this backend
const char* trgBackend = schema->GetTriggerBackendFromHandle( tblHandle, trgHandle );
if ( strcmp( trgBackend, VTK_SQL_ALLBACKENDS ) && strcmp( trgBackend, this->GetClassName() ) )
{
continue;
}
// Get trigger creation syntax (backend-dependent)
vtkStdString trgStr = this->GetTriggerSpecification( schema, tblHandle, trgHandle );
// If not empty, execute query
if ( trgStr.size() )
{
query->SetQuery( vtkStdString( trgStr ) );
if ( ! query->Execute() )
{
vtkGenericWarningMacro( "Unable to effect the schema: unable to execute query.\nDetails: "
<< query->GetLastErrorText() );
query->RollbackTransaction();
query->Delete();
return false;
}
}
else // if ( trgStr.size() )
{
query->RollbackTransaction();
query->Delete();
return false;
}
}
}
// If triggers are specified but not supported, don't quit, but let the user know it
else if ( numTrg )
{
vtkGenericWarningMacro( "Triggers are not supported by this SQL backend; ignoring them." );
}
} // for ( int tblHandle = 0; tblHandle < numTbl; ++ tblHandle )
// Commit the transaction.
if ( ! query->CommitTransaction() )
{
vtkGenericWarningMacro( "Unable to effect the schema: unable to commit transaction.\nDetails: "
<< query->GetLastErrorText() );
query->Delete();
return false;
}
query->Delete();
return true;
}