a_login has asked for the wisdom of the Perl Monks concerning the following question:

hey.

i'm wondering if there is a function for perl that will take a string suchas:

$string = "(+goat -boy) +hair"
and an array of column names like
@cols = qw(t1.c1 t1.c5 r1.c8 ...)
and construct a the conditional part of an sql query from that. so that $cond in
'select * from t1 where $cond'
looks something like:

$cond ="
(t1.c1='goat' not t1.c1='boy') and t1.c1='hair' or
(t1.c5='goat' not t1.c5='boy') and t1.c5='hair' or
(t1.c8='goat' not t1.c8='boy') and t1.c8='hair' ... "
  • Comment on converting boolean strings into conditions for database queries

Replies are listed 'Best First'.
Re: converting boolean strings into conditions for database queries
by tachyon (Chancellor) on Aug 13, 2001 at 13:35 UTC

    You would need to write your own function like this one.

    $string = "(+goat -boy) +hair"; @cols = qw(t1.c1 t1.c5 t1.c8); print my_cond($string,@cols); sub my_cond { my ($string,@cols) = @_; my $cond = ''; for my $col (@cols) { $_ = $string; s/(\w+)/'$1'/g; s/\+/and $col=/g; s/-/not $col=/g; $cond .= $_." or\n"; } $cond =~ s/^(\()and /$1/gm; $cond =~ s/ or$//; return $cond; }
Re: converting boolean strings into conditions for database queries
by merlyn (Sage) on Aug 13, 2001 at 21:50 UTC
      i've come back to this after a while. i've looked at the perl module you mentioned above (or rather Text::Query::BuildSQL), but i'm not really sure how to use it. pardon my ignorance, but can anyone point me to some examples? i really appreciate the help.