e08cb56d0757f3f285f64d6c99b86c40946e05b0
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / SQLite.pm
1 package DBIx::Class::Schema::Loader::DBI::SQLite;
2
3 use strict;
4 use warnings;
5 use base qw/
6     DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7     DBIx::Class::Schema::Loader::DBI
8 /;
9 use Carp::Clan qw/^DBIx::Class/;
10 use Text::Balanced qw( extract_bracketed );
11 use Class::C3;
12
13 our $VERSION = '0.07000';
14
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
24   __PACKAGE__->loader_options( debug => 1 );
25
26   1;
27
28 =head1 DESCRIPTION
29
30 See L<DBIx::Class::Schema::Loader::Base>.
31
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
43 =cut
44
45 sub rescan {
46     my ($self, $schema) = @_;
47
48     $schema->storage->disconnect if $schema->storage;
49     $self->next::method($schema);
50 }
51
52 sub _extra_column_info {
53     my ($self, $table, $col_name, $info, $dbi_info) = @_;
54     my %extra_info;
55
56     my $dbh = $self->schema->storage->dbh;
57     my $has_autoinc = eval {
58       my $get_seq = $self->{_cache}{sqlite_sequence}
59         ||= $dbh->prepare(q{SELECT count(*) FROM sqlite_sequence WHERE name = ?});
60       $get_seq->execute($table);
61       my ($ret) = $get_seq->fetchrow_array;
62       $get_seq->finish;
63       $ret;
64     };
65
66     if (!$@ && $has_autoinc) {
67         my $sth = $dbh->prepare(
68             "pragma table_info(" . $dbh->quote_identifier($table) . ")"
69         );
70         $sth->execute;
71         my $cols = $sth->fetchall_hashref('name');
72         if ($cols->{$col_name}{pk}) {
73             $extra_info{is_auto_increment} = 1;
74         }
75     }
76
77     return \%extra_info;
78 }
79
80 sub _table_fk_info {
81     my ($self, $table) = @_;
82
83     my $dbh = $self->schema->storage->dbh;
84     my $sth = $dbh->prepare(
85         "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")"
86     );
87     $sth->execute;
88
89     my @rels;
90     while (my $fk = $sth->fetchrow_hashref) {
91         my $rel = $rels[ $fk->{id} ] ||= {
92             local_columns => [],
93             remote_columns => undef,
94             remote_table => lc $fk->{table}
95         };
96
97         push @{ $rel->{local_columns} }, lc $fk->{from};
98         push @{ $rel->{remote_columns} }, lc $fk->{to} if defined $fk->{to};
99         warn "This is supposed to be the same rel but remote_table changed from ",
100             $rel->{remote_table}, " to ", $fk->{table}
101             if $rel->{remote_table} ne lc $fk->{table};
102     }
103     $sth->finish;
104     return \@rels;
105 }
106
107 sub _table_uniq_info {
108     my ($self, $table) = @_;
109
110     my $dbh = $self->schema->storage->dbh;
111     my $sth = $dbh->prepare(
112         "pragma index_list(" . $dbh->quote($table) . ")"
113     );
114     $sth->execute;
115
116     my @uniqs;
117     while (my $idx = $sth->fetchrow_hashref) {
118         next unless $idx->{unique};
119         my $name = $idx->{name};
120
121         my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")");
122         $get_idx_sth->execute;
123         my @cols;
124         while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
125             push @cols, lc $idx_row->{name};
126         }
127         $get_idx_sth->finish;
128         push @uniqs, [ $name => \@cols ];
129     }
130     $sth->finish;
131     return \@uniqs;
132 }
133
134 sub _tables_list {
135     my ($self, $opts) = @_;
136
137     my $dbh = $self->schema->storage->dbh;
138     my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
139     $sth->execute;
140     my @tables;
141     while ( my $row = $sth->fetchrow_hashref ) {
142         next unless $row->{type} =~ /^(?:table|view)\z/i;
143         next if $row->{tbl_name} =~ /^sqlite_/;
144         push @tables, $row->{tbl_name};
145     }
146     $sth->finish;
147     return $self->_filter_tables(\@tables, $opts);
148 }
149
150 =head1 SEE ALSO
151
152 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
153 L<DBIx::Class::Schema::Loader::DBI>
154
155 =head1 AUTHOR
156
157 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
158
159 =head1 LICENSE
160
161 This library is free software; you can redistribute it and/or modify it under
162 the same terms as Perl itself.
163
164 =cut
165
166 1;