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