rels are still fucked in 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 _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_qualifier}.'_'.$row->{fktable_owner}.'_'
98 .$row->{fktable_name}.'_'.$row->{fkcolumn_name};
99
100         push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
101         push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
102         $remote_table->{$fk} = $row->{pktable_name};
103     }
104
105     foreach my $fk (keys %$remote_table) {
106         push @rels, {
107                      local_columns => \@{$local_cols->{$fk}},
108                      remote_columns => \@{$remote_cols->{$fk}},
109                      remote_table => $remote_table->{$fk},
110                     };
111
112     }
113     return \@rels;
114 }
115
116 sub _table_uniq_info {
117     my ($self, $table) = @_;
118
119     my $dbh = $self->schema->storage->dbh;
120     my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname='$table', \@nomsg='nomsg'});
121     $sth->execute;
122
123     my $constraints;
124     while (my $row = $sth->fetchrow_hashref) {
125         if (exists $row->{constraint_type}) {
126             my $type = $row->{constraint_type} || '';
127             if ($type =~ /^unique/i) {
128                 my $name = $row->{constraint_name};
129                 push @{$constraints->{$name}},
130                     ( split /,/, $row->{constraint_keys} );
131             }
132         } else {
133             my $def = $row->{definition} || next;
134             next unless $def =~ /^unique/i;
135             my $name = $row->{name};
136             my ($keys) = $def =~ /\((.*)\)/;
137             $keys =~ s/\s*//g;
138             my @keys = split /,/ => $keys;
139             push @{$constraints->{$name}}, @keys;
140         }
141     }
142
143     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
144     return \@uniqs;
145 }
146
147 sub _extra_column_info {
148     my ($self, $info) = @_;
149     my %extra_info;
150
151     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
152
153     my $dbh = $self->schema->storage->dbh;
154     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'});
155     $sth->execute();
156
157     if ($sth->fetchrow_array) {
158         $extra_info{is_auto_increment} = 1;
159     }
160
161     return \%extra_info;
162 }
163
164 =head1 SEE ALSO
165
166 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
167 L<DBIx::Class::Schema::Loader::DBI>
168
169 =head1 AUTHOR
170
171 Justin Hunter C<justin.d.hunter@gmail.com>
172
173 =cut
174
175 1;