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