* Set special variables from a query result * - ERROR: true/false, whether an error occurred on this query * - SQLSTATE: code of error, or "00000" if no error, or "" if unknown * - ROW_COUNT: how many rows were returned or affected, or "0" * - LAST_ERROR_SQLSTATE: same for last error * - LAST_ERROR_MESSAGE: message of last error * * Note: current policy is to apply this only to the results
| 410 | * entered by the user, not queries generated by slash commands. |
| 411 | */ |
| 412 | static void |
| 413 | SetResultVariables(PGresult *results, bool success) |
| 414 | { |
| 415 | if (success) |
| 416 | { |
| 417 | const char *ntuples = PQcmdTuples(results); |
| 418 | |
| 419 | SetVariable(pset.vars, "ERROR", "false"); |
| 420 | SetVariable(pset.vars, "SQLSTATE", "00000"); |
| 421 | SetVariable(pset.vars, "ROW_COUNT", *ntuples ? ntuples : "0"); |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | const char *code = PQresultErrorField(results, PG_DIAG_SQLSTATE); |
| 426 | const char *mesg = PQresultErrorField(results, PG_DIAG_MESSAGE_PRIMARY); |
| 427 | |
| 428 | SetVariable(pset.vars, "ERROR", "true"); |
| 429 | |
| 430 | /* |
| 431 | * If there is no SQLSTATE code, use an empty string. This can happen |
| 432 | * for libpq-detected errors (e.g., lost connection, ENOMEM). |
| 433 | */ |
| 434 | if (code == NULL) |
| 435 | code = ""; |
| 436 | SetVariable(pset.vars, "SQLSTATE", code); |
| 437 | SetVariable(pset.vars, "ROW_COUNT", "0"); |
| 438 | SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", code); |
| 439 | SetVariable(pset.vars, "LAST_ERROR_MESSAGE", mesg ? mesg : ""); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | |
| 444 | /* |
no test coverage detected