Merging branches/DBIx-Class-Schema-Loader-refactor back into trunk:
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI.pm
1 package DBIx::Class::Schema::Loader::DBI;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class::Schema::Loader::Base Class::Accessor::Fast/;
6 use Class::C3;
7 use Carp;
8 use UNIVERSAL::require;
9
10 =head1 NAME
11
12 DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation.
13
14 =head1 SYNOPSIS
15
16 See L<DBIx::Class::Schema::Loader::Base>
17
18 =head1 DESCRIPTION
19
20 This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for
21 DBI-based storage backends, and implements the common functionality between them.
22
23 See L<DBIx::Class::Schema::Loader::Base> for the available options.
24
25 =head1 METHODS
26
27 =head2 new
28
29 Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific
30 things.
31
32 =cut
33
34 sub new {
35     my $self = shift->next::method(@_);
36
37     # rebless to vendor-specific class if it exists and loads
38     my $dbh = $self->schema->storage->dbh;
39     my $driver = $dbh->{Driver}->{Name};
40     my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
41     $subclass->require;
42     if($@ && $@ !~ /^Can't locate /) {
43         die "Failed to require $subclass: $@";
44     }
45     elsif(!$@) {
46         bless $self, "DBIx::Class::Schema::Loader::DBI::${driver}";
47     }
48
49     # Set up the default quoting character and name seperators
50     $self->{_quoter} = $self->schema->storage->sql_maker->quote_char
51                     || $dbh->get_info(29)
52                     || q{"};
53
54     $self->{_namesep} = $self->schema->storage->sql_maker->name_sep
55                      || $dbh->get_info(41)
56                      || q{.};
57
58     # For our usage as regex matches, concatenating multiple quoter
59     # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
60     if( ref $self->{_quoter} eq 'ARRAY') {
61         $self->{_quoter} = join(q{}, @{$self->{_quoter}});
62     }
63
64     $self->_setup;
65
66     $self;
67 }
68
69 # Override this in vendor modules to do things at the end of ->new()
70 sub _setup { }
71
72 # Returns an array of table names
73 sub _tables_list { 
74     my $self = shift;
75
76     my $dbh = $self->schema->storage->dbh;
77     my @tables = $dbh->tables(undef, $self->db_schema, '%', '%');
78     s/\Q$self->{_quoter}\E//g for @tables;
79     s/^.*\Q$self->{_namesep}\E// for @tables;
80
81     return @tables;
82 }
83
84 # Returns an arrayref of column names
85 sub _table_columns {
86     my ($self, $table) = @_;
87
88     my $dbh = $self->schema->storage->dbh;
89
90     if($self->{db_schema}) {
91         $table = $self->{db_schema} . $self->{_namesep} . $table;
92     }
93
94     my $sth = $dbh->prepare("SELECT * FROM $table WHERE 1=0");
95     $sth->execute;
96     return \@{$sth->{NAME_lc}};
97 }
98
99 # Returns arrayref of pk col names
100 sub _table_pk_info { 
101     my ( $self, $table ) = @_;
102
103     my $dbh = $self->schema->storage->dbh;
104
105     my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
106     s/\Q$self->{_quoter}\E//g for @primary;
107
108     return \@primary;
109 }
110
111 # Override this for uniq info
112 sub _table_uniq_info {
113     warn "No UNIQUE information can be gathered for this vendor";
114     return [];
115 }
116
117 # Find relationships
118 sub _table_fk_info {
119     my ($self, $table) = @_;
120
121     my $dbh = $self->schema->storage->dbh;
122     my $sth = $dbh->foreign_key_info( '', '', '', '',
123         $self->db_schema, $table );
124     return [] if !$sth;
125
126     my %rels;
127
128     my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
129     while(my $raw_rel = $sth->fetchrow_arrayref) {
130         my $uk_tbl  = $raw_rel->[2];
131         my $uk_col  = lc $raw_rel->[3];
132         my $fk_col  = lc $raw_rel->[7];
133         my $relid   = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
134         $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
135         $uk_col =~ s/\Q$self->{_quoter}\E//g;
136         $fk_col =~ s/\Q$self->{_quoter}\E//g;
137         $relid  =~ s/\Q$self->{_quoter}\E//g;
138         $rels{$relid}->{tbl} = $uk_tbl;
139         $rels{$relid}->{cols}->{$uk_col} = $fk_col;
140     }
141
142     my @rels;
143     foreach my $relid (keys %rels) {
144         push(@rels, {
145             remote_columns => [ keys   %{$rels{$relid}->{cols}} ],
146             local_columns  => [ values %{$rels{$relid}->{cols}} ],
147             remote_table   => $rels{$relid}->{tbl},
148         });
149     }
150
151     return \@rels;
152 }
153
154 =head1 SEE ALSO
155
156 L<DBIx::Class::Schema::Loader>
157
158 =cut
159
160 1;