making some progress on Sybase
[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 qw/
6     DBIx::Class::Schema::Loader::DBI
7     DBIx::Class::Schema::Loader::DBI::Sybase::Common
8 /;
9 use Carp::Clan qw/^DBIx::Class/;
10 use Class::C3;
11
12 our $VERSION = '0.04999_06';
13
14 =head1 NAME
15
16 DBIx::Class::Schema::Loader::DBI::Sybase - DBIx::Class::Schema::Loader::DBI Sybase Implementation.
17
18 =head1 SYNOPSIS
19
20   package My::Schema;
21   use base qw/DBIx::Class::Schema::Loader/;
22
23   __PACKAGE__->loader_options( debug => 1 );
24
25   1;
26
27 =head1 DESCRIPTION
28
29 See L<DBIx::Class::Schema::Loader::Base>.
30
31 =cut
32
33 sub _setup {
34     my $self = shift;
35
36     $self->next::method(@_);
37     $self->{db_schema} ||= $self->_build_db_schema;
38     $self->_set_quote_char_and_name_sep;
39 }
40
41 sub _rebless {
42     my $self = shift;
43
44     my $dbh = $self->schema->storage->dbh;
45     my $DBMS_VERSION = @{$dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2];
46     if ($DBMS_VERSION =~ /^Microsoft /i) {
47         my $subclass = 'DBIx::Class::Schema::Loader::DBI::MSSQL';
48         if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
49             bless $self, $subclass;
50             $self->_rebless;
51       }
52     }
53 }
54
55 sub _table_columns {
56     my ($self, $table) = @_;
57
58     my $dbh = $self->schema->storage->dbh;
59     my $columns = $dbh->selectcol_arrayref(qq{SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = '$table' AND type = 'U')});
60
61     return $columns;
62 }
63
64 sub _table_pk_info {
65     my ($self, $table) = @_;
66
67     my $dbh = $self->schema->storage->dbh;
68     my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
69     $sth->execute;
70
71     my @keydata;
72
73     while (my $row = $sth->fetchrow_hashref) {
74         push @keydata, $row->{column_name};
75     }
76
77     return \@keydata;
78 }
79
80 sub _table_fk_info {
81     my ($self, $table) = @_;
82
83     my ($local_cols, $remote_cols, $remote_table, @rels);
84     my $dbh = $self->schema->storage->dbh;
85     # hide "Object does not exist in this database." when trying to fetch fkeys
86     $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; }; 
87     my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
88     $sth->execute;
89
90     while (my $row = $sth->fetchrow_hashref) {
91         next unless $row->{FK_NAME};
92         my $fk = $row->{FK_NAME};
93         push @{$local_cols->{$fk}}, $row->{FKCOLUMN_NAME};
94         push @{$remote_cols->{$fk}}, $row->{PKCOLUMN_NAME};
95         $remote_table->{$fk} = $row->{PKTABLE_NAME};
96     }
97
98     foreach my $fk (keys %$remote_table) {
99         push @rels, {
100                      local_columns => \@{$local_cols->{$fk}},
101                      remote_columns => \@{$remote_cols->{$fk}},
102                      remote_table => $remote_table->{$fk},
103                     };
104
105     }
106     return \@rels;
107 }
108
109 sub _table_uniq_info {
110     my ($self, $table) = @_;
111
112     my $dbh = $self->schema->storage->dbh;
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         if (exists $row->{constraint_type}) {
119             my $type = $row->{constraint_type} || '';
120             if ($type =~ /^unique/i) {
121                 my $name = $row->{constraint_name};
122                 push @{$constraints->{$name}},
123                     ( split /,/, $row->{constraint_keys} );
124             }
125         } else {
126             my $def = $row->{definition} || next;
127             next unless $def =~ /^unique/i;
128             my $name = $row->{name};
129             my ($keys) = $def =~ /\((.*)\)/;
130             $keys =~ s/\s*//g;
131             my @keys = split /,/ => $keys;
132             push @{$constraints->{$name}}, @keys;
133         }
134     }
135
136     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
137     return \@uniqs;
138 }
139
140 sub _extra_column_info {
141     my ($self, $info) = @_;
142     my %extra_info;
143
144     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
145
146     my $dbh = $self->schema->storage->dbh;
147     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'});
148     $sth->execute();
149
150     if ($sth->fetchrow_array) {
151         $extra_info{is_auto_increment} = 1;
152     }
153
154     return \%extra_info;
155 }
156
157 =head1 SEE ALSO
158
159 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
160 L<DBIx::Class::Schema::Loader::DBI>
161
162 =head1 AUTHOR
163
164 Justin Hunter C<justin.d.hunter@gmail.com>
165
166 =cut
167
168 1;