pg: Statement execution + better error reporting
This commit is contained in:
parent
922f19e39b
commit
711300b227
6 changed files with 124 additions and 26 deletions
99
c/pgconn.c
99
c/pgconn.c
|
|
@ -11,7 +11,7 @@ static SV *fupg_conn_errsv(PGconn *conn, const char *action) {
|
|||
hv_stores(hv, "action", newSVpv(action, 0));
|
||||
hv_stores(hv, "severity", newSVpvs("FATAL")); /* Connection-related errors are always fatal */
|
||||
hv_stores(hv, "message", newSVpv(PQerrorMessage(conn), 0));
|
||||
return sv_bless(sv_2mortal(newRV_noinc((SV *)hv)), gv_stashpvs("FU::PG::error", GV_ADD));
|
||||
return fu_croak_hv(hv, "FU::PG::error", "FATAL: %s", PQerrorMessage(conn));
|
||||
}
|
||||
|
||||
static void fupg_conn_croak(fupg_conn *c, const char *action) {
|
||||
|
|
@ -23,11 +23,10 @@ static void fupg_conn_croak(fupg_conn *c, const char *action) {
|
|||
static void fupg_result_croak(PGresult *r, const char *action, const char *query) {
|
||||
dTHX;
|
||||
HV *hv = newHV();
|
||||
char *s;
|
||||
hv_stores(hv, "action", newSVpv(action, 0));
|
||||
s = PQresultErrorField(r, PG_DIAG_SEVERITY_NONLOCALIZED);
|
||||
char *s = PQresultErrorField(r, PG_DIAG_SEVERITY_NONLOCALIZED);
|
||||
hv_stores(hv, "severity", newSVpv(s ? s : "FATAL", 0));
|
||||
if (query) hv_stores(hv, "query", newSVpv(s, 0));
|
||||
if (query) hv_stores(hv, "query", newSVpv(query, 0));
|
||||
|
||||
/* If the PGresult is not an error, assume it's an unexpected resultStatus */
|
||||
s = PQresultErrorField(r, PG_DIAG_MESSAGE_PRIMARY);
|
||||
|
|
@ -36,11 +35,12 @@ static void fupg_result_croak(PGresult *r, const char *action, const char *query
|
|||
/* I like the verbose error messages. Doesn't include anything that's not
|
||||
* also fetched below, but saves me from having to do the formatting
|
||||
* manually. */
|
||||
char *verbose = NULL;
|
||||
if (s) {
|
||||
s = PQresultVerboseErrorMessage(r, 2 /* PQERRORS_VERBOSE */, 1 /* PQSHOW_CONTEXT_ERRORS */);
|
||||
verbose = PQresultVerboseErrorMessage(r, PQERRORS_VERBOSE, PQSHOW_CONTEXT_ERRORS);
|
||||
if (s) {
|
||||
hv_stores(hv, "verbose_message", newSVpv(s, 0));
|
||||
PQfreemem(s);
|
||||
hv_stores(hv, "verbose_message", newSVpv(verbose, 0));
|
||||
PQfreemem(verbose);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -60,7 +60,13 @@ static void fupg_result_croak(PGresult *r, const char *action, const char *query
|
|||
if ((s = PQresultErrorField(r, PG_DIAG_SOURCE_FUNCTION))) hv_stores(hv, "source_function", newSVpv(s, 0));
|
||||
|
||||
PQclear(r);
|
||||
croak_sv(sv_bless(sv_2mortal(newRV_noinc((SV *)hv)), gv_stashpvs("FU::PG::error", GV_ADD)));
|
||||
croak_sv(verbose
|
||||
? fu_croak_hv(hv, "FU::PG::error", "%s", SvPV_nolen(*hv_fetchs(hv, "verbose_message", 0)))
|
||||
: fu_croak_hv(hv, "FU::PG::error", "%s: %s",
|
||||
SvPV_nolen(*hv_fetchs(hv, "severity", 0)),
|
||||
SvPV_nolen(*hv_fetchs(hv, "message", 0))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
static SV *fupg_connect(pTHX_ const char *str) {
|
||||
|
|
@ -81,6 +87,17 @@ static void fupg_destroy(fupg_conn *c) {
|
|||
safefree(c);
|
||||
}
|
||||
|
||||
static SV *fupg_exec_result(pTHX_ PGresult *r) {
|
||||
SV *ret = &PL_sv_undef;
|
||||
char *tup = PQcmdTuples(r);
|
||||
if (tup && *tup) {
|
||||
ret = sv_2mortal(newSVpv(tup, 0));
|
||||
SvIV(ret);
|
||||
SvIOK_only(ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static SV *fupg_exec(pTHX_ fupg_conn *c, const char *sql) {
|
||||
PGresult *r = PQexec(c->conn, sql);
|
||||
if (!r) fupg_conn_croak(c, "exec");
|
||||
|
|
@ -90,13 +107,7 @@ static SV *fupg_exec(pTHX_ fupg_conn *c, const char *sql) {
|
|||
case PGRES_TUPLES_OK: break;
|
||||
default: fupg_result_croak(r, "exec", sql);
|
||||
}
|
||||
SV *ret = &PL_sv_undef;
|
||||
char *tup = PQcmdTuples(r);
|
||||
if (tup && *tup) {
|
||||
ret = sv_2mortal(newSVpv(tup, 0));
|
||||
SvIV(ret);
|
||||
SvIOK_only(ret);
|
||||
}
|
||||
SV *ret = fupg_exec_result(r);
|
||||
PQclear(r);
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -112,6 +123,10 @@ typedef struct {
|
|||
char name[32];
|
||||
int prepared;
|
||||
PGresult *describe;
|
||||
/* Set during execute */
|
||||
int paramn;
|
||||
char **param;
|
||||
PGresult *result;
|
||||
} fupg_st;
|
||||
|
||||
static SV *fupg_q(pTHX_ fupg_conn *c, const char *query, I32 ax, I32 argc) {
|
||||
|
|
@ -122,8 +137,8 @@ static SV *fupg_q(pTHX_ fupg_conn *c, const char *query, I32 ax, I32 argc) {
|
|||
st->query = savepv(query);
|
||||
if (argc > 2) {
|
||||
st->bind = safemalloc((argc-2) * sizeof(SV *));
|
||||
I32 i = 2;
|
||||
while (i < argc) {
|
||||
I32 i;
|
||||
for (i=2; i < argc; i++) {
|
||||
st->bind[st->bindn] = newSV(0);
|
||||
sv_setsv(st->bind[st->bindn], ST(i));
|
||||
st->bindn++;
|
||||
|
|
@ -157,7 +172,7 @@ static void fupg_st_prepare(pTHX_ fupg_st *st) {
|
|||
}
|
||||
|
||||
static SV *fupg_st_params(pTHX_ fupg_st *st) {
|
||||
fupg_st_prepare(st);
|
||||
fupg_st_prepare(aTHX_ st);
|
||||
int i, nparams = PQnparams(st->describe);
|
||||
AV *av = newAV_alloc_x(nparams);
|
||||
for (i=0; i<nparams; i++) {
|
||||
|
|
@ -169,7 +184,7 @@ static SV *fupg_st_params(pTHX_ fupg_st *st) {
|
|||
}
|
||||
|
||||
static SV *fupg_st_columns(pTHX_ fupg_st *st) {
|
||||
fupg_st_prepare(st);
|
||||
fupg_st_prepare(aTHX_ st);
|
||||
int i, ncols = PQnfields(st->describe);
|
||||
AV *av = newAV_alloc_x(ncols);
|
||||
for (i=0; i<ncols; i++) {
|
||||
|
|
@ -184,6 +199,47 @@ static SV *fupg_st_columns(pTHX_ fupg_st *st) {
|
|||
return sv_2mortal(newRV_noinc((SV *)av));
|
||||
}
|
||||
|
||||
static void fupg_st_execute(pTHX_ fupg_st *st) {
|
||||
/* Disallow fetching the results more than once. I don't see a reason why
|
||||
* someone would need that and disallowing it leaves room for fetching the
|
||||
* results in a streaming fashion without breaking API compat. */
|
||||
if (st->result) croak("Invalid attempt to execute statement multiple times");
|
||||
|
||||
/* TODO: prepare can be skipped when prepared statement caching is disabled and (text-format queries or no bind params) */
|
||||
fupg_st_prepare(aTHX_ st);
|
||||
st->param = safemalloc(st->bindn * sizeof(char *));
|
||||
int i;
|
||||
for (i=0; i<st->bindn; i++) {
|
||||
st->param[i] = SvPVutf8_nolen(st->bind[i]);
|
||||
st->paramn++;
|
||||
}
|
||||
|
||||
/* I'm not super fond of this approach. Storing the full query results in a
|
||||
* PGresult involves unnecessary parsing, memory allocation and copying.
|
||||
* The wire protocol is sufficiently simple that I could parse the query
|
||||
* results directly from the network buffers without much additional code,
|
||||
* and that would be much more efficient. Alas, libpq doesn't let me do
|
||||
* that.
|
||||
* There is the option of fetching results in chunked mode, but from what I
|
||||
* gather that just saves a bit of memory in exchange for more and smaller
|
||||
* malloc()/free()'s. Performance-wise, it probably won't be much of an
|
||||
* improvement */
|
||||
PGresult *r = PQexecPrepared(st->conn->conn, st->name, st->paramn, (const char * const*)st->param, NULL, NULL, 0);
|
||||
if (!r) fupg_conn_croak(st->conn , "exec");
|
||||
switch (PQresultStatus(r)) {
|
||||
case PGRES_EMPTY_QUERY:
|
||||
case PGRES_COMMAND_OK:
|
||||
case PGRES_TUPLES_OK: break;
|
||||
default: fupg_result_croak(r, "exec", st->query);
|
||||
}
|
||||
st->result = r;
|
||||
}
|
||||
|
||||
static SV *fupg_st_exec(pTHX_ fupg_st *st) {
|
||||
fupg_st_execute(aTHX_ st);
|
||||
return fupg_exec_result(st->result);
|
||||
}
|
||||
|
||||
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 */
|
||||
|
|
@ -192,7 +248,10 @@ static void fupg_st_destroy(fupg_st *st) {
|
|||
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);
|
||||
/* XXX: These point into bind SVs (for now): for (i=0; i < st->paramn; i++) safefree(st->param[i]); */
|
||||
if (st->param) safefree(st->param);
|
||||
PQclear(st->describe);
|
||||
PQclear(st->result);
|
||||
SvREFCNT_dec(st->conn->self);
|
||||
safefree(st);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue