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.07001';
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 # A hack so that qualify_objects can be tested on SQLite, SQLite does not
53 # actually have schemas.
57 local $self->{db_schema};
58 return $self->next::method(@_);
63 local $self->{db_schema};
64 return $self->next::method(@_);
68 sub _columns_info_for {
72 my $result = $self->next::method(@_);
74 my $dbh = $self->schema->storage->dbh;
75 local $dbh->{FetchHashKeyName} = 'NAME_lc';
77 my $sth = $dbh->prepare(
78 "pragma table_info(" . $dbh->quote_identifier($table) . ")"
81 my $cols = $sth->fetchall_hashref('name');
83 while (my ($col_name, $info) = each %$result) {
84 if ($cols->{$col_name}{pk} && lc($cols->{$col_name}{type}) eq 'integer') {
85 $info->{is_auto_increment} = 1;
89 while (my ($col, $info) = each %$result) {
90 if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') {
91 ${ $info->{default_value} } = 'current_timestamp';
99 my ($self, $table) = @_;
101 my $dbh = $self->schema->storage->dbh;
102 my $sth = $dbh->prepare(
103 "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")"
108 while (my $fk = $sth->fetchrow_hashref) {
109 my $rel = $rels[ $fk->{id} ] ||= {
111 remote_columns => undef,
112 remote_table => $fk->{table}
115 push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
116 push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
117 warn "This is supposed to be the same rel but remote_table changed from ",
118 $rel->{remote_table}, " to ", $fk->{table}
119 if $rel->{remote_table} ne $fk->{table};
125 sub _table_uniq_info {
126 my ($self, $table) = @_;
128 my $dbh = $self->schema->storage->dbh;
129 my $sth = $dbh->prepare(
130 "pragma index_list(" . $dbh->quote($table) . ")"
135 while (my $idx = $sth->fetchrow_hashref) {
136 next unless $idx->{unique};
138 my $name = $idx->{name};
140 my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")");
141 $get_idx_sth->execute;
143 while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
144 push @cols, $self->_lc($idx_row->{name});
146 $get_idx_sth->finish;
148 # Rename because SQLite complains about sqlite_ prefixes on identifiers
149 # and ignores constraint names in DDL.
150 $name = (join '_', @cols) . '_unique';
152 push @uniqs, [ $name => \@cols ];
159 my ($self, $opts) = @_;
161 my $dbh = $self->schema->storage->dbh;
162 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
165 while ( my $row = $sth->fetchrow_hashref ) {
166 next unless $row->{type} =~ /^(?:table|view)\z/i;
167 next if $row->{tbl_name} =~ /^sqlite_/;
168 push @tables, $row->{tbl_name};
171 return $self->_filter_tables(\@tables, $opts);
176 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
177 L<DBIx::Class::Schema::Loader::DBI>
181 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
185 This library is free software; you can redistribute it and/or modify it under
186 the same terms as Perl itself.