* This function prints a query result that is a binary-format fetch from * a table defined as in the comment above. We split it out because the * main() function uses it twice. */
| 57 | * main() function uses it twice. |
| 58 | */ |
| 59 | static void |
| 60 | show_binary_results(PGresult *res) |
| 61 | { |
| 62 | int i, |
| 63 | j; |
| 64 | int i_fnum, |
| 65 | t_fnum, |
| 66 | b_fnum; |
| 67 | |
| 68 | /* Use PQfnumber to avoid assumptions about field order in result */ |
| 69 | i_fnum = PQfnumber(res, "i"); |
| 70 | t_fnum = PQfnumber(res, "t"); |
| 71 | b_fnum = PQfnumber(res, "b"); |
| 72 | |
| 73 | for (i = 0; i < PQntuples(res); i++) |
| 74 | { |
| 75 | char *iptr; |
| 76 | char *tptr; |
| 77 | char *bptr; |
| 78 | int blen; |
| 79 | int ival; |
| 80 | |
| 81 | /* Get the field values (we ignore possibility they are null!) */ |
| 82 | iptr = PQgetvalue(res, i, i_fnum); |
| 83 | tptr = PQgetvalue(res, i, t_fnum); |
| 84 | bptr = PQgetvalue(res, i, b_fnum); |
| 85 | |
| 86 | /* |
| 87 | * The binary representation of INT4 is in network byte order, which |
| 88 | * we'd better coerce to the local byte order. |
| 89 | */ |
| 90 | ival = ntohl(*((uint32_t *) iptr)); |
| 91 | |
| 92 | /* |
| 93 | * The binary representation of TEXT is, well, text, and since libpq |
| 94 | * was nice enough to append a zero byte to it, it'll work just fine |
| 95 | * as a C string. |
| 96 | * |
| 97 | * The binary representation of BYTEA is a bunch of bytes, which could |
| 98 | * include embedded nulls so we have to pay attention to field length. |
| 99 | */ |
| 100 | blen = PQgetlength(res, i, b_fnum); |
| 101 | |
| 102 | printf("tuple %d: got\n", i); |
| 103 | printf(" i = (%d bytes) %d\n", |
| 104 | PQgetlength(res, i, i_fnum), ival); |
| 105 | printf(" t = (%d bytes) '%s'\n", |
| 106 | PQgetlength(res, i, t_fnum), tptr); |
| 107 | printf(" b = (%d bytes) ", blen); |
| 108 | for (j = 0; j < blen; j++) |
| 109 | printf("\\%03o", bptr[j]); |
| 110 | printf("\n\n"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | int |
| 115 | main(int argc, char **argv) |
no test coverage detected