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 { |
29 | my $self = shift; |
fe67d343 |
30 | |
acfcc1fb |
31 | my $dbh = $self->schema->storage->dbh; |
32 | my $sth = $dbh->prepare(<<'EOF'); |
33 | select t.table_name |
34 | from information_schema.tables t |
35 | where t.table_schema = ? |
36 | EOF |
37 | $sth->execute($self->db_schema); |
fe67d343 |
38 | |
acfcc1fb |
39 | my @tables = map lc $_, map @$_, @{ $sth->fetchall_arrayref }; |
40 | |
41 | return $self->_filter_tables(@tables); |
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 | |
62 | my ($local_cols, $remote_cols, $remote_table, @rels); |
63 | my $dbh = $self->schema->storage->dbh; |
64 | my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'}); |
65 | $sth->execute; |
66 | |
67 | while (my $row = $sth->fetchrow_hashref) { |
65f74457 |
68 | my $fk = $row->{FK_NAME}; |
69 | push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME}; |
70 | push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME}; |
71 | $remote_table->{$fk} = $row->{PKTABLE_NAME}; |
fe67d343 |
72 | } |
73 | |
74 | foreach my $fk (keys %$remote_table) { |
65f74457 |
75 | push @rels, { |
76 | local_columns => \@{$local_cols->{$fk}}, |
77 | remote_columns => \@{$remote_cols->{$fk}}, |
78 | remote_table => $remote_table->{$fk}, |
79 | }; |
fe67d343 |
80 | |
81 | } |
82 | return \@rels; |
83 | } |
84 | |
85 | sub _table_uniq_info { |
86 | my ($self, $table) = @_; |
87 | |
88 | my $dbh = $self->schema->storage->dbh; |
89 | my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU |
90 | JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME) |
91 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME) |
772367d3 |
92 | WHERE CCU.TABLE_NAME = @{[ $dbh->quote($table) ]} AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION}); |
fe67d343 |
93 | $sth->execute; |
94 | my $constraints; |
95 | while (my $row = $sth->fetchrow_hashref) { |
96 | my $name = lc $row->{CONSTRAINT_NAME}; |
97 | my $col = lc $row->{COLUMN_NAME}; |
98 | push @{$constraints->{$name}}, $col; |
99 | } |
100 | |
101 | my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints; |
102 | return \@uniqs; |
103 | } |
104 | |
105 | sub _extra_column_info { |
45be2ce7 |
106 | my ($self, $table, $column, $info, $dbi_info) = @_; |
fe67d343 |
107 | my %extra_info; |
108 | |
fe67d343 |
109 | my $dbh = $self->schema->storage->dbh; |
772367d3 |
110 | my $sth = $dbh->prepare(qq{ |
111 | SELECT COLUMN_NAME |
112 | FROM INFORMATION_SCHEMA.COLUMNS |
113 | WHERE COLUMNPROPERTY(object_id(@{[ $dbh->quote($table) ]}, 'U'), '$column', 'IsIdentity') = 1 |
114 | AND TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]} |
115 | }); |
fe67d343 |
116 | $sth->execute(); |
117 | |
118 | if ($sth->fetchrow_array) { |
119 | $extra_info{is_auto_increment} = 1; |
120 | } |
121 | |
5c6fb0a1 |
122 | # get default |
123 | $sth = $dbh->prepare(qq{ |
124 | SELECT COLUMN_DEFAULT |
125 | FROM INFORMATION_SCHEMA.COLUMNS |
772367d3 |
126 | WHERE TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]} |
5c6fb0a1 |
127 | }); |
128 | $sth->execute; |
129 | my ($default) = $sth->fetchrow_array; |
130 | |
131 | if (defined $default) { |
132 | # strip parens |
133 | $default =~ s/^\( (.*) \)\z/$1/x; |
134 | |
8823ddfc |
135 | # Literal strings are in ''s, numbers are in ()s (in some versions of |
136 | # MSSQL, in others they are unquoted) everything else is a function. |
5c6fb0a1 |
137 | $extra_info{default_value} = |
8823ddfc |
138 | $default =~ /^['(] (.*) [)']\z/x ? $1 : |
139 | $default =~ /^\d/ ? $default : \$default; |
5c6fb0a1 |
140 | } |
141 | |
fe67d343 |
142 | return \%extra_info; |
143 | } |
144 | |
fe67d343 |
145 | =head1 SEE ALSO |
146 | |
acfcc1fb |
147 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>, |
148 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>, |
fe67d343 |
149 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
150 | L<DBIx::Class::Schema::Loader::DBI> |
151 | |
152 | =head1 AUTHOR |
153 | |
9cc8e7e1 |
154 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
fe67d343 |
155 | |
be80bba7 |
156 | =head1 LICENSE |
0852b7b8 |
157 | |
be80bba7 |
158 | This library is free software; you can redistribute it and/or modify it under |
159 | the same terms as Perl itself. |
0852b7b8 |
160 | |
fe67d343 |
161 | =cut |
162 | |
163 | 1; |