1 package DBIx::Class::Schema::Loader::DBI::SQLite;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
9 use Carp::Clan qw/^DBIx::Class/;
10 use Text::Balanced qw( extract_bracketed );
13 our $VERSION = '0.07000';
17 DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation.
21 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
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.
38 $self->next::method(@_);
40 if (not defined $self->preserve_case) {
41 $self->preserve_case(0);
46 my ($self, $schema) = @_;
48 $schema->storage->disconnect if $schema->storage;
49 $self->next::method($schema);
52 sub _columns_info_for {
56 my $result = $self->next::method(@_);
58 my $dbh = $self->schema->storage->dbh;
59 local $dbh->{FetchHashKeyName} = 'NAME_lc';
61 my $has_autoinc = eval {
62 my $get_seq = $self->{_cache}{sqlite_sequence}
63 ||= $dbh->prepare(q{SELECT count(*) FROM sqlite_sequence WHERE name = ?});
64 $get_seq->execute($table);
65 my ($ret) = $get_seq->fetchrow_array;
70 if (!$@ && $has_autoinc) {
71 my $sth = $dbh->prepare(
72 "pragma table_info(" . $dbh->quote_identifier($table) . ")"
75 my $cols = $sth->fetchall_hashref('name');
77 while (my ($col_name, $info) = each %$result) {
78 if ($cols->{$col_name}{pk}) {
79 $info->{is_auto_increment} = 1;
84 while (my ($col, $info) = each %$result) {
85 if (eval { ${ $info->{default_value} } eq 'CURRENT_TIMESTAMP' }) {
86 ${ $info->{default_value} } = 'current_timestamp';
94 my ($self, $table) = @_;
96 my $dbh = $self->schema->storage->dbh;
97 my $sth = $dbh->prepare(
98 "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")"
103 while (my $fk = $sth->fetchrow_hashref) {
104 my $rel = $rels[ $fk->{id} ] ||= {
106 remote_columns => undef,
107 remote_table => $fk->{table}
110 push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
111 push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
112 warn "This is supposed to be the same rel but remote_table changed from ",
113 $rel->{remote_table}, " to ", $fk->{table}
114 if $rel->{remote_table} ne $fk->{table};
120 sub _table_uniq_info {
121 my ($self, $table) = @_;
123 my $dbh = $self->schema->storage->dbh;
124 my $sth = $dbh->prepare(
125 "pragma index_list(" . $dbh->quote($table) . ")"
130 while (my $idx = $sth->fetchrow_hashref) {
131 next unless $idx->{unique};
132 my $name = $idx->{name};
134 my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")");
135 $get_idx_sth->execute;
137 while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
138 push @cols, $self->_lc($idx_row->{name});
140 $get_idx_sth->finish;
141 push @uniqs, [ $name => \@cols ];
148 my ($self, $opts) = @_;
150 my $dbh = $self->schema->storage->dbh;
151 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
154 while ( my $row = $sth->fetchrow_hashref ) {
155 next unless $row->{type} =~ /^(?:table|view)\z/i;
156 next if $row->{tbl_name} =~ /^sqlite_/;
157 push @tables, $row->{tbl_name};
160 return $self->_filter_tables(\@tables, $opts);
165 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
166 L<DBIx::Class::Schema::Loader::DBI>
170 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
174 This library is free software; you can redistribute it and/or modify it under
175 the same terms as Perl itself.