Release 0.07047
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / SQLite.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI::SQLite;
2
3use strict;
4use warnings;
383bd2a8 5use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
942bd5e0 6use mro 'c3';
c4a69b87 7use DBIx::Class::Schema::Loader::Table ();
996be9ee 8
306bf770 9our $VERSION = '0.07047';
32f784fc 10
996be9ee 11=head1 NAME
12
13DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation.
14
996be9ee 15=head1 DESCRIPTION
16
bc1cb85e 17See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
996be9ee 18
3b7f80f9 19=head1 METHODS
20
21=head2 rescan
22
bc1cb85e 23SQLite will fail all further commands on a connection if the underlying schema
24has been modified. Therefore, any runtime changes requiring C<rescan> also
25require us to re-connect to the database. The C<rescan> method here handles
26that reconnection for you, but beware that this must occur for any other open
27sqlite connections as well.
3b7f80f9 28
996be9ee 29=cut
30
bc1cb85e 31sub _setup {
32 my $self = shift;
33
34 $self->next::method(@_);
35
36 if (not defined $self->preserve_case) {
37 $self->preserve_case(0);
38 }
3b61a7ca 39
c4a69b87 40 if ($self->db_schema) {
41 warn <<'EOF';
42db_schema is not supported on SQLite, the option is implemented only for qualify_objects testing.
43EOF
44 if ($self->db_schema->[0] eq '%') {
45 $self->db_schema(undef);
46 }
47 }
bc1cb85e 48}
49
3b7f80f9 50sub rescan {
51 my ($self, $schema) = @_;
52
53 $schema->storage->disconnect if $schema->storage;
54 $self->next::method($schema);
55}
56
007e3511 57sub _columns_info_for {
58 my $self = shift;
59 my ($table) = @_;
60
61 my $result = $self->next::method(@_);
a8df0345 62
c4a69b87 63 local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
007e3511 64
c4a69b87 65 my $sth = $self->dbh->prepare(
494e0205 66 "pragma table_info(" . $self->dbh->quote_identifier($table) . ")"
97ab24bc 67 );
68 $sth->execute;
69 my $cols = $sth->fetchall_hashref('name');
70
116431d6 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
692404d1 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.
97ab24bc 83 while (my ($col_name, $info) = each %$result) {
494e0205 84 if ($cols{$col_name}{pk}) {
85 $num_pk++;
86 if (lc($cols{$col_name}{type}) eq 'integer') {
87 $pk_col = $col_name;
88 }
692404d1 89 }
007e3511 90 }
91
92 while (my ($col, $info) = each %$result) {
268cc246 93 if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') {
007e3511 94 ${ $info->{default_value} } = 'current_timestamp';
dca5cd02 95 }
692404d1 96 if ($num_pk == 1 and defined $pk_col and $pk_col eq $col) {
494e0205 97 $info->{is_auto_increment} = 1;
692404d1 98 }
a8df0345 99 }
100
007e3511 101 return $result;
996be9ee 102}
103
104sub _table_fk_info {
105 my ($self, $table) = @_;
106
c4a69b87 107 my $sth = $self->dbh->prepare(
108 "pragma foreign_key_list(" . $self->dbh->quote_identifier($table) . ")"
dca5cd02 109 );
110 $sth->execute;
996be9ee 111
dca5cd02 112 my @rels;
113 while (my $fk = $sth->fetchrow_hashref) {
114 my $rel = $rels[ $fk->{id} ] ||= {
115 local_columns => [],
116 remote_columns => undef,
c4a69b87 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 ),
dca5cd02 125 };
126
bc1cb85e 127 push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
128 push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
3b61a7ca 129
130 $rel->{attrs} ||= {
131 on_delete => uc $fk->{on_delete},
132 on_update => uc $fk->{on_update},
133 };
134
dca5cd02 135 warn "This is supposed to be the same rel but remote_table changed from ",
c4a69b87 136 $rel->{remote_table}->name, " to ", $fk->{table}
137 if $rel->{remote_table}->name ne $fk->{table};
dca5cd02 138 }
139 $sth->finish;
3b61a7ca 140
141 # now we need to determine whether each FK is DEFERRABLE, this can only be
142 # done by parsing the DDL from sqlite_master
143
144 my $ddl = $self->dbh->selectcol_arrayref(<<"EOF", undef, $table->name, $table->name)->[0];
145select sql from sqlite_master
146where name = ? and tbl_name = ?
147EOF
148
149 foreach my $fk (@rels) {
add8bcf0 150 my $local_cols = '"?' . (join '"? \s* , \s* "?', map quotemeta, @{ $fk->{local_columns} }) . '"?';
151 my $remote_cols = '"?' . (join '"? \s* , \s* "?', map quotemeta, @{ $fk->{remote_columns} || [] }) . '"?';
3b61a7ca 152 my ($deferrable_clause) = $ddl =~ /
153 foreign \s+ key \s* \( \s* $local_cols \s* \) \s* references \s* (?:\S+|".+?(?<!")") \s*
154 (?:\( \s* $remote_cols \s* \) \s*)?
155 (?:(?:
494e0205 156 on \s+ (?:delete|update) \s+ (?:set \s+ null|set \s+ default|cascade|restrict|no \s+ action)
3b61a7ca 157 |
494e0205 158 match \s* (?:\S+|".+?(?<!")")
3b61a7ca 159 ) \s*)*
160 ((?:not)? \s* deferrable)?
161 /sxi;
162
163 if ($deferrable_clause) {
164 $fk->{attrs}{is_deferrable} = $deferrable_clause =~ /not/i ? 0 : 1;
165 }
166 else {
add8bcf0 167 # check for inline constraint if 1 local column
168 if (@{ $fk->{local_columns} } == 1) {
169 my ($local_col) = @{ $fk->{local_columns} };
170 my ($remote_col) = @{ $fk->{remote_columns} || [] };
171 $remote_col ||= '';
172
173 my ($deferrable_clause) = $ddl =~ /
174 "?\Q$local_col\E"? \s* (?:\w+\s*)* (?: \( \s* \d\+ (?:\s*,\s*\d+)* \s* \) )? \s*
d4b60b9a 175 references \s+ (?:\S+|".+?(?<!")") (?:\s* \( \s* "?\Q$remote_col\E"? \s* \))? \s*
add8bcf0 176 (?:(?:
177 on \s+ (?:delete|update) \s+ (?:set \s+ null|set \s+ default|cascade|restrict|no \s+ action)
178 |
179 match \s* (?:\S+|".+?(?<!")")
180 ) \s*)*
181 ((?:not)? \s* deferrable)?
182 /sxi;
183
184 if ($deferrable_clause) {
185 $fk->{attrs}{is_deferrable} = $deferrable_clause =~ /not/i ? 0 : 1;
186 }
187 else {
188 $fk->{attrs}{is_deferrable} = 0;
189 }
190 }
191 else {
192 $fk->{attrs}{is_deferrable} = 0;
193 }
3b61a7ca 194 }
195 }
196
dca5cd02 197 return \@rels;
996be9ee 198}
199
200sub _table_uniq_info {
201 my ($self, $table) = @_;
202
c4a69b87 203 my $sth = $self->dbh->prepare(
204 "pragma index_list(" . $self->dbh->quote($table) . ")"
dca5cd02 205 );
206 $sth->execute;
996be9ee 207
dca5cd02 208 my @uniqs;
209 while (my $idx = $sth->fetchrow_hashref) {
210 next unless $idx->{unique};
4a1323d2 211
dca5cd02 212 my $name = $idx->{name};
213
c4a69b87 214 my $get_idx_sth = $self->dbh->prepare("pragma index_info(" . $self->dbh->quote($name) . ")");
dca5cd02 215 $get_idx_sth->execute;
216 my @cols;
217 while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
bc1cb85e 218 push @cols, $self->_lc($idx_row->{name});
dca5cd02 219 }
220 $get_idx_sth->finish;
4a1323d2 221
222 # Rename because SQLite complains about sqlite_ prefixes on identifiers
223 # and ignores constraint names in DDL.
224 $name = (join '_', @cols) . '_unique';
225
dca5cd02 226 push @uniqs, [ $name => \@cols ];
227 }
228 $sth->finish;
6c4f5a4a 229 return [ sort { $a->[0] cmp $b->[0] } @uniqs ];
996be9ee 230}
231
232sub _tables_list {
5784b2b9 233 my ($self) = @_;
5223f24a 234
c4a69b87 235 my $sth = $self->dbh->prepare("SELECT * FROM sqlite_master");
996be9ee 236 $sth->execute;
237 my @tables;
238 while ( my $row = $sth->fetchrow_hashref ) {
26da4cc3 239 next unless $row->{type} =~ /^(?:table|view)\z/i;
522ee84e 240 next if $row->{tbl_name} =~ /^sqlite_/;
c4a69b87 241 push @tables, DBIx::Class::Schema::Loader::Table->new(
242 loader => $self,
243 name => $row->{tbl_name},
244 ($self->db_schema ? (
245 schema => $self->db_schema->[0],
246 ignore_schema => 1, # for qualify_objects tests
247 ) : ()),
248 );
996be9ee 249 }
3b7f80f9 250 $sth->finish;
5784b2b9 251 return $self->_filter_tables(\@tables);
996be9ee 252}
253
68c6c83f 254sub _table_info_matches {
255 my ($self, $table, $info) = @_;
256
257 my $table_schema = $table->schema;
258 $table_schema = 'main' if !defined $table_schema;
259 return $info->{TABLE_SCHEM} eq $table_schema
260 && $info->{TABLE_NAME} eq $table->name;
261}
262
996be9ee 263=head1 SEE ALSO
264
265L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
266L<DBIx::Class::Schema::Loader::DBI>
267
b87ab391 268=head1 AUTHORS
be80bba7 269
b87ab391 270See L<DBIx::Class::Schema::Loader/AUTHORS>.
be80bba7 271
272=head1 LICENSE
273
274This library is free software; you can redistribute it and/or modify it under
275the same terms as Perl itself.
276
996be9ee 277=cut
278
2791;
3b61a7ca 280# vim:et sts=4 sw=4 tw=0: