determine db_schema for mssql if not supplied
Rafael Kitover [Wed, 1 Jul 2009 04:03:12 +0000 (04:03 +0000)]
lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm

index fef1f2e..8611bf7 100644 (file)
@@ -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  { '"' }