------------------------------------------------------------------------------
| 214 | |
| 215 | //------------------------------------------------------------------------------ |
| 216 | bool vtkPostgreSQLQuery::Execute() |
| 217 | { |
| 218 | if (this->Query == nullptr) |
| 219 | { |
| 220 | vtkErrorMacro("Cannot execute before a query has been set."); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | vtkPostgreSQLDatabase* db = vtkPostgreSQLDatabase::SafeDownCast(this->Database); |
| 225 | assert(db); |
| 226 | |
| 227 | // If a query is already in progress clear out its results so we can |
| 228 | // begin anew. |
| 229 | if (this->QueryInternals) |
| 230 | { |
| 231 | this->DeleteQueryResults(); |
| 232 | } |
| 233 | |
| 234 | if (!db->IsOpen()) |
| 235 | { |
| 236 | this->SetLastErrorText("Cannot execute query. Database connection is closed."); |
| 237 | vtkErrorMacro(<< "Cannot execute query. Database connection is closed."); |
| 238 | this->Active = false; |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | this->QueryInternals = new vtkPostgreSQLQueryPrivate; |
| 243 | this->QueryInternals->QueryResults = PQexec(db->Connection->Connection, this->Query); |
| 244 | |
| 245 | bool returnStatus; |
| 246 | switch (PQresultStatus(this->QueryInternals->QueryResults)) |
| 247 | { |
| 248 | case PGRES_EMPTY_QUERY: |
| 249 | { |
| 250 | returnStatus = true; |
| 251 | this->Active = false; |
| 252 | this->DeleteQueryResults(); |
| 253 | vtkWarningMacro(<< "Query string was set but empty."); |
| 254 | this->SetLastErrorText(nullptr); |
| 255 | }; |
| 256 | break; |
| 257 | |
| 258 | case PGRES_COMMAND_OK: // success on a command returning no data |
| 259 | { |
| 260 | returnStatus = true; |
| 261 | this->Active = true; |
| 262 | this->DeleteQueryResults(); |
| 263 | this->SetLastErrorText(nullptr); |
| 264 | }; |
| 265 | break; |
| 266 | |
| 267 | case PGRES_TUPLES_OK: |
| 268 | { |
| 269 | returnStatus = true; |
| 270 | this->Active = true; |
| 271 | this->SetLastErrorText(nullptr); |
| 272 | }; |
| 273 | break; |