------------------------------------------------------------------------------
| 396 | |
| 397 | //------------------------------------------------------------------------------ |
| 398 | bool vtkSQLDatabase::EffectSchema(vtkSQLDatabaseSchema* schema, bool dropIfExists) |
| 399 | { |
| 400 | if (!this->IsOpen()) |
| 401 | { |
| 402 | vtkGenericWarningMacro("Unable to effect the schema: no database is open"); |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | // Instantiate an empty query and begin the transaction. |
| 407 | vtkSQLQuery* query = this->GetQueryInstance(); |
| 408 | if (!query->BeginTransaction()) |
| 409 | { |
| 410 | vtkGenericWarningMacro("Unable to effect the schema: unable to begin transaction"); |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | // Loop over preamble statements of the schema and execute them only if they are relevant |
| 415 | int numPre = schema->GetNumberOfPreambles(); |
| 416 | for (int preHandle = 0; preHandle < numPre; ++preHandle) |
| 417 | { |
| 418 | // Don't execute if the statement is not for this backend |
| 419 | const char* preBackend = schema->GetPreambleBackendFromHandle(preHandle); |
| 420 | if (strcmp(preBackend, VTK_SQL_ALLBACKENDS) != 0 && |
| 421 | strcmp(preBackend, this->GetClassName()) != 0) |
| 422 | { |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | vtkStdString preStr = schema->GetPreambleActionFromHandle(preHandle); |
| 427 | query->SetQuery(preStr.c_str()); |
| 428 | if (!query->Execute()) |
| 429 | { |
| 430 | vtkGenericWarningMacro("Unable to effect the schema: unable to execute query.\nDetails: " |
| 431 | << query->GetLastErrorText()); |
| 432 | query->RollbackTransaction(); |
| 433 | query->Delete(); |
| 434 | return false; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Loop over all tables of the schema and create them |
| 439 | int numTbl = schema->GetNumberOfTables(); |
| 440 | for (int tblHandle = 0; tblHandle < numTbl; ++tblHandle) |
| 441 | { |
| 442 | // Construct the CREATE TABLE query for this table |
| 443 | std::string queryStr("CREATE TABLE "); |
| 444 | queryStr += this->GetTablePreamble(dropIfExists); |
| 445 | queryStr += schema->GetTableNameFromHandle(tblHandle); |
| 446 | queryStr += " ("; |
| 447 | |
| 448 | // Loop over all columns of the current table |
| 449 | int numCol = schema->GetNumberOfColumnsInTable(tblHandle); |
| 450 | if (numCol < 0) |
| 451 | { |
| 452 | query->RollbackTransaction(); |
| 453 | query->Delete(); |
| 454 | return false; |
| 455 | } |