use strict;
use warnings;
-use base 'DBIx::Class::Schema::Loader::DBI';
+use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common';
use Carp::Clan qw/^DBIx::Class/;
use Class::C3;
-our $VERSION = '0.04999_06';
+our $VERSION = '0.05003';
=head1 NAME
DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
-=head1 SYNOPSIS
-
- package My::Schema;
- use base qw/DBIx::Class::Schema::Loader/;
-
- __PACKAGE__->loader_options( debug => 1 );
-
- 1;
-
=head1 DESCRIPTION
-See L<DBIx::Class::Schema::Loader::Base>.
-
-=cut
-
-sub _rebless {
- my $self = shift;
+Base driver for Microsoft SQL Server, used by
+L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server> for support
+via L<DBD::Sybase> and
+L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server> for support via
+L<DBD::ODBC>.
- $self->schema->storage->sql_maker->quote_char([qw/[ ]/])
- unless $self->schema->storage->sql_maker->quote_char;
-
- $self->schema->storage->sql_maker->name_sep('.')
- unless $self->schema->storage->sql_maker->name_sep;
-}
-
-sub _setup {
- my $self = shift;
-
- $self->next::method(@_);
-
- $self->{db_schema} ||= $self->_determine_db_schema;
-}
+See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for
+usage information.
-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)");
+=cut
- my $db_schema = 'dbo'; # default
+sub _tables_list {
+ my ($self, $opts) = @_;
- eval {
- my $sth = $dbh->prepare('sp_tables');
- $sth->execute;
- while (my $row = $sth->fetchrow_hashref) {
- next unless $row->{TABLE_NAME} eq $test_table;
+ my $dbh = $self->schema->storage->dbh;
+ my $sth = $dbh->prepare(<<'EOF');
+SELECT t.table_name
+FROM information_schema.tables t
+WHERE t.table_schema = ?
+EOF
+ $sth->execute($self->db_schema);
- $db_schema = $row->{TABLE_OWNER};
- last;
- }
- $sth->finish;
- };
- my $exception = $@;
- $dbh->do("drop table $test_table");
- croak $exception if $exception;
+ my @tables = map @$_, @{ $sth->fetchall_arrayref };
- return $db_schema;
+ return $self->_filter_tables(\@tables, $opts);
}
-
-# DBD::Sybase doesn't implement get_info properly
-#sub _build_quoter { [qw/[ ]/] }
-sub _build_quoter { '"' }
-sub _build_namesep { '.' }
-
sub _table_pk_info {
my ($self, $table) = @_;
my $dbh = $self->schema->storage->dbh;
my ($self, $table) = @_;
my $dbh = $self->schema->storage->dbh;
- my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
- JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
- JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
- WHERE CCU.TABLE_NAME = '$table' AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
+ local $dbh->{FetchHashKeyName} = 'NAME_lc';
+
+ my $sth = $dbh->prepare(qq{
+SELECT ccu.constraint_name, ccu.column_name
+FROM information_schema.constraint_column_usage ccu
+JOIN information_schema.table_constraints tc on (ccu.constraint_name = tc.constraint_name)
+JOIN information_schema.key_column_usage kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name)
+wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
+ });
$sth->execute;
my $constraints;
while (my $row = $sth->fetchrow_hashref) {
- my $name = lc $row->{CONSTRAINT_NAME};
- my $col = lc $row->{COLUMN_NAME};
+ my $name = $row->{constraint_name};
+ my $col = lc $row->{column_name};
push @{$constraints->{$name}}, $col;
}
return \@uniqs;
}
-sub _extra_column_info {
- my ($self, $info) = @_;
- my %extra_info;
-
- my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
+sub _columns_info_for {
+ my $self = shift;
+ my ($table) = @_;
+
+ my $result = $self->next::method(@_);
+
+ while (my ($col, $info) = each %$result) {
+ my $dbh = $self->schema->storage->dbh;
+
+ my $sth = $dbh->prepare(qq{
+SELECT column_name
+FROM information_schema.columns
+WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1
+AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
+ });
+ if (eval { $sth->execute; $sth->fetchrow_array }) {
+ $info->{is_auto_increment} = 1;
+ $info->{data_type} =~ s/\s*identity//i;
+ delete $info->{size};
+ }
- my $dbh = $self->schema->storage->dbh;
- my $sth = $dbh->prepare(qq{SELECT COLUMN_NAME
- FROM INFORMATION_SCHEMA.COLUMNS
- WHERE COLUMNPROPERTY(object_id('$table', 'U'), '$column', 'IsIdentity') = 1 AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column'
- });
- $sth->execute();
-
- if ($sth->fetchrow_array) {
- $extra_info{is_auto_increment} = 1;
+# get default
+ $sth = $dbh->prepare(qq{
+SELECT column_default
+FROM information_schema.columns
+wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
+ });
+ my ($default) = eval { $sth->execute; $sth->fetchrow_array };
+
+ if (defined $default) {
+ # strip parens
+ $default =~ s/^\( (.*) \)\z/$1/x;
+
+ # Literal strings are in ''s, numbers are in ()s (in some versions of
+ # MSSQL, in others they are unquoted) everything else is a function.
+ $info->{default_value} =
+ $default =~ /^['(] (.*) [)']\z/x ? $1 :
+ $default =~ /^\d/ ? $default : \$default;
+ }
}
- return \%extra_info;
+ return $result;
}
-
=head1 SEE ALSO
+L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
+L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
L<DBIx::Class::Schema::Loader::DBI>
=head1 AUTHOR
-Justin Hunter C<justin.d.hunter@gmail.com>
+See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
+
+=head1 LICENSE
+
+This library is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
=cut
1;
+# vim:et sts=4 sw=4 tw=0: