FU::Pg: Rename q() and Q() to sql() and SQL()

Because this easily confuses syntax highlighters and some humans with
the q// string syntax. Also for consistency with the 'fu->sql()'
aliases.

The old names are still available as alias.
This commit is contained in:
Yorhel 2025-12-04 14:05:51 +01:00
parent 876613d03f
commit 8140fefbca
10 changed files with 206 additions and 200 deletions

View file

@ -37,7 +37,7 @@ subtest '$conn->exec', sub {
ok !defined $conn->exec('');
is $conn->exec('SELECT 1'), 1;
ok !eval { $conn->q('SELEXT')->param_types; };
ok !eval { $conn->sql('SELEXT')->param_types; };
okerr ERROR => prepare => qr/syntax error/;
is $conn->exec('SET client_encoding=utf8'), undef;
@ -46,7 +46,7 @@ subtest '$conn->exec', sub {
subtest '$st prepare & exec', sub {
{
my $st = $conn->q('SELECT 1');
my $st = $conn->sql('SELECT 1');
is_deeply $st->param_types, [];
is_deeply $st->columns, [{ name => '?column?', oid => 23 }];
@ -63,7 +63,7 @@ subtest '$st prepare & exec', sub {
}
{
my $st = $conn->q("SELECT \$1::int AS a, \$2::char(5) AS \"\x{1F603}\"", 1, 2);
my $st = $conn->sql("SELECT \$1::int AS a, \$2::char(5) AS \"\x{1F603}\"", 1, 2);
is_deeply $st->param_types, [ 23, 1042 ];
is_deeply $st->columns, [
{ oid => 23, name => 'a' },
@ -74,28 +74,28 @@ subtest '$st prepare & exec', sub {
is $conn->exec('SELECT 1 FROM pg_prepared_statements'), 0;
ok !eval { $conn->q('SELECT 1', 1)->exec; 1 };
ok !eval { $conn->sql('SELECT 1', 1)->exec; 1 };
like $@, qr/bind message supplies 1 parameters, but prepared statement/;
ok !eval { $conn->q('SELECT $1')->exec; 1 };
ok !eval { $conn->sql('SELECT $1')->exec; 1 };
like $@, qr/bind message supplies 0 parameters, but prepared statement/;
# prepare + describe won't let us detect empty queries, hmm...
is_deeply $conn->q('')->param_types, [];
is_deeply $conn->q('')->columns, [];
is_deeply $conn->sql('')->param_types, [];
is_deeply $conn->sql('')->columns, [];
ok !eval { $conn->q('')->exec; 1 };
ok !eval { $conn->sql('')->exec; 1 };
okerr FATAL => exec => qr/unexpected status code/;
is $conn->q('SET client_encoding=utf8')->exec, undef;
is $conn->sql('SET client_encoding=utf8')->exec, undef;
ok !eval { $conn->q('select 1; select 2')->exec; 1 };
ok !eval { $conn->sql('select 1; select 2')->exec; 1 };
okerr ERROR => exec => qr/cannot insert multiple commands into a prepared statement/;
# Interleaved
{
my $x = $conn->q('SELECT 1 as a');
my $y = $conn->q('SELECT 2 as b');
my $x = $conn->sql('SELECT 1 as a');
my $y = $conn->sql('SELECT 2 as b');
is_deeply $x->columns, [ { oid => 23, name => 'a' } ];
is_deeply $y->columns, [ { oid => 23, name => 'b' } ];
is $x->val, 1;
@ -104,137 +104,137 @@ subtest '$st prepare & exec', sub {
};
subtest '$st->val', sub {
ok !eval { $conn->q('SELECT')->val; 1 };
ok !eval { $conn->sql('SELECT')->val; 1 };
like $@, qr/on query returning no data/;
ok !eval { $conn->q('SELECT 1, 2')->val; 1 };
ok !eval { $conn->sql('SELECT 1, 2')->val; 1 };
like $@, qr/on query returning more than one column/;
ok !eval { $conn->q('SELECT 1 UNION SELECT 2')->val; 1 };
ok !eval { $conn->sql('SELECT 1 UNION SELECT 2')->val; 1 };
like $@, qr/on query returning more than one row/;
ok !defined $conn->q('SELECT 1 WHERE false')->val;
ok !defined $conn->q('SELECT null')->val;
is $conn->q('SELECT $1::text', "\x{1F603}")->val, "\x{1F603}";
ok !defined $conn->sql('SELECT 1 WHERE false')->val;
ok !defined $conn->sql('SELECT null')->val;
is $conn->sql('SELECT $1::text', "\x{1F603}")->val, "\x{1F603}";
};
subtest '$st->rowl', sub {
ok !eval { $conn->q('SELECT 1 UNION SELECT 2')->rowl; 1 };
ok !eval { $conn->sql('SELECT 1 UNION SELECT 2')->rowl; 1 };
like $@, qr/on query returning more than one row/;
ok !eval { $conn->q('SELEXT')->rowl; 1; };
is scalar $conn->q('SELECT')->rowl, 0;
is scalar $conn->q('SELECT 1, 2')->rowl, 2;
is_deeply [$conn->q('SELECT')->rowl], [];
is_deeply [$conn->q('SELECT 1, null')->rowl], [1, undef];
is_deeply [$conn->q('SELECT 1, $1', undef)->rowl], [1, undef];
is_deeply [$conn->q('SELECT 1, $1::int', undef)->text_params(0)->rowl], [1, undef];
is_deeply [$conn->q('SELECT 1 WHERE false')->rowl], [];
ok !eval { $conn->sql('SELEXT')->rowl; 1; };
is scalar $conn->sql('SELECT')->rowl, 0;
is scalar $conn->sql('SELECT 1, 2')->rowl, 2;
is_deeply [$conn->sql('SELECT')->rowl], [];
is_deeply [$conn->sql('SELECT 1, null')->rowl], [1, undef];
is_deeply [$conn->sql('SELECT 1, $1', undef)->rowl], [1, undef];
is_deeply [$conn->sql('SELECT 1, $1::int', undef)->text_params(0)->rowl], [1, undef];
is_deeply [$conn->sql('SELECT 1 WHERE false')->rowl], [];
};
subtest '$st->rowa', sub {
ok !eval { $conn->q('SELECT 1 UNION SELECT 2')->rowa; 1 };
ok !eval { $conn->sql('SELECT 1 UNION SELECT 2')->rowa; 1 };
like $@, qr/on query returning more than one row/;
ok !eval { $conn->q('SELEXT')->rowa; 1; };
is $conn->q('SELECT 1 WHERE false')->rowa, undef;
is_deeply $conn->q('SELECT')->rowa, [];
is_deeply $conn->q('SELECT 1, 2')->rowa, [1, 2];
is_deeply $conn->q('SELECT 1, null')->rowa, [1, undef];
is_deeply $conn->q('SELECT 1, $1', undef)->rowa, [1, undef];
is_deeply $conn->q('SELECT 1, $1::int', undef)->text_params(0)->rowa, [1, undef];
ok !eval { $conn->sql('SELEXT')->rowa; 1; };
is $conn->sql('SELECT 1 WHERE false')->rowa, undef;
is_deeply $conn->sql('SELECT')->rowa, [];
is_deeply $conn->sql('SELECT 1, 2')->rowa, [1, 2];
is_deeply $conn->sql('SELECT 1, null')->rowa, [1, undef];
is_deeply $conn->sql('SELECT 1, $1', undef)->rowa, [1, undef];
is_deeply $conn->sql('SELECT 1, $1::int', undef)->text_params(0)->rowa, [1, undef];
};
subtest '$st->rowh', sub {
ok !eval { $conn->q('SELECT 1 UNION SELECT 2')->rowh; 1 };
ok !eval { $conn->sql('SELECT 1 UNION SELECT 2')->rowh; 1 };
like $@, qr/on query returning more than one row/;
ok !eval { $conn->q('SELECT 1 as a, 2 as a')->rowh; 1 };
ok !eval { $conn->sql('SELECT 1 as a, 2 as a')->rowh; 1 };
like $@, qr/Query returns multiple columns with the same name/;
is $conn->q('SELECT 1 WHERE false')->rowh, undef;
is_deeply $conn->q('SELECT')->rowh, {};
is_deeply $conn->q('SELECT 1 as a, 2 as b')->rowh, {a => 1, b => 2};
is_deeply $conn->q('SELECT 1 as a, null as b')->rowh, {a => 1, b => undef};
is_deeply $conn->q('SELECT 1 as a, $1::int as b', undef)->rowh, {a => 1, b => undef};
is $conn->sql('SELECT 1 WHERE false')->rowh, undef;
is_deeply $conn->sql('SELECT')->rowh, {};
is_deeply $conn->sql('SELECT 1 as a, 2 as b')->rowh, {a => 1, b => 2};
is_deeply $conn->sql('SELECT 1 as a, null as b')->rowh, {a => 1, b => undef};
is_deeply $conn->sql('SELECT 1 as a, $1::int as b', undef)->rowh, {a => 1, b => undef};
};
subtest '$st->alla', sub {
is_deeply $conn->q('SELECT 1 WHERE false')->alla, [];
is_deeply $conn->q('SELECT')->alla, [[]];
is_deeply $conn->q('SELECT 1')->alla, [[1]];
is_deeply $conn->q('SELECT 1, null UNION ALL SELECT NULL, 2')->alla, [[1,undef],[undef,2]];
is_deeply $conn->sql('SELECT 1 WHERE false')->alla, [];
is_deeply $conn->sql('SELECT')->alla, [[]];
is_deeply $conn->sql('SELECT 1')->alla, [[1]];
is_deeply $conn->sql('SELECT 1, null UNION ALL SELECT NULL, 2')->alla, [[1,undef],[undef,2]];
};
subtest '$st->allh', sub {
ok !eval { $conn->q('SELECT 1 as a, 2 as a')->allh; 1 };
ok !eval { $conn->sql('SELECT 1 as a, 2 as a')->allh; 1 };
like $@, qr/Query returns multiple columns with the same name/;
is_deeply $conn->q('SELECT 1 WHERE false')->allh, [];
is_deeply $conn->q('SELECT')->allh, [{}];
is_deeply $conn->q('SELECT 1 a')->allh, [{a=>1}];
is_deeply $conn->q('SELECT 1 a, null b UNION ALL SELECT NULL, 2')->allh, [{a=>1,b=>undef},{a=>undef,b=>2}];
is_deeply $conn->sql('SELECT 1 WHERE false')->allh, [];
is_deeply $conn->sql('SELECT')->allh, [{}];
is_deeply $conn->sql('SELECT 1 a')->allh, [{a=>1}];
is_deeply $conn->sql('SELECT 1 a, null b UNION ALL SELECT NULL, 2')->allh, [{a=>1,b=>undef},{a=>undef,b=>2}];
};
subtest '$st->flat', sub {
is_deeply $conn->q('SELECT 1 WHERE false')->flat, [];
is_deeply $conn->q('SELECT')->flat, [];
is_deeply $conn->q('SELECT 1')->flat, [1];
is_deeply $conn->q('SELECT 1, null UNION ALL SELECT NULL, 2')->flat, [1,undef,undef,2];
is_deeply $conn->sql('SELECT 1 WHERE false')->flat, [];
is_deeply $conn->sql('SELECT')->flat, [];
is_deeply $conn->sql('SELECT 1')->flat, [1];
is_deeply $conn->sql('SELECT 1, null UNION ALL SELECT NULL, 2')->flat, [1,undef,undef,2];
};
subtest '$st->kvv', sub {
ok !eval { $conn->q('SELECT')->kvv; 1; };
ok !eval { $conn->sql('SELECT')->kvv; 1; };
like $@, qr/returning no data/;
ok !eval { $conn->q('SELECT 1, 2, 3')->kvv; 1; };
ok !eval { $conn->sql('SELECT 1, 2, 3')->kvv; 1; };
like $@, qr/returning more than two columns/;
ok !eval { $conn->q('SELECT 1 UNION ALL SELECT 1')->kvv; 1; };
ok !eval { $conn->sql('SELECT 1 UNION ALL SELECT 1')->kvv; 1; };
like $@, qr/is duplicated/;
is_deeply $conn->q('SELECT 1 WHERE false')->kvv, {};
is_deeply $conn->q('SELECT 1')->kvv, {1=>1};
is_deeply $conn->q('SELECT 1, null UNION ALL SELECT 3, 2')->kvv, {1=>undef,3=>2};
$conn->q('SELECT 1')->kvv->{1} = 0;
is_deeply $conn->sql('SELECT 1 WHERE false')->kvv, {};
is_deeply $conn->sql('SELECT 1')->kvv, {1=>1};
is_deeply $conn->sql('SELECT 1, null UNION ALL SELECT 3, 2')->kvv, {1=>undef,3=>2};
$conn->sql('SELECT 1')->kvv->{1} = 0;
};
subtest '$st->kva', sub {
ok !eval { $conn->q('SELECT')->kva; 1; };
ok !eval { $conn->sql('SELECT')->kva; 1; };
like $@, qr/returning no data/;
ok !eval { $conn->q('SELECT 1 UNION ALL SELECT 1')->kva; 1; };
ok !eval { $conn->sql('SELECT 1 UNION ALL SELECT 1')->kva; 1; };
like $@, qr/is duplicated/;
is_deeply $conn->q('SELECT 1 WHERE false')->kva, {};
is_deeply $conn->q('SELECT 1')->kva, {1=>[]};
is_deeply $conn->q("SELECT 1, null, 'hi' UNION ALL SELECT 3, 2, 'ok'")->kva,
is_deeply $conn->sql('SELECT 1 WHERE false')->kva, {};
is_deeply $conn->sql('SELECT 1')->kva, {1=>[]};
is_deeply $conn->sql("SELECT 1, null, 'hi' UNION ALL SELECT 3, 2, 'ok'")->kva,
{1=>[undef,'hi'], 3=>[2, 'ok']};
};
subtest '$st->kvh', sub {
ok !eval { $conn->q('SELECT')->kvh; 1; };
ok !eval { $conn->sql('SELECT')->kvh; 1; };
like $@, qr/returning no data/;
ok !eval { $conn->q('SELECT 1 UNION ALL SELECT 1')->kvh; 1; };
ok !eval { $conn->sql('SELECT 1 UNION ALL SELECT 1')->kvh; 1; };
like $@, qr/is duplicated/;
ok !eval { $conn->q('SELECT 1, 2, 3')->kvh; 1; };
ok !eval { $conn->sql('SELECT 1, 2, 3')->kvh; 1; };
like $@, qr/Query returns multiple columns with the same name/;
is_deeply $conn->q('SELECT 1 WHERE false')->kvh, {};
is_deeply $conn->q('SELECT 1')->kvh, {1=>{}};
is_deeply $conn->q("SELECT 1 as a , null as a, 'hi' as b UNION ALL SELECT 3, 2, 'ok'")->kvh,
is_deeply $conn->sql('SELECT 1 WHERE false')->kvh, {};
is_deeply $conn->sql('SELECT 1')->kvh, {1=>{}};
is_deeply $conn->sql("SELECT 1 as a , null as a, 'hi' as b UNION ALL SELECT 3, 2, 'ok'")->kvh,
{1=>{a=>undef,b=>'hi'}, 3=>{a=>2,b=>'ok'}};
};
subtest 'txn', sub {
$conn->exec('CREATE TEMPORARY TABLE fupg_tst (id int)');
$conn->txn->exec('INSERT INTO fupg_tst VALUES (1)'); # rolled back
is $conn->q('SELECT COUNT(*) FROM fupg_tst')->val, 0;
is $conn->sql('SELECT COUNT(*) FROM fupg_tst')->val, 0;
my $st = $conn->q('SELECT COUNT(*) FROM fupg_tst');
my $st = $conn->sql('SELECT COUNT(*) FROM fupg_tst');
my $sst;
{
my $txn = $conn->txn;
@ -246,13 +246,13 @@ subtest 'txn', sub {
ok !eval { $conn->exec('SELECT 1'); 1 };
like $@, qr/Invalid operation on the top-level connection/;
ok !eval { $conn->q('SELECT 1'); 1 };
ok !eval { $conn->sql('SELECT 1'); 1 };
like $@, qr/Invalid operation on the top-level connection/;
ok !eval { $conn->txn; 1 };
like $@, qr/Invalid operation on the top-level connection/;
$txn->exec('INSERT INTO fupg_tst VALUES (1)');
$sst = $txn->q('SELECT 1');
$sst = $txn->sql('SELECT 1');
is $conn->status, 'txn_idle';
is $txn->status, 'idle';
@ -268,7 +268,7 @@ subtest 'txn', sub {
like $@, qr/Invalid operation on a transaction that has already been marked as done/;
ok !eval { $txn->exec('select 1'); 1 };
like $@, qr/Invalid operation on a transaction that has already been marked as done/;
ok !eval { $txn->q('select 1'); 1 };
ok !eval { $txn->sql('select 1'); 1 };
like $@, qr/Invalid operation on a transaction that has already been marked as done/;
ok !eval { $conn->exec('SELECT 1'); 1 };
@ -295,7 +295,7 @@ subtest 'txn', sub {
{
my $txn = $conn->txn;
my $st = $txn->q('SELECT count(*) FROM fupg_tst WHERE id = 2');
my $st = $txn->sql('SELECT count(*) FROM fupg_tst WHERE id = 2');
{
my $sub = $txn->txn;
is $conn->status, 'txn_idle';
@ -316,7 +316,7 @@ subtest 'txn', sub {
is $txn->status, 'idle';
is $st->val, 0;
$st = $txn->q('SELECT count(*) FROM fupg_tst WHERE id = 2');
$st = $txn->sql('SELECT count(*) FROM fupg_tst WHERE id = 2');
{
my $sub = $txn->txn;
$sub->exec('INSERT INTO fupg_tst VALUES (2)');
@ -339,19 +339,19 @@ subtest 'txn', sub {
$sub->commit;
}
# We didn't commit $txn, so $sub got aborted as well
is $conn->q('SELECT count(*) FROM fupg_tst WHERE id = 3')->val, 0;
is $conn->sql('SELECT count(*) FROM fupg_tst WHERE id = 3')->val, 0;
};
{
local $_ = 'x';
my $st = $conn->q('SELECT $1', $_);
my $st = $conn->sql('SELECT $1', $_);
$_ = 'y';
is $st->val, 'x', 'shallow copy';
}
{
my $x = [1,2];
my $st = $conn->q('SELECT $1::int[]', $x)->text(0);
my $st = $conn->sql('SELECT $1::int[]', $x)->text(0);
$x->[1] = 3;
is_deeply $st->val, [1,3], 'not deep copy';
}
@ -360,7 +360,7 @@ subtest 'txn', sub {
{
# Exact format returned by escape_literal() can differ between Postgres versions and configurations.
my $x = q{"' \" \\};
is $conn->q('SELECT '.$conn->escape_literal($x))->val, $x;
is $conn->sql('SELECT '.$conn->escape_literal($x))->val, $x;
# Format can also change, but unsure how to test this otherwise.
is $conn->escape_identifier('hel\l"o'), '"hel\l""o"';
@ -371,32 +371,32 @@ subtest 'Prepared statement cache', sub {
my $txn = $conn->txn;
$txn->cache;
my $numexec = sub($sql) {
$txn->q('SELECT generic_plans + custom_plans FROM pg_prepared_statements WHERE statement = $1', $sql)->cache(0)->val
$txn->sql('SELECT generic_plans + custom_plans FROM pg_prepared_statements WHERE statement = $1', $sql)->cache(0)->val
};
is $txn->q('SELECT 1')->val, 1;
is $txn->sql('SELECT 1')->val, 1;
is $numexec->('SELECT 1'), 1;
my $sql = 'SELECT $1::int as a, $2::text as b';
ok !defined $numexec->($sql);
my $params = $txn->q($sql)->param_types;
my $params = $txn->sql($sql)->param_types;
is_deeply $params, [23, 25];
is $numexec->($sql), 0;
my $cparams = $txn->q($sql)->param_types;
my $cparams = $txn->sql($sql)->param_types;
is_deeply $cparams, $params;
my $cols = $txn->q($sql)->columns;
my $cols = $txn->sql($sql)->columns;
is_deeply $cols, [{ name => 'a', oid => 23 }, { name => 'b', oid => 25 }];
my $ccols = $txn->q($sql)->columns;
my $ccols = $txn->sql($sql)->columns;
is_deeply $ccols, $cols;
$txn->q($sql, 0, '')->exec;
$txn->sql($sql, 0, '')->exec;
is $numexec->($sql), 1;
$txn->q($sql, 0, '')->exec;
$txn->sql($sql, 0, '')->exec;
is $numexec->($sql), 2;
is $numexec->('SELECT 1'), 1;
$txn->q('SELECT 2')->exec;
$txn->sql('SELECT 2')->exec;
ok !defined $numexec->('SELECT 1');
is $numexec->('SELECT 2'), 1;
@ -415,7 +415,7 @@ subtest 'Tracing', sub {
my @log;
$conn->query_trace(sub($st) { push @log, $st });
is_deeply $conn->q('SELECT 1 AS a, $1 AS b', 123)->text_params(0)->rowa, [ 1, 123 ];
is_deeply $conn->sql('SELECT 1 AS a, $1 AS b', 123)->text_params(0)->rowa, [ 1, 123 ];
is scalar @log, 1;
my $st = shift @log;
is ref $st, 'FU::Pg::st';
@ -451,7 +451,7 @@ subtest 'Tracing', sub {
};
{
my $st = $conn->q("SELECT 1");
my $st = $conn->sql("SELECT 1");
undef $conn; # statement keeps the connection alive
is $st->val, 1;
}

View file

@ -82,9 +82,9 @@ is $conn->status, 'idle';
$c->write($bin);
$c->close;
is $txn->q('SELECT sum(v) FROM fupg_copy_test')->val, 1+1+2+2+3+3;
is $txn->sql('SELECT sum(v) FROM fupg_copy_test')->val, 1+1+2+2+3+3;
$txn->rollback;
}
is $conn->q('SELECT sum(v) FROM fupg_copy_test')->val, 1+2+3;
is $conn->sql('SELECT sum(v) FROM fupg_copy_test')->val, 1+2+3;
done_testing;

View file

@ -10,31 +10,31 @@ my $conn = FU::Pg->connect($ENV{FU_TEST_DB});
$conn->_debug_trace(0);
is_deeply $conn->Q('SELECT', 1, '::int')->param_types, [23];
is_deeply $conn->Q('SELECT 1', IN([1,2,3]))->param_types, [1007];
is $conn->Q('SELECT 1', IN([1,2,3]))->val, 1;
is_deeply $conn->SQL('SELECT', 1, '::int')->param_types, [23];
is_deeply $conn->SQL('SELECT 1', IN([1,2,3]))->param_types, [1007];
is $conn->SQL('SELECT 1', IN([1,2,3]))->val, 1;
ok !eval { $conn->q('SELECT $1::aclitem', '')->exec; 1 };
ok !eval { $conn->sql('SELECT $1::aclitem', '')->exec; 1 };
like $@, qr/Unable to send type/;
subtest 'type overrides', sub {
$conn->set_type(int4 => recv => 'bytea');
is $conn->q('SELECT 5::int4')->val, "\0\0\0\5";
is_deeply $conn->q('SELECT ARRAY[5::int4]')->val, ["\0\0\0\5"];
is $conn->sql('SELECT 5::int4')->val, "\0\0\0\5";
is_deeply $conn->sql('SELECT ARRAY[5::int4]')->val, ["\0\0\0\5"];
$conn->set_type(int4 => send => 'bytea');
is $conn->q('SELECT $1::int4', "\0\0\0\5")->val, 5;
is_deeply $conn->q('SELECT $1::int4[]', ["\0\0\0\5"])->val, [5];
is $conn->sql('SELECT $1::int4', "\0\0\0\5")->val, 5;
is_deeply $conn->sql('SELECT $1::int4[]', ["\0\0\0\5"])->val, [5];
$conn->set_type(int4 => 'int2');
ok !eval { $conn->q('SELECT 5::int4')->val };
ok !eval { $conn->sql('SELECT 5::int4')->val };
like $@, qr/Error parsing value/;
ok !eval { $conn->q('SELECT $1::int4', 5)->val };
ok !eval { $conn->sql('SELECT $1::int4', 5)->val };
like $@, qr/insufficient data left in message/;
$conn->set_type(int4 => undef);
is $conn->q('SELECT 5::int4')->val, 5;
is $conn->sql('SELECT 5::int4')->val, 5;
ok !eval { $conn->set_type(int4 => 1007); };
like $@, qr/Cannot set a type to array/;
@ -46,23 +46,23 @@ subtest 'type overrides', sub {
subtest 'type override callback', sub {
$conn->set_type(text => recv => sub { length $_[0] });
is $conn->q('SELECT $1', 'a')->val, 1;
is $conn->q('SELECT $1', 'ab')->val, 2;
is $conn->q('SELECT $1', 'abc')->val, 3;
is $conn->q('SELECT $1', 'abcd')->val, 4;
is $conn->sql('SELECT $1', 'a')->val, 1;
is $conn->sql('SELECT $1', 'ab')->val, 2;
is $conn->sql('SELECT $1', 'abc')->val, 3;
is $conn->sql('SELECT $1', 'abcd')->val, 4;
$conn->set_type(text => send => sub { 'l'.length $_[0] });
is $conn->q('SELECT $1', 'a')->val, 'l1';
is $conn->q('SELECT $1', 'ab')->val, 'l2';
is $conn->q('SELECT $1', 'abc')->val, 'l3';
is $conn->q('SELECT $1', 'abcd')->val, 'l4';
is $conn->sql('SELECT $1', 'a')->val, 'l1';
is $conn->sql('SELECT $1', 'ab')->val, 'l2';
is $conn->sql('SELECT $1', 'abc')->val, 'l3';
is $conn->sql('SELECT $1', 'abcd')->val, 'l4';
};
subtest 'custom types', sub {
my $txn = $conn->txn;
is $txn->Q('SELECT 1', IN([1,2,3]))->val, 1;
is $txn->SQL('SELECT 1', IN([1,2,3]))->val, 1;
$txn->exec(<<~_);
CREATE TYPE fupg_test_enum AS ENUM('aa', 'bb', 'ccccccccccccccccccc');
@ -73,21 +73,21 @@ subtest 'custom types', sub {
domain fupg_test_domain
);
_
is $txn->q("SELECT 'aa'::fupg_test_enum")->val, 'aa';
is $txn->q('SELECT $1::fupg_test_enum', 'ccccccccccccccccccc')->val, 'ccccccccccccccccccc';
is $txn->sql("SELECT 'aa'::fupg_test_enum")->val, 'aa';
is $txn->sql('SELECT $1::fupg_test_enum', 'ccccccccccccccccccc')->val, 'ccccccccccccccccccc';
is_deeply $txn->q("SELECT '{aa,bb,null}'::fupg_test_enum[]")->val, ['aa','bb',undef];
is $txn->q('SELECT $1::fupg_test_enum[]', ['aa','bb',undef])->text_results->val, '{aa,bb,NULL}';
is_deeply $txn->sql("SELECT '{aa,bb,null}'::fupg_test_enum[]")->val, ['aa','bb',undef];
is $txn->sql('SELECT $1::fupg_test_enum[]', ['aa','bb',undef])->text_results->val, '{aa,bb,NULL}';
is $txn->q("SELECT 'aa'::fupg_test_domain")->val, 'aa';
is $txn->q('SELECT $1::fupg_test_domain', 'bb')->val, 'bb';
is $txn->sql("SELECT 'aa'::fupg_test_domain")->val, 'aa';
is $txn->sql('SELECT $1::fupg_test_domain', 'bb')->val, 'bb';
is_deeply $txn->q("SELECT '{aa,bb,null}'::fupg_test_domain[]")->val, ['aa','bb',undef];
is $txn->q('SELECT $1::fupg_test_domain[]', ['aa','bb',undef])->text_results->val, '{aa,bb,NULL}';
is_deeply $txn->sql("SELECT '{aa,bb,null}'::fupg_test_domain[]")->val, ['aa','bb',undef];
is $txn->sql('SELECT $1::fupg_test_domain[]', ['aa','bb',undef])->text_results->val, '{aa,bb,NULL}';
my $val = { a => undef, aenum => ['aa','bb'], domain => 'aa' };
is_deeply $txn->q("SELECT '(,\"{aa,bb}\",aa)'::fupg_test_record")->val, $val;
is $txn->q('SELECT $1::fupg_test_record', $val)->text_results->val, '(,"{aa,bb}",aa)';
is_deeply $txn->sql("SELECT '(,\"{aa,bb}\",aa)'::fupg_test_record")->val, $val;
is $txn->sql('SELECT $1::fupg_test_record', $val)->text_results->val, '(,"{aa,bb}",aa)';
$txn->exec(<<~_);
CREATE TEMPORARY TABLE fupg_test_table (
@ -96,7 +96,7 @@ subtest 'custom types', sub {
);
_
$val = $txn->q(q{SELECT '{"(\"(2,{},bb)\",)","(\"(,,)\",bb)"}'::fupg_test_table[]})->val;
$val = $txn->sql(q{SELECT '{"(\"(2,{},bb)\",)","(\"(,,)\",bb)"}'::fupg_test_table[]})->val;
is_deeply $val, [
{ rec => { a => 2, aenum => [], domain => 'bb' }, dom => undef },
{ rec => { a => undef, aenum => undef, domain => undef }, dom => 'bb' },
@ -106,7 +106,7 @@ subtest 'custom types', sub {
$val->[1]{rec} = 0;
$val->[1]{dom} = 0;
is $txn->q('SELECT $1::fupg_test_table[]', [
is $txn->sql('SELECT $1::fupg_test_table[]', [
{ rec => { a => 2, aenum => [], domain => 'bb' }, dom => undef },
{ rec => {}, dom => 'bb', extra => 1 },
])->text_results->val, '{"(\"(2,{},bb)\",)","(\"(,,)\",bb)"}';
@ -114,46 +114,46 @@ subtest 'custom types', sub {
# Wonky Postgres behavior: selecting a domain directly actually returns the
# underlying type, but going through an array does work.
$conn->set_type(fupg_test_domain => 21);
is_deeply $txn->q("SELECT ARRAY['aa'::fupg_test_domain]")->val, [0x6161];
is_deeply $txn->sql("SELECT ARRAY['aa'::fupg_test_domain]")->val, [0x6161];
# Bind param type doesn't match column type, argh.
is $txn->q('SELECT $1::fupg_test_domain', 0x6161)->val, 'aa';
is $txn->sql('SELECT $1::fupg_test_domain', 0x6161)->val, 'aa';
# Same for selecting from a table :(
$txn->exec("INSERT INTO fupg_test_table VALUES (NULL, 'bb')");
is $txn->q("SELECT dom FROM fupg_test_table")->val, 'bb';
is $txn->sql("SELECT dom FROM fupg_test_table")->val, 'bb';
$conn->set_type(fupg_test_enum => 21);
is $txn->q("SELECT dom FROM fupg_test_table")->val, 0x6262;
is $txn->sql("SELECT dom FROM fupg_test_table")->val, 0x6262;
};
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;
ok $txn->SQL('INSERT INTO fupg_test_tbl', VALUES {desc => 5, ok => 10, 'hello world', 15})->exec;
is $txn->SQL('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;
plan skip_all => 'type not loaded in the database' if !$conn->sql("SELECT 1 FROM pg_type WHERE typname = 'vndbtag'")->val;
for my $t (qw/a zz xxx/) {
is $conn->q('SELECT $1::vndbtag', $t)->val, $t;
is $conn->q('SELECT $1::vndbtag', $t)->text_params->val, $t;
is $conn->q('SELECT $1::vndbtag', $t)->text_results->val, $t;
is $conn->sql('SELECT $1::vndbtag', $t)->val, $t;
is $conn->sql('SELECT $1::vndbtag', $t)->text_params->val, $t;
is $conn->sql('SELECT $1::vndbtag', $t)->text_results->val, $t;
}
ok !eval { $conn->q('SELECT $1::vndbtag', '')->val };
ok !eval { $conn->q('SELECT $1::vndbtag', 'abcd')->val };
ok !eval { $conn->sql('SELECT $1::vndbtag', '')->val };
ok !eval { $conn->sql('SELECT $1::vndbtag', 'abcd')->val };
for my $t (qw/a123 zz992883231 xxx18388123/) {
is $conn->q('SELECT $1::vndbid', $t)->val, $t;
is $conn->q('SELECT $1::vndbid', $t)->text_params->val, $t;
is $conn->q('SELECT $1::vndbid', $t)->text_results->val, $t;
is $conn->sql('SELECT $1::vndbid', $t)->val, $t;
is $conn->sql('SELECT $1::vndbid', $t)->text_params->val, $t;
is $conn->sql('SELECT $1::vndbid', $t)->text_results->val, $t;
}
ok !eval { $conn->q('SELECT $1::vndbid', '')->val };
ok !eval { $conn->q('SELECT $1::vndbid', 'ab')->val };
ok !eval { $conn->q('SELECT $1::vndbid', 'ab1219229999999999')->val };
ok !eval { $conn->sql('SELECT $1::vndbid', '')->val };
ok !eval { $conn->sql('SELECT $1::vndbid', 'ab')->val };
ok !eval { $conn->sql('SELECT $1::vndbid', 'ab1219229999999999')->val };
};
done_testing;

View file

@ -22,7 +22,7 @@ sub v($type, $p_in, @args) {
my $oid;
utf8::encode($test);
{
my $st = $conn->q("SELECT \$1::$type", $s_in)->text_params;
my $st = $conn->sql("SELECT \$1::$type", $s_in)->text_params;
$oid = $st->param_types->[0];
my $array = $st->flat;
my $res = $array->[0];
@ -32,11 +32,11 @@ sub v($type, $p_in, @args) {
$array->[0] = 0; # Must be writable
}
{
my $res = $conn->q("SELECT \$1::$type", $p_in)->text_results->val;
my $res = $conn->sql("SELECT \$1::$type", $p_in)->text_results->val;
is $res, $s_out, "$test bin->text";
}
{
my $res = $conn->q("SELECT \$1::$type", $p_in)->val;
my $res = $conn->sql("SELECT \$1::$type", $p_in)->val;
is_deeply $res, $p_out, "$test bin->bin";
}
{
@ -52,11 +52,11 @@ sub v($type, $p_in, @args) {
sub f($type, $p_in) {
my $test = "$type $p_in" =~ s/\n/\\n/rg;
utf8::encode($test);
ok !eval { $conn->q("SELECT \$1::$type", $p_in)->val; 1 }, "$test fail";
ok !eval { $conn->sql("SELECT \$1::$type", $p_in)->val; 1 }, "$test fail";
}
{ # void
my $array = $conn->q('SELECT pg_sleep(0)')->flat;
my $array = $conn->sql('SELECT pg_sleep(0)')->flat;
ok !defined $array->[0];
$array->[0] = 0;
}
@ -187,11 +187,11 @@ f 'oidvector', [undef];
# Example from https://www.postgresql.org/docs/17/arrays.html#ARRAYS-IO
# Lower bounds are discarded.
is_deeply $conn->q("SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[]")->val, [[[1,2,3],[4,5,6]]];
is_deeply $conn->sql("SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[]")->val, [[[1,2,3],[4,5,6]]];
is $conn->q('SELECT ($1::int2[])[2]', [1,2,3,4])->val, 2;
is $conn->q('SELECT ($1::int2vector)[1]', [1,2,3,4])->val, 2;
is $conn->q('SELECT ($1::oidvector)[1]', [1,2,3,4])->val, 2;
is $conn->sql('SELECT ($1::int2[])[2]', [1,2,3,4])->val, 2;
is $conn->sql('SELECT ($1::int2vector)[1]', [1,2,3,4])->val, 2;
is $conn->sql('SELECT ($1::oidvector)[1]', [1,2,3,4])->val, 2;
is_deeply [$conn->bin2text(
16, $conn->perl2bin(16, 1),
@ -207,7 +207,7 @@ is_deeply [$conn->bin2text(
}
{
my $v = $conn->q("SELECT '{t,f,NULL}'::bool[]")->val;
my $v = $conn->sql("SELECT '{t,f,NULL}'::bool[]")->val;
is_deeply $v, [true, false, undef];
$_ = 0 for @$v;
}