Search code examples
perldbix-class

How to run plain/arbitrary SQL with DBIx::Class?


I need to run plain SQL with DBIx::Class:

select proc_name();

Is there a way to do this with DBIx::Class?

UPD
I know I can do different connection with DBI and then:

$dbh->do( 'select proc_name();' )

But I need to run it within same transaction

UPD
FAQ does not cover that

UPD
For downvoters: I know DBIx::Class is not for plain SQL. But from one side sometimes query is too complex to rewrite it as DBIx::Class understand, from the other side we need functionality of DBIx::Class


Solution

  • Thanks to mst on #dbix-class IRC channel for help.

    I should use dbh_do

      my @stuff = $schema->storage->dbh_do(
        sub {
          my ($storage, $dbh, @cols) = @_;
          my $cols = join(q{, }, @cols);
          $dbh->selectrow_array("SELECT $cols FROM foo");
        },
        @column_list
      );
    

    UPD

    DBIx::Class::Report module suggest another way to run complex queries and get DBIx::Class::Result objects for rows as result

    UPD

    Possible DBIx::Raw will be interesting too