Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

DBIx Class Concatenating columns

by epoch1 (Acolyte)
on Mar 06, 2017 at 16:03 UTC ( [id://1183766]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I'm trying to concatenate some columns using DBIx Class but I can't figure out how to do it.

I have a database that has separate date and time columns that I need to concatenate into a single field, Id usually use something like

select col1, col3, CONCAT(date, ' ', time) AS startTime...

Here's my select code:

my $rs = $self->db->resultset('MODELCLASS')->search(undef, { select => [ 'col1', 'col2', 'date' . ' ' . 'time' ], as => [ qw/ id title time /], rows => 10 });

Can anyone tell me how I can do this?

Cheers

Replies are listed 'Best First'.
Re: DBIx Class Concatenating columns
by Corion (Patriarch) on Mar 06, 2017 at 16:59 UTC

    I believe that the "SQL abstraction" provided by DBIx::Class is just SQL::Abstract. If that is true, then you can supply function calls as in the documentation:

    \$sql_string

    So maybe the following works:

    my $rs = $self->db->resultset('MODELCLASS')->search(undef, { select => [ 'col1', 'col2', \"concat( date, ' ', time )", ], as => [ qw/ id title time /], rows => 10 });
Re: DBIx Class Concatenating columns
by thanos1983 (Parson) on Mar 06, 2017 at 16:49 UTC

    Hello epoch1,

    I do not know match about DBIx::Class but a possible solution to your problem could be DBIx::Class::Manual::Joining.

    From the manual "USING JOINS":

    $schema->resultset('CD')->search( { 'Title' => 'Funky CD', 'tracks.Name' => { like => 'T%' } }, { join => 'tracks', order_by => ['tracks.id'], } );

    Take a look, it might be what you are looking for.

    Update: another similar question/solution with join (Can I join the column names with search_related in DBIx?).

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: DBIx Class Concatenating columns
by Anonymous Monk on Mar 06, 2017 at 18:02 UTC
    If you would use plain ANSI SQL you would not have these problems. You spend more time trying to make your objects do what you what then actually getting work done, and the end result is usually harder to maintain and extend much less understand.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1183766]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found