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

Doug Scoular dscoular at cisco.com
Fri Mar 6 04:09:59 GMT 2009


Hi All,
	Just wondering if there is a recommended way
	to test whether a table exists in a DBIx::Class
	database independent way ?

	At the moment I'm doing it this ugly way:

sub tableExists {
   my $self = shift;
   my $name = shift;

   my $schema = $self->_getSchema(); # Get our DBIx::Class schema.
   my $table = $schema->source($name)->from(); # Get our actual table name.
   my $dbh = $schema->storage->dbh; # Get our DBI handle.
   # Try preparing a select on the table in question.
   eval { $dbh->prepare("select count(*) from $table where 1=0") };
   if ($dbh->err) {
     # Table probably does not exist.
     return 0;
   }
   else {
     return 1;
   }
}

	But I'm not sure if it's very elegant. I got the prepare
	code by googling. Is there a better way ?

	Any thoughts much appreciated.

	Cheers,

	Doug
-- 
"The big print giveth and the small print taketh away"




More information about the DBIx-Class mailing list