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