1 package DBIx::Class::Schema::Loader::DBI::SQLite;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
10 use DBIx::Class::Schema::Loader::Table ();
12 our $VERSION = '0.07018';
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);
43 if ($self->db_schema) {
45 db_schema is not supported on SQLite, the option is implemented only for qualify_objects testing.
47 if ($self->db_schema->[0] eq '%') {
48 $self->db_schema(undef);
54 my ($self, $schema) = @_;
56 $schema->storage->disconnect if $schema->storage;
57 $self->next::method($schema);
60 sub _columns_info_for {
64 my $result = $self->next::method(@_);
66 local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
68 my $sth = $self->dbh->prepare(
69 "pragma table_info(" . $self->dbh->quote_identifier($table) . ")"
72 my $cols = $sth->fetchall_hashref('name');
74 # copy and case according to preserve_case mode
75 # no need to check for collisions, SQLite does not allow them
77 while (my ($col, $info) = each %$cols) {
78 $cols{ $self->_lc($col) } = $info;
81 my ($num_pk, $pk_col) = (0);
82 # SQLite doesn't give us the info we need to do this nicely :(
83 # If there is exactly one column marked PK, and its type is integer,
84 # set it is_auto_increment. This isn't 100%, but it's better than the
86 while (my ($col_name, $info) = each %$result) {
87 if ($cols{$col_name}{pk}) {
89 if (lc($cols{$col_name}{type}) eq 'integer') {
95 while (my ($col, $info) = each %$result) {
96 if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') {
97 ${ $info->{default_value} } = 'current_timestamp';
99 if ($num_pk == 1 and defined $pk_col and $pk_col eq $col) {
100 $info->{is_auto_increment} = 1;
108 my ($self, $table) = @_;
110 my $sth = $self->dbh->prepare(
111 "pragma foreign_key_list(" . $self->dbh->quote_identifier($table) . ")"
116 while (my $fk = $sth->fetchrow_hashref) {
117 my $rel = $rels[ $fk->{id} ] ||= {
119 remote_columns => undef,
120 remote_table => DBIx::Class::Schema::Loader::Table->new(
122 name => $fk->{table},
123 ($self->db_schema ? (
124 schema => $self->db_schema->[0],
130 push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
131 push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
132 warn "This is supposed to be the same rel but remote_table changed from ",
133 $rel->{remote_table}->name, " to ", $fk->{table}
134 if $rel->{remote_table}->name ne $fk->{table};
140 sub _table_uniq_info {
141 my ($self, $table) = @_;
143 my $sth = $self->dbh->prepare(
144 "pragma index_list(" . $self->dbh->quote($table) . ")"
149 while (my $idx = $sth->fetchrow_hashref) {
150 next unless $idx->{unique};
152 my $name = $idx->{name};
154 my $get_idx_sth = $self->dbh->prepare("pragma index_info(" . $self->dbh->quote($name) . ")");
155 $get_idx_sth->execute;
157 while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
158 push @cols, $self->_lc($idx_row->{name});
160 $get_idx_sth->finish;
162 # Rename because SQLite complains about sqlite_ prefixes on identifiers
163 # and ignores constraint names in DDL.
164 $name = (join '_', @cols) . '_unique';
166 push @uniqs, [ $name => \@cols ];
173 my ($self, $opts) = @_;
175 my $sth = $self->dbh->prepare("SELECT * FROM sqlite_master");
178 while ( my $row = $sth->fetchrow_hashref ) {
179 next unless $row->{type} =~ /^(?:table|view)\z/i;
180 next if $row->{tbl_name} =~ /^sqlite_/;
181 push @tables, DBIx::Class::Schema::Loader::Table->new(
183 name => $row->{tbl_name},
184 ($self->db_schema ? (
185 schema => $self->db_schema->[0],
186 ignore_schema => 1, # for qualify_objects tests
191 return $self->_filter_tables(\@tables, $opts);
196 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
197 L<DBIx::Class::Schema::Loader::DBI>
201 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
205 This library is free software; you can redistribute it and/or modify it under
206 the same terms as Perl itself.