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