pg: Statement preparing + inspection; less wonky object handling?

This commit is contained in:
Yorhel 2025-02-05 11:49:22 +01:00
parent c51b5f3598
commit 187417f160
5 changed files with 186 additions and 10 deletions

View file

@ -1,5 +1,7 @@
typedef struct {
SV *self;
PGconn *conn;
UV prep_counter;
} fupg_conn;
@ -61,7 +63,7 @@ static void fupg_result_croak(PGresult *r, const char *action, const char *query
croak_sv(sv_bless(sv_2mortal(newRV_noinc((SV *)hv)), gv_stashpvs("FU::PG::error", GV_ADD)));
}
static fupg_conn *fupg_connect(pTHX_ const char *str) {
static SV *fupg_connect(pTHX_ const char *str) {
PGconn *conn = PQconnectdb(str);
if (PQstatus(conn) != 0) {
SV *sv = fupg_conn_errsv(conn, "connect");
@ -71,7 +73,7 @@ static fupg_conn *fupg_connect(pTHX_ const char *str) {
fupg_conn *c = safecalloc(1, sizeof(fupg_conn));
c->conn = conn;
return c;
return fupg_selfobj(c, "FU::PG::conn");
}
static void fupg_destroy(fupg_conn *c) {
@ -79,7 +81,7 @@ static void fupg_destroy(fupg_conn *c) {
safefree(c);
}
static SV *fupg_exec(fupg_conn *c, const char *sql) {
static SV *fupg_exec(pTHX_ fupg_conn *c, const char *sql) {
PGresult *r = PQexec(c->conn, sql);
if (!r) fupg_conn_croak(c, "exec");
switch (PQresultStatus(r)) {
@ -98,3 +100,99 @@ static SV *fupg_exec(fupg_conn *c, const char *sql) {
PQclear(r);
return ret;
}
typedef struct {
/* Set in $conn->q() */
SV *self;
fupg_conn *conn; /* has a refcnt on conn->self */
char *query;
SV **bind;
int bindn;
/* Set during prepare */
char name[32];
int prepared;
PGresult *describe;
} fupg_st;
static SV *fupg_q(pTHX_ fupg_conn *c, const char *query, I32 ax, I32 argc) {
fupg_st *st = safecalloc(1, sizeof(fupg_st));
st->conn = c;
SvREFCNT_inc(c->self);
st->query = savepv(query);
if (argc > 2) {
st->bind = safemalloc((argc-2) * sizeof(SV *));
I32 i = 2;
while (i < argc) {
st->bind[st->bindn] = newSV(0);
sv_setsv(st->bind[st->bindn], ST(i));
st->bindn++;
}
}
return fupg_selfobj(st, "FU::PG::st");
}
static void fupg_st_prepare(pTHX_ fupg_st *st) {
if (st->describe) return;
if (st->prepared) croak("invalid attempt to re-prepare invalid statement");
/* TODO: This is where we check for any cached prepared statements */
snprintf(st->name, sizeof(st->name), "fupg%"UVuf, ++st->conn->prep_counter);
/* TODO: Pipeline these two commands, no need for two round-trips with the server */
PGresult *r = PQprepare(st->conn->conn, st->name, st->query, 0, NULL);
if (!r) fupg_conn_croak(st->conn , "prepare");
if (PQresultStatus(r) != PGRES_COMMAND_OK)
fupg_result_croak(r, "prepare", st->query);
PQclear(r);
st->prepared = 1;
r = PQdescribePrepared(st->conn->conn, st->name);
if (!r) fupg_conn_croak(st->conn , "prepare");
if (PQresultStatus(r) != PGRES_COMMAND_OK)
fupg_result_croak(r, "prepare", st->query);
st->describe = r;
}
static SV *fupg_st_params(pTHX_ fupg_st *st) {
fupg_st_prepare(st);
int i, nparams = PQnparams(st->describe);
AV *av = newAV_alloc_x(nparams);
for (i=0; i<nparams; i++) {
HV *hv = newHV();
hv_stores(hv, "oid", newSViv(PQparamtype(st->describe, i)));
av_push_simple(av, newRV_noinc((SV *)hv));
}
return sv_2mortal(newRV_noinc((SV *)av));
}
static SV *fupg_st_columns(pTHX_ fupg_st *st) {
fupg_st_prepare(st);
int i, ncols = PQnfields(st->describe);
AV *av = newAV_alloc_x(ncols);
for (i=0; i<ncols; i++) {
HV *hv = newHV();
const char *name = PQfname(st->describe, i);
hv_stores(hv, "name", newSVpvn_utf8(name, strlen(name), 1));
hv_stores(hv, "oid", newSViv(PQftype(st->describe, i)));
int tmod = PQfmod(st->describe, i);
if (tmod >= 0) hv_stores(hv, "typemod", newSViv(tmod));
av_push_simple(av, newRV_noinc((SV *)hv));
}
return sv_2mortal(newRV_noinc((SV *)av));
}
static void fupg_st_destroy(fupg_st *st) {
int i;
/* Ignore failure, this is just a best-effort attempt to free up resources on the backend */
if (st->prepared) PQclear(PQclosePrepared(st->conn->conn, st->name));
if (st->query) safefree(st->query);
for (i=0; i < st->bindn; i++) SvREFCNT_dec(st->bind[i]);
if (st->bind) safefree(st->bind);
if (st->describe) PQclear(st->describe);
SvREFCNT_dec(st->conn->self);
safefree(st);
}