Search code examples
perldbix-class

What is the DBIx::Class syntax for the CASE WHEN ... THEN SQL syntax?


Does anyone know what's the DBIx::Class equivalent of an SQL query such as:

SELECT cdr_id,
CASE WHEN service_id = 'GPRS' THEN 'KB' WHEN service_id = 'SMS' THEN 'SMS' END AS unit
FROM ...

Thanks


Solution

  • my $rs = $schema->resultset( 'table' )->
        search_rs( {} ,
                   { '+columns' => {
                         unit => 
                             \do { "CASE WHEN me.service_id='GPRS' THEN 'KB' " .
                                    "WHEN me.service_id='SMS' THEN 'SMS' END" }
                   } ) ;
    

    Something along this line should work.