Pg: Support type override configuration

This commit is contained in:
Yorhel 2025-02-27 18:24:14 +01:00
parent 3662931fc2
commit 327fd9ea50
5 changed files with 215 additions and 34 deletions

View file

@ -7,12 +7,16 @@ typedef void (*fupg_send_fn)(pTHX_ const fupg_tio *, SV *, fustr *);
/* Receive function, takes a binary string and should return a Perl value. */
typedef SV *(*fupg_recv_fn)(pTHX_ const fupg_tio *, const char *, int);
typedef struct {
char n[64];
} fupg_name;
/* Record/composite type definition */
typedef struct {
int nattrs;
struct {
Oid oid;
char name[64];
fupg_name name;
} attrs[];
} fupg_record;
@ -34,7 +38,7 @@ struct fupg_tio {
typedef struct {
Oid oid;
Oid elemoid; /* For arrays & domain types; relid for records */
char name[64];
fupg_name name;
fupg_send_fn send;
fupg_recv_fn recv;
} fupg_type;
@ -377,7 +381,7 @@ RECVFN(record) {
r = ctx->record.tio[i].recv(aTHX_ ctx->record.tio+i, buf, vlen);
buf += vlen; len -= vlen;
}
hv_store(hv, ctx->record.info->attrs[i].name, -strlen(ctx->record.info->attrs[i].name), r, 0);
hv_store(hv, ctx->record.info->attrs[i].name.n, -strlen(ctx->record.info->attrs[i].name.n), r, 0);
}
return SvREFCNT_inc(sv);
}
@ -393,7 +397,7 @@ SENDFN(record) {
I32 i;
for (i=0; i<ctx->record.info->nattrs; i++) {
fustr_writebeI(32, out, ctx->record.info->attrs[i].oid);
SV **rsv = hv_fetch(hv, ctx->record.info->attrs[i].name, -strlen(ctx->record.info->attrs[i].name), 0);
SV **rsv = hv_fetch(hv, ctx->record.info->attrs[i].name.n, -strlen(ctx->record.info->attrs[i].name.n), 0);
if (!rsv || !*rsv) {
fustr_writebeI(32, out, -1);
continue;
@ -711,8 +715,8 @@ SENDFN(date) {
B( 5069, "xid8", uint8 )
static const fupg_type fupg_builtin[] = {
#define B(oid, name, fun) { oid, 0, name"\0", fupg_send_##fun, fupg_recv_##fun },
#define A(oid, name, eoid) { oid, eoid, name"\0", fupg_send_array, fupg_recv_array },
#define B(oid, name, fun) { oid, 0, {name"\0"}, fupg_send_##fun, fupg_recv_##fun },
#define A(oid, name, eoid) { oid, eoid, {name"\0"}, fupg_send_array, fupg_recv_array },
BUILTINS
#undef B
#undef A
@ -737,3 +741,11 @@ static const fupg_type *fupg_type_byoid(const fupg_type *list, int len, Oid oid)
static const fupg_type *fupg_builtin_byoid(Oid oid) {
return fupg_type_byoid(fupg_builtin, FUPG_BUILTIN, oid);
}
static const fupg_type *fupg_builtin_byname(const char *name) {
size_t i;
for (i=0; i<FUPG_BUILTIN; i++)
if (strcmp(fupg_builtin[i].name.n, name) == 0)
return fupg_builtin+i;
return NULL;
}