pg: Add dynamic type loading & support enum types

Least efficient way to support enums, really. *shrug*
This commit is contained in:
Yorhel 2025-02-08 17:24:41 +01:00
parent 2aaec6a218
commit 7b76d94719
5 changed files with 114 additions and 29 deletions

20
t/pgtypes-dynamic.t Normal file
View file

@ -0,0 +1,20 @@
use v5.36;
use Test::More;
plan skip_all => $@ if !eval { require FU::PG; } && $@ =~ /Unable to load libpq/;
die $@ if $@;
plan skip_all => 'Please set FU_TEST_DB to a PostgreSQL connection string to run these tests' if !$ENV{FU_TEST_DB};
my $conn = FU::PG->connect($ENV{FU_TEST_DB});
ok !eval { $conn->q('SELECT $1::aclitem', '')->exec; 1 };
like $@, qr/Unable to use type/;
{
my $txn = $conn->txn;
$txn->exec("CREATE TYPE fupg_test_enum AS ENUM('a', 'b', 'ccccccccccccccccccc')");
is $txn->q("SELECT 'a'::fupg_test_enum")->val, 'a';
is $txn->q('SELECT $1::fupg_test_enum', 'ccccccccccccccccccc')->val, 'ccccccccccccccccccc';
}
done_testing;

View file

@ -17,23 +17,27 @@ sub v($type, $p_in, @args) {
my $s_in = @args > 1 && defined $args[1] ? $args[1] : $p_in;
my $s_out = @args > 2 && defined $args[2] ? $args[2] : $s_in;
my $test = "$type $s_in" =~ s/\n/\\n/rg;
utf8::encode($test);
{
my $res = $conn->q("SELECT \$1::$type", $s_in)->text_params->val;
ok is_bool($res), "$type $s_in is bool" if $type eq 'bool';
ok created_as_number($res), "$type $s_in is number" if $type =~ /^int/;
is_deeply $res, $p_out, "$type $s_in text->bin" =~ s/\n/\\n/rg;
ok is_bool($res), "$test is bool" if $type eq 'bool';
ok created_as_number($res), "$test is number" if $type =~ /^(int|float)/;
is_deeply $res, $p_out, "$test text->bin";
}
{
my $res = $conn->q("SELECT \$1::$type", $p_in)->text_results->val;
is $res, $s_out, "$type $s_out bin->text" =~ s/\n/\\n/rg;
is $res, $s_out, "$test bin->text";
}
{
my $res = $conn->q("SELECT \$1::$type", $p_in)->val;
is_deeply $res, $p_out, "$type $s_in bin->bin" =~ s/\n/\\n/rg;
is_deeply $res, $p_out, "$test bin->bin";
}
}
sub f($type, $p_in) {
ok !eval { $conn->q("SELECT \$1::$type", $p_in)->val; 1 }, "$type $p_in fail";
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";
}
v bool => true, undef, 1, 't';