Pg: Set appropriate quote_identifier for $conn->Q()

This commit is contained in:
Yorhel 2025-05-22 09:53:41 +02:00
parent 81a3d3c608
commit 2083ab2a6f
3 changed files with 27 additions and 2 deletions

12
FU.xs
View file

@ -217,6 +217,12 @@ void query_trace(fupg_conn *c, SV *cb)
SvGETMAGIC(cb); SvGETMAGIC(cb);
c->trace = SvOK(cb) ? SvREFCNT_inc(cb) : NULL; c->trace = SvOK(cb) ? SvREFCNT_inc(cb) : NULL;
void conn(fupg_conn *c)
CODE:
ST(0) = sv_newmortal();
sv_setrv_inc(ST(0), c->self);
sv_bless(ST(0), gv_stashpv("FU::Pg::conn", 0));
void status(fupg_conn *c) void status(fupg_conn *c)
CODE: CODE:
ST(0) = sv_2mortal(newSVpv(fupg_conn_status(c), 0)); ST(0) = sv_2mortal(newSVpv(fupg_conn_status(c), 0));
@ -317,6 +323,12 @@ void cache(fupg_txn *x, ...)
CODE: CODE:
FUPG_STFLAGS; FUPG_STFLAGS;
void conn(fupg_txn *t)
CODE:
ST(0) = sv_newmortal();
sv_setrv_inc(ST(0), t->conn->self);
sv_bless(ST(0), gv_stashpv("FU::Pg::conn", 0));
void status(fupg_txn *t) void status(fupg_txn *t)
CODE: CODE:
ST(0) = sv_2mortal(newSVpv(fupg_txn_status(t), 0)); ST(0) = sv_2mortal(newSVpv(fupg_txn_status(t), 0));

View file

@ -10,7 +10,11 @@ package FU::Pg::conn {
sub Q { sub Q {
require FU::SQL; require FU::SQL;
my $s = shift; my $s = shift;
my($sql, $params) = FU::SQL::SQL(@_)->compile(placeholder_style => 'pg', in_style => 'pg'); my($sql, $params) = FU::SQL::SQL(@_)->compile(
placeholder_style => 'pg',
in_style => 'pg',
quote_identifier => sub { $s->conn->escape_identifier(@_) },
);
$s->q($sql, @$params); $s->q($sql, @$params);
} }
@ -208,7 +212,8 @@ used.
=item $conn->Q(@args) =item $conn->Q(@args)
Same as C<< $conn->q() >> but uses L<FU::SQL> to construct the query and bind Same as C<< $conn->q() >> but uses L<FU::SQL> to construct the query and bind
parameters. parameters. Uses the 'pg' C<in_style> and C<< $conn->escape_identifier() >> for
identifier quoting.
=back =back

View file

@ -127,6 +127,14 @@ subtest 'custom types', sub {
}; };
subtest 'identifier quoting', sub {
my $txn = $conn->txn;
$txn->exec('CREATE TEMPORARY TABLE fupg_test_tbl ("desc" int, ok int, "hello world" int)');
ok $txn->Q('INSERT INTO fupg_test_tbl', VALUES {desc => 5, ok => 10, 'hello world', 15})->exec;
is $txn->Q('SELECT', IDENT 'hello world', 'FROM fupg_test_tbl')->val, 15;
};
subtest 'vndbid', sub { subtest 'vndbid', sub {
plan skip_all => 'type not loaded in the database' if !$conn->q("SELECT 1 FROM pg_type WHERE typname = 'vndbtag'")->val; plan skip_all => 'type not loaded in the database' if !$conn->q("SELECT 1 FROM pg_type WHERE typname = 'vndbtag'")->val;