pg: Better error reporting + basic exec() method
This commit is contained in:
parent
b242176071
commit
c51b5f3598
5 changed files with 173 additions and 28 deletions
64
c/libpq.h
Normal file
64
c/libpq.h
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/* libpq is not being linked directly and there's no build-time dependency on it.
|
||||
* This means we need to manually copy over some definitions from libpq-fe.h
|
||||
*/
|
||||
|
||||
typedef struct PGconn PGconn;
|
||||
typedef struct PGresult PGresult;
|
||||
|
||||
typedef enum {
|
||||
PGRES_EMPTY_QUERY = 0, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PGRES_COPY_OUT, PGRES_COPY_IN,
|
||||
PGRES_BAD_RESPONSE, PGRES_NONFATAL_ERROR, PGRES_FATAL_ERROR, PGRES_COPY_BOTH,
|
||||
PGRES_SINGLE_TUPLE, PGRES_PIPELINE_SYNC, PGRES_PIPELINE_ABORTED, PGRES_TUPLES_CHUNK
|
||||
} ExecStatusType;
|
||||
typedef enum { PQERRORS_TERSE, PQERRORS_DEFAULT, PQERRORS_VERBOSE, PQERRORS_SQLSTATE } PGVerbosity;
|
||||
typedef enum { PQSHOW_CONTEXT_NEVER, PQSHOW_CONTEXT_ERRORS, PQSHOW_CONTEXT_ALWAYS } PGContextVisibility;
|
||||
|
||||
#define PG_DIAG_SEVERITY 'S'
|
||||
#define PG_DIAG_SEVERITY_NONLOCALIZED 'V'
|
||||
#define PG_DIAG_SQLSTATE 'C'
|
||||
#define PG_DIAG_MESSAGE_PRIMARY 'M'
|
||||
#define PG_DIAG_MESSAGE_DETAIL 'D'
|
||||
#define PG_DIAG_MESSAGE_HINT 'H'
|
||||
#define PG_DIAG_STATEMENT_POSITION 'P'
|
||||
#define PG_DIAG_INTERNAL_POSITION 'p'
|
||||
#define PG_DIAG_INTERNAL_QUERY 'q'
|
||||
#define PG_DIAG_CONTEXT 'W'
|
||||
#define PG_DIAG_SCHEMA_NAME 's'
|
||||
#define PG_DIAG_TABLE_NAME 't'
|
||||
#define PG_DIAG_COLUMN_NAME 'c'
|
||||
#define PG_DIAG_DATATYPE_NAME 'd'
|
||||
#define PG_DIAG_CONSTRAINT_NAME 'n'
|
||||
#define PG_DIAG_SOURCE_FILE 'F'
|
||||
#define PG_DIAG_SOURCE_LINE 'L'
|
||||
#define PG_DIAG_SOURCE_FUNCTION 'R'
|
||||
|
||||
#define PG_FUNCS \
|
||||
X(PQclear, void, PGresult *) \
|
||||
X(PQconnectdb, PGconn *, const char *) \
|
||||
X(PQcmdTuples, char *, PGresult *) \
|
||||
X(PQerrorMessage, char *, const PGconn *) \
|
||||
X(PQexec, PGresult *, PGconn *, const char *) \
|
||||
X(PQfinish, void, PGconn *) \
|
||||
X(PQfreemem, void, void *) \
|
||||
X(PQlibVersion, int, void) \
|
||||
X(PQresStatus, char *, ExecStatusType) \
|
||||
X(PQresultErrorField, char *, const PGresult *, int) \
|
||||
X(PQresultErrorMessage, char *, const PGresult *res) \
|
||||
X(PQresultStatus, ExecStatusType, const PGresult *) \
|
||||
X(PQresultVerboseErrorMessage, char *, const PGresult *, PGVerbosity, PGContextVisibility) \
|
||||
X(PQserverVersion, int, const PGconn *) \
|
||||
X(PQstatus, int, const PGconn *)
|
||||
|
||||
#define X(n, r, ...) static r (*n)(__VA_ARGS__);
|
||||
PG_FUNCS
|
||||
#undef X
|
||||
|
||||
static void fupg_load() {
|
||||
void *handle = dlopen("libpq.so", RTLD_LAZY);
|
||||
if (!handle) croak("Unable to load libpq: %s", dlerror());
|
||||
#define X(n, ...) if (!(n = dlsym(handle, #n))) croak("Unable to load libpq: %s", dlerror());
|
||||
PG_FUNCS
|
||||
#undef X
|
||||
}
|
||||
|
||||
#undef PG_FUNCS
|
||||
Loading…
Add table
Add a link
Reference in a new issue