From: Rafael Kitover Date: Wed, 1 Jul 2009 04:03:12 +0000 (+0000) Subject: determine db_schema for mssql if not supplied X-Git-Tag: 0.04999_08~2^2~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=76503d57f3406c41298dee6f1230e691a878537c;p=dbsrgits%2FDBIx-Class-Schema-Loader.git determine db_schema for mssql if not supplied --- diff --git a/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm b/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm index fef1f2e..8611bf7 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm @@ -41,9 +41,38 @@ sub _setup { my $self = shift; $self->next::method(@_); - $self->{db_schema} ||= 'dbo'; + + $self->{db_schema} ||= $self->_determine_db_schema; } +sub _determine_db_schema { + my $self = shift; + my $dbh = $self->schema->storage->dbh; + + my $test_table = "_loader_test_$$"; + $dbh->do("create table $test_table (id integer)"); + + my $db_schema = 'dbo'; # default + + eval { + my $sth = $dbh->prepare('sp_tables'); + $sth->execute; + while (my $row = $sth->fetchrow_hashref) { + next unless $row->{TABLE_NAME} eq $test_table; + + $db_schema = $row->{TABLE_OWNER}; + last; + } + $sth->finish; + }; + my $exception = $@; + $dbh->do("drop table $test_table"); + croak $exception if $exception; + + return $db_schema; +} + + # DBD::Sybase doesn't implement get_info properly #sub _build_quoter { [qw/[ ]/] } sub _build_quoter { '"' }