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/;
12 our $VERSION = '0.07008';
16 DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation.
20 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
26 SQLite will fail all further commands on a connection if the underlying schema
27 has been modified. Therefore, any runtime changes requiring C<rescan> also
28 require us to re-connect to the database. The C<rescan> method here handles
29 that reconnection for you, but beware that this must occur for any other open
30 sqlite connections as well.
37 $self->next::method(@_);
39 if (not defined $self->preserve_case) {
40 $self->preserve_case(0);
45 my ($self, $schema) = @_;
47 $schema->storage->disconnect if $schema->storage;
48 $self->next::method($schema);
51 # A hack so that qualify_objects can be tested on SQLite, SQLite does not
52 # actually have schemas.
56 local $self->{db_schema};
57 return $self->next::method(@_);
62 local $self->{db_schema};
63 return $self->next::method(@_);
67 sub _columns_info_for {
71 my $result = $self->next::method(@_);
73 my $dbh = $self->schema->storage->dbh;
74 local $dbh->{FetchHashKeyName} = 'NAME_lc';
76 my $sth = $dbh->prepare(
77 "pragma table_info(" . $dbh->quote_identifier($table) . ")"
80 my $cols = $sth->fetchall_hashref('name');
82 my ($num_pk, $pk_col) = (0);
83 # SQLite doesn't give us the info we need to do this nicely :(
84 # If there is exactly one column marked PK, and its type is integer,
85 # set it is_auto_increment. This isn't 100%, but it's better than the
87 while (my ($col_name, $info) = each %$result) {
88 if ($cols->{$col_name}{pk}) {
90 if (lc($cols->{$col_name}{type}) eq 'integer') {
96 while (my ($col, $info) = each %$result) {
97 if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') {
98 ${ $info->{default_value} } = 'current_timestamp';
100 if ($num_pk == 1 and defined $pk_col and $pk_col eq $col) {
101 $info->{is_auto_increment} = 1;
109 my ($self, $table) = @_;
111 my $dbh = $self->schema->storage->dbh;
112 my $sth = $dbh->prepare(
113 "pragma foreign_key_list(" . $dbh->quote_identifier($table) . ")"
118 while (my $fk = $sth->fetchrow_hashref) {
119 my $rel = $rels[ $fk->{id} ] ||= {
121 remote_columns => undef,
122 remote_table => $fk->{table}
125 push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
126 push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
127 warn "This is supposed to be the same rel but remote_table changed from ",
128 $rel->{remote_table}, " to ", $fk->{table}
129 if $rel->{remote_table} ne $fk->{table};
135 sub _table_uniq_info {
136 my ($self, $table) = @_;
138 my $dbh = $self->schema->storage->dbh;
139 my $sth = $dbh->prepare(
140 "pragma index_list(" . $dbh->quote($table) . ")"
145 while (my $idx = $sth->fetchrow_hashref) {
146 next unless $idx->{unique};
148 my $name = $idx->{name};
150 my $get_idx_sth = $dbh->prepare("pragma index_info(" . $dbh->quote($name) . ")");
151 $get_idx_sth->execute;
153 while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
154 push @cols, $self->_lc($idx_row->{name});
156 $get_idx_sth->finish;
158 # Rename because SQLite complains about sqlite_ prefixes on identifiers
159 # and ignores constraint names in DDL.
160 $name = (join '_', @cols) . '_unique';
162 push @uniqs, [ $name => \@cols ];
169 my ($self, $opts) = @_;
171 my $dbh = $self->schema->storage->dbh;
172 my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
175 while ( my $row = $sth->fetchrow_hashref ) {
176 next unless $row->{type} =~ /^(?:table|view)\z/i;
177 next if $row->{tbl_name} =~ /^sqlite_/;
178 push @tables, $row->{tbl_name};
181 return $self->_filter_tables(\@tables, $opts);
186 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
187 L<DBIx::Class::Schema::Loader::DBI>
191 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
195 This library is free software; you can redistribute it and/or modify it under
196 the same terms as Perl itself.