Firebird: mixed case support (wip)
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / MSSQL.pm
1 package DBIx::Class::Schema::Loader::DBI::MSSQL;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common';
6 use Carp::Clan qw/^DBIx::Class/;
7 use Class::C3;
8
9 our $VERSION = '0.05003';
10
11 =head1 NAME
12
13 DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
14
15 =head1 SYNOPSIS
16
17   package My::Schema;
18   use base qw/DBIx::Class::Schema::Loader/;
19
20   __PACKAGE__->loader_options( debug => 1 );
21
22   1;
23
24 =head1 DESCRIPTION
25
26 See L<DBIx::Class::Schema::Loader::Base>.
27
28 =cut
29
30 sub _table_pk_info {
31     my ($self, $table) = @_;
32     my $dbh = $self->schema->storage->dbh;
33     my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
34     $sth->execute;
35
36     my @keydata;
37
38     while (my $row = $sth->fetchrow_hashref) {
39         push @keydata, lc $row->{COLUMN_NAME};
40     }
41
42     return \@keydata;
43 }
44
45 sub _table_fk_info {
46     my ($self, $table) = @_;
47
48     my ($local_cols, $remote_cols, $remote_table, @rels);
49     my $dbh = $self->schema->storage->dbh;
50     my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
51     $sth->execute;
52
53     while (my $row = $sth->fetchrow_hashref) {
54         my $fk = $row->{FK_NAME};
55         push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
56         push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
57         $remote_table->{$fk} = $row->{PKTABLE_NAME};
58     }
59
60     foreach my $fk (keys %$remote_table) {
61         push @rels, {
62                       local_columns => \@{$local_cols->{$fk}},
63                       remote_columns => \@{$remote_cols->{$fk}},
64                       remote_table => $remote_table->{$fk},
65                     };
66
67     }
68     return \@rels;
69 }
70
71 sub _table_uniq_info {
72     my ($self, $table) = @_;
73
74     my $dbh = $self->schema->storage->dbh;
75     my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
76                                JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
77                                JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
78                                WHERE CCU.TABLE_NAME = @{[ $dbh->quote($table) ]} AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
79     $sth->execute;
80     my $constraints;
81     while (my $row = $sth->fetchrow_hashref) {
82         my $name = lc $row->{CONSTRAINT_NAME};
83         my $col  = lc $row->{COLUMN_NAME};
84         push @{$constraints->{$name}}, $col;
85     }
86
87     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
88     return \@uniqs;
89 }
90
91 sub _extra_column_info {
92     my ($self, $table, $column, $info, $dbi_info) = @_;
93     my %extra_info;
94
95     my $dbh = $self->schema->storage->dbh;
96     my $sth = $dbh->prepare(qq{
97         SELECT COLUMN_NAME 
98         FROM INFORMATION_SCHEMA.COLUMNS
99         WHERE COLUMNPROPERTY(object_id(@{[ $dbh->quote($table) ]}, 'U'), '$column', 'IsIdentity') = 1
100           AND TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]}
101     });
102     $sth->execute();
103
104     if ($sth->fetchrow_array) {
105         $extra_info{is_auto_increment} = 1;
106     }
107
108 # get default
109     $sth = $dbh->prepare(qq{
110         SELECT COLUMN_DEFAULT
111         FROM INFORMATION_SCHEMA.COLUMNS
112         WHERE TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]}
113     });
114     $sth->execute;
115     my ($default) = $sth->fetchrow_array;
116
117     if (defined $default) {
118         # strip parens
119         $default =~ s/^\( (.*) \)\z/$1/x;
120
121         # Literal strings are in ''s, numbers are in ()s (in some versions of
122         # MSSQL, in others they are unquoted) everything else is a function.
123         $extra_info{default_value} =
124             $default =~ /^['(] (.*) [)']\z/x ? $1 :
125                 $default =~ /^\d/ ? $default : \$default;
126     }
127
128     return \%extra_info;
129 }
130
131 =head1 SEE ALSO
132
133 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
134 L<DBIx::Class::Schema::Loader::DBI>
135
136 =head1 AUTHOR
137
138 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
139
140 =head1 LICENSE
141
142 This library is free software; you can redistribute it and/or modify it under
143 the same terms as Perl itself.
144
145 =cut
146
147 1;