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 | |
9990e58f |
13 | our $VERSION = '0.07000'; |
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 | |
007e3511 |
52 | sub _columns_info_for { |
53 | my $self = shift; |
54 | my ($table) = @_; |
55 | |
56 | my $result = $self->next::method(@_); |
a8df0345 |
57 | |
dca5cd02 |
58 | my $dbh = $self->schema->storage->dbh; |
007e3511 |
59 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; |
60 | |
a7ada784 |
61 | my $has_autoinc = eval { |
45be2ce7 |
62 | my $get_seq = $self->{_cache}{sqlite_sequence} |
dca5cd02 |
63 | ||= $dbh->prepare(q{SELECT count(*) FROM sqlite_sequence WHERE name = ?}); |
64 | $get_seq->execute($table); |
a7ada784 |
65 | my ($ret) = $get_seq->fetchrow_array; |
66 | $get_seq->finish; |
67 | $ret; |
dca5cd02 |
68 | }; |
dca5cd02 |
69 | |
a7ada784 |
70 | if (!$@ && $has_autoinc) { |
dca5cd02 |
71 | my $sth = $dbh->prepare( |
72 | "pragma table_info(" . $dbh->quote_identifier($table) . ")" |
73 | ); |
74 | $sth->execute; |
75 | my $cols = $sth->fetchall_hashref('name'); |
007e3511 |
76 | |
77 | while (my ($col_name, $info) = each %$result) { |
78 | if ($cols->{$col_name}{pk}) { |
79 | $info->{is_auto_increment} = 1; |
80 | } |
81 | } |
82 | } |
83 | |
84 | while (my ($col, $info) = each %$result) { |
85 | if (eval { ${ $info->{default_value} } eq 'CURRENT_TIMESTAMP' }) { |
86 | ${ $info->{default_value} } = 'current_timestamp'; |
dca5cd02 |
87 | } |
a8df0345 |
88 | } |
89 | |
007e3511 |
90 | return $result; |
996be9ee |
91 | } |
92 | |
93 | sub _table_fk_info { |
94 | my ($self, $table) = @_; |
95 | |
dca5cd02 |
96 | my $dbh = $self->schema->storage->dbh; |
97 | my $sth = $dbh->prepare( |
98 | "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")" |
99 | ); |
100 | $sth->execute; |
996be9ee |
101 | |
dca5cd02 |
102 | my @rels; |
103 | while (my $fk = $sth->fetchrow_hashref) { |
104 | my $rel = $rels[ $fk->{id} ] ||= { |
105 | local_columns => [], |
106 | remote_columns => undef, |
bc1cb85e |
107 | remote_table => $fk->{table} |
dca5cd02 |
108 | }; |
109 | |
bc1cb85e |
110 | push @{ $rel->{local_columns} }, $self->_lc($fk->{from}); |
111 | push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to}; |
dca5cd02 |
112 | warn "This is supposed to be the same rel but remote_table changed from ", |
113 | $rel->{remote_table}, " to ", $fk->{table} |
bc1cb85e |
114 | if $rel->{remote_table} ne $fk->{table}; |
dca5cd02 |
115 | } |
116 | $sth->finish; |
117 | return \@rels; |
996be9ee |
118 | } |
119 | |
120 | sub _table_uniq_info { |
121 | my ($self, $table) = @_; |
122 | |
dca5cd02 |
123 | my $dbh = $self->schema->storage->dbh; |
124 | my $sth = $dbh->prepare( |
125 | "pragma index_list(" . $dbh->quote($table) . ")" |
126 | ); |
127 | $sth->execute; |
996be9ee |
128 | |
dca5cd02 |
129 | my @uniqs; |
130 | while (my $idx = $sth->fetchrow_hashref) { |
131 | next unless $idx->{unique}; |
132 | my $name = $idx->{name}; |
133 | |
134 | my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")"); |
135 | $get_idx_sth->execute; |
136 | my @cols; |
137 | while (my $idx_row = $get_idx_sth->fetchrow_hashref) { |
bc1cb85e |
138 | push @cols, $self->_lc($idx_row->{name}); |
dca5cd02 |
139 | } |
140 | $get_idx_sth->finish; |
141 | push @uniqs, [ $name => \@cols ]; |
142 | } |
143 | $sth->finish; |
144 | return \@uniqs; |
996be9ee |
145 | } |
146 | |
147 | sub _tables_list { |
bfb43060 |
148 | my ($self, $opts) = @_; |
5223f24a |
149 | |
996be9ee |
150 | my $dbh = $self->schema->storage->dbh; |
5223f24a |
151 | my $sth = $dbh->prepare("SELECT * FROM sqlite_master"); |
996be9ee |
152 | $sth->execute; |
153 | my @tables; |
154 | while ( my $row = $sth->fetchrow_hashref ) { |
26da4cc3 |
155 | next unless $row->{type} =~ /^(?:table|view)\z/i; |
522ee84e |
156 | next if $row->{tbl_name} =~ /^sqlite_/; |
996be9ee |
157 | push @tables, $row->{tbl_name}; |
158 | } |
3b7f80f9 |
159 | $sth->finish; |
bfb43060 |
160 | return $self->_filter_tables(\@tables, $opts); |
996be9ee |
161 | } |
162 | |
163 | =head1 SEE ALSO |
164 | |
165 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
166 | L<DBIx::Class::Schema::Loader::DBI> |
167 | |
be80bba7 |
168 | =head1 AUTHOR |
169 | |
9cc8e7e1 |
170 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
171 | |
172 | =head1 LICENSE |
173 | |
174 | This library is free software; you can redistribute it and/or modify it under |
175 | the same terms as Perl itself. |
176 | |
996be9ee |
177 | =cut |
178 | |
179 | 1; |