/* ML type : pgresult_ -> int */ EXTERNML value pq_cmdtuples(value pgresval) { const char* s = PQcmdTuples(PGresult_val(pgresval)); if (s == NULL) failwith("pq_cmdtuples"); return Val_long(atoi(s)); }
/* ML type : pgresult_ -> int -> int -> bool */ EXTERNML value pq_getbool(value pgresval, value tupno, value fieldno) { char* v; checkbounds(pgresval, tupno, fieldno, "pq_getbool"); v = PQgetvalue(PGresult_val(pgresval), Long_val(tupno), Long_val(fieldno)); if (v == NULL) failwith("pq_getbool"); return Val_bool(!strcmp(v, "t")); }
/* ML type : pgresult_ -> int -> int -> string */ EXTERNML value pq_getstring(value pgresval, value tupno, value fieldno) { char* v; checkbounds(pgresval, tupno, fieldno, "pq_getstring"); v = PQgetvalue(PGresult_val(pgresval), Long_val(tupno), Long_val(fieldno)); if (v == NULL) failwith("pq_getstring"); return copy_string(v); }
/* ML type : pgresult_ -> int -> int -> int */ EXTERNML value pq_getint(value pgresval, value tupno, value fieldno) { char* v; checkbounds(pgresval, tupno, fieldno, "pq_getint"); v = PQgetvalue(PGresult_val(pgresval), Long_val(tupno), Long_val(fieldno)); if (v == NULL) failwith("pq_getint"); return Val_long(atoi(v)); }
/* ML type : pgresult_ -> int -> int -> real */ value pq_getreal(value pgresval, value tupno, value fieldno) { char* v; checkbounds(pgresval, tupno, fieldno, "pq_getreal"); v = PQgetvalue(PGresult_val(pgresval), Long_val(tupno), Long_val(fieldno)); if (v == NULL) failwith("pq_getreal"); return copy_double(atof(v)); }
void checkbounds(value pgresval, value tupno, value fieldno, char* fcn) { PGresult* pgres = PGresult_val(pgresval); int t = Long_val(tupno); int f = Long_val(fieldno); checkfbound(pgres, f, fcn); if (t < 0 || t >= PQntuples(pgres)) { char buf[128]; sprintf(buf, "Postgres.%s: illegal tuple number %d; must be in [0..%d]", fcn, t, PQntuples(pgres)-1); failwith(buf); } }
/* ML type : pgresult_ -> pgresultstatus */ EXTERNML value pq_resultstatus(value pgresval) { switch (PQresultStatus(PGresult_val(pgresval))) { case PGRES_EMPTY_QUERY: return Atom(Empty_query); case PGRES_COMMAND_OK: return Atom(Command_ok); case PGRES_TUPLES_OK: return Atom(Tuples_ok); case PGRES_COPY_OUT: return Atom(Copy_out); case PGRES_COPY_IN: return Atom(Copy_in); case PGRES_BAD_RESPONSE: return Atom(Bad_response); case PGRES_NONFATAL_ERROR: return Atom(Nonfatal_error); case PGRES_FATAL_ERROR: return Atom(Tuples_ok); default: failwith("mpq:pg_resultstatus: internal error"); } }
void pgresult_finalize(value pgresval) { PGresult* pgres = PGresult_val(pgresval); PQclear(pgres); }
/* ML type : pgresult_ -> int -> int -> bool */ EXTERNML value pq_getisnull(value pgresval, value tupno, value fieldno) { checkbounds(pgresval, tupno, fieldno, "pq_getisnull"); return Val_bool(PQgetisnull(PGresult_val(pgresval), Long_val(tupno), Long_val(fieldno))); }
/* ML type : pgresult_ -> int -> int */ EXTERNML value pq_fsize(value pgresval, value fieldno) { checkfbound(PGresult_val(pgresval), Long_val(fieldno), "pq_ftype"); return Val_long(PQfsize(PGresult_val(pgresval), Long_val(fieldno))); }
/* ML type : pgresult_ -> string -> int */ EXTERNML value pq_fnumber(value pgresval, value fieldname) { return Val_long(PQfnumber(PGresult_val(pgresval), String_val(fieldname))); }
/* ML type : pgresult_ -> int -> string */ EXTERNML value pq_fname(value pgresval, value fieldno) { checkfbound(PGresult_val(pgresval), Long_val(fieldno), "pq_ftype"); return copy_string(PQfname(PGresult_val(pgresval), Long_val(fieldno))); }
/* ML type : pgresult_ -> int */ EXTERNML value pq_nfields(value pgresval) { return Val_long(PQnfields(PGresult_val(pgresval))); }
/* ML type : pgresult_ -> int */ EXTERNML value pq_ntuples(value pgresval) { return Val_long(PQntuples(PGresult_val(pgresval))); }