chrestomanci has asked for the wisdom of the Perl Monks concerning the following question:
I am looking for a simple way to search in a database for all rows where a particular column has a case insensitive sub-string match to a search string.
Google told me to use the LOWER() SQL function, so I wrote some literal SQL like this:
SELECT * FROM tblItem me where( lower(me.name) LIKE '%example%' )It works nicey, however I am having trouble finding a neat way to translate it into a DBIx::Class::ResultSet search term.
Google searches found some posts on the DBIx-Class mailing list that said that I should write:
my $searched_rs = $all_items->search({'LOWER(me.name)' => {'LIKE'=>'%example%'})However this did not work. I got an error: DBI Exception: DBD::mysql::st execute failed: Unknown column 'LOWER(me.name)'
From reading the FAQ, it said that the only way to use functions on the left side of a search term is to pass the whole query as literal SQL. That gives me:
my $searched_rs = $all_items->search( \[ 'LOWER(me.name) LIKE ?',[ plain_value => "%example%"] ] );That works, but it is ugly, does not look to be very portable, feels against the spirit of using SQL::Abstract to insulate the perl program from the different database dialects of SQL.
Is there a neater way to do case insensitive sub-string searches? I am using MySQL and SQLite BTW.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBIx::Class case insensitve substring search.
by duelafn (Parson) on May 25, 2011 at 20:26 UTC | |
|
Re: DBIx::Class case insensitve substring search.
by bart (Canon) on May 26, 2011 at 11:18 UTC | |
by duelafn (Parson) on May 26, 2011 at 16:25 UTC | |
|
Re: DBIx::Class case insensitve substring search.
by Khen1950fx (Canon) on May 25, 2011 at 14:56 UTC | |
by chrestomanci (Priest) on May 25, 2011 at 19:08 UTC |