fix for ^sqlite_ tables from chromatic
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / SQLite.pm
index 603c672..51530b4 100644 (file)
@@ -3,8 +3,9 @@ package DBIx::Class::Schema::Loader::DBI::SQLite;
 use strict;
 use warnings;
 use base qw/DBIx::Class::Schema::Loader::DBI/;
-use Class::C3;
+use Carp::Clan qw/^DBIx::Class/;
 use Text::Balanced qw( extract_bracketed );
+use Class::C3;
 
 =head1 NAME
 
@@ -33,8 +34,8 @@ sub _sqlite_parse_table {
     my @uniqs;
 
     my $dbh = $self->schema->storage->dbh;
-    my $sth = $dbh->prepare(<<"");
-SELECT sql FROM sqlite_master WHERE tbl_name = ?
+    my $sth = $self->{_cache}->{sqlite_master}
+        ||= $dbh->prepare(q{SELECT sql FROM sqlite_master WHERE tbl_name = ?});
 
     $sth->execute($table);
     my ($sql) = $sth->fetchrow_array;
@@ -76,12 +77,12 @@ SELECT sql FROM sqlite_master WHERE tbl_name = ?
         # Grab reference
         chomp $col;
 
-        if($col =~ /^(.*)\s+UNIQUE/) {
+        if($col =~ /^(.*)\s+UNIQUE/i) {
             my $colname = $1;
             $colname =~ s/\s+.*$//;
             push(@uniqs, [ "${colname}_unique" => [ lc $colname ] ]);
         }
-        elsif($col =~/^\s*UNIQUE\s*\(\s*(.*)\)/) {
+        elsif($col =~/^\s*UNIQUE\s*\(\s*(.*)\)/i) {
             my $cols = $1;
             $cols =~ s/\s+$//;
             my @cols = map { lc } split(/\s*,\s*/, $cols);
@@ -89,7 +90,7 @@ SELECT sql FROM sqlite_master WHERE tbl_name = ?
             push(@uniqs, [ $name => \@cols ]);
         }
 
-        next if $col !~ /^(.*)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix;
+        next if $col !~ /^(.*\S)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix;
 
         my ($cols, $f_table, $f_cols) = ($1, $2, $3);
 
@@ -105,7 +106,7 @@ SELECT sql FROM sqlite_master WHERE tbl_name = ?
         my $rcols;
         if($f_cols) {
             my @f_cols = map { s/\s*//g; lc $_ } split(/\s*,\s*/,$f_cols);
-            die "Mismatched column count in rel for $table => $f_table"
+            croak "Mismatched column count in rel for $table => $f_table"
               if @cols != @f_cols;
             $rcols = \@f_cols;
         }
@@ -139,12 +140,14 @@ sub _table_uniq_info {
 
 sub _tables_list {
     my $self = shift;
+
     my $dbh = $self->schema->storage->dbh;
-    my $sth  = $dbh->prepare("SELECT * FROM sqlite_master");
+    my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
     $sth->execute;
     my @tables;
     while ( my $row = $sth->fetchrow_hashref ) {
         next unless lc( $row->{type} ) eq 'table';
+        next if $row->{tbl_name} =~ /^sqlite_/;
         push @tables, $row->{tbl_name};
     }
     return @tables;