dbquery-pg.c (3454B)
1 /* 2 Copyright (C) 2012, 2013 Tomas Hlavaty <tom@logand.com> 3 4 Permission is hereby granted, free of charge, to any person 5 obtaining a copy of this software and associated documentation 6 files (the "Software"), to deal in the Software without 7 restriction, including without limitation the rights to use, copy, 8 modify, merge, publish, distribute, sublicense, and/or sell copies 9 of the Software, and to permit persons to whom the Software is 10 furnished to do so, subject to the following conditions: 11 12 The above copyright notice and this permission notice shall be 13 included in all copies or substantial portions of the Software. 14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 DEALINGS IN THE SOFTWARE. 23 */ 24 25 #include <stdio.h> 26 #include <libpq-fe.h> 27 28 #include "common.c" 29 30 int main(int argc, char *argv[]) { 31 if(argc != 2) die("usage: %s conninfo", argv[0]); 32 PGconn *conn = PQconnectdb(argv[1]); 33 if(!conn || CONNECTION_OK != PQstatus(conn)) die("error: connection failed"); 34 for(;;) { 35 if(eof()) return 0; 36 buf = heap; 37 int i, j, cmd = rnum(), nargs = rnum(); 38 PGresult *z; 39 { 40 Oid atype[nargs]; 41 char *aval[nargs]; 42 int alen[nargs], afmt[nargs]; 43 char *q, *stm; 44 switch(cmd) { 45 case QUERY: 46 q = rstr(); 47 for(i = 0; i < nargs; i++) { 48 atype[i] = rnum(); 49 aval[i] = rstr(); 50 alen[i] = 0; 51 afmt[i] = 0; 52 } 53 z = PQexecParams(conn, q, nargs, atype, (const char * const *) aval, alen, afmt, 0); 54 if(PGRES_TUPLES_OK != PQresultStatus(z)) goto fail; 55 goto rows; 56 case PREPARE: 57 stm = rstr(); 58 q = rstr(); 59 for(i = 0; i < nargs; i++) 60 atype[i] = rnum(); 61 z = PQprepare(conn, stm, q, nargs, atype); 62 if(PGRES_COMMAND_OK != PQresultStatus(z)) goto fail; 63 pnum(cmd); 64 goto done; 65 case EXECUTE: 66 stm = rstr(); 67 for(i = 0; i < nargs; i++) { 68 aval[i] = rstr(); 69 alen[i] = 0; 70 afmt[i] = 0; 71 } 72 z = PQexecPrepared(conn, stm, nargs, (const char * const *) aval, alen, afmt, 0); 73 if(PGRES_TUPLES_OK != PQresultStatus(z)) goto fail; 74 goto rows; 75 default: die("unexpected command %d", cmd); 76 } 77 } 78 rows: 79 pnum(cmd); 80 int n = PQntuples(z), m = PQnfields(z); 81 pnum(n); 82 pnum(m); 83 for(j = 0; j < m; j++) { 84 pnum(PQftype(z, j)); 85 pqstr(PQfname(z, j)); 86 } 87 for(i = 0; i < n; i++) { 88 for(j = 0; j < m; j++) { 89 if(PQgetisnull(z, i, j)) 90 pnil(); 91 else 92 pqstr(PQgetvalue(z, i, j)); 93 /* switch(PQftype(z, j)) { */ 94 /* case 1700: // float */ 95 /* case 23: // int */ 96 /* pstr(PQgetvalue(z, i, j)); */ 97 /* break; */ 98 /* default: */ 99 /* pqstr(PQgetvalue(z, i, j)); */ 100 /* } */ 101 } 102 } 103 goto done; 104 fail: 105 pnum(ERROR); 106 pqstr(PQresultErrorField(z, PG_DIAG_SQLSTATE)); 107 pqstr(PQerrorMessage(conn)); 108 done: 109 fflush(stdout); 110 PQclear(z); 111 } 112 }