* Use this to print query results * * result: result of a successful query * opt: formatting options * fout: where to print to * is_pager: true if caller has already redirected fout to be a pager pipe * flog: if not null, also print the data there (for --log-file option) */
| 3425 | * flog: if not null, also print the data there (for --log-file option) |
| 3426 | */ |
| 3427 | void |
| 3428 | printQuery(const PGresult *result, const printQueryOpt *opt, |
| 3429 | FILE *fout, bool is_pager, FILE *flog) |
| 3430 | { |
| 3431 | printTableContent cont; |
| 3432 | int i, |
| 3433 | r, |
| 3434 | c; |
| 3435 | |
| 3436 | if (cancel_pressed) |
| 3437 | return; |
| 3438 | |
| 3439 | printTableInit(&cont, &opt->topt, opt->title, |
| 3440 | PQnfields(result), PQntuples(result)); |
| 3441 | |
| 3442 | /* Assert caller supplied enough translate_columns[] entries */ |
| 3443 | Assert(opt->translate_columns == NULL || |
| 3444 | opt->n_translate_columns >= cont.ncolumns); |
| 3445 | |
| 3446 | for (i = 0; i < cont.ncolumns; i++) |
| 3447 | { |
| 3448 | printTableAddHeader(&cont, PQfname(result, i), |
| 3449 | opt->translate_header, |
| 3450 | column_type_alignment(PQftype(result, i))); |
| 3451 | } |
| 3452 | |
| 3453 | /* set cells */ |
| 3454 | for (r = 0; r < cont.nrows; r++) |
| 3455 | { |
| 3456 | for (c = 0; c < cont.ncolumns; c++) |
| 3457 | { |
| 3458 | char *cell; |
| 3459 | bool mustfree = false; |
| 3460 | bool translate; |
| 3461 | |
| 3462 | if (PQgetisnull(result, r, c)) |
| 3463 | cell = opt->nullPrint ? opt->nullPrint : ""; |
| 3464 | else |
| 3465 | { |
| 3466 | cell = PQgetvalue(result, r, c); |
| 3467 | if (cont.aligns[c] == 'r' && opt->topt.numericLocale) |
| 3468 | { |
| 3469 | cell = format_numeric_locale(cell); |
| 3470 | mustfree = true; |
| 3471 | } |
| 3472 | } |
| 3473 | |
| 3474 | translate = (opt->translate_columns && opt->translate_columns[c]); |
| 3475 | printTableAddCell(&cont, cell, translate, mustfree); |
| 3476 | } |
| 3477 | } |
| 3478 | |
| 3479 | /* set footers */ |
| 3480 | if (opt->footers) |
| 3481 | { |
| 3482 | char **footer; |
| 3483 | |
| 3484 | for (footer = opt->footers; *footer; footer++) |
no test coverage detected