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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestMySQLDatabase.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.
-------------------------------------------------------------------------*/
// .SECTION Thanks
// Thanks to Andrew Wilson and Philippe Pebay from Sandia National Laboratories
// for implementing this test.
//
// You should set VTK_MYSQL_TEST_URL to the address of a MySQL
// database that you can use for testing. For example, if you have
// MySQL running on foo.mycompany.com, your test user is called
// 'vtktest' and the database is called 'elephant', the URL looks like
// 'mysql://vtktest@foo.mycompany.com/elephant'.
//
// We recommend creating a user and a 'vtktest' database, then giving
// the user in question rights *only* to that database. The password
// 'vtktest' is hard-coded into this file for the moment until we
// decide whether it's a good idea to put any kind of password in
// CMake.
#include "vtkMySQLDatabase.h"
#include "vtkSQLQuery.h"
#include "vtkRowQueryToTable.h"
#include "vtkSQLDatabaseSchema.h"
#include "vtkStdString.h"
#include "vtkTable.h"
#include "vtkTimePointUtility.h"
#include "vtkVariant.h"
#include "vtkVariantArray.h"
#include "vtkToolkits.h"
#include "DatabaseSchemaWith2Tables.h"
#include "vtkIOMySQLTestingCxxConfigure.h"
#include <vector>
int TestMySQLDatabase( int, char ** const )
{
vtkMySQLDatabase* db = vtkMySQLDatabase::SafeDownCast( vtkSQLDatabase::CreateFromURL( VTK_MYSQL_TEST_URL ) );
// Temp code to for linkage ...
vtkMySQLDatabase* tmp = vtkMySQLDatabase::New();
bool status = db->Open("vtktest");
if ( ! status )
{
cerr << "Couldn't open database.\n";
return 1;
}
vtkSQLQuery* query = db->GetQueryInstance();
vtkStdString createQuery( "CREATE TABLE IF NOT EXISTS people (name TEXT, age INTEGER, weight FLOAT)" );
cout << createQuery << endl;
query->SetQuery( createQuery.c_str() );
if ( !query->Execute() )
{
cerr << "Create query failed" << endl;
return 1;
}
for ( int i = 0; i < 40; ++ i )
{
char insertQuery[200];
sprintf( insertQuery, "INSERT INTO people VALUES('John Doe %d', %d, %d)",
i, i, 10*i );
cout << insertQuery << endl;
query->SetQuery( insertQuery );
if ( !query->Execute() )
{
cerr << "Insert query " << i << " failed" << endl;
return 1;
}
}
const char* queryText = "SELECT name, age, weight FROM people WHERE age <= 20";
query->SetQuery( queryText );
cerr << endl << "Running query: " << query->GetQuery() << endl;
cerr << endl << "Using vtkSQLQuery directly to execute query:" << endl;
if ( !query->Execute() )
{
cerr << "Query failed" << endl;
return 1;
}
for ( int col = 0; col < query->GetNumberOfFields(); ++ col )
{
if ( col > 0 )
{
cerr << ", ";
}
cerr << query->GetFieldName( col );
}
cerr << endl;
while ( query->NextRow() )
{
for ( int field = 0; field < query->GetNumberOfFields(); ++ field )
{
if ( field > 0 )
{
cerr << ", ";
}
cerr << query->DataValue( field ).ToString().c_str();
}
cerr << endl;
}
cerr << endl << "Using vtkSQLQuery to execute query and retrieve by row:" << endl;
if ( !query->Execute() )
{
cerr << "Query failed" << endl;
return 1;
}
for ( int col = 0; col < query->GetNumberOfFields(); ++ col )
{
if ( col > 0 )
{
cerr << ", ";
}
cerr << query->GetFieldName( col );
}
cerr << endl;
vtkVariantArray* va = vtkVariantArray::New();
while ( query->NextRow( va ) )
{
for ( int field = 0; field < va->GetNumberOfValues(); ++ field )
{
if ( field > 0 )
{
cerr << ", ";
}
cerr << va->GetValue( field ).ToString().c_str();
}
cerr << endl;
}
va->Delete();
cerr << endl << "Using vtkRowQueryToTable to execute query:" << endl;
vtkRowQueryToTable* reader = vtkRowQueryToTable::New();
reader->SetQuery( query );
reader->Update();
vtkTable* table = reader->GetOutput();
for ( vtkIdType col = 0; col < table->GetNumberOfColumns(); ++ col )
{
table->GetColumn( col )->Print( cerr );
}
cerr << endl;
for ( vtkIdType row = 0; row < table->GetNumberOfRows(); ++ row )
{
for ( vtkIdType col = 0; col < table->GetNumberOfColumns(); ++ col )
{
vtkVariant v = table->GetValue( row, col );
cerr << "row " << row << ", col " << col << " - "
<< v.ToString() << " ( " << vtkImageScalarTypeNameMacro( v.GetType()) << " )" << endl;
}
}
query->SetQuery( "DROP TABLE people" );
if ( ! query->Execute() )
{
cerr << "DROP TABLE people query failed" << endl;
return 1;
}
reader->Delete();
query->Delete();
db->Delete();
// ----------------------------------------------------------------------
// Testing transformation of a schema into a MySQL database
// 1. Create the schema
DatabaseSchemaWith2Tables schema;
// 2. Convert the schema into a MySQL database
cerr << "@@ Converting the schema into a MySQL database...";
db = vtkMySQLDatabase::SafeDownCast( vtkSQLDatabase::CreateFromURL( VTK_MYSQL_TEST_URL ) );
status = db->Open("vtktest");
if ( ! status )
{
cerr << "Couldn't open database.\n";
return 1;
}
status = db->EffectSchema( schema.GetSchema() );
if ( ! status )
{
cerr << "Could not effect test schema.\n";
return 1;
}
cerr << " done." << endl;
// 3. Count tables of the newly created database
cerr << "@@ Counting tables of the newly created database... ";
query = db->GetQueryInstance();
query->SetQuery( "SHOW TABLES" );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
return 1;
}
std::vector<vtkStdString> tables;
while ( query->NextRow() )
{
tables.push_back( query->DataValue( 0 ).ToString() );
}
int numTbl = tables.size();
if ( numTbl != schema->GetNumberOfTables() )
{
cerr << "Found an incorrect number of tables: "
<< numTbl
<< " != "
<< schema->GetNumberOfTables()
<< endl;
return 1;
}
cerr << numTbl
<< " found.\n";
// 4. Inspect these tables
cerr << "@@ Inspecting these tables..." << "\n";
vtkStdString queryStr;
int tblHandle = schema.GetTableBHandle();
for ( tblHandle = 0; tblHandle < numTbl; ++ tblHandle )
{
vtkStdString tblName( schema->GetTableNameFromHandle( tblHandle ) );
cerr << " Table: "
<< tblName
<< "\n";
if ( tblName != tables[tblHandle] )
{
cerr << "Fetched an incorrect name: "
<< tables[tblHandle]
<< " != "
<< tblName
<< endl;
return 1;
}
// 4.1 Check columns
queryStr = "DESCRIBE ";
queryStr += tblName;
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
return 1;
}
int numFields = query->GetNumberOfFields();
int colHandle = 0;
for ( ; query->NextRow(); ++ colHandle )
{
for ( int field = 0; field < numFields; ++ field )
{
if ( field )
{
cerr << ", ";
}
else // if ( field )
{
vtkStdString colName ( schema->GetColumnNameFromHandle( tblHandle, colHandle ) );
if ( colName != query->DataValue( field ).ToString() )
{
cerr << "Found an incorrect column name: "
<< query->DataValue( field ).ToString()
<< " != "
<< colName
<< endl;
return 1;
}
cerr << " Column: ";
}
cerr << query->DataValue( field ).ToString().c_str();
}
cerr << endl;
}
if ( colHandle != schema->GetNumberOfColumnsInTable( tblHandle ) )
{
cerr << "Found an incorrect number of columns: "
<< colHandle
<< " != "
<< schema->GetNumberOfColumnsInTable( tblHandle )
<< endl;
return 1;
}
// 4.2 Check indices
queryStr = "SHOW INDEX FROM ";
queryStr += tblName;
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
return 1;
}
numFields = query->GetNumberOfFields();
int idxHandle = -1;
while ( query->NextRow() )
{
int cnmHandle = atoi( query->DataValue( 3 ).ToString() ) - 1;
if ( ! cnmHandle )
{
++ idxHandle;
}
vtkStdString colName ( schema->GetIndexColumnNameFromHandle( tblHandle, idxHandle, cnmHandle ) );
for ( int field = 0; field < numFields; ++ field )
{
if ( field )
{
cerr << ", ";
}
else // if ( field )
{
cerr << " Index: ";
}
cerr << query->DataValue( field ).ToString().c_str();
}
cerr << endl;
if ( colName != query->DataValue( 4 ).ToString() )
{
cerr << "Fetched an incorrect column name: "
<< query->DataValue( 4 ).ToString()
<< " != "
<< colName
<< endl;
return 1;
}
}
if ( idxHandle + 1 != schema->GetNumberOfIndicesInTable( tblHandle ) )
{
cerr << "Found an incorrect number of indices: "
<< idxHandle + 1
<< " != "
<< schema->GetNumberOfIndicesInTable( tblHandle )
<< endl;
return 1;
}
}
// 5. Populate these tables using the trigger mechanism
cerr << "@@ Populating table atable...";
queryStr = "INSERT INTO atable (somename,somenmbr) VALUES ( 'Bas-Rhin', 67 )";
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
query->Delete();
db->Delete();
return 1;
}
queryStr = "INSERT INTO atable (somename,somenmbr) VALUES ( 'Hautes-Pyrenees', 65 )";
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
query->Delete();
db->Delete();
return 1;
}
queryStr = "INSERT INTO atable (somename,somenmbr) VALUES ( 'Vosges', 88 )";
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
query->Delete();
db->Delete();
return 1;
}
cerr << " done." << endl;
// 6. Check that the trigger-dependent table has indeed been populated
cerr << "@@ Checking trigger-dependent table btable...\n";
queryStr = "SELECT somevalue FROM btable ORDER BY somevalue DESC";
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
query->Delete();
db->Delete();
return 1;
}
cerr << " Entries in column somevalue of table btable, in descending order:\n";
static const char *dpts[] = { "88", "67", "65" };
int numDpt = 0;
for ( ; query->NextRow(); ++ numDpt )
{
if ( query->DataValue( 0 ).ToString() != dpts[numDpt] )
{
cerr << "Found an incorrect value: "
<< query->DataValue( 0 ).ToString()
<< " != "
<< dpts[numDpt]
<< endl;
query->Delete();
db->Delete();
return 1;
}
cerr << " "
<< query->DataValue( 0 ).ToString()
<< "\n";
}
if ( numDpt != 3 )
{
cerr << "Found an incorrect number of entries: "
<< numDpt
<< " != "
<< 3
<< endl;
query->Delete();
db->Delete();
return 1;
}
cerr << " done." << endl;
// 7. Test EscapeString.
cerr << "@@ Escaping a naughty string...";
queryStr =
"INSERT INTO atable (somename,somenmbr) VALUES ( " +
query->EscapeString( vtkStdString( "Str\"ang'eS\ntring" ), true ) +
", 2 )";
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
query->Delete();
db->Delete();
return 1;
}
cerr << " done." << endl;
// 8. Read back the escaped string to verify it worked.
cerr << "@@ Reading it back... <";
queryStr = "SELECT somename FROM atable WHERE somenmbr=2";
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
query->Delete();
db->Delete();
return 1;
}
if ( ! query->NextRow() )
{
cerr << "Query returned no results" << endl;
query->Delete();
db->Delete();
return 1;
}
cerr << query->DataValue( 0 ).ToString().c_str() << "> ";
cerr << " done." << endl;
// 8. Drop tables
cerr << "@@ Dropping these tables...";
for ( std::vector<vtkStdString>::iterator it = tables.begin();
it != tables.end(); ++ it )
{
queryStr = "DROP TABLE ";
queryStr += *it;
query->SetQuery( queryStr );
if ( ! query->Execute() )
{
cerr << "Query failed" << endl;
return 1;
}
}
cerr << " done." << endl;
// ----------------------------------------------------------------------
// Testing time values in database
cerr << "@@ Testing time values" << endl;
query->SetQuery("create table if not exists time (_date DATE, _time TIME, _timestamp TIMESTAMP, _datetime DATETIME, _year YEAR);");
cerr << query->GetQuery() << endl;
if ( ! query->Execute() )
{
cerr << "Time table creation failed" << endl;
return 1;
}
query->SetQuery("insert into time values ('2008-01-01', '01:23:45', '2008-01-01 01:23:45', '2008-01-01 01:23:45', 2008);");
cerr << query->GetQuery() << endl;
if ( ! query->Execute() )
{
cerr << "Time table insert failed" << endl;
return 1;
}
query->SetQuery("select * from time");
cerr << query->GetQuery() << endl;
if ( ! query->Execute() )
{
cerr << "Time table select failed" << endl;
return 1;
}
query->NextRow();
vtkTypeUInt64 date = vtkTimePointUtility::ISO8601ToTimePoint( query->DataValue( 0 ).ToString() );
vtkTypeUInt64 time = vtkTimePointUtility::ISO8601ToTimePoint( query->DataValue( 1 ).ToString() );
vtkTypeUInt64 timestamp = vtkTimePointUtility::ISO8601ToTimePoint( query->DataValue( 2 ).ToString() );
vtkTypeUInt64 datetime = vtkTimePointUtility::ISO8601ToTimePoint( query->DataValue( 3 ).ToString() );
int month, day, year, hour, minute, second, msec;
vtkTimePointUtility::GetDate(date, year, month, day);
if ( year != 2008 || month != 1 || day != 1 )
{
cerr << "Date read incorrectly" << endl;
return 1;
}
vtkTimePointUtility::GetTime(time, hour, minute, second, msec);
if ( hour != 1 || minute != 23 || second != 45 )
{
cerr << "Time read incorrectly" << endl;
return 1;
}
vtkTimePointUtility::GetDateTime(timestamp, year, month, day, hour, minute, second, msec);
if ( year != 2008 || month != 1 || day != 1 || hour != 1 || minute != 23 || second != 45 )
{
cerr << "Timestamp read incorrectly" << endl;
return 1;
}
vtkTimePointUtility::GetDateTime(datetime, year, month, day, hour, minute, second, msec);
if ( year != 2008 || month != 1 || day != 1 || hour != 1 || minute != 23 || second != 45 )
{
cerr << "Datetime read incorrectly" << endl;
return 1;
}
year = query->DataValue( 4 ).ToInt();
if ( year != 2008 )
{
cerr << "Year read incorrectly" << endl;
return 1;
}
query->SetQuery("drop table time;");
cerr << query->GetQuery() << endl;
if ( ! query->Execute() )
{
cerr << "Time table drop failed" << endl;
return 1;
}
// Clean up
db->Delete();
query->Delete();
return 0;
}
|