Commit | Line | Data |
996be9ee |
1 | package DBIx::Class::Schema::Loader::DBI::SQLite; |
2 | |
3 | use strict; |
4 | use warnings; |
893c0d45 |
5 | use base qw/ |
6 | DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault |
7 | DBIx::Class::Schema::Loader::DBI |
8 | /; |
fa994d3c |
9 | use Carp::Clan qw/^DBIx::Class/; |
996be9ee |
10 | use Text::Balanced qw( extract_bracketed ); |
25328cc4 |
11 | use Class::C3; |
996be9ee |
12 | |
f3cfe4e6 |
13 | our $VERSION = '0.08000'; |
32f784fc |
14 | |
996be9ee |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation. |
18 | |
996be9ee |
19 | =head1 DESCRIPTION |
20 | |
bc1cb85e |
21 | See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>. |
996be9ee |
22 | |
3b7f80f9 |
23 | =head1 METHODS |
24 | |
25 | =head2 rescan |
26 | |
bc1cb85e |
27 | SQLite will fail all further commands on a connection if the underlying schema |
28 | has been modified. Therefore, any runtime changes requiring C<rescan> also |
29 | require us to re-connect to the database. The C<rescan> method here handles |
30 | that reconnection for you, but beware that this must occur for any other open |
31 | sqlite connections as well. |
3b7f80f9 |
32 | |
996be9ee |
33 | =cut |
34 | |
bc1cb85e |
35 | sub _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 |
45 | sub 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 |
68 | sub _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 | |
692404d1 |
83 | my ($num_pk, $pk_col) = (0); |
84 | # SQLite doesn't give us the info we need to do this nicely :( |
85 | # If there is exactly one column marked PK, and its type is integer, |
86 | # set it is_auto_increment. This isn't 100%, but it's better than the |
87 | # alternatives. |
97ab24bc |
88 | while (my ($col_name, $info) = each %$result) { |
692404d1 |
89 | if ($cols->{$col_name}{pk}) { |
90 | $num_pk ++; |
91 | if (lc($cols->{$col_name}{type}) eq 'integer') { |
92 | $pk_col = $col_name; |
93 | } |
97ab24bc |
94 | } |
007e3511 |
95 | } |
96 | |
97 | while (my ($col, $info) = each %$result) { |
268cc246 |
98 | if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') { |
007e3511 |
99 | ${ $info->{default_value} } = 'current_timestamp'; |
dca5cd02 |
100 | } |
692404d1 |
101 | if ($num_pk == 1 and defined $pk_col and $pk_col eq $col) { |
102 | $info->{is_auto_increment} = 1; |
103 | } |
a8df0345 |
104 | } |
105 | |
007e3511 |
106 | return $result; |
996be9ee |
107 | } |
108 | |
109 | sub _table_fk_info { |
110 | my ($self, $table) = @_; |
111 | |
dca5cd02 |
112 | my $dbh = $self->schema->storage->dbh; |
113 | my $sth = $dbh->prepare( |
114 | "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")" |
115 | ); |
116 | $sth->execute; |
996be9ee |
117 | |
dca5cd02 |
118 | my @rels; |
119 | while (my $fk = $sth->fetchrow_hashref) { |
120 | my $rel = $rels[ $fk->{id} ] ||= { |
121 | local_columns => [], |
122 | remote_columns => undef, |
bc1cb85e |
123 | remote_table => $fk->{table} |
dca5cd02 |
124 | }; |
125 | |
bc1cb85e |
126 | push @{ $rel->{local_columns} }, $self->_lc($fk->{from}); |
127 | push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to}; |
dca5cd02 |
128 | warn "This is supposed to be the same rel but remote_table changed from ", |
129 | $rel->{remote_table}, " to ", $fk->{table} |
bc1cb85e |
130 | if $rel->{remote_table} ne $fk->{table}; |
dca5cd02 |
131 | } |
132 | $sth->finish; |
133 | return \@rels; |
996be9ee |
134 | } |
135 | |
136 | sub _table_uniq_info { |
137 | my ($self, $table) = @_; |
138 | |
dca5cd02 |
139 | my $dbh = $self->schema->storage->dbh; |
140 | my $sth = $dbh->prepare( |
141 | "pragma index_list(" . $dbh->quote($table) . ")" |
142 | ); |
143 | $sth->execute; |
996be9ee |
144 | |
dca5cd02 |
145 | my @uniqs; |
146 | while (my $idx = $sth->fetchrow_hashref) { |
147 | next unless $idx->{unique}; |
4a1323d2 |
148 | |
dca5cd02 |
149 | my $name = $idx->{name}; |
150 | |
151 | my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")"); |
152 | $get_idx_sth->execute; |
153 | my @cols; |
154 | while (my $idx_row = $get_idx_sth->fetchrow_hashref) { |
bc1cb85e |
155 | push @cols, $self->_lc($idx_row->{name}); |
dca5cd02 |
156 | } |
157 | $get_idx_sth->finish; |
4a1323d2 |
158 | |
159 | # Rename because SQLite complains about sqlite_ prefixes on identifiers |
160 | # and ignores constraint names in DDL. |
161 | $name = (join '_', @cols) . '_unique'; |
162 | |
dca5cd02 |
163 | push @uniqs, [ $name => \@cols ]; |
164 | } |
165 | $sth->finish; |
166 | return \@uniqs; |
996be9ee |
167 | } |
168 | |
169 | sub _tables_list { |
bfb43060 |
170 | my ($self, $opts) = @_; |
5223f24a |
171 | |
996be9ee |
172 | my $dbh = $self->schema->storage->dbh; |
5223f24a |
173 | my $sth = $dbh->prepare("SELECT * FROM sqlite_master"); |
996be9ee |
174 | $sth->execute; |
175 | my @tables; |
176 | while ( my $row = $sth->fetchrow_hashref ) { |
26da4cc3 |
177 | next unless $row->{type} =~ /^(?:table|view)\z/i; |
522ee84e |
178 | next if $row->{tbl_name} =~ /^sqlite_/; |
996be9ee |
179 | push @tables, $row->{tbl_name}; |
180 | } |
3b7f80f9 |
181 | $sth->finish; |
bfb43060 |
182 | return $self->_filter_tables(\@tables, $opts); |
996be9ee |
183 | } |
184 | |
185 | =head1 SEE ALSO |
186 | |
187 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
188 | L<DBIx::Class::Schema::Loader::DBI> |
189 | |
be80bba7 |
190 | =head1 AUTHOR |
191 | |
9cc8e7e1 |
192 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
193 | |
194 | =head1 LICENSE |
195 | |
196 | This library is free software; you can redistribute it and/or modify it under |
197 | the same terms as Perl itself. |
198 | |
996be9ee |
199 | =cut |
200 | |
201 | 1; |