From 8545bbce275692e1e4ac9023320c7c4a8451bfdc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 3 May 2010 12:49:51 +0200 Subject: [PATCH] DBUtils auf LIKE version von add_token gebracht. --- SL/DBUtils.pm | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/SL/DBUtils.pm b/SL/DBUtils.pm index 4a4ac25c1..5fc33cedc 100644 --- a/SL/DBUtils.pm +++ b/SL/DBUtils.pm @@ -19,6 +19,14 @@ sub conv_i { return (defined($value) && "$value" ne "") ? $value * 1 : $default; } +# boolean escape +sub conv_b { + my ($value, $default) = @_; + return !defined $value && defined $default ? $default + : $value ? 't' + : 'f'; +} + sub conv_date { my ($value) = @_; return (defined($value) && "$value" ne "") ? $value : undef; @@ -307,16 +315,29 @@ sub add_token { my %params = @_; my $col = $params{col}; my $val = $params{val}; - my $method = $params{method} || '='; my $escape = $params{esc} || sub { $_ }; + my $method = $params{esc} =~ /^start|end|substr$/ ? 'ILIKE' : $params{method} || '='; $val = [ $val ] unless ref $val eq 'ARRAY'; my %escapes = ( id => \&conv_i, + bool => \&conv_b, date => \&conv_date, + start => sub { $_[0] . '%' }, + end => sub { '%' . $_[0] }, + substr => sub { '%' . $_[0] . '%' }, ); + my $_long_token = sub { + my $op = shift; + sub { + my $col = shift; + return scalar @_ ? join ' OR ', ("$col $op ?") x scalar @_, + : undef; + } + }; + my %methods = ( '=' => sub { my $col = shift; @@ -324,6 +345,7 @@ sub add_token { : scalar @_ == 1 ? sprintf '%s = ?', $col : undef; }, + map({ $_ => $_long_token->($_) } qw(LIKE ILIKE >= <= > <)), ); $method = $methods{$method} || $method; -- 2.20.1