From: Sven Schöling Date: Mon, 3 May 2010 10:49:51 +0000 (+0200) Subject: DBUtils auf LIKE version von add_token gebracht. X-Git-Tag: release-3.0.0beta1~343^2~11 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=8545bbce275692e1e4ac9023320c7c4a8451bfdc;p=kivitendo-erp.git DBUtils auf LIKE version von add_token gebracht. --- 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;