Commit | Line | Data |
fe67d343 |
1 | package DBIx::Class::Schema::Loader::DBI::MSSQL; |
2 | |
3 | use strict; |
4 | use warnings; |
de82711a |
5 | use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common'; |
fe67d343 |
6 | use Carp::Clan qw/^DBIx::Class/; |
7 | use Class::C3; |
8 | |
e42ec4ef |
9 | our $VERSION = '0.05003'; |
fe67d343 |
10 | |
11 | =head1 NAME |
12 | |
13 | DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation. |
14 | |
acfcc1fb |
15 | =head1 DESCRIPTION |
fe67d343 |
16 | |
acfcc1fb |
17 | Base driver for Microsoft SQL Server, used by |
18 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server> for support |
19 | via L<DBD::Sybase> and |
20 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server> for support via |
21 | L<DBD::ODBC>. |
fe67d343 |
22 | |
acfcc1fb |
23 | See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for |
24 | usage information. |
fe67d343 |
25 | |
acfcc1fb |
26 | =cut |
fe67d343 |
27 | |
acfcc1fb |
28 | sub _tables_list { |
bfb43060 |
29 | my ($self, $opts) = @_; |
fe67d343 |
30 | |
acfcc1fb |
31 | my $dbh = $self->schema->storage->dbh; |
32 | my $sth = $dbh->prepare(<<'EOF'); |
020f3c3a |
33 | SELECT t.table_name |
34 | FROM information_schema.tables t |
43170884 |
35 | WHERE lower(t.table_schema) = ? |
acfcc1fb |
36 | EOF |
43170884 |
37 | $sth->execute(lc $self->db_schema); |
fe67d343 |
38 | |
bfb43060 |
39 | my @tables = map @$_, @{ $sth->fetchall_arrayref }; |
acfcc1fb |
40 | |
bfb43060 |
41 | return $self->_filter_tables(\@tables, $opts); |
acfcc1fb |
42 | } |
fe67d343 |
43 | |
fe67d343 |
44 | sub _table_pk_info { |
45 | my ($self, $table) = @_; |
46 | my $dbh = $self->schema->storage->dbh; |
47 | my $sth = $dbh->prepare(qq{sp_pkeys '$table'}); |
48 | $sth->execute; |
49 | |
50 | my @keydata; |
51 | |
52 | while (my $row = $sth->fetchrow_hashref) { |
65f74457 |
53 | push @keydata, lc $row->{COLUMN_NAME}; |
fe67d343 |
54 | } |
55 | |
56 | return \@keydata; |
57 | } |
58 | |
59 | sub _table_fk_info { |
60 | my ($self, $table) = @_; |
61 | |
a3e05a4b |
62 | my ($local_cols, $remote_cols, $remote_table, @rels, $sth); |
fe67d343 |
63 | my $dbh = $self->schema->storage->dbh; |
a3e05a4b |
64 | eval { |
ea8d5d7c |
65 | $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'}); |
a3e05a4b |
66 | $sth->execute; |
67 | }; |
fe67d343 |
68 | |
a3e05a4b |
69 | while (my $row = eval { $sth->fetchrow_hashref }) { |
65f74457 |
70 | my $fk = $row->{FK_NAME}; |
71 | push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME}; |
72 | push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME}; |
bfb43060 |
73 | $remote_table->{$fk} = $row->{PKTABLE_NAME}; |
fe67d343 |
74 | } |
75 | |
76 | foreach my $fk (keys %$remote_table) { |
65f74457 |
77 | push @rels, { |
78 | local_columns => \@{$local_cols->{$fk}}, |
79 | remote_columns => \@{$remote_cols->{$fk}}, |
80 | remote_table => $remote_table->{$fk}, |
81 | }; |
fe67d343 |
82 | |
83 | } |
84 | return \@rels; |
85 | } |
86 | |
87 | sub _table_uniq_info { |
88 | my ($self, $table) = @_; |
89 | |
90 | my $dbh = $self->schema->storage->dbh; |
020f3c3a |
91 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; |
92 | |
93 | my $sth = $dbh->prepare(qq{ |
94 | SELECT ccu.constraint_name, ccu.column_name |
95 | FROM information_schema.constraint_column_usage ccu |
96 | JOIN information_schema.table_constraints tc on (ccu.constraint_name = tc.constraint_name) |
97 | JOIN information_schema.key_column_usage kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name) |
bfb43060 |
98 | wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position |
020f3c3a |
99 | }); |
fe67d343 |
100 | $sth->execute; |
101 | my $constraints; |
102 | while (my $row = $sth->fetchrow_hashref) { |
020f3c3a |
103 | my $name = $row->{constraint_name}; |
104 | my $col = lc $row->{column_name}; |
fe67d343 |
105 | push @{$constraints->{$name}}, $col; |
106 | } |
107 | |
108 | my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints; |
109 | return \@uniqs; |
110 | } |
111 | |
9c9197d6 |
112 | sub _columns_info_for { |
113 | my $self = shift; |
114 | my ($table) = @_; |
115 | |
116 | my $result = $self->next::method(@_); |
117 | |
118 | while (my ($col, $info) = each %$result) { |
119 | my $dbh = $self->schema->storage->dbh; |
020f3c3a |
120 | |
9c9197d6 |
121 | my $sth = $dbh->prepare(qq{ |
020f3c3a |
122 | SELECT column_name |
123 | FROM information_schema.columns |
bfb43060 |
124 | WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1 |
125 | AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]} |
9c9197d6 |
126 | }); |
020f3c3a |
127 | if (eval { $sth->execute; $sth->fetchrow_array }) { |
9c9197d6 |
128 | $info->{is_auto_increment} = 1; |
129 | $info->{data_type} =~ s/\s*identity//i; |
130 | delete $info->{size}; |
131 | } |
fe67d343 |
132 | |
5c6fb0a1 |
133 | # get default |
9c9197d6 |
134 | $sth = $dbh->prepare(qq{ |
020f3c3a |
135 | SELECT column_default |
136 | FROM information_schema.columns |
bfb43060 |
137 | wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]} |
9c9197d6 |
138 | }); |
020f3c3a |
139 | my ($default) = eval { $sth->execute; $sth->fetchrow_array }; |
9c9197d6 |
140 | |
141 | if (defined $default) { |
142 | # strip parens |
143 | $default =~ s/^\( (.*) \)\z/$1/x; |
144 | |
145 | # Literal strings are in ''s, numbers are in ()s (in some versions of |
146 | # MSSQL, in others they are unquoted) everything else is a function. |
147 | $info->{default_value} = |
148 | $default =~ /^['(] (.*) [)']\z/x ? $1 : |
149 | $default =~ /^\d/ ? $default : \$default; |
150 | } |
5c6fb0a1 |
151 | } |
152 | |
9c9197d6 |
153 | return $result; |
fe67d343 |
154 | } |
155 | |
fe67d343 |
156 | =head1 SEE ALSO |
157 | |
acfcc1fb |
158 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>, |
159 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>, |
fe67d343 |
160 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
161 | L<DBIx::Class::Schema::Loader::DBI> |
162 | |
163 | =head1 AUTHOR |
164 | |
9cc8e7e1 |
165 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
fe67d343 |
166 | |
be80bba7 |
167 | =head1 LICENSE |
0852b7b8 |
168 | |
be80bba7 |
169 | This library is free software; you can redistribute it and/or modify it under |
170 | the same terms as Perl itself. |
0852b7b8 |
171 | |
fe67d343 |
172 | =cut |
173 | |
174 | 1; |
bfb43060 |
175 | # vim:et sts=4 sw=4 tw=0: |