initial import of Sybase/MSSQL support
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Sybase.pm
1 package DBIx::Class::Schema::Loader::DBI::Sybase;
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::Sybase - DBIx::Class::Schema::Loader::DBI Sybase 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
30 sub _setup {
31     my $self = shift;
32
33     $self->next::method(@_);
34     $self->{db_schema} ||= 'dbo';
35 }
36
37 sub _rebless {
38     my $self = shift;
39
40     my $dbh = $self->schema->storage->dbh;
41     my $DBMS_VERSION = @{$dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2];
42     if ($DBMS_VERSION =~ /^Microsoft /i) {
43       my $subclass = 'DBIx::Class::Schema::Loader::DBI::MSSQL';
44       if ($self->load_optional_class($subclass)) {
45         bless $self, $subclass unless $self->isa($subclass);
46         $self->_rebless;
47       }
48     }
49 }
50
51 sub _table_columns {
52     my ($self, $table) = @_;
53
54     my $dbh = $self->schema->storage->dbh;
55     my $columns = $dbh->selectcol_arrayref(qq{SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = '$table' AND type = 'U')});
56
57     return $columns;
58 }
59
60 sub _table_pk_info {
61     my ($self, $table) = @_;
62
63     my $dbh = $self->schema->storage->dbh;
64     my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
65     $sth->execute;
66
67     my @keydata;
68
69     while (my $row = $sth->fetchrow_hashref) {
70       push @keydata, lc $row->{column_name};
71     }
72
73     return \@keydata;
74 }
75
76 sub _table_fk_info {
77     my ($self, $table) = @_;
78
79     my ($local_cols, $remote_cols, $remote_table, @rels);
80     my $dbh = $self->schema->storage->dbh;
81     # hide "Object does not exist in this database." when trying to fetch fkeys
82     $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; }; 
83     my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
84     $sth->execute;
85
86     while (my $row = $sth->fetchrow_hashref) {
87       next unless $row->{FK_NAME};
88       my $fk = $row->{FK_NAME};
89       push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
90       push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
91       $remote_table->{$fk} = $row->{PKTABLE_NAME};
92     }
93
94     foreach my $fk (keys %$remote_table) {
95       push @rels, {
96                     local_columns => \@{$local_cols->{$fk}},
97                     remote_columns => \@{$remote_cols->{$fk}},
98                     remote_table => $remote_table->{$fk},
99                   };
100
101     }
102     return \@rels;
103 }
104
105 sub _table_uniq_info {
106     my ($self, $table) = @_;
107
108     my $dbh = $self->schema->storage->dbh;
109 #    my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
110 #                               JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
111 #                               JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
112 #                               WHERE CCU.TABLE_NAME = '$table' AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
113     my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname='$table', \@nomsg='nomsg'});
114     $sth->execute;
115
116     my $constraints;
117     while (my $row = $sth->fetchrow_hashref) {
118         my $type = $row->{constraint_type} || '';
119         if ($type =~ /^unique/i) {
120           my $name = lc $row->{constraint_name};
121           push @{$constraints->{$name}}, ( split /,/, lc $row->{constraint_keys} );
122         }
123     }
124
125     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
126     return \@uniqs;
127 }
128
129 sub _extra_column_info {
130     my ($self, $info) = @_;
131     my %extra_info;
132
133     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
134
135     my $dbh = $self->schema->storage->dbh;
136     my $sth = $dbh->prepare(qq{SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = '$table') AND (status & 0x80) = 0x80 AND name = '$column'});
137     $sth->execute();
138
139     if ($sth->fetchrow_array) {
140         $extra_info{is_auto_increment} = 1;
141     }
142
143     return \%extra_info;
144 }
145
146 =head1 SEE ALSO
147
148 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
149 L<DBIx::Class::Schema::Loader::DBI>
150
151 =head1 AUTHOR
152
153 Justin Hunter C<justin.d.hunter@gmail.com>
154
155 =cut
156
157 1;