Commit failing test for composite PKs get marked is_auto_increment
[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/;
fa994d3c 9use Carp::Clan qw/^DBIx::Class/;
996be9ee 10use Text::Balanced qw( extract_bracketed );
25328cc4 11use Class::C3;
996be9ee 12
6b1d4f76 13our $VERSION = '0.07001';
32f784fc 14
996be9ee 15=head1 NAME
16
17DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation.
18
996be9ee 19=head1 DESCRIPTION
20
bc1cb85e 21See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
996be9ee 22
3b7f80f9 23=head1 METHODS
24
25=head2 rescan
26
bc1cb85e 27SQLite will fail all further commands on a connection if the underlying schema
28has been modified. Therefore, any runtime changes requiring C<rescan> also
29require us to re-connect to the database. The C<rescan> method here handles
30that reconnection for you, but beware that this must occur for any other open
31sqlite connections as well.
3b7f80f9 32
996be9ee 33=cut
34
bc1cb85e 35sub _setup {
36 my $self = shift;
37
38 $self->next::method(@_);
39
40 if (not defined $self->preserve_case) {
41 $self->preserve_case(0);
42 }
43}
44
3b7f80f9 45sub rescan {
46 my ($self, $schema) = @_;
47
48 $schema->storage->disconnect if $schema->storage;
49 $self->next::method($schema);
50}
51
69219349 52# A hack so that qualify_objects can be tested on SQLite, SQLite does not
53# actually have schemas.
54{
55 sub _table_as_sql {
56 my $self = shift;
57 local $self->{db_schema};
58 return $self->next::method(@_);
59 }
60
61 sub _table_pk_info {
62 my $self = shift;
63 local $self->{db_schema};
64 return $self->next::method(@_);
65 }
66}
67
007e3511 68sub _columns_info_for {
69 my $self = shift;
70 my ($table) = @_;
71
72 my $result = $self->next::method(@_);
a8df0345 73
dca5cd02 74 my $dbh = $self->schema->storage->dbh;
007e3511 75 local $dbh->{FetchHashKeyName} = 'NAME_lc';
76
97ab24bc 77 my $sth = $dbh->prepare(
78 "pragma table_info(" . $dbh->quote_identifier($table) . ")"
79 );
80 $sth->execute;
81 my $cols = $sth->fetchall_hashref('name');
82
83 while (my ($col_name, $info) = each %$result) {
84 if ($cols->{$col_name}{pk} && lc($cols->{$col_name}{type}) eq 'integer') {
85 $info->{is_auto_increment} = 1;
86 }
007e3511 87 }
88
89 while (my ($col, $info) = each %$result) {
268cc246 90 if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') {
007e3511 91 ${ $info->{default_value} } = 'current_timestamp';
dca5cd02 92 }
a8df0345 93 }
94
007e3511 95 return $result;
996be9ee 96}
97
98sub _table_fk_info {
99 my ($self, $table) = @_;
100
dca5cd02 101 my $dbh = $self->schema->storage->dbh;
102 my $sth = $dbh->prepare(
103 "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")"
104 );
105 $sth->execute;
996be9ee 106
dca5cd02 107 my @rels;
108 while (my $fk = $sth->fetchrow_hashref) {
109 my $rel = $rels[ $fk->{id} ] ||= {
110 local_columns => [],
111 remote_columns => undef,
bc1cb85e 112 remote_table => $fk->{table}
dca5cd02 113 };
114
bc1cb85e 115 push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
116 push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
dca5cd02 117 warn "This is supposed to be the same rel but remote_table changed from ",
118 $rel->{remote_table}, " to ", $fk->{table}
bc1cb85e 119 if $rel->{remote_table} ne $fk->{table};
dca5cd02 120 }
121 $sth->finish;
122 return \@rels;
996be9ee 123}
124
125sub _table_uniq_info {
126 my ($self, $table) = @_;
127
dca5cd02 128 my $dbh = $self->schema->storage->dbh;
129 my $sth = $dbh->prepare(
130 "pragma index_list(" . $dbh->quote($table) . ")"
131 );
132 $sth->execute;
996be9ee 133
dca5cd02 134 my @uniqs;
135 while (my $idx = $sth->fetchrow_hashref) {
136 next unless $idx->{unique};
4a1323d2 137
dca5cd02 138 my $name = $idx->{name};
139
140 my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")");
141 $get_idx_sth->execute;
142 my @cols;
143 while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
bc1cb85e 144 push @cols, $self->_lc($idx_row->{name});
dca5cd02 145 }
146 $get_idx_sth->finish;
4a1323d2 147
148 # Rename because SQLite complains about sqlite_ prefixes on identifiers
149 # and ignores constraint names in DDL.
150 $name = (join '_', @cols) . '_unique';
151
dca5cd02 152 push @uniqs, [ $name => \@cols ];
153 }
154 $sth->finish;
155 return \@uniqs;
996be9ee 156}
157
158sub _tables_list {
bfb43060 159 my ($self, $opts) = @_;
5223f24a 160
996be9ee 161 my $dbh = $self->schema->storage->dbh;
5223f24a 162 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
996be9ee 163 $sth->execute;
164 my @tables;
165 while ( my $row = $sth->fetchrow_hashref ) {
26da4cc3 166 next unless $row->{type} =~ /^(?:table|view)\z/i;
522ee84e 167 next if $row->{tbl_name} =~ /^sqlite_/;
996be9ee 168 push @tables, $row->{tbl_name};
169 }
3b7f80f9 170 $sth->finish;
bfb43060 171 return $self->_filter_tables(\@tables, $opts);
996be9ee 172}
173
174=head1 SEE ALSO
175
176L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
177L<DBIx::Class::Schema::Loader::DBI>
178
be80bba7 179=head1 AUTHOR
180
9cc8e7e1 181See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
be80bba7 182
183=head1 LICENSE
184
185This library is free software; you can redistribute it and/or modify it under
186the same terms as Perl itself.
187
996be9ee 188=cut
189
1901;