added preserve_case option
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / SQLite.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI::SQLite;
2
3use strict;
4use warnings;
893c0d45 5use base qw/
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
8/;
fa994d3c 9use Carp::Clan qw/^DBIx::Class/;
996be9ee 10use Text::Balanced qw( extract_bracketed );
25328cc4 11use Class::C3;
996be9ee 12
9990e58f 13our $VERSION = '0.07000';
32f784fc 14
996be9ee 15=head1 NAME
16
17DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation.
18
996be9ee 19=head1 DESCRIPTION
20
bc1cb85e 21See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
996be9ee 22
3b7f80f9 23=head1 METHODS
24
25=head2 rescan
26
bc1cb85e 27SQLite will fail all further commands on a connection if the underlying schema
28has been modified. Therefore, any runtime changes requiring C<rescan> also
29require us to re-connect to the database. The C<rescan> method here handles
30that reconnection for you, but beware that this must occur for any other open
31sqlite connections as well.
3b7f80f9 32
996be9ee 33=cut
34
bc1cb85e 35sub _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 45sub rescan {
46 my ($self, $schema) = @_;
47
48 $schema->storage->disconnect if $schema->storage;
49 $self->next::method($schema);
50}
51
a8df0345 52sub _extra_column_info {
45be2ce7 53 my ($self, $table, $col_name, $info, $dbi_info) = @_;
a8df0345 54 my %extra_info;
55
dca5cd02 56 my $dbh = $self->schema->storage->dbh;
a7ada784 57 my $has_autoinc = eval {
45be2ce7 58 my $get_seq = $self->{_cache}{sqlite_sequence}
dca5cd02 59 ||= $dbh->prepare(q{SELECT count(*) FROM sqlite_sequence WHERE name = ?});
60 $get_seq->execute($table);
a7ada784 61 my ($ret) = $get_seq->fetchrow_array;
62 $get_seq->finish;
63 $ret;
dca5cd02 64 };
dca5cd02 65
a7ada784 66 if (!$@ && $has_autoinc) {
dca5cd02 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 }
a8df0345 75 }
76
77 return \%extra_info;
996be9ee 78}
79
80sub _table_fk_info {
81 my ($self, $table) = @_;
82
dca5cd02 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;
996be9ee 88
dca5cd02 89 my @rels;
90 while (my $fk = $sth->fetchrow_hashref) {
91 my $rel = $rels[ $fk->{id} ] ||= {
92 local_columns => [],
93 remote_columns => undef,
bc1cb85e 94 remote_table => $fk->{table}
dca5cd02 95 };
96
bc1cb85e 97 push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
98 push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
dca5cd02 99 warn "This is supposed to be the same rel but remote_table changed from ",
100 $rel->{remote_table}, " to ", $fk->{table}
bc1cb85e 101 if $rel->{remote_table} ne $fk->{table};
dca5cd02 102 }
103 $sth->finish;
104 return \@rels;
996be9ee 105}
106
107sub _table_uniq_info {
108 my ($self, $table) = @_;
109
dca5cd02 110 my $dbh = $self->schema->storage->dbh;
111 my $sth = $dbh->prepare(
112 "pragma index_list(" . $dbh->quote($table) . ")"
113 );
114 $sth->execute;
996be9ee 115
dca5cd02 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) {
bc1cb85e 125 push @cols, $self->_lc($idx_row->{name});
dca5cd02 126 }
127 $get_idx_sth->finish;
128 push @uniqs, [ $name => \@cols ];
129 }
130 $sth->finish;
131 return \@uniqs;
996be9ee 132}
133
134sub _tables_list {
bfb43060 135 my ($self, $opts) = @_;
5223f24a 136
996be9ee 137 my $dbh = $self->schema->storage->dbh;
5223f24a 138 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
996be9ee 139 $sth->execute;
140 my @tables;
141 while ( my $row = $sth->fetchrow_hashref ) {
26da4cc3 142 next unless $row->{type} =~ /^(?:table|view)\z/i;
522ee84e 143 next if $row->{tbl_name} =~ /^sqlite_/;
996be9ee 144 push @tables, $row->{tbl_name};
145 }
3b7f80f9 146 $sth->finish;
bfb43060 147 return $self->_filter_tables(\@tables, $opts);
996be9ee 148}
149
150=head1 SEE ALSO
151
152L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
153L<DBIx::Class::Schema::Loader::DBI>
154
be80bba7 155=head1 AUTHOR
156
9cc8e7e1 157See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
be80bba7 158
159=head1 LICENSE
160
161This library is free software; you can redistribute it and/or modify it under
162the same terms as Perl itself.
163
996be9ee 164=cut
165
1661;