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 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
|
/*=========================================================================
Program Toolkit
Module: vtkODBCDatabase.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 (c) Sandia Corporation
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------
*/
/*
* Microsoft's own version of sqltypes.h relies on some typedefs and
* macros in windows.h. This next fragment tells VTK to include the
* whole thing without any of its usual #defines to keep the size
* manageable. No WIN32_LEAN_AND_MEAN for us!
*/
#if defined(_WIN32) && !defined(__CYGWIN__)
# include <vtkWindows.h>
#endif
#include <vtkSQLDatabaseSchema.h>
#include "vtkODBCDatabase.h"
#include "vtkODBCQuery.h"
#include "vtkODBCInternals.h"
#include "vtkObjectFactory.h"
#include "vtkStringArray.h"
#include <vtksys/SystemTools.hxx>
#include <vtksys/ios/sstream>
#include <assert.h>
#include <string.h>
#include <sql.h>
#include <sqlext.h>
// ----------------------------------------------------------------------------
vtkStandardNewMacro(vtkODBCDatabase);
// ----------------------------------------------------------------------------
static vtkStdString GetErrorMessage(SQLSMALLINT handleType,
SQLHANDLE handle,
int *code=0)
{
SQLINTEGER sqlNativeCode = 0;
SQLSMALLINT messageLength = 0;
SQLRETURN status;
SQLCHAR state[SQL_SQLSTATE_SIZE + 1];
SQLCHAR description[SQL_MAX_MESSAGE_LENGTH + 1];
vtkStdString finalResult;
int i = 1;
// There may be several error messages queued up so we need to loop
// until we've got everything.
vtksys_ios::ostringstream messagebuf;
do
{
status = SQLGetDiagRec(handleType, handle,
i,
state,
&sqlNativeCode,
description,
SQL_MAX_MESSAGE_LENGTH,
&messageLength);
description[SQL_MAX_MESSAGE_LENGTH] = 0;
if (status == SQL_SUCCESS || status == SQL_SUCCESS_WITH_INFO)
{
if (code)
{
*code = sqlNativeCode;
}
if (i > 1)
{
messagebuf << ", ";
}
messagebuf << state << ' ' << description;
}
else if (status == SQL_ERROR || status == SQL_INVALID_HANDLE)
{
return vtkStdString(messagebuf.str());
}
++i;
} while (status != SQL_NO_DATA);
return vtkStdString(messagebuf.str());
}
// ----------------------------------------------------------------------------
// COLUMN is zero-indexed but ODBC indexes from 1. Sigh. Aren't
// standards fun?
//
// Also, this will need to be updated when we start handling Unicode
// characters.
static vtkStdString odbcGetString(SQLHANDLE statement,
int column,
int columnSize)
{
vtkStdString returnString;
SQLRETURN status = SQL_ERROR;
SQLLEN lengthIndicator;
// Make sure we've got room to store the results but don't go past 64k
if (columnSize <= 0)
{
columnSize = 1024;
}
else if (columnSize > 65536)
{
columnSize = 65536;
}
else
{
// make room for the null terminator
++ columnSize;
}
char *buffer = new char[columnSize];
while (true)
{
status = SQLGetData(statement,
column+1,
SQL_C_CHAR,
static_cast<SQLPOINTER>(buffer),
columnSize,
&lengthIndicator);
if (status == SQL_SUCCESS ||
status == SQL_SUCCESS_WITH_INFO)
{
if (lengthIndicator == SQL_NULL_DATA ||
lengthIndicator == SQL_NO_TOTAL)
{
break;
}
int resultSize = 0;
if (status == SQL_SUCCESS_WITH_INFO)
{
// SQL_SUCCESS_WITH_INFO means that there's more data to
// retrieve so we have to do it in chunks -- hence the while
// loop.
resultSize = columnSize - 1;
}
else
{
resultSize = lengthIndicator;
}
buffer[resultSize] = 0;
returnString += buffer;
}
else if (status == SQL_NO_DATA)
{
// we're done
break;
}
else
{
cerr << "odbcGetString: Error "
<< status << " in SQLGetData\n";
break;
}
}
delete [] buffer;
return returnString;
}
// ----------------------------------------------------------------------------
vtkODBCDatabase::vtkODBCDatabase()
{
this->Internals = new vtkODBCInternals;
this->Tables = vtkStringArray::New();
this->Tables->Register(this);
this->Tables->Delete();
this->LastErrorText = NULL;
this->Record = vtkStringArray::New();
this->Record->Register(this);
this->Record->Delete();
this->UserName = NULL;
this->HostName = NULL;
this->DataSourceName = NULL;
this->DatabaseName = NULL;
this->Password = NULL;
this->ServerPort = -1; // use whatever the driver defaults to
// Initialize instance variables
this->DatabaseType = 0;
this->SetDatabaseType("ODBC");
}
// ----------------------------------------------------------------------------
vtkODBCDatabase::~vtkODBCDatabase()
{
if ( this->IsOpen() )
{
this->Close();
}
if ( this->DatabaseType )
{
this->SetDatabaseType(0);
}
this->SetLastErrorText(NULL);
this->SetUserName(NULL);
this->SetHostName(NULL);
this->SetPassword(NULL);
this->SetDataSourceName(NULL);
this->SetDatabaseName(NULL);
delete this->Internals;
this->Tables->UnRegister(this);
this->Record->UnRegister(this);
}
// ----------------------------------------------------------------------------
bool vtkODBCDatabase::IsSupported(int feature)
{
switch (feature)
{
case VTK_SQL_FEATURE_BATCH_OPERATIONS:
case VTK_SQL_FEATURE_NAMED_PLACEHOLDERS:
return false;
case VTK_SQL_FEATURE_POSITIONAL_PLACEHOLDERS:
#if MYSQL_VERSION_ID >= 40108
return true;
#else
return false;
#endif
case VTK_SQL_FEATURE_PREPARED_QUERIES:
{
return true;
}
case VTK_SQL_FEATURE_UNICODE:
return false; // not until we have vtkStdWideString
case VTK_SQL_FEATURE_QUERY_SIZE:
case VTK_SQL_FEATURE_BLOB:
case VTK_SQL_FEATURE_LAST_INSERT_ID:
case VTK_SQL_FEATURE_TRANSACTIONS:
return true;
default:
{
vtkErrorMacro(<< "Unknown SQL feature code " << feature << "! See "
<< "vtkSQLDatabase.h for a list of possible features.");
return false;
};
}
}
// ----------------------------------------------------------------------------
bool vtkODBCDatabase::Open(const char *password)
{
if ( ! this->DataSourceName )
{
this->SetLastErrorText("Cannot open database because database ID is null.");
vtkErrorMacro(<< this->GetLastErrorText());
return false;
}
if ( this->IsOpen() )
{
vtkGenericWarningMacro( "Open(): Database is already open." );
return true;
}
SQLRETURN status;
status = SQLAllocHandle(SQL_HANDLE_ENV,
SQL_NULL_HANDLE,
& (this->Internals->Environment));
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
// We don't actually have a valid SQL handle yet so I don't think
// we can actually retrieve an error message.
vtksys_ios::ostringstream sbuf;
sbuf << "vtkODBCDatabase::Open: Unable to allocate environment handle. "
<< "Return code " << status << ", error message: "
<< GetErrorMessage(SQL_HANDLE_ENV,
this->Internals->Environment);
this->SetLastErrorText(sbuf.str().c_str());
return false;
}
else
{
vtkDebugMacro(<<"Successfully allocated environment handle.");
status = SQLSetEnvAttr(this->Internals->Environment,
SQL_ATTR_ODBC_VERSION,
reinterpret_cast<SQLPOINTER>(SQL_OV_ODBC3),
SQL_IS_UINTEGER);
}
// Create the connection string itself
vtkStdString connectionString;
if (strstr(this->DataSourceName, ".dsn") != NULL)
{
// the data source is a file of some sort
connectionString = "FILEDSN=";
connectionString += this->DataSourceName;
}
else if (strstr(this->DataSourceName, "DRIVER") != NULL ||
strstr(this->DataSourceName, "SERVER"))
{
connectionString = this->DataSourceName;
}
else
{
connectionString = "DSN=";
connectionString += this->DataSourceName;
}
if (this->UserName != NULL && strlen(this->UserName) > 0)
{
connectionString += ";UID=";
connectionString += this->UserName;
}
if (password != NULL)
{
connectionString += ";PWD=";
connectionString += password;
}
if (this->DatabaseName != NULL && strlen(this->DatabaseName) > 0)
{
connectionString += ";DATABASE=";
connectionString += this->DatabaseName;
}
// Get a handle to connect with
status = SQLAllocHandle(SQL_HANDLE_DBC,
this->Internals->Environment,
&(this->Internals->Connection));
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errbuf;
errbuf << "Error allocating ODBC connection handle: "
<< GetErrorMessage(SQL_HANDLE_ENV, this->Internals->Environment);
this->SetLastErrorText(errbuf.str().c_str());
return false;
}
vtkDebugMacro(<<"ODBC connection handle successfully allocated");
#ifdef ODBC_DRIVER_IS_IODBC
// Set the driver name so we know who to blame
vtkStdString driverName("vtkODBCDatabase driver");
status = SQLSetConnectAttr(this->Internals->Connection,
SQL_APPLICATION_NAME,
driverName.c_str(),
driverName.size());
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errbuf;
errbuf << "Error setting driver name: "
<< GetErrorMessage(SQL_HANDLE_DBC, this->Internals->Connection);
this->SetLastErrorText(errbuf.str().c_str());
return false;
}
else
{
vtkDebugMacro(<<"Successfully set driver name on connect string.");
}
#endif
SQLSMALLINT cb;
SQLTCHAR connectionOut[1024];
status = SQLDriverConnect(this->Internals->Connection,
NULL,
(SQLCHAR *)(connectionString.c_str()),
connectionString.size(),
connectionOut,
1024,
&cb,
SQL_DRIVER_NOPROMPT);
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream sbuf;
sbuf << "vtkODBCDatabase::Open: Error during connection: "
<< GetErrorMessage(SQL_HANDLE_DBC, this->Internals->Connection);
this->SetLastErrorText(sbuf.str().c_str());
return false;
}
vtkDebugMacro(<<"Connection successful.");
return true;
}
// ----------------------------------------------------------------------------
void vtkODBCDatabase::Close()
{
if (! this->IsOpen())
{
return; // not an error
}
else
{
SQLRETURN status;
if (this->Internals->Connection != SQL_NULL_HDBC)
{
status = SQLDisconnect(this->Internals->Connection);
if (status != SQL_SUCCESS)
{
vtkWarningMacro(<< "ODBC Close: Unable to disconnect data source");
}
status = SQLFreeHandle(SQL_HANDLE_DBC, this->Internals->Connection);
if (status != SQL_SUCCESS)
{
vtkWarningMacro(<< "ODBC Close: Unable to free connection handle");
}
this->Internals->Connection = NULL;
}
if (this->Internals->Environment != SQL_NULL_HENV)
{
status = SQLFreeHandle(SQL_HANDLE_ENV, this->Internals->Environment);
if (status != SQL_SUCCESS)
{
vtkWarningMacro(<< "ODBC Close: Unable to free environment handle");
}
this->Internals->Environment = NULL;
}
}
}
// ----------------------------------------------------------------------------
bool vtkODBCDatabase::IsOpen()
{
return (this->Internals->Connection != SQL_NULL_HDBC);
}
// ----------------------------------------------------------------------------
vtkSQLQuery *vtkODBCDatabase::GetQueryInstance()
{
vtkODBCQuery *query = vtkODBCQuery::New();
query->SetDatabase(this);
return query;
}
// ----------------------------------------------------------------------------
const char*vtkODBCDatabase::GetLastErrorText()
{
return this->LastErrorText;
}
// ----------------------------------------------------------------------------
vtkStringArray *vtkODBCDatabase::GetTables()
{
this->Tables->Resize(0);
if (!this->IsOpen())
{
vtkErrorMacro(<<"GetTables(): Database is closed!");
return this->Tables;
}
else
{
SQLHANDLE statement;
SQLRETURN status = SQLAllocHandle(SQL_HANDLE_STMT,
this->Internals->Connection,
&statement);
if (status != SQL_SUCCESS)
{
vtkErrorMacro(<< "vtkODBCDatabase::GetTables: Unable to allocate statement");
return this->Tables;
}
status = SQLSetStmtAttr(statement,
SQL_ATTR_CURSOR_TYPE,
static_cast<SQLPOINTER>(SQL_CURSOR_FORWARD_ONLY),
SQL_IS_UINTEGER);
vtkStdString tableType("TABLE,");
status = SQLTables(statement, NULL, 0, NULL, 0, NULL, 0,
(SQLCHAR *)(tableType.c_str()),
tableType.size());
if (status != SQL_SUCCESS)
{
vtkErrorMacro(<< "vtkODBCDatabase::GetTables: Unable to execute table list");
return this->Tables;
}
status = SQLFetchScroll(statement, SQL_FETCH_NEXT, 0);
while (status == SQL_SUCCESS)
{
vtkStdString fieldVal = odbcGetString(statement, 2, -1);
this->Tables->InsertNextValue(fieldVal);
status = SQLFetchScroll(statement, SQL_FETCH_NEXT, 0);
}
status = SQLFreeHandle(SQL_HANDLE_STMT, statement);
if (status != SQL_SUCCESS)
{
vtkErrorMacro(<<"vtkODBCDatabase::GetTables: Unable to free statement handle. Error "
<< status);
}
return this->Tables;
}
}
// ----------------------------------------------------------------------------
vtkStringArray *vtkODBCDatabase::GetRecord(const char *table)
{
this->Record->Reset();
this->Record->Allocate(20);
if (!this->IsOpen())
{
vtkErrorMacro(<<"GetRecord: Database is not open!");
return this->Record;
}
SQLHANDLE statement;
SQLRETURN status = SQLAllocHandle(SQL_HANDLE_STMT,
this->Internals->Connection,
&statement);
if (status != SQL_SUCCESS)
{
vtkErrorMacro(<<"vtkODBCDatabase: Unable to allocate statement: error "
<< status);
return this->Record;
}
status = SQLSetStmtAttr(statement,
SQL_ATTR_METADATA_ID,
reinterpret_cast<SQLPOINTER>(SQL_TRUE),
SQL_IS_INTEGER);
if (status != SQL_SUCCESS)
{
vtkErrorMacro(<<"vtkODBCDatabase::GetRecord: Unable to set SQL_ATTR_METADATA_ID attribute on query. Return code: " << status);
return NULL;
}
status = SQLSetStmtAttr(statement,
SQL_ATTR_CURSOR_TYPE,
static_cast<SQLPOINTER>(SQL_CURSOR_FORWARD_ONLY),
SQL_IS_UINTEGER);
status = SQLColumns(statement,
NULL, // catalog
0,
NULL, // schema
0,
(SQLCHAR *)(table),
strlen(table),
NULL, // column
0);
if (status != SQL_SUCCESS && status != 0)
{
vtkStdString error = GetErrorMessage(SQL_HANDLE_STMT, statement);
vtkErrorMacro(<<"vtkODBCDatabase::GetRecord: Unable to retrieve column list (SQLColumns): error " << error.c_str());
this->SetLastErrorText(error.c_str());
SQLFreeHandle(SQL_HANDLE_STMT, statement);
return this->Record;
}
status = SQLFetchScroll(statement, SQL_FETCH_NEXT, 0);
if (status != SQL_SUCCESS)
{
vtkStdString error = GetErrorMessage(SQL_HANDLE_STMT, statement);
vtkErrorMacro(<<"vtkODBCDatabase::GetRecord: Unable to retrieve column list (SQLFetchScroll): error " << error.c_str());
this->SetLastErrorText(error.c_str());
SQLFreeHandle(SQL_HANDLE_STMT, statement);
return this->Record;
}
while (status == SQL_SUCCESS)
{
vtkStdString fieldName = odbcGetString(statement, 3, -1);
this->Record->InsertNextValue(fieldName);
status = SQLFetchScroll(statement, SQL_FETCH_NEXT, 0);
}
status = SQLFreeHandle(SQL_HANDLE_STMT, statement);
if (status != SQL_SUCCESS)
{
vtkErrorMacro("vtkODBCDatabase: Unable to free statement handle: error "
<< status);
}
return this->Record;
}
// ----------------------------------------------------------------------------
void vtkODBCDatabase::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "DataSourceName: ";
if(this->DataSourceName==0)
{
os << "(none)" << endl;
}
else
{
os << this->DataSourceName << endl;
}
os << indent << "DatabaseName: ";
if(this->DatabaseName==0)
{
os << "(none)" << endl;
}
else
{
os << this->DatabaseName << endl;
}
os << indent << "UserName: ";
if(this->UserName==0)
{
os << "(none)" << endl;
}
else
{
os << this->UserName << endl;
}
os << indent << "HostName: ";
if(this->HostName==0)
{
os << "(none)" << endl;
}
else
{
os << this->HostName << endl;
}
os << indent << "Password: ";
if(this->Password==0)
{
os << "(none)" << endl;
}
else
{
os << "not displayed for security reason." << endl;
}
os << indent << "ServerPort: " << this->ServerPort << endl;
os << indent << "DatabaseType: "
<< (this->DatabaseType ? this->DatabaseType : "NULL") << endl;
}
// ----------------------------------------------------------------------------
bool vtkODBCDatabase::HasError()
{
return this->LastErrorText != NULL;
}
// ----------------------------------------------------------------------------
vtkStdString vtkODBCDatabase::GetURL()
{
return vtkStdString("GetURL on ODBC databases is not yet implemented");
}
// ----------------------------------------------------------------------------
bool vtkODBCDatabase::ParseURL(const char *URL)
{
std::string urlstr( URL ? URL : "" );
std::string protocol;
std::string username;
std::string unused;
std::string dsname;
std::string dataport;
std::string database;
// Okay now for all the other database types get more detailed info
if ( ! vtksys::SystemTools::ParseURL(
urlstr, protocol, username, unused, dsname, dataport, database) )
{
vtkErrorMacro( "Invalid URL: \"" << urlstr.c_str() << "\"" );
return false;
}
if ( protocol == "odbc" )
{
this->SetUserName(username.c_str());
this->SetServerPort(atoi(dataport.c_str()));
this->SetDatabaseName(database.c_str());
this->SetDataSourceName(dsname.c_str());
return true;
}
return false;
}
// ----------------------------------------------------------------------------
vtkStdString vtkODBCDatabase::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 NOT NULL";
break;
case vtkSQLDatabaseSchema::SMALLINT:
colTypeStr = "SMALLINT";
break;
case vtkSQLDatabaseSchema::INTEGER:
colTypeStr = "INT";
break;
case vtkSQLDatabaseSchema::BIGINT:
colTypeStr = "BIGINT";
break;
case vtkSQLDatabaseSchema::VARCHAR:
colTypeStr = "VARCHAR";
break;
case vtkSQLDatabaseSchema::TEXT:
colTypeStr = "TEXT";
break;
case vtkSQLDatabaseSchema::REAL:
colTypeStr = "FLOAT";
break;
case vtkSQLDatabaseSchema::DOUBLE:
colTypeStr = "DOUBLE PRECISION";
break;
case vtkSQLDatabaseSchema::BLOB:
colTypeStr = "BLOB";
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; // Eventually will make DB schemata handle (M,D) sizes
break;
case vtkSQLDatabaseSchema::DOUBLE:
colSizeType = 0; // Eventually will make DB schemata handle (M,D) sizes
break;
case vtkSQLDatabaseSchema::BLOB:
colSizeType = 1;
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 vtkODBCDatabase::GetIndexSpecification(
vtkSQLDatabaseSchema* schema,
int tblHandle,
int idxHandle,
bool& skipped)
{
skipped = false;
vtkStdString queryStr = ", ";
bool mustUseName = true;
int idxType = schema->GetIndexTypeFromHandle( tblHandle, idxHandle );
switch ( idxType )
{
case vtkSQLDatabaseSchema::PRIMARY_KEY:
queryStr += "PRIMARY KEY ";
mustUseName = false;
break;
case vtkSQLDatabaseSchema::UNIQUE:
queryStr += "UNIQUE ";
break;
case vtkSQLDatabaseSchema::INDEX:
queryStr += "INDEX ";
break;
default:
return vtkStdString();
}
// No index_name for PRIMARY KEYs
if ( mustUseName )
{
queryStr += schema->GetIndexNameFromHandle( tblHandle, idxHandle );
}
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;
}
// ----------------------------------------------------------------------------
bool vtkODBCDatabase::CreateDatabase(const char *dbName,
bool dropExisting = false )
{
if ( dropExisting )
{
this->DropDatabase( dbName );
}
vtkStdString queryStr;
queryStr = "CREATE DATABASE ";
queryStr += dbName;
vtkSQLQuery* query = this->GetQueryInstance();
query->SetQuery( queryStr.c_str() );
bool status = query->Execute();
query->Delete();
// Close and re-open in case we deleted and recreated the current database
this->Close();
this->Open( this->Password );
return status;
}
// ----------------------------------------------------------------------------
bool vtkODBCDatabase::DropDatabase(const char *dbName)
{
vtkStdString queryStr;
queryStr = "DROP DATABASE ";
queryStr += dbName;
vtkSQLQuery* query = this->GetQueryInstance();
query->SetQuery( queryStr.c_str() );
bool status = query->Execute();
query->Delete();
return status;
}
|