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 a8c675f..03cab07 100644 (file)
@@ -24,12 +24,12 @@ See L<DBIx::Class::Schema::Loader>.
 
 =cut
 
-sub _db_classes {
+sub _loader_db_classes {
     return qw/DBIx::Class::PK::Auto::SQLite/;
 }
 
 # XXX this really needs a re-factor
-sub _relationships {
+sub _loader_relationships {
     my $class = shift;
     foreach my $table ( $class->tables ) {
 
@@ -75,30 +75,43 @@ SELECT sql FROM sqlite_master WHERE tbl_name = ?
             $col =~ s/^\s+//gs;
 
             # Grab reference
-            if ( $col =~ /^\((.*)\)\s+REFERENCES\s+(\w+)\s*\((.*)\)/i ) {
-                chomp $col;
+            chomp $col;
+           next if $col !~ /^(.*)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix;
 
-                my ($cols, $f_table, $f_cols) = ($1, $2, $3);
+            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;
-            
-                my $cond = {};
+                $cond = {};
                 for(my $i = 0 ; $i < @cols; $i++) {
                     $cond->{$f_cols[$i]} = $cols[$i];
                 }
-
-                eval { $class->_belongs_to_many( $table, $f_table, $cond ) };
-                warn qq/\# belongs_to_many failed "$@"\n\n/
-                  if $@ && $class->debug_loader;
+                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 {
+sub _loader_tables {
     my $class = shift;
     my $dbh = $class->storage->dbh;
     my $sth  = $dbh->prepare("SELECT * FROM sqlite_master");
@@ -111,7 +124,7 @@ sub _tables {
     return @tables;
 }
 
-sub _table_info {
+sub _loader_table_info {
     my ( $class, $table ) = @_;
 
     # find all columns.