release 0.07003
[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 Carp::Clan qw/^DBIx::Class/;
10 use mro 'c3';
11
12 our $VERSION = '0.07003';
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
44 sub rescan {
45     my ($self, $schema) = @_;
46
47     $schema->storage->disconnect if $schema->storage;
48     $self->next::method($schema);
49 }
50
51 # A hack so that qualify_objects can be tested on SQLite, SQLite does not
52 # actually have schemas.
53 {
54     sub _table_as_sql {
55         my $self = shift;
56         local $self->{db_schema};
57         return $self->next::method(@_);
58     }
59
60     sub _table_pk_info {
61         my $self = shift;
62         local $self->{db_schema};
63         return $self->next::method(@_);
64     }
65 }
66
67 sub _columns_info_for {
68     my $self = shift;
69     my ($table) = @_;
70
71     my $result = $self->next::method(@_);
72
73     my $dbh = $self->schema->storage->dbh;
74     local $dbh->{FetchHashKeyName} = 'NAME_lc';
75
76     my $sth = $dbh->prepare(
77       "pragma table_info(" . $dbh->quote_identifier($table) . ")"
78     );
79     $sth->execute;
80     my $cols = $sth->fetchall_hashref('name');
81
82     my ($num_pk, $pk_col) = (0);
83     # SQLite doesn't give us the info we need to do this nicely :(
84     # If there is exactly one column marked PK, and its type is integer,
85     # set it is_auto_increment. This isn't 100%, but it's better than the
86     # alternatives.
87     while (my ($col_name, $info) = each %$result) {
88       if ($cols->{$col_name}{pk}) {
89         $num_pk ++;
90         if (lc($cols->{$col_name}{type}) eq 'integer') {
91           $pk_col = $col_name;
92         }
93       }
94     }
95
96     while (my ($col, $info) = each %$result) {
97         if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') {
98             ${ $info->{default_value} } = 'current_timestamp';
99         }
100         if ($num_pk == 1 and defined $pk_col and $pk_col eq $col) {
101           $info->{is_auto_increment} = 1;
102         }
103     }
104
105     return $result;
106 }
107
108 sub _table_fk_info {
109     my ($self, $table) = @_;
110
111     my $dbh = $self->schema->storage->dbh;
112     my $sth = $dbh->prepare(
113         "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")"
114     );
115     $sth->execute;
116
117     my @rels;
118     while (my $fk = $sth->fetchrow_hashref) {
119         my $rel = $rels[ $fk->{id} ] ||= {
120             local_columns => [],
121             remote_columns => undef,
122             remote_table => $fk->{table}
123         };
124
125         push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
126         push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
127         warn "This is supposed to be the same rel but remote_table changed from ",
128             $rel->{remote_table}, " to ", $fk->{table}
129             if $rel->{remote_table} ne $fk->{table};
130     }
131     $sth->finish;
132     return \@rels;
133 }
134
135 sub _table_uniq_info {
136     my ($self, $table) = @_;
137
138     my $dbh = $self->schema->storage->dbh;
139     my $sth = $dbh->prepare(
140         "pragma index_list(" . $dbh->quote($table) . ")"
141     );
142     $sth->execute;
143
144     my @uniqs;
145     while (my $idx = $sth->fetchrow_hashref) {
146         next unless $idx->{unique};
147
148         my $name = $idx->{name};
149
150         my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")");
151         $get_idx_sth->execute;
152         my @cols;
153         while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
154             push @cols, $self->_lc($idx_row->{name});
155         }
156         $get_idx_sth->finish;
157
158         # Rename because SQLite complains about sqlite_ prefixes on identifiers
159         # and ignores constraint names in DDL.
160         $name = (join '_', @cols) . '_unique';
161
162         push @uniqs, [ $name => \@cols ];
163     }
164     $sth->finish;
165     return \@uniqs;
166 }
167
168 sub _tables_list {
169     my ($self, $opts) = @_;
170
171     my $dbh = $self->schema->storage->dbh;
172     my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
173     $sth->execute;
174     my @tables;
175     while ( my $row = $sth->fetchrow_hashref ) {
176         next unless $row->{type} =~ /^(?:table|view)\z/i;
177         next if $row->{tbl_name} =~ /^sqlite_/;
178         push @tables, $row->{tbl_name};
179     }
180     $sth->finish;
181     return $self->_filter_tables(\@tables, $opts);
182 }
183
184 =head1 SEE ALSO
185
186 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
187 L<DBIx::Class::Schema::Loader::DBI>
188
189 =head1 AUTHOR
190
191 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
192
193 =head1 LICENSE
194
195 This library is free software; you can redistribute it and/or modify it under
196 the same terms as Perl itself.
197
198 =cut
199
200 1;