sybase rels sort of work now, more than half-way through common tests
[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 _is_case_sensitive { 1 }
34
35 sub _setup {
36     my $self = shift;
37
38     $self->next::method(@_);
39     $self->{db_schema} ||= $self->_build_db_schema;
40     $self->_set_quote_char_and_name_sep;
41 }
42
43 sub _rebless {
44     my $self = shift;
45
46     my $dbh = $self->schema->storage->dbh;
47     my $DBMS_VERSION = @{$dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2];
48     if ($DBMS_VERSION =~ /^Microsoft /i) {
49         my $subclass = 'DBIx::Class::Schema::Loader::DBI::MSSQL';
50         if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
51             bless $self, $subclass;
52             $self->_rebless;
53       }
54     }
55 }
56
57 sub _table_columns {
58     my ($self, $table) = @_;
59
60     my $dbh = $self->schema->storage->dbh;
61     my $columns = $dbh->selectcol_arrayref(qq{SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = '$table' AND type = 'U')});
62
63     return $columns;
64 }
65
66 sub _table_pk_info {
67     my ($self, $table) = @_;
68
69     my $dbh = $self->schema->storage->dbh;
70     my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
71     $sth->execute;
72
73     my @keydata;
74
75     while (my $row = $sth->fetchrow_hashref) {
76         push @keydata, $row->{column_name};
77     }
78
79     return \@keydata;
80 }
81
82 sub _table_fk_info {
83     my ($self, $table) = @_;
84
85     my ($local_cols, $remote_cols, $remote_table, @rels);
86     my $dbh = $self->schema->storage->dbh;
87
88     local $dbh->{FetchHashKeyName} = 'NAME_lc';
89
90     # hide "Object does not exist in this database." when trying to fetch fkeys
91     $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; }; 
92     my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
93     $sth->execute;
94
95     while (my $row = $sth->fetchrow_hashref) {
96         my $fk = $row->{fk_name} ||
97 'fk_'.$row->{fktable_name}.'_'.$row->{pktable_name};
98
99         push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
100         push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
101         $remote_table->{$fk} = $row->{pktable_name};
102     }
103
104     foreach my $fk (keys %$remote_table) {
105         push @rels, {
106                      local_columns => \@{$local_cols->{$fk}},
107                      remote_columns => \@{$remote_cols->{$fk}},
108                      remote_table => $remote_table->{$fk},
109                     };
110
111     }
112     return \@rels;
113 }
114
115 sub _table_uniq_info {
116     my ($self, $table) = @_;
117
118     my $dbh = $self->schema->storage->dbh;
119     my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname='$table', \@nomsg='nomsg'});
120     $sth->execute;
121
122     my $constraints;
123     while (my $row = $sth->fetchrow_hashref) {
124         if (exists $row->{constraint_type}) {
125             my $type = $row->{constraint_type} || '';
126             if ($type =~ /^unique/i) {
127                 my $name = $row->{constraint_name};
128                 push @{$constraints->{$name}},
129                     ( split /,/, $row->{constraint_keys} );
130             }
131         } else {
132             my $def = $row->{definition} || next;
133             next unless $def =~ /^unique/i;
134             my $name = $row->{name};
135             my ($keys) = $def =~ /\((.*)\)/;
136             $keys =~ s/\s*//g;
137             my @keys = split /,/ => $keys;
138             push @{$constraints->{$name}}, @keys;
139         }
140     }
141
142     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
143     return \@uniqs;
144 }
145
146 sub _extra_column_info {
147     my ($self, $info) = @_;
148     my %extra_info;
149
150     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
151
152     my $dbh = $self->schema->storage->dbh;
153     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'});
154     $sth->execute();
155
156     if ($sth->fetchrow_array) {
157         $extra_info{is_auto_increment} = 1;
158     }
159
160     return \%extra_info;
161 }
162
163 =head1 SEE ALSO
164
165 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
166 L<DBIx::Class::Schema::Loader::DBI>
167
168 =head1 AUTHOR
169
170 Justin Hunter C<justin.d.hunter@gmail.com>
171
172 =cut
173
174 1;