[Dbix-class] Re: Determining if a ResultsSource/table exists

Tom Hukins tom at eborcom.com
Fri Mar 6 09:33:53 GMT 2009


On Fri, Mar 06, 2009 at 03:09:59PM +1100, Doug Scoular wrote:
> Just wondering if there is a recommended way to test whether a table
> exists in a DBIx::Class database independent way ?

That dosen't sound like a DBIx::Class problem:  DBIx::Class deals with
result sources, not tables.  You can see whether a result source
exists by checking you receive a defined value when trying to retrieve
one.

However, at the lower DBI level you might want to see whether a table exists.
I have some old code that I recall working the MySQL, SQLite and SQL Server
2000:

sub table_exists {
    my $self        = shift;
    my %arg         = @_;

    my $sth         = $self->table_info(
        '%',
        '%',
        $arg{table},
    );
    while ( my $row = $sth->fetchrow_hashref() ) {
        return $arg{table} if $row->{TABLE_NAME} eq $arg{table};
    }
    return;
}

This code lived in a DBI::db subclass, so I could do:

$dbh->table_exists(table => 'TABLE_NAME');

Tom



More information about the DBIx-Class mailing list