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