Do SQLite loading without any hairy regexes.
[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.05003';
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, $sth, $col_num) = @_;
54     ($table, $col_name) = @{$table}{qw/TABLE_NAME COLUMN_NAME/} if ref $table;
55     my %extra_info;
56
57     my $dbh = $self->schema->storage->dbh;
58     my $get_seq;
59     eval {
60       $get_seq = $self->{_cache}->{sqlite_sequence}
61         ||= $dbh->prepare(q{SELECT count(*) FROM sqlite_sequence WHERE name = ?});
62       $get_seq->execute($table);
63     };
64     return {} if $@;
65     my ($has_autoinc) = $get_seq->fetchrow_array;
66     $get_seq->finish;
67
68     if ($has_autoinc) {
69         my $sth = $dbh->prepare(
70             "pragma table_info(" . $dbh->quote_identifier($table) . ")"
71         );
72         $sth->execute;
73         my $cols = $sth->fetchall_hashref('name');
74         if ($cols->{$col_name}{pk}) {
75             $extra_info{is_auto_increment} = 1;
76         }
77     }
78
79     return \%extra_info;
80 }
81
82 sub _table_fk_info {
83     my ($self, $table) = @_;
84
85     my $dbh = $self->schema->storage->dbh;
86     my $sth = $dbh->prepare(
87         "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")"
88     );
89     $sth->execute;
90
91     my @rels;
92     while (my $fk = $sth->fetchrow_hashref) {
93         my $rel = $rels[ $fk->{id} ] ||= {
94             local_columns => [],
95             remote_columns => undef,
96             remote_table => lc $fk->{table}
97         };
98
99         push @{ $rel->{local_columns} }, lc $fk->{from};
100         push @{ $rel->{remote_columns} }, lc $fk->{to} if defined $fk->{to};
101         warn "This is supposed to be the same rel but remote_table changed from ",
102             $rel->{remote_table}, " to ", $fk->{table}
103             if $rel->{remote_table} ne lc $fk->{table};
104     }
105     $sth->finish;
106     return \@rels;
107 }
108
109 sub _table_uniq_info {
110     my ($self, $table) = @_;
111
112     my $dbh = $self->schema->storage->dbh;
113     my $sth = $dbh->prepare(
114         "pragma index_list(" . $dbh->quote($table) . ")"
115     );
116     $sth->execute;
117
118     my @uniqs;
119     while (my $idx = $sth->fetchrow_hashref) {
120         next unless $idx->{unique};
121         my $name = $idx->{name};
122
123         my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")");
124         $get_idx_sth->execute;
125         my @cols;
126         while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
127             push @cols, lc $idx_row->{name};
128         }
129         $get_idx_sth->finish;
130         push @uniqs, [ $name => \@cols ];
131     }
132     $sth->finish;
133     return \@uniqs;
134 }
135
136 sub _tables_list {
137     my $self = shift;
138
139     my $dbh = $self->schema->storage->dbh;
140     my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
141     $sth->execute;
142     my @tables;
143     while ( my $row = $sth->fetchrow_hashref ) {
144         next unless lc( $row->{type} ) eq 'table';
145         next if $row->{tbl_name} =~ /^sqlite_/;
146         push @tables, $row->{tbl_name};
147     }
148     $sth->finish;
149     return @tables;
150 }
151
152 =head1 SEE ALSO
153
154 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
155 L<DBIx::Class::Schema::Loader::DBI>
156
157 =head1 AUTHOR
158
159 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
160
161 =head1 LICENSE
162
163 This library is free software; you can redistribute it and/or modify it under
164 the same terms as Perl itself.
165
166 =cut
167
168 1;