------------------------------------------------------------------------------
| 219 | |
| 220 | //------------------------------------------------------------------------------ |
| 221 | bool vtkPostgreSQLDatabase::Open(const char* password) |
| 222 | { |
| 223 | if (!this->HostName || !this->DatabaseName) |
| 224 | { |
| 225 | this->SetLastErrorText("Cannot open database because HostName and/or DatabaseName are null."); |
| 226 | vtkErrorMacro(<< this->GetLastErrorText()); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | if (this->Connection) |
| 231 | { |
| 232 | if (this->ConnectionMTime > this->URLMTime) |
| 233 | { |
| 234 | return true; // we already had that database open. |
| 235 | } |
| 236 | this->Close(); // close the old connection before opening a new one |
| 237 | } |
| 238 | |
| 239 | std::string options; |
| 240 | options = "dbname="; |
| 241 | options += this->DatabaseName; |
| 242 | |
| 243 | if (this->ServerPort > 0) |
| 244 | { |
| 245 | options += " port="; |
| 246 | std::ostringstream stream; |
| 247 | stream << this->ServerPort; |
| 248 | options += stream.str(); |
| 249 | } |
| 250 | if (this->User && strlen(this->User) > 0) |
| 251 | { |
| 252 | options += " user="; |
| 253 | options += this->User; |
| 254 | } |
| 255 | if (password && this->Password != password) |
| 256 | { |
| 257 | delete[] this->Password; |
| 258 | this->Password = password ? vtksys::SystemTools::DuplicateString(password) : nullptr; |
| 259 | } |
| 260 | if (this->Password && strlen(this->Password) > 0) |
| 261 | { |
| 262 | options += " password="; |
| 263 | options += this->Password; |
| 264 | } |
| 265 | if (this->ConnectOptions && strlen(this->ConnectOptions) > 0) |
| 266 | { |
| 267 | options += this->ConnectOptions; |
| 268 | } |
| 269 | |
| 270 | // If localhost is specified, try the local socket connection |
| 271 | // first. Only if that doesn't work will we try the loopback |
| 272 | // device. |
| 273 | if (!strcmp(this->HostName, "localhost")) |
| 274 | { |
| 275 | if (this->OpenInternal(options.c_str())) |
| 276 | { |
| 277 | this->SetLastErrorText(nullptr); |
| 278 | return true; |