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

@ -13,6 +13,13 @@ package FU::Pg::conn {
my($sql, $params) = FU::SQL::SQL(@_)->compile(placeholder_style => 'pg', in_style => 'pg');
$s->q($sql, @$params);
}
sub set_type($s, $n, @arg) {
Carp::confess("Invalid number of arguments") if @arg == 0 || (@arg > 1 && @arg % 2);
return $s->_set_type($n, $arg[0], $arg[0]) if @arg == 1;
my %arg = @arg;
$s->_set_type($n, $arg{send}, $arg{recv});
}
};
*FU::Pg::txn::Q = \*FU::Pg::conn::Q;
@ -625,12 +632,51 @@ any of these.
=back
=head3 Overriding types
The default conversion for each type can be changed:
=over
=item $conn->set_type($affected_type, $type)
=item $conn->set_type($affected_type, send => $type, recv => $type)
Change how C<$affected_type> is being converted when used as a bind parameter
(I<send>) or when received from query results (I<recv>). The two-argument
version is equivalent to setting I<send> and I<recv> to the same C<$type>.
Types can be specified either by their numeric I<Oid> or by name. In the latter
case, the name must exactly match the internal type name used by PostgreSQL.
Note that this "internal type name" does not always match the names used in
documentation. For example, I<smallint>, I<integer> and I<bigint> should be
specified as I<int2>, I<int4> and I<int8>, respectively, and the I<char> type
is internally called I<bpchar>. The full list of recognized types in your
database can be queried with:
SELECT oid, typname FROM pg_type;
The C<$affected_type> does not actually have to exist in the database, this
method only stores the type in its internal configuration, which is consulted
upon executing a query that takes the type as bind parameter or when it returns
a column of that type.
The given C<$type> arguments must refer to a built-in type supported by this
module. Types can also be set to I<undef> to restore the conversion to its
default.
=back
I<TODO:> Type override examples and a warning about domain types.
I<TODO:> Some handy special types for overriding common conversions.
I<TODO:> Support for custom types through callbacks.
I<TODO:> Methods to convert between the various formats.
I<TODO:> Methods to query type info.
I<TODO:> Custom per-type configuration.
=head2 Errors
All methods can throw an exception on error. When possible, the error message