Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 151c565

Browse files
author
Yorhel
committed
Add support for passing PostgreSQL ARRAY values
Behavior mimics the 'IN' context.
1 parent 2b51e1c commit 151c565

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/SQL/Interp.pm

+28
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ sub _sql_interp {
181181
_error_item($idx, \@items);
182182
}
183183
}
184+
elsif ($sql =~ /\bARRAY\s*$/si) {
185+
$item = [ $$item ] if ref $item eq 'SCALAR';
186+
187+
# allow double references
188+
$item = $$item if ref $item eq 'REF' ;
189+
190+
if (ref $item eq 'ARRAY') {
191+
$sql .= '[' . join(', ', map {
192+
_sql_interp_data($_);
193+
} @$item) . ']';
194+
}
195+
else {
196+
_error_item($idx, \@items);
197+
}
198+
}
184199
elsif ($sql =~ /\b(?:ON\s+DUPLICATE\s+KEY\s+UPDATE|SET)\s*$/si && ref $item eq 'HASH') {
185200
_error('Hash has zero elements.') if keys %$item == 0;
186201
$sql .= " " . join(', ', map {
@@ -618,6 +633,19 @@ I<This is not commonly used.>
618633
# Example usage (where $x and $y are table references):
619634
'SELECT * FROM', $x, 'JOIN', $y
620635
636+
=head3 Context ('ARRAY', $x)
637+
638+
A scalarref or arrayref can be turned into an array value. Such values are supported by PostgreSQL.
639+
640+
IN: 'SELECT ARRAY', $aref
641+
OUT: 'SELECT ARRAY[?, ?]', @$aref
642+
643+
IN: 'SELECT ARRAY', $sref
644+
OUT: 'SELECT ARRAY[?]', $$sref
645+
646+
IN: 'SELECT ARRAY', []
647+
OUT: 'SELECT ARRAY[]'
648+
621649
=head3 Other Rules
622650
623651
Whitespace is automatically added between parameters:

t/sql_interp.t

+14
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ interp_test(['WHERE field in', $v0],
178178
['WHERE 1=0'],
179179
'IN lowercase'); # fails in 0.31
180180

181+
# ARRAY
182+
interp_test(['SELECT ARRAY', $maybe_array],
183+
['SELECT ARRAY[?, ?]', @$maybe_array],
184+
'ARRAY');
185+
interp_test(['SELECT ARRAY', \$maybe_array],
186+
['SELECT ARRAY[?, ?]', @$maybe_array],
187+
'ARRAY ref');
188+
interp_test(['SELECT ARRAY', \$v0],
189+
['SELECT ARRAY[]'],
190+
'ARRAY empty');
191+
interp_test(['SELECT ARRAY', \$x],
192+
['SELECT ARRAY[?]', $x],
193+
'ARRAY scalar');
194+
181195
# SET
182196
interp_test(['UPDATE mytable SET', $h],
183197
["UPDATE mytable SET $hi->{keys}[0]=?, $hi->{keys}[1]=?", @{$hi->{values}}],

0 commit comments

Comments
 (0)