Commit | Line | Data |
fe67d343 |
1 | package DBIx::Class::Schema::Loader::DBI::MSSQL; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base 'DBIx::Class::Schema::Loader::DBI'; |
6 | use Carp::Clan qw/^DBIx::Class/; |
7 | use Class::C3; |
8 | |
9 | our $VERSION = '0.04999_06'; |
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 | |
6ed0a90c |
30 | sub _rebless { |
31 | my $self = shift; |
32 | |
33 | $self->schema->storage->sql_maker->quote_char([qw/[ ]/]) |
34 | unless $self->schema->storage->sql_maker->quote_char; |
35 | |
36 | $self->schema->storage->sql_maker->name_sep('.') |
37 | unless $self->schema->storage->sql_maker->name_sep; |
38 | } |
39 | |
fe67d343 |
40 | sub _setup { |
41 | my $self = shift; |
42 | |
43 | $self->next::method(@_); |
76503d57 |
44 | |
45 | $self->{db_schema} ||= $self->_determine_db_schema; |
fe67d343 |
46 | } |
47 | |
76503d57 |
48 | sub _determine_db_schema { |
49 | my $self = shift; |
50 | my $dbh = $self->schema->storage->dbh; |
51 | |
52 | my $test_table = "_loader_test_$$"; |
53 | $dbh->do("create table $test_table (id integer)"); |
54 | |
55 | my $db_schema = 'dbo'; # default |
56 | |
57 | eval { |
58 | my $sth = $dbh->prepare('sp_tables'); |
59 | $sth->execute; |
60 | while (my $row = $sth->fetchrow_hashref) { |
61 | next unless $row->{TABLE_NAME} eq $test_table; |
62 | |
63 | $db_schema = $row->{TABLE_OWNER}; |
64 | last; |
65 | } |
66 | $sth->finish; |
67 | }; |
68 | my $exception = $@; |
69 | $dbh->do("drop table $test_table"); |
70 | croak $exception if $exception; |
71 | |
72 | return $db_schema; |
73 | } |
74 | |
75 | |
77d3753e |
76 | # DBD::Sybase doesn't implement get_info properly |
77 | #sub _build_quoter { [qw/[ ]/] } |
78 | sub _build_quoter { '"' } |
79 | sub _build_namesep { '.' } |
80 | |
fe67d343 |
81 | sub _table_pk_info { |
82 | my ($self, $table) = @_; |
83 | my $dbh = $self->schema->storage->dbh; |
84 | my $sth = $dbh->prepare(qq{sp_pkeys '$table'}); |
85 | $sth->execute; |
86 | |
87 | my @keydata; |
88 | |
89 | while (my $row = $sth->fetchrow_hashref) { |
65f74457 |
90 | push @keydata, lc $row->{COLUMN_NAME}; |
fe67d343 |
91 | } |
92 | |
93 | return \@keydata; |
94 | } |
95 | |
96 | sub _table_fk_info { |
97 | my ($self, $table) = @_; |
98 | |
99 | my ($local_cols, $remote_cols, $remote_table, @rels); |
100 | my $dbh = $self->schema->storage->dbh; |
101 | my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'}); |
102 | $sth->execute; |
103 | |
104 | while (my $row = $sth->fetchrow_hashref) { |
65f74457 |
105 | my $fk = $row->{FK_NAME}; |
106 | push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME}; |
107 | push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME}; |
108 | $remote_table->{$fk} = $row->{PKTABLE_NAME}; |
fe67d343 |
109 | } |
110 | |
111 | foreach my $fk (keys %$remote_table) { |
65f74457 |
112 | push @rels, { |
113 | local_columns => \@{$local_cols->{$fk}}, |
114 | remote_columns => \@{$remote_cols->{$fk}}, |
115 | remote_table => $remote_table->{$fk}, |
116 | }; |
fe67d343 |
117 | |
118 | } |
119 | return \@rels; |
120 | } |
121 | |
122 | sub _table_uniq_info { |
123 | my ($self, $table) = @_; |
124 | |
125 | my $dbh = $self->schema->storage->dbh; |
126 | my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU |
127 | JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME) |
128 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME) |
129 | WHERE CCU.TABLE_NAME = '$table' AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION}); |
130 | $sth->execute; |
131 | my $constraints; |
132 | while (my $row = $sth->fetchrow_hashref) { |
133 | my $name = lc $row->{CONSTRAINT_NAME}; |
134 | my $col = lc $row->{COLUMN_NAME}; |
135 | push @{$constraints->{$name}}, $col; |
136 | } |
137 | |
138 | my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints; |
139 | return \@uniqs; |
140 | } |
141 | |
142 | sub _extra_column_info { |
143 | my ($self, $info) = @_; |
144 | my %extra_info; |
145 | |
146 | my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/}; |
147 | |
148 | my $dbh = $self->schema->storage->dbh; |
149 | my $sth = $dbh->prepare(qq{SELECT COLUMN_NAME |
150 | FROM INFORMATION_SCHEMA.COLUMNS |
151 | WHERE COLUMNPROPERTY(object_id('$table', 'U'), '$column', 'IsIdentity') = 1 AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column' |
152 | }); |
153 | $sth->execute(); |
154 | |
155 | if ($sth->fetchrow_array) { |
156 | $extra_info{is_auto_increment} = 1; |
157 | } |
158 | |
159 | return \%extra_info; |
160 | } |
161 | |
162 | |
163 | =head1 SEE ALSO |
164 | |
165 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
166 | L<DBIx::Class::Schema::Loader::DBI> |
167 | |
168 | =head1 AUTHOR |
169 | |
170 | Justin Hunter C<justin.d.hunter@gmail.com> |
171 | |
172 | =cut |
173 | |
174 | 1; |