------------------------------------------------------------------------------
| 524 | |
| 525 | //------------------------------------------------------------------------------ |
| 526 | bool vtkPostgreSQLDatabase::CreateDatabase(const char* dbName, bool dropExisting) |
| 527 | { |
| 528 | if (!dbName) |
| 529 | { |
| 530 | vtkErrorMacro("Databases must have a non-nullptr name"); |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | bool dropCurrentlyConnected = false; |
| 535 | if (this->DatabaseName && !strcmp(this->DatabaseName, dbName)) |
| 536 | { |
| 537 | dropCurrentlyConnected = true; |
| 538 | if (dropExisting) |
| 539 | { |
| 540 | // we can't drop a database we're connected to... |
| 541 | this->SetDatabaseName("template1"); |
| 542 | this->Open(); |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | // this will fail... let it. then report the error via LastErrorText |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (!this->Connection) |
| 551 | { |
| 552 | if (this->DatabaseName && !strcmp(this->DatabaseName, dbName)) |
| 553 | { |
| 554 | // we can't connect to a database we haven't created yet and aren't connected to... |
| 555 | this->SetDatabaseName("template1"); |
| 556 | dropCurrentlyConnected = true; |
| 557 | } |
| 558 | bool err = true; |
| 559 | if (this->DatabaseName && this->HostName) |
| 560 | { |
| 561 | err = !this->Open(); |
| 562 | } |
| 563 | if (err) |
| 564 | { |
| 565 | vtkErrorMacro("Must be connected to a server to create a database."); |
| 566 | return false; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (dropExisting) |
| 571 | { |
| 572 | this->DropDatabase(dbName); |
| 573 | } |
| 574 | |
| 575 | std::string qstr("CREATE DATABASE \""); |
| 576 | qstr += dbName; |
| 577 | qstr += "\""; |
| 578 | vtkSQLQuery* query = this->GetQueryInstance(); |
| 579 | query->SetQuery(qstr.c_str()); |
| 580 | if (!query->Execute()) |
| 581 | { |
| 582 | this->SetLastErrorText(query->GetLastErrorText()); |
| 583 | vtkErrorMacro( |