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 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSQLDatabaseSchema.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 "vtkSQLDatabaseSchema.h"
#include "vtkObjectFactory.h"
#include "vtkStdString.h"
#include <stdarg.h> // va_list
#include <vector>
// ----------------------------------------------------------------------
vtkStandardNewMacro(vtkSQLDatabaseSchema);
// ----------------------------------------------------------------------
class vtkSQLDatabaseSchemaInternals
{
public: // NB: use of string instead of char* here to avoid leaks on destruction.
struct Statement
{
vtkStdString Name;
vtkStdString Action; // may have backend-specific stuff
vtkStdString Backend; // only active for this backend, if != ""
};
struct Column
{
vtkSQLDatabaseSchema::DatabaseColumnType Type;
int Size; // used when required, ignored otherwise (e.g. varchar)
vtkStdString Name;
vtkStdString Attributes; // may have backend-specific stuff
};
struct Index
{
vtkSQLDatabaseSchema::DatabaseIndexType Type;
vtkStdString Name;
std::vector<vtkStdString> ColumnNames;
};
struct Trigger
{
vtkSQLDatabaseSchema::DatabaseTriggerType Type;
vtkStdString Name;
vtkStdString Action; // may have backend-specific stuff
vtkStdString Backend; // only active for this backend, if != ""
};
struct Option
{
vtkStdString Text;
vtkStdString Backend;
};
struct Table
{
vtkStdString Name;
std::vector<Column> Columns;
std::vector<Index> Indices;
std::vector<Trigger> Triggers;
std::vector<Option> Options;
};
std::vector<Statement> Preambles;
std::vector<Table> Tables;
};
// ----------------------------------------------------------------------
vtkSQLDatabaseSchema::vtkSQLDatabaseSchema()
{
this->Name = 0;
this->Internals = new vtkSQLDatabaseSchemaInternals;
}
// ----------------------------------------------------------------------
vtkSQLDatabaseSchema::~vtkSQLDatabaseSchema()
{
this->SetName( 0 );
delete this->Internals;
}
// ----------------------------------------------------------------------
void vtkSQLDatabaseSchema::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "Name: ";
if (this->Name)
{
os << this->Name << "\n";
}
else
{
os << "(null)" << "\n";
}
os << indent << "Internals: " << this->Internals << "\n";
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::AddPreamble(
const char* preName, const char* preAction, const char* preBackend )
{
if ( ! preName )
{
vtkErrorMacro( "Cannot add preamble with empty name" );
return -1;
}
vtkSQLDatabaseSchemaInternals::Statement newPre;
int preHandle = static_cast<int>(this->Internals->Preambles.size());
newPre.Name = preName;
newPre.Action = preAction;
newPre.Backend = preBackend;
this->Internals->Preambles.push_back( newPre );
return preHandle;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::AddTable( const char* tblName )
{
if ( ! tblName )
{
vtkErrorMacro( "Cannot add table with empty name" );
return -1;
}
vtkSQLDatabaseSchemaInternals::Table newTbl;
int tblHandle = static_cast<int>(this->Internals->Tables.size());
newTbl.Name = tblName;
this->Internals->Tables.push_back( newTbl );
return tblHandle;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::AddColumnToIndex(
int tblHandle, int idxHandle, int colHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot add column to index of non-existent table " << tblHandle );
return -1;
}
vtkSQLDatabaseSchemaInternals::Table* table = &this->Internals->Tables[tblHandle];
if ( colHandle < 0 || colHandle >= static_cast<int>( table->Columns.size() ) )
{
vtkErrorMacro( "Cannot add non-existent column " << colHandle << " in table " << tblHandle );
return -1;
}
if ( idxHandle < 0 || idxHandle >= static_cast<int>( table->Indices.size() ) )
{
vtkErrorMacro( "Cannot add column to non-existent index " << idxHandle << " of table " << tblHandle );
return -1;
}
table->Indices[idxHandle].ColumnNames.push_back( table->Columns[colHandle].Name );
return static_cast<int>( table->Indices[idxHandle].ColumnNames.size() - 1 );
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::AddColumnToTable(
int tblHandle, int colType, const char* colName,
int colSize, const char* colOpts )
{
if ( ! colName )
{
vtkErrorMacro( "Cannot add column with empty name to table " << tblHandle );
return -1;
}
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot add column to non-existent table " << tblHandle );
return -1;
}
// DCT: This trick avoids copying a Column structure the way push_back would:
int colHandle = static_cast<int>(this->Internals->Tables[tblHandle].Columns.size());
this->Internals->Tables[tblHandle].Columns.resize( colHandle + 1 );
vtkSQLDatabaseSchemaInternals::Column* column = &this->Internals->Tables[tblHandle].Columns[colHandle];
column->Type = static_cast<DatabaseColumnType>( colType );
column->Size = colSize;
column->Name = colName;
column->Attributes = colOpts;
return colHandle;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::AddIndexToTable(
int tblHandle, int idxType, const char* idxName )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot add index to non-existent table " << tblHandle );
return -1;
}
int idxHandle = static_cast<int>(this->Internals->Tables[tblHandle].Indices.size());
this->Internals->Tables[tblHandle].Indices.resize( idxHandle + 1 );
vtkSQLDatabaseSchemaInternals::Index* index = &this->Internals->Tables[tblHandle].Indices[idxHandle];
index->Type = static_cast<DatabaseIndexType>( idxType );
index->Name = idxName;
return idxHandle;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::AddTriggerToTable(
int tblHandle, int trgType, const char* trgName,
const char* trgAction, const char* trgBackend )
{
if ( ! trgName )
{
vtkErrorMacro( "Cannot add trigger with empty name to table " << tblHandle );
return -1;
}
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot add trigger to non-existent table " << tblHandle );
return -1;
}
int trgHandle = static_cast<int>(this->Internals->Tables[tblHandle].Triggers.size());
this->Internals->Tables[tblHandle].Triggers.resize( trgHandle + 1 );
vtkSQLDatabaseSchemaInternals::Trigger* trigger = &this->Internals->Tables[tblHandle].Triggers[trgHandle];
trigger->Type = static_cast<DatabaseTriggerType>( trgType );
trigger->Name = trgName;
trigger->Action = trgAction;
trigger->Backend = trgBackend;
return trgHandle;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::AddOptionToTable(
int tblHandle, const char* optText, const char* optBackend )
{
if ( ! optText )
{
vtkErrorMacro( "Cannot add NULL option to table " << tblHandle );
return -1;
}
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot add option to non-existent table " << tblHandle );
return -1;
}
int optHandle = static_cast<int>( this->Internals->Tables[tblHandle].Options.size() );
this->Internals->Tables[tblHandle].Options.resize( optHandle + 1 );
vtkSQLDatabaseSchemaInternals::Option* optn = &this->Internals->Tables[tblHandle].Options[optHandle];
optn->Text = optText;
optn->Backend = optBackend ? optBackend : VTK_SQL_ALLBACKENDS;
return optHandle;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetPreambleHandleFromName( const char* preName )
{
int i;
int ntab = static_cast<int>(this->Internals->Preambles.size());
vtkStdString preNameStr( preName );
for ( i = 0; i < ntab; ++i )
{
if ( this->Internals->Preambles[i].Name == preNameStr )
{
return i;
}
}
return -1;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetPreambleNameFromHandle( int preHandle )
{
if ( preHandle < 0 || preHandle >= this->GetNumberOfPreambles() )
{
vtkErrorMacro( "Cannot get name of non-existent preamble " << preHandle );
return 0;
}
return this->Internals->Preambles[preHandle].Name;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetPreambleActionFromHandle( int preHandle )
{
if ( preHandle < 0 || preHandle >= this->GetNumberOfPreambles() )
{
vtkErrorMacro( "Cannot get action of non-existent preamble " << preHandle );
return 0;
}
return this->Internals->Preambles[preHandle].Action;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetPreambleBackendFromHandle( int preHandle )
{
if ( preHandle < 0 || preHandle >= this->GetNumberOfPreambles() )
{
vtkErrorMacro( "Cannot get backend of non-existent preamble " << preHandle );
return 0;
}
return this->Internals->Preambles[preHandle].Backend;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetTableHandleFromName( const char* tblName )
{
int i;
int ntab = static_cast<int>(this->Internals->Tables.size());
vtkStdString tblNameStr( tblName );
for ( i = 0; i < ntab; ++i )
{
if ( this->Internals->Tables[i].Name == tblNameStr )
{
return i;
}
}
return -1;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetTableNameFromHandle( int tblHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get name of non-existent table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Name;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetIndexHandleFromName(
const char* tblName, const char* idxName )
{
int tblHandle = this->GetTableHandleFromName( tblName );
if ( tblHandle < 0 )
{
return -1;
}
int i;
int nidx = static_cast<int>(this->Internals->Tables[tblHandle].Indices.size());
vtkStdString idxNameStr( idxName );
for ( i = 0; i < nidx ; ++ i )
{
if ( this->Internals->Tables[tblHandle].Indices[i].Name == idxNameStr )
{
return i;
}
}
return -1;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetIndexNameFromHandle(
int tblHandle, int idxHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get name of an index in non-existent table " << tblHandle );
return 0;
}
if ( idxHandle < 0 || idxHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Indices.size() ) )
{
vtkErrorMacro( "Cannot get name of non-existent index " << idxHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Indices[idxHandle].Name;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetIndexTypeFromHandle(
int tblHandle, int idxHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get type of an index in non-existent table " << tblHandle );
return -1;
}
if ( idxHandle < 0 || idxHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Indices.size() ) )
{
vtkErrorMacro( "Cannot get type of non-existent index " << idxHandle << " in table " << tblHandle );
return -1;
}
return static_cast<int>( this->Internals->Tables[tblHandle].Indices[idxHandle].Type );
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetIndexColumnNameFromHandle(
int tblHandle, int idxHandle, int cnmHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get column name of an index in non-existent table " << tblHandle );
return 0;
}
if ( idxHandle < 0 || idxHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Indices.size() ) )
{
vtkErrorMacro( "Cannot get column name of non-existent index " << idxHandle << " in table " << tblHandle );
return 0;
}
if ( cnmHandle < 0 || cnmHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Indices[idxHandle].ColumnNames.size() ) )
{
vtkErrorMacro( "Cannot get column name of non-existent column " << cnmHandle << " of index " << idxHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Indices[idxHandle].ColumnNames[cnmHandle];
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetColumnHandleFromName(
const char* tblName, const char* colName )
{
int tblHandle = this->GetTableHandleFromName( tblName );
if ( tblHandle < 0 )
{
return -1;
}
int i;
int ncol = static_cast<int>(this->Internals->Tables[tblHandle].Columns.size());
vtkStdString colNameStr( colName );
for ( i = 0; i < ncol ; ++ i )
{
if ( this->Internals->Tables[tblHandle].Columns[i].Name == colNameStr )
{
return i;
}
}
return -1;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetColumnNameFromHandle(
int tblHandle, int colHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get name of a column in non-existent table " << tblHandle );
return 0;
}
if ( colHandle < 0 || colHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Columns.size() ) )
{
vtkErrorMacro( "Cannot get name of non-existent column " << colHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Columns[colHandle].Name;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetColumnTypeFromHandle(
int tblHandle, int colHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get type of a column in non-existent table " << tblHandle );
return -1;
}
if ( colHandle < 0 || colHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Columns.size() ) )
{
vtkErrorMacro( "Cannot get type of non-existent column " << colHandle << " in table " << tblHandle );
return -1;
}
return static_cast<int>( this->Internals->Tables[tblHandle].Columns[colHandle].Type );
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetColumnSizeFromHandle(
int tblHandle, int colHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get size of a column in non-existent table " << tblHandle );
return -1;
}
if ( colHandle < 0 || colHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Columns.size() ) )
{
vtkErrorMacro( "Cannot get size of non-existent column " << colHandle << " in table " << tblHandle );
return -1;
}
return static_cast<int>( this->Internals->Tables[tblHandle].Columns[colHandle].Size );
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetColumnAttributesFromHandle(
int tblHandle, int colHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get attributes of a column in non-existent table " << tblHandle );
return 0;
}
if ( colHandle < 0 || colHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Columns.size() ) )
{
vtkErrorMacro( "Cannot get attributes of non-existent column " << colHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Columns[colHandle].Attributes;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetTriggerHandleFromName(
const char* tblName, const char* trgName )
{
int tblHandle = this->GetTableHandleFromName( tblName );
if ( tblHandle < 0 )
{
return -1;
}
int i;
int ntrg = static_cast<int>(this->Internals->Tables[tblHandle].Triggers.size());
vtkStdString trgNameStr( trgName );
for ( i = 0; i < ntrg ; ++ i )
{
if ( this->Internals->Tables[tblHandle].Triggers[i].Name == trgNameStr )
{
return i;
}
}
return -1;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetTriggerNameFromHandle(
int tblHandle, int trgHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get name of a trigger in non-existent table " << tblHandle );
return 0;
}
if ( trgHandle < 0 || trgHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Triggers.size() ) )
{
vtkErrorMacro( "Cannot get name of non-existent trigger " << trgHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Triggers[trgHandle].Name;
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetTriggerTypeFromHandle(
int tblHandle, int trgHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get type of a trigger in non-existent table " << tblHandle );
return -1;
}
if ( trgHandle < 0 || trgHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Triggers.size() ) )
{
vtkErrorMacro( "Cannot get type of non-existent trigger " << trgHandle << " in table " << tblHandle );
return -1;
}
return this->Internals->Tables[tblHandle].Triggers[trgHandle].Type;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetTriggerActionFromHandle(
int tblHandle, int trgHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get action of a trigger in non-existent table " << tblHandle );
return 0;
}
if ( trgHandle < 0 || trgHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Triggers.size() ) )
{
vtkErrorMacro( "Cannot get action of non-existent trigger " << trgHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Triggers[trgHandle].Action;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetTriggerBackendFromHandle(
int tblHandle, int trgHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get backend of a trigger in non-existent table " << tblHandle );
return 0;
}
if ( trgHandle < 0 || trgHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Triggers.size() ) )
{
vtkErrorMacro( "Cannot get backend of non-existent trigger " << trgHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Triggers[trgHandle].Backend;
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetOptionTextFromHandle(
int tblHandle, int optHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get text of an option in non-existent table " << tblHandle );
return 0;
}
if ( optHandle < 0 || optHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Options.size() ) )
{
vtkErrorMacro( "Cannot get text of non-existent option " << optHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Options[optHandle].Text.c_str();
}
// ----------------------------------------------------------------------
const char* vtkSQLDatabaseSchema::GetOptionBackendFromHandle(
int tblHandle, int optHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get backend of an option in non-existent table " << tblHandle );
return 0;
}
if ( optHandle < 0 || optHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Options.size() ) )
{
vtkErrorMacro( "Cannot get backend of non-existent option " << optHandle << " in table " << tblHandle );
return 0;
}
return this->Internals->Tables[tblHandle].Options[optHandle].Backend.c_str();
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::AddTableMultipleArguments( const char* tblName, ... )
{
int tblHandle = this->AddTable( tblName );
int token;
int dtyp;
int size;
int curIndexHandle;
const char* name;
const char* attr;
const char* bcke;
va_list args;
va_start( args, tblName );
while ( ( token = va_arg( args, int ) ) != END_TABLE_TOKEN )
{
switch ( token )
{
case COLUMN_TOKEN:
dtyp = va_arg( args, int );
name = va_arg( args, const char* );
size = va_arg( args, int );
attr = va_arg( args, const char* );
this->AddColumnToTable( tblHandle, dtyp, name, size, attr );
break;
case INDEX_TOKEN:
dtyp = va_arg( args, int );
name = va_arg( args, const char* );
curIndexHandle = this->AddIndexToTable( tblHandle, dtyp, name );
while ( ( token = va_arg( args, int ) ) != END_INDEX_TOKEN )
{
name = va_arg( args, const char* );
dtyp = this->GetColumnHandleFromName( tblName, name );
this->AddColumnToIndex( tblHandle, curIndexHandle, dtyp );
}
break;
case TRIGGER_TOKEN:
dtyp = va_arg( args, int );
name = va_arg( args, const char* );
attr = va_arg( args, const char* );
bcke = va_arg( args, const char* );
this->AddTriggerToTable( tblHandle, dtyp, name, attr, bcke );
break;
case OPTION_TOKEN:
attr = va_arg( args, const char* );
bcke = va_arg( args, const char* );
this->AddOptionToTable( tblHandle, attr, bcke );
break;
default:
{
vtkErrorMacro( "Bad token " << token << " passed to AddTable" );
va_end( args );
return -1;
}
}
}
va_end( args );
return tblHandle;
}
// ----------------------------------------------------------------------
void vtkSQLDatabaseSchema::Reset()
{
this->Internals->Tables.clear();
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetNumberOfPreambles()
{
return static_cast<int>(this->Internals->Preambles.size());
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetNumberOfTables()
{
return static_cast<int>(this->Internals->Tables.size());
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetNumberOfColumnsInTable( int tblHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get the number of columns of non-existent table " << tblHandle );
return -1;
}
return static_cast<int>(this->Internals->Tables[tblHandle].Columns.size());
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetNumberOfIndicesInTable( int tblHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get the number of indices of non-existent table " << tblHandle );
return -1;
}
return static_cast<int>(this->Internals->Tables[tblHandle].Indices.size());
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetNumberOfColumnNamesInIndex( int tblHandle, int idxHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get the number of column names in index of non-existent table " << tblHandle );
return -1;
}
if ( idxHandle < 0 || idxHandle >= static_cast<int>( this->Internals->Tables[tblHandle].Indices.size() ) )
{
vtkErrorMacro( "Cannot get the number of column names of non-existent index " << idxHandle << " in table " << tblHandle );
return -1;
}
return static_cast<int>(this->Internals->Tables[tblHandle].Indices[idxHandle].ColumnNames.size());
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetNumberOfTriggersInTable( int tblHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get the number of triggers of non-existent table " << tblHandle );
return -1;
}
return static_cast<int>(this->Internals->Tables[tblHandle].Triggers.size());
}
// ----------------------------------------------------------------------
int vtkSQLDatabaseSchema::GetNumberOfOptionsInTable( int tblHandle )
{
if ( tblHandle < 0 || tblHandle >= this->GetNumberOfTables() )
{
vtkErrorMacro( "Cannot get the number of options of non-existent table " << tblHandle );
return -1;
}
return static_cast<int>(this->Internals->Tables[tblHandle].Options.size());
}
|