Search code examples
mysqlperldbix-class

Perl DBIx::Class Truncate table


I’m looking for a way to truncate a table with DBIx::Class but I ain’t able to find anything on the web except some suggestions such as delete all the rows instead of truncating the table. Unfortunately, TRUNCATE being much faster, I’d wish to do it properly. For now, I think I’ll use DBI for truncating but it is a bit of a drawback.

Is this possible in any way ?

Thanks in advance :-)


Solution

  • You can execute SQL directly within your DBIx::Class Model with a small helper function:

    sub _sql_do {
      my ( $self , $sql ) = @_ ;
    
      return $self->storage->dbh_do(
        sub {
          my ( $storage , $dbh , $sql ) = @_ ;
          my $sth = $dbh->prepare( $sql ) ;
          $sth->execute() ;
        },
        $sql
      );
    }