------------------------------------------------------------------------------
| 487 | |
| 488 | //------------------------------------------------------------------------------ |
| 489 | bool vtkMySQLQuery::Execute() |
| 490 | { |
| 491 | this->Active = false; |
| 492 | |
| 493 | if (this->Query == nullptr) |
| 494 | { |
| 495 | vtkErrorMacro(<< "Cannot execute before a query has been set."); |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | this->Internals->FreeResult(); |
| 500 | |
| 501 | vtkMySQLDatabase* dbContainer = static_cast<vtkMySQLDatabase*>(this->Database); |
| 502 | assert(dbContainer != nullptr); |
| 503 | |
| 504 | if (!dbContainer->IsOpen()) |
| 505 | { |
| 506 | vtkErrorMacro(<< "Cannot execute query. Database is closed."); |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | vtkDebugMacro(<< "Execute(): Query ready to execute."); |
| 511 | |
| 512 | if (this->Query != nullptr && this->Internals->Statement == nullptr) |
| 513 | { |
| 514 | MYSQL* db = dbContainer->Private->Connection; |
| 515 | assert(db != nullptr); |
| 516 | |
| 517 | int result = mysql_query(db, this->Query); |
| 518 | if (result == 0) |
| 519 | { |
| 520 | // The query probably succeeded. |
| 521 | this->Internals->Result = mysql_store_result(db); |
| 522 | |
| 523 | // Statements like INSERT are supposed to return empty result sets, |
| 524 | // but sometimes it is an error for mysql_store_result to return null. |
| 525 | // If Result is null, but mysql_field_count is non-zero, it is an error. |
| 526 | // See: http://dev.mysql.com/doc/refman/5.0/en/null-mysql-store-result.html |
| 527 | if (this->Internals->Result || mysql_field_count(db) == 0) |
| 528 | { |
| 529 | // The query definitely succeeded. |
| 530 | this->SetLastErrorText(nullptr); |
| 531 | // mysql_field_count will return 0 for statements like INSERT. |
| 532 | // set Active to false so that we don't call mysql_fetch_row on a nullptr |
| 533 | // argument and segfault |
| 534 | this->Active = mysql_field_count(db) != 0; |
| 535 | return true; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | // There was an error in mysql_query or mysql_store_result |
| 540 | this->Active = false; |
| 541 | this->SetLastErrorText(mysql_error(db)); |
| 542 | vtkErrorMacro(<< "Query returned an error: " << this->GetLastErrorText()); |
| 543 | return false; |
| 544 | } |
| 545 | } |
| 546 | else |