Firebird: mixed case support (wip)
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / MSSQL.pm
CommitLineData
fe67d343 1package DBIx::Class::Schema::Loader::DBI::MSSQL;
2
3use strict;
4use warnings;
de82711a 5use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common';
fe67d343 6use Carp::Clan qw/^DBIx::Class/;
7use Class::C3;
8
e42ec4ef 9our $VERSION = '0.05003';
fe67d343 10
11=head1 NAME
12
13DBIx::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
26See L<DBIx::Class::Schema::Loader::Base>.
27
28=cut
29
fe67d343 30sub _table_pk_info {
31 my ($self, $table) = @_;
32 my $dbh = $self->schema->storage->dbh;
33 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
34 $sth->execute;
35
36 my @keydata;
37
38 while (my $row = $sth->fetchrow_hashref) {
65f74457 39 push @keydata, lc $row->{COLUMN_NAME};
fe67d343 40 }
41
42 return \@keydata;
43}
44
45sub _table_fk_info {
46 my ($self, $table) = @_;
47
48 my ($local_cols, $remote_cols, $remote_table, @rels);
49 my $dbh = $self->schema->storage->dbh;
50 my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
51 $sth->execute;
52
53 while (my $row = $sth->fetchrow_hashref) {
65f74457 54 my $fk = $row->{FK_NAME};
55 push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
56 push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
57 $remote_table->{$fk} = $row->{PKTABLE_NAME};
fe67d343 58 }
59
60 foreach my $fk (keys %$remote_table) {
65f74457 61 push @rels, {
62 local_columns => \@{$local_cols->{$fk}},
63 remote_columns => \@{$remote_cols->{$fk}},
64 remote_table => $remote_table->{$fk},
65 };
fe67d343 66
67 }
68 return \@rels;
69}
70
71sub _table_uniq_info {
72 my ($self, $table) = @_;
73
74 my $dbh = $self->schema->storage->dbh;
75 my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
76 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
77 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
772367d3 78 WHERE CCU.TABLE_NAME = @{[ $dbh->quote($table) ]} AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
fe67d343 79 $sth->execute;
80 my $constraints;
81 while (my $row = $sth->fetchrow_hashref) {
82 my $name = lc $row->{CONSTRAINT_NAME};
83 my $col = lc $row->{COLUMN_NAME};
84 push @{$constraints->{$name}}, $col;
85 }
86
87 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
88 return \@uniqs;
89}
90
91sub _extra_column_info {
45be2ce7 92 my ($self, $table, $column, $info, $dbi_info) = @_;
fe67d343 93 my %extra_info;
94
fe67d343 95 my $dbh = $self->schema->storage->dbh;
772367d3 96 my $sth = $dbh->prepare(qq{
97 SELECT COLUMN_NAME
98 FROM INFORMATION_SCHEMA.COLUMNS
99 WHERE COLUMNPROPERTY(object_id(@{[ $dbh->quote($table) ]}, 'U'), '$column', 'IsIdentity') = 1
100 AND TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]}
101 });
fe67d343 102 $sth->execute();
103
104 if ($sth->fetchrow_array) {
105 $extra_info{is_auto_increment} = 1;
106 }
107
5c6fb0a1 108# get default
109 $sth = $dbh->prepare(qq{
110 SELECT COLUMN_DEFAULT
111 FROM INFORMATION_SCHEMA.COLUMNS
772367d3 112 WHERE TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]}
5c6fb0a1 113 });
114 $sth->execute;
115 my ($default) = $sth->fetchrow_array;
116
117 if (defined $default) {
118 # strip parens
119 $default =~ s/^\( (.*) \)\z/$1/x;
120
8823ddfc 121 # Literal strings are in ''s, numbers are in ()s (in some versions of
122 # MSSQL, in others they are unquoted) everything else is a function.
5c6fb0a1 123 $extra_info{default_value} =
8823ddfc 124 $default =~ /^['(] (.*) [)']\z/x ? $1 :
125 $default =~ /^\d/ ? $default : \$default;
5c6fb0a1 126 }
127
fe67d343 128 return \%extra_info;
129}
130
fe67d343 131=head1 SEE ALSO
132
133L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
134L<DBIx::Class::Schema::Loader::DBI>
135
136=head1 AUTHOR
137
9cc8e7e1 138See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
fe67d343 139
be80bba7 140=head1 LICENSE
0852b7b8 141
be80bba7 142This library is free software; you can redistribute it and/or modify it under
143the same terms as Perl itself.
0852b7b8 144
fe67d343 145=cut
146
1471;