X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FLoader%2FDBI%2FMSSQL.pm;h=0653228ffac5c28a0980362a61c713b5ac5fd449;hb=116431d67fe62da10229791aef6cc0d5a87802d9;hp=d76ef9abc357fc7735aa3899bfedecf31992fd0a;hpb=18eb280f9b71785a12c105299f813358faa47143;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm b/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm index d76ef9a..0653228 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm @@ -75,12 +75,22 @@ EOF return grep !/^(?:#|guest|INFORMATION_SCHEMA|sys)/, @$owners; } +sub _current_db { + my $self = shift; + return ($self->dbh->selectrow_array('SELECT db_name()'))[0]; +} + +sub _switch_db { + my ($self, $db) = @_; + $self->dbh->do("use [$db]"); +} + sub _setup { my $self = shift; $self->next::method(@_); - my ($current_db) = $self->dbh->selectrow_array('SELECT db_name()'); + my $current_db = $self->_current_db; if (ref $self->db_schema eq 'HASH') { if (keys %{ $self->db_schema } < 2) { @@ -168,9 +178,16 @@ EOF # XXX why does databasepropertyex() not work over DBD::ODBC ? # # more on collations here: http://msdn.microsoft.com/en-us/library/ms143515.aspx - my ($collation_name) = - eval { $self->dbh->selectrow_array("SELECT collation_name FROM sys.databases WHERE name = @{[ $self->dbh->quote($db) ]}") } - || eval { $self->dbh->selectrow_array("SELECT CAST(databasepropertyex(@{[ $self->dbh->quote($db) ]}, 'Collation') AS VARCHAR)") }; + + my $current_db = $self->_current_db; + + $self->_switch_db($db); + + my $collation_name = + (eval { $self->dbh->selectrow_array("SELECT collation_name FROM [$db].sys.databases WHERE name = @{[ $self->dbh->quote($db) ]}") })[0] + || (eval { $self->dbh->selectrow_array("SELECT CAST(databasepropertyex(@{[ $self->dbh->quote($db) ]}, 'Collation') AS VARCHAR)") })[0]; + + $self->_switch_db($current_db); if (not $collation_name) { warn <<"EOF"; @@ -232,7 +249,7 @@ sub _table_pk_info { my $db = $table->database; - return $self->dbh->selectcol_arrayref(<<"EOF") + my $pk = $self->dbh->selectcol_arrayref(<<"EOF"); SELECT kcu.column_name FROM [$db].INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc JOIN [$db].INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu @@ -244,6 +261,10 @@ WHERE tc.table_name = @{[ $self->dbh->quote($table->name) ]} AND tc.constraint_type = 'PRIMARY KEY' ORDER BY kcu.ordinal_position EOF + + $pk = [ map $self->_lc($_), @$pk ]; + + return $pk; } sub _table_fk_info { @@ -279,8 +300,8 @@ EOF my %rels; while (my ($fk, $remote_schema, $remote_table, $col, $remote_col) = $sth->fetchrow_array) { - push @{ $rels{$fk}{local_columns} }, $col; - push @{ $rels{$fk}{remote_columns} }, $remote_col; + push @{ $rels{$fk}{local_columns} }, $self->_lc($col); + push @{ $rels{$fk}{remote_columns} }, $self->_lc($remote_col); $rels{$fk}{remote_table} = DBIx::Class::Schema::Loader::Table::Sybase->new( loader => $self, @@ -338,7 +359,10 @@ SELECT character_maximum_length, data_type, datetime_precision, column_default FROM [$db].INFORMATION_SCHEMA.COLUMNS WHERE table_name = @{[ $self->dbh->quote($table->name) ]} AND table_schema = @{[ $self->dbh->quote($table->schema) ]} - AND column_name = @{[ $self->dbh->quote($col) ]} + AND @{[ $self->preserve_case ? + "column_name = @{[ $self->dbh->quote($col) ]}" + : + "lower(column_name) = @{[ $self->dbh->quote(lc $col) ]}" ]} EOF $info->{data_type} = $data_type; @@ -361,7 +385,10 @@ WHERE object_id = ( FROM [$db].sys.schemas WHERE name = @{[ $self->dbh->quote($table->schema) ]} ) -) AND name = @{[ $self->dbh->quote($col) ]} +) AND @{[ $self->preserve_case ? + "name = @{[ $self->dbh->quote($col) ]}" + : + "lower(name) = @{[ $self->dbh->quote(lc $col) ]}" ]} EOF if ($is_identity) { $info->{is_auto_increment} = 1;