some shuffling/refactoring of the relationship code, and a TODO file added
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / SQLite.pm
index 70859dd..03cab07 100644 (file)
@@ -1,9 +1,8 @@
 package DBIx::Class::Schema::Loader::SQLite;
 
 use strict;
-use base 'DBIx::Class::Schema::Loader::Generic';
+use base qw/DBIx::Class::Schema::Loader::Generic/;
 use Text::Balanced qw( extract_bracketed );
-use DBI;
 use Carp;
 
 =head1 NAME
@@ -17,7 +16,6 @@ DBIx::Class::Schema::Loader::SQLite - DBIx::Class::Schema::Loader SQLite Impleme
   # $loader is a DBIx::Class::Schema::Loader::SQLite
   my $loader = DBIx::Class::Schema::Loader->new(
     dsn       => "dbi:SQLite:dbname=/path/to/dbfile",
-    namespace => "Data",
   );
 
 =head1 DESCRIPTION
@@ -26,15 +24,16 @@ See L<DBIx::Class::Schema::Loader>.
 
 =cut
 
-sub _db_classes {
+sub _loader_db_classes {
     return qw/DBIx::Class::PK::Auto::SQLite/;
 }
 
-sub _relationships {
-    my $self = shift;
-    foreach my $table ( $self->tables ) {
+# XXX this really needs a re-factor
+sub _loader_relationships {
+    my $class = shift;
+    foreach my $table ( $class->tables ) {
 
-        my $dbh = $self->{_storage}->dbh;
+        my $dbh = $class->storage->dbh;
         my $sth = $dbh->prepare(<<"");
 SELECT sql FROM sqlite_master WHERE tbl_name = ?
 
@@ -69,29 +68,52 @@ SELECT sql FROM sqlite_master WHERE tbl_name = ?
             # find multi-col fks below
             $col =~ s/\-\-comma\-\-/,/g;
 
-            # CDBI doesn't have built-in support multi-col fks, so ignore them
-            next if $col =~ s/^\s*FOREIGN\s+KEY\s*//i && $col =~ /^\([^,)]+,/;
+            $col =~ s/^\s*FOREIGN\s+KEY\s*//i;
 
             # Strip punctuations around key and table names
-            $col =~ s/[()\[\]'"]/ /g;
+            $col =~ s/[\[\]'"]/ /g;
             $col =~ s/^\s+//gs;
 
             # Grab reference
-            if ( $col =~ /^(\w+).*REFERENCES\s+(\w+)\s*(\w+)?/i ) {
-                chomp $col;
-                warn qq/\# Found foreign key definition "$col"\n\n/
-                  if $self->debug;
-                eval { $self->_belongs_to_many( $table, $1, $2, $3 ) };
-                warn qq/\# belongs_to_many failed "$@"\n\n/
-                  if $@ && $self->debug;
+            chomp $col;
+           next if $col !~ /^(.*)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix;
+
+            my ($cols, $f_table, $f_cols) = ($1, $2, $3);
+
+            if($cols =~ /^\(/) { # Table-level
+                $cols =~ s/^\(\s*//;
+                $cols =~ s/\s*\)$//;
             }
+            else {               # Inline
+                $cols =~ s/\s+.*$//;
+            }
+
+            my $cond;
+
+            if($f_cols) {
+                my @cols = map { s/\s*//g; $_ } split(/\s*,\s*/,$cols);
+                my @f_cols = map { s/\s*//g; $_ } split(/\s*,\s*/,$f_cols);
+                die "Mismatched column count in rel for $table => $f_table"
+                  if @cols != @f_cols;
+                $cond = {};
+                for(my $i = 0 ; $i < @cols; $i++) {
+                    $cond->{$f_cols[$i]} = $cols[$i];
+                }
+                eval { $class->_loader_make_cond_rel( $table, $f_table, $cond ) };
+            }
+            else {
+                eval { $class->_loader_make_simple_rel( $table, $f_table, $cols ) };
+            }
+
+            warn qq/\# belongs_to_many failed "$@"\n\n/
+              if $@ && $class->_loader_debug;
         }
     }
 }
 
-sub _tables {
-    my $self = shift;
-    my $dbh = $self->{_storage}->dbh;
+sub _loader_tables {
+    my $class = shift;
+    my $dbh = $class->storage->dbh;
     my $sth  = $dbh->prepare("SELECT * FROM sqlite_master");
     $sth->execute;
     my @tables;
@@ -102,11 +124,11 @@ sub _tables {
     return @tables;
 }
 
-sub _table_info {
-    my ( $self, $table ) = @_;
+sub _loader_table_info {
+    my ( $class, $table ) = @_;
 
     # find all columns.
-    my $dbh = $self->{_storage}->dbh;
+    my $dbh = $class->storage->dbh;
     my $sth = $dbh->prepare("PRAGMA table_info('$table')");
     $sth->execute();
     my @columns;
@@ -134,7 +156,7 @@ SQL
         @pks = ($primary);
     }
     else {
-        my ($pks) = $sql =~ m/PRIMARY\s+KEY\s*\(\s*([^)]+)\s*\)/;
+        my ($pks) = $sql =~ m/PRIMARY\s+KEY\s*\(\s*([^)]+)\s*\)/i;
         @pks = split( m/\s*\,\s*/, $pks ) if $pks;
     }
     return ( \@columns, \@pks );