* SendQuery: send the query string to the backend * (and print out results) * * Note: This is the "front door" way to send a query. That is, use it to * send queries actually entered by the user. These queries will be subject to * single step mode. * To send "back door" queries (generated by slash commands, etc.) in a * controlled way, use PSQLexec(). * * Returns true if the query execute
| 1187 | * Returns true if the query executed successfully, false otherwise. |
| 1188 | */ |
| 1189 | bool |
| 1190 | SendQuery(const char *query) |
| 1191 | { |
| 1192 | PGresult *results; |
| 1193 | PGTransactionStatusType transaction_status; |
| 1194 | double elapsed_msec = 0; |
| 1195 | bool OK = false; |
| 1196 | int i; |
| 1197 | bool on_error_rollback_savepoint = false; |
| 1198 | static bool on_error_rollback_warning = false; |
| 1199 | |
| 1200 | if (!pset.db) |
| 1201 | { |
| 1202 | pg_log_error("You are currently not connected to a database."); |
| 1203 | goto sendquery_cleanup; |
| 1204 | } |
| 1205 | |
| 1206 | if (pset.singlestep) |
| 1207 | { |
| 1208 | char buf[3]; |
| 1209 | |
| 1210 | fflush(stderr); |
| 1211 | printf(_("***(Single step mode: verify command)*******************************************\n" |
| 1212 | "%s\n" |
| 1213 | "***(press return to proceed or enter x and return to cancel)********************\n"), |
| 1214 | query); |
| 1215 | fflush(stdout); |
| 1216 | if (fgets(buf, sizeof(buf), stdin) != NULL) |
| 1217 | if (buf[0] == 'x') |
| 1218 | goto sendquery_cleanup; |
| 1219 | if (cancel_pressed) |
| 1220 | goto sendquery_cleanup; |
| 1221 | } |
| 1222 | else if (pset.echo == PSQL_ECHO_QUERIES) |
| 1223 | { |
| 1224 | puts(query); |
| 1225 | fflush(stdout); |
| 1226 | } |
| 1227 | |
| 1228 | if (pset.logfile) |
| 1229 | { |
| 1230 | fprintf(pset.logfile, |
| 1231 | _("********* QUERY **********\n" |
| 1232 | "%s\n" |
| 1233 | "**************************\n\n"), query); |
| 1234 | fflush(pset.logfile); |
| 1235 | } |
| 1236 | |
| 1237 | SetCancelConn(pset.db); |
| 1238 | |
| 1239 | transaction_status = PQtransactionStatus(pset.db); |
| 1240 | |
| 1241 | if (transaction_status == PQTRANS_IDLE && |
| 1242 | !pset.autocommit && |
| 1243 | !command_no_begin(query)) |
| 1244 | { |
| 1245 | results = PQexec(pset.db, "BEGIN"); |
| 1246 | if (PQresultStatus(results) != PGRES_COMMAND_OK) |
no test coverage detected