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 | |
e42ec4ef |
13 | our $VERSION = '0.05003'; |
32f784fc |
14 | |
996be9ee |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation. |
18 | |
19 | =head1 SYNOPSIS |
20 | |
21 | package My::Schema; |
22 | use base qw/DBIx::Class::Schema::Loader/; |
23 | |
59cfa251 |
24 | __PACKAGE__->loader_options( debug => 1 ); |
996be9ee |
25 | |
26 | 1; |
27 | |
28 | =head1 DESCRIPTION |
29 | |
30 | See L<DBIx::Class::Schema::Loader::Base>. |
31 | |
3b7f80f9 |
32 | =head1 METHODS |
33 | |
34 | =head2 rescan |
35 | |
36 | SQLite will fail all further commands on a connection if the |
37 | underlying schema has been modified. Therefore, any runtime |
38 | changes requiring C<rescan> also require us to re-connect |
39 | to the database. The C<rescan> method here handles that |
40 | reconnection for you, but beware that this must occur for |
41 | any other open sqlite connections as well. |
42 | |
996be9ee |
43 | =cut |
44 | |
3b7f80f9 |
45 | sub rescan { |
46 | my ($self, $schema) = @_; |
47 | |
48 | $schema->storage->disconnect if $schema->storage; |
49 | $self->next::method($schema); |
50 | } |
51 | |
a8df0345 |
52 | sub _extra_column_info { |
78b7ccaa |
53 | my ($self, $table, $col_name, $sth, $col_num) = @_; |
632cb26f |
54 | ($table, $col_name) = @{$table}{qw/TABLE_NAME COLUMN_NAME/} if ref $table; |
a8df0345 |
55 | my %extra_info; |
56 | |
dca5cd02 |
57 | my $dbh = $self->schema->storage->dbh; |
a7ada784 |
58 | my $has_autoinc = eval { |
59 | my $get_seq = $self->{_cache}->{sqlite_sequence} |
dca5cd02 |
60 | ||= $dbh->prepare(q{SELECT count(*) FROM sqlite_sequence WHERE name = ?}); |
61 | $get_seq->execute($table); |
a7ada784 |
62 | my ($ret) = $get_seq->fetchrow_array; |
63 | $get_seq->finish; |
64 | $ret; |
dca5cd02 |
65 | }; |
dca5cd02 |
66 | |
a7ada784 |
67 | if (!$@ && $has_autoinc) { |
dca5cd02 |
68 | my $sth = $dbh->prepare( |
69 | "pragma table_info(" . $dbh->quote_identifier($table) . ")" |
70 | ); |
71 | $sth->execute; |
72 | my $cols = $sth->fetchall_hashref('name'); |
73 | if ($cols->{$col_name}{pk}) { |
74 | $extra_info{is_auto_increment} = 1; |
75 | } |
a8df0345 |
76 | } |
77 | |
78 | return \%extra_info; |
996be9ee |
79 | } |
80 | |
81 | sub _table_fk_info { |
82 | my ($self, $table) = @_; |
83 | |
dca5cd02 |
84 | my $dbh = $self->schema->storage->dbh; |
85 | my $sth = $dbh->prepare( |
86 | "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")" |
87 | ); |
88 | $sth->execute; |
996be9ee |
89 | |
dca5cd02 |
90 | my @rels; |
91 | while (my $fk = $sth->fetchrow_hashref) { |
92 | my $rel = $rels[ $fk->{id} ] ||= { |
93 | local_columns => [], |
94 | remote_columns => undef, |
95 | remote_table => lc $fk->{table} |
96 | }; |
97 | |
98 | push @{ $rel->{local_columns} }, lc $fk->{from}; |
99 | push @{ $rel->{remote_columns} }, lc $fk->{to} if defined $fk->{to}; |
100 | warn "This is supposed to be the same rel but remote_table changed from ", |
101 | $rel->{remote_table}, " to ", $fk->{table} |
102 | if $rel->{remote_table} ne lc $fk->{table}; |
103 | } |
104 | $sth->finish; |
105 | return \@rels; |
996be9ee |
106 | } |
107 | |
108 | sub _table_uniq_info { |
109 | my ($self, $table) = @_; |
110 | |
dca5cd02 |
111 | my $dbh = $self->schema->storage->dbh; |
112 | my $sth = $dbh->prepare( |
113 | "pragma index_list(" . $dbh->quote($table) . ")" |
114 | ); |
115 | $sth->execute; |
996be9ee |
116 | |
dca5cd02 |
117 | my @uniqs; |
118 | while (my $idx = $sth->fetchrow_hashref) { |
119 | next unless $idx->{unique}; |
120 | my $name = $idx->{name}; |
121 | |
122 | my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")"); |
123 | $get_idx_sth->execute; |
124 | my @cols; |
125 | while (my $idx_row = $get_idx_sth->fetchrow_hashref) { |
126 | push @cols, lc $idx_row->{name}; |
127 | } |
128 | $get_idx_sth->finish; |
129 | push @uniqs, [ $name => \@cols ]; |
130 | } |
131 | $sth->finish; |
132 | return \@uniqs; |
996be9ee |
133 | } |
134 | |
135 | sub _tables_list { |
136 | my $self = shift; |
5223f24a |
137 | |
996be9ee |
138 | my $dbh = $self->schema->storage->dbh; |
5223f24a |
139 | my $sth = $dbh->prepare("SELECT * FROM sqlite_master"); |
996be9ee |
140 | $sth->execute; |
141 | my @tables; |
142 | while ( my $row = $sth->fetchrow_hashref ) { |
143 | next unless lc( $row->{type} ) eq 'table'; |
522ee84e |
144 | next if $row->{tbl_name} =~ /^sqlite_/; |
996be9ee |
145 | push @tables, $row->{tbl_name}; |
146 | } |
3b7f80f9 |
147 | $sth->finish; |
0abc441b |
148 | return $self->_filter_tables(@tables); |
996be9ee |
149 | } |
150 | |
151 | =head1 SEE ALSO |
152 | |
153 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
154 | L<DBIx::Class::Schema::Loader::DBI> |
155 | |
be80bba7 |
156 | =head1 AUTHOR |
157 | |
9cc8e7e1 |
158 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
159 | |
160 | =head1 LICENSE |
161 | |
162 | This library is free software; you can redistribute it and/or modify it under |
163 | the same terms as Perl itself. |
164 | |
996be9ee |
165 | =cut |
166 | |
167 | 1; |