From 2083ab2a6f3793ce560996dc2c8120c74c61a78b Mon Sep 17 00:00:00 2001 From: Yorhel Date: Thu, 22 May 2025 09:53:41 +0200 Subject: [PATCH] Pg: Set appropriate quote_identifier for $conn->Q() --- FU.xs | 12 ++++++++++++ FU/Pg.pm | 9 +++++++-- t/pgtypes-dynamic.t | 8 ++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/FU.xs b/FU.xs index 1c342be..7a387f9 100644 --- a/FU.xs +++ b/FU.xs @@ -217,6 +217,12 @@ void query_trace(fupg_conn *c, SV *cb) SvGETMAGIC(cb); 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) CODE: ST(0) = sv_2mortal(newSVpv(fupg_conn_status(c), 0)); @@ -317,6 +323,12 @@ void cache(fupg_txn *x, ...) CODE: 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) CODE: ST(0) = sv_2mortal(newSVpv(fupg_txn_status(t), 0)); diff --git a/FU/Pg.pm b/FU/Pg.pm index 4732daf..2e7baf8 100644 --- a/FU/Pg.pm +++ b/FU/Pg.pm @@ -10,7 +10,11 @@ package FU::Pg::conn { sub Q { require FU::SQL; 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); } @@ -208,7 +212,8 @@ used. =item $conn->Q(@args) Same as C<< $conn->q() >> but uses L to construct the query and bind -parameters. +parameters. Uses the 'pg' C and C<< $conn->escape_identifier() >> for +identifier quoting. =back diff --git a/t/pgtypes-dynamic.t b/t/pgtypes-dynamic.t index 2751a86..79abd92 100644 --- a/t/pgtypes-dynamic.t +++ b/t/pgtypes-dynamic.t @@ -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 { plan skip_all => 'type not loaded in the database' if !$conn->q("SELECT 1 FROM pg_type WHERE typname = 'vndbtag'")->val;