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 | |
af8f6279 |
45 | $self->{db_schema} ||= $self->_build_db_schema; |
fe67d343 |
46 | } |
47 | |
af8f6279 |
48 | sub _build_db_schema { |
76503d57 |
49 | my $self = shift; |
50 | my $dbh = $self->schema->storage->dbh; |
51 | |
52 | my $test_table = "_loader_test_$$"; |
76503d57 |
53 | |
54 | my $db_schema = 'dbo'; # default |
55 | |
56 | eval { |
af8f6279 |
57 | $dbh->do("create table $test_table (id integer)"); |
76503d57 |
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; |
af8f6279 |
67 | $dbh->do("drop table $test_table"); |
76503d57 |
68 | }; |
69 | my $exception = $@; |
af8f6279 |
70 | eval { $dbh->do("drop table $test_table") }; |
71 | carp "Could not determine db_schema, defaulting to $db_schema : $exception" |
72 | if $exception; |
76503d57 |
73 | |
74 | return $db_schema; |
75 | } |
76 | |
77 | |
77d3753e |
78 | # DBD::Sybase doesn't implement get_info properly |
79 | #sub _build_quoter { [qw/[ ]/] } |
80 | sub _build_quoter { '"' } |
81 | sub _build_namesep { '.' } |
82 | |
fe67d343 |
83 | sub _table_pk_info { |
84 | my ($self, $table) = @_; |
85 | my $dbh = $self->schema->storage->dbh; |
86 | my $sth = $dbh->prepare(qq{sp_pkeys '$table'}); |
87 | $sth->execute; |
88 | |
89 | my @keydata; |
90 | |
91 | while (my $row = $sth->fetchrow_hashref) { |
65f74457 |
92 | push @keydata, lc $row->{COLUMN_NAME}; |
fe67d343 |
93 | } |
94 | |
95 | return \@keydata; |
96 | } |
97 | |
98 | sub _table_fk_info { |
99 | my ($self, $table) = @_; |
100 | |
101 | my ($local_cols, $remote_cols, $remote_table, @rels); |
102 | my $dbh = $self->schema->storage->dbh; |
103 | my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'}); |
104 | $sth->execute; |
105 | |
106 | while (my $row = $sth->fetchrow_hashref) { |
65f74457 |
107 | my $fk = $row->{FK_NAME}; |
108 | push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME}; |
109 | push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME}; |
110 | $remote_table->{$fk} = $row->{PKTABLE_NAME}; |
fe67d343 |
111 | } |
112 | |
113 | foreach my $fk (keys %$remote_table) { |
65f74457 |
114 | push @rels, { |
115 | local_columns => \@{$local_cols->{$fk}}, |
116 | remote_columns => \@{$remote_cols->{$fk}}, |
117 | remote_table => $remote_table->{$fk}, |
118 | }; |
fe67d343 |
119 | |
120 | } |
121 | return \@rels; |
122 | } |
123 | |
124 | sub _table_uniq_info { |
125 | my ($self, $table) = @_; |
126 | |
127 | my $dbh = $self->schema->storage->dbh; |
128 | my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU |
129 | JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME) |
130 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME) |
131 | WHERE CCU.TABLE_NAME = '$table' AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION}); |
132 | $sth->execute; |
133 | my $constraints; |
134 | while (my $row = $sth->fetchrow_hashref) { |
135 | my $name = lc $row->{CONSTRAINT_NAME}; |
136 | my $col = lc $row->{COLUMN_NAME}; |
137 | push @{$constraints->{$name}}, $col; |
138 | } |
139 | |
140 | my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints; |
141 | return \@uniqs; |
142 | } |
143 | |
144 | sub _extra_column_info { |
145 | my ($self, $info) = @_; |
146 | my %extra_info; |
147 | |
148 | my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/}; |
149 | |
150 | my $dbh = $self->schema->storage->dbh; |
151 | my $sth = $dbh->prepare(qq{SELECT COLUMN_NAME |
152 | FROM INFORMATION_SCHEMA.COLUMNS |
153 | WHERE COLUMNPROPERTY(object_id('$table', 'U'), '$column', 'IsIdentity') = 1 AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column' |
154 | }); |
155 | $sth->execute(); |
156 | |
157 | if ($sth->fetchrow_array) { |
158 | $extra_info{is_auto_increment} = 1; |
159 | } |
160 | |
161 | return \%extra_info; |
162 | } |
163 | |
164 | |
165 | =head1 SEE ALSO |
166 | |
167 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
168 | L<DBIx::Class::Schema::Loader::DBI> |
169 | |
170 | =head1 AUTHOR |
171 | |
172 | Justin Hunter C<justin.d.hunter@gmail.com> |
173 | |
174 | =cut |
175 | |
176 | 1; |