release 0.07014
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / SQLite.pm
1 package DBIx::Class::Schema::Loader::DBI::SQLite;
2
3 use strict;
4 use warnings;
5 use base qw/
6     DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7     DBIx::Class::Schema::Loader::DBI
8 /;
9 use mro 'c3';
10 use DBIx::Class::Schema::Loader::Table ();
11
12 our $VERSION = '0.07014';
13
14 =head1 NAME
15
16 DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation.
17
18 =head1 DESCRIPTION
19
20 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
21
22 =head1 METHODS
23
24 =head2 rescan
25
26 SQLite will fail all further commands on a connection if the underlying schema
27 has been modified.  Therefore, any runtime changes requiring C<rescan> also
28 require us to re-connect to the database.  The C<rescan> method here handles
29 that reconnection for you, but beware that this must occur for any other open
30 sqlite connections as well.
31
32 =cut
33
34 sub _setup {
35     my $self = shift;
36
37     $self->next::method(@_);
38
39     if (not defined $self->preserve_case) {
40         $self->preserve_case(0);
41     }
42     
43     if ($self->db_schema) {
44         warn <<'EOF';
45 db_schema is not supported on SQLite, the option is implemented only for qualify_objects testing.
46 EOF
47         if ($self->db_schema->[0] eq '%') {
48             $self->db_schema(undef);
49         }
50     }
51 }
52
53 sub rescan {
54     my ($self, $schema) = @_;
55
56     $schema->storage->disconnect if $schema->storage;
57     $self->next::method($schema);
58 }
59
60 sub _columns_info_for {
61     my $self = shift;
62     my ($table) = @_;
63
64     my $result = $self->next::method(@_);
65
66     local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
67
68     my $sth = $self->dbh->prepare(
69       "pragma table_info(" . $self->dbh->quote_identifier($table) . ")"
70     );
71     $sth->execute;
72     my $cols = $sth->fetchall_hashref('name');
73
74     my ($num_pk, $pk_col) = (0);
75     # SQLite doesn't give us the info we need to do this nicely :(
76     # If there is exactly one column marked PK, and its type is integer,
77     # set it is_auto_increment. This isn't 100%, but it's better than the
78     # alternatives.
79     while (my ($col_name, $info) = each %$result) {
80       if ($cols->{$col_name}{pk}) {
81         $num_pk ++;
82         if (lc($cols->{$col_name}{type}) eq 'integer') {
83           $pk_col = $col_name;
84         }
85       }
86     }
87
88     while (my ($col, $info) = each %$result) {
89         if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') {
90             ${ $info->{default_value} } = 'current_timestamp';
91         }
92         if ($num_pk == 1 and defined $pk_col and $pk_col eq $col) {
93           $info->{is_auto_increment} = 1;
94         }
95     }
96
97     return $result;
98 }
99
100 sub _table_fk_info {
101     my ($self, $table) = @_;
102
103     my $sth = $self->dbh->prepare(
104         "pragma foreign_key_list(" . $self->dbh->quote_identifier($table) . ")"
105     );
106     $sth->execute;
107
108     my @rels;
109     while (my $fk = $sth->fetchrow_hashref) {
110         my $rel = $rels[ $fk->{id} ] ||= {
111             local_columns => [],
112             remote_columns => undef,
113             remote_table => DBIx::Class::Schema::Loader::Table->new(
114                 loader => $self,
115                 name   => $fk->{table},
116                 ($self->db_schema ? (
117                     schema        => $self->db_schema->[0],
118                     ignore_schema => 1,
119                 ) : ()),
120             ),
121         };
122
123         push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
124         push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
125         warn "This is supposed to be the same rel but remote_table changed from ",
126             $rel->{remote_table}->name, " to ", $fk->{table}
127             if $rel->{remote_table}->name ne $fk->{table};
128     }
129     $sth->finish;
130     return \@rels;
131 }
132
133 sub _table_uniq_info {
134     my ($self, $table) = @_;
135
136     my $sth = $self->dbh->prepare(
137         "pragma index_list(" . $self->dbh->quote($table) . ")"
138     );
139     $sth->execute;
140
141     my @uniqs;
142     while (my $idx = $sth->fetchrow_hashref) {
143         next unless $idx->{unique};
144
145         my $name = $idx->{name};
146
147         my $get_idx_sth = $self->dbh->prepare("pragma index_info(" . $self->dbh->quote($name) . ")");
148         $get_idx_sth->execute;
149         my @cols;
150         while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
151             push @cols, $self->_lc($idx_row->{name});
152         }
153         $get_idx_sth->finish;
154
155         # Rename because SQLite complains about sqlite_ prefixes on identifiers
156         # and ignores constraint names in DDL.
157         $name = (join '_', @cols) . '_unique';
158
159         push @uniqs, [ $name => \@cols ];
160     }
161     $sth->finish;
162     return \@uniqs;
163 }
164
165 sub _tables_list {
166     my ($self, $opts) = @_;
167
168     my $sth = $self->dbh->prepare("SELECT * FROM sqlite_master");
169     $sth->execute;
170     my @tables;
171     while ( my $row = $sth->fetchrow_hashref ) {
172         next unless $row->{type} =~ /^(?:table|view)\z/i;
173         next if $row->{tbl_name} =~ /^sqlite_/;
174         push @tables, DBIx::Class::Schema::Loader::Table->new(
175             loader => $self,
176             name   => $row->{tbl_name},
177             ($self->db_schema ? (
178                 schema        => $self->db_schema->[0],
179                 ignore_schema => 1, # for qualify_objects tests
180             ) : ()),
181         );
182     }
183     $sth->finish;
184     return $self->_filter_tables(\@tables, $opts);
185 }
186
187 =head1 SEE ALSO
188
189 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
190 L<DBIx::Class::Schema::Loader::DBI>
191
192 =head1 AUTHOR
193
194 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
195
196 =head1 LICENSE
197
198 This library is free software; you can redistribute it and/or modify it under
199 the same terms as Perl itself.
200
201 =cut
202
203 1;