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.
In reply to DBIx::Class case insensitve substring search. by chrestomanci
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |