. ' }';
}
-# Setup has_a and has_many relationships
-sub _loader_make_relations {
-
- my ( $class, $table, $other, $cond ) = @_;
- my $table_class = $class->_loader_find_table_class($table);
- my $other_class = $class->_loader_find_table_class($other);
-
- my $table_relname = lc $table;
- my $other_relname = lc $other;
+# Inflect a relationship name
+# XXX (should pluralize, but currently also tends to de-pluralize plurals)
+sub _loader_inflect_relname {
+ my ($class, $relname) = @_;
if(my $inflections = $class->_loader_inflect) {
- $table_relname = $inflections->{$table_relname}
- if exists $inflections->{$table_relname};
+ $relname = $inflections->{$relname}
+ if exists $inflections->{$relname};
}
else {
- $table_relname = Lingua::EN::Inflect::PL($table_relname);
+ $relname = Lingua::EN::Inflect::PL($relname);
}
- if(ref($cond) eq 'HASH') {
- # for single-column case, set the relname to the column name,
- # to make filter accessors work
- if(scalar keys %$cond == 1) {
- my ($col) = keys %$cond;
- $other_relname = $cond->{$col};
- }
+ return $relname;
+}
- my $rev_cond = { reverse %$cond };
+# Set up a simple relation with just a local col and foreign table
+sub _loader_make_simple_rel {
+ my ($class, $table, $other, $col) = @_;
- my $cond_printable = _loader_stringify_hash($cond)
- if $class->_loader_debug;
- my $rev_cond_printable = _loader_stringify_hash($rev_cond)
- if $class->_loader_debug;
+ my $table_class = $class->_loader_find_table_class($table);
+ my $other_class = $class->_loader_find_table_class($other);
+ my $table_relname = $class->_loader_inflect_relname(lc $table);
- warn qq/\# Belongs_to relationship\n/ if $class->_loader_debug;
+ warn qq/\# Belongs_to relationship\n/ if $class->_loader_debug;
+ warn qq/$table_class->belongs_to( '$col' => '$other_class' );\n\n/
+ if $class->_loader_debug;
+ $table_class->belongs_to( $col => $other_class );
- warn qq/$table_class->belongs_to( '$other_relname' => '$other_class',/
- . qq/$cond_printable);\n\n/
- if $class->_loader_debug;
+ warn qq/\# Has_many relationship\n/ if $class->_loader_debug;
+ warn qq/$other_class->has_many( '$table_relname' => '$table_class',/
+ . qq/$col);\n\n/
+ if $class->_loader_debug;
- $table_class->belongs_to( $other_relname => $other_class, $cond);
+ $other_class->has_many( $table_relname => $table_class, $col);
+}
- warn qq/\# Has_many relationship\n/ if $class->_loader_debug;
+# Set up a complex relation based on a hashref condition
+sub _loader_make_cond_rel {
+ my ( $class, $table, $other, $cond ) = @_;
- warn qq/$other_class->has_many( '$table_relname' => '$table_class',/
- . qq/$rev_cond_printable);\n\n/
- . qq/);\n\n/
- if $class->_loader_debug;
+ my $table_class = $class->_loader_find_table_class($table);
+ my $other_class = $class->_loader_find_table_class($other);
+ my $table_relname = $class->_loader_inflect_relname(lc $table);
+ my $other_relname = lc $other;
- $other_class->has_many( $table_relname => $table_class, $rev_cond);
- }
- else { # implicit stuff, just a col name
- warn qq/\# Belongs_to relationship\n/ if $class->_loader_debug;
- warn qq/$table_class->belongs_to( '$cond' => '$other_class' );\n\n/
- if $class->_loader_debug;
- $table_class->belongs_to( $cond => $other_class );
-
- warn qq/\# Has_many relationship\n/ if $class->_loader_debug;
- warn qq/$other_class->has_many( '$table_relname' => '$table_class',/
- . qq/$cond);\n\n/
- if $class->_loader_debug;
-
- $other_class->has_many( $table_relname => $table_class, $cond);
+ # for single-column case, set the relname to the column name,
+ # to make filter accessors work
+ if(scalar keys %$cond == 1) {
+ my ($col) = keys %$cond;
+ $other_relname = $cond->{$col};
}
+
+ my $rev_cond = { reverse %$cond };
+
+ my $cond_printable = _loader_stringify_hash($cond)
+ if $class->_loader_debug;
+ my $rev_cond_printable = _loader_stringify_hash($rev_cond)
+ if $class->_loader_debug;
+
+ warn qq/\# Belongs_to relationship\n/ if $class->_loader_debug;
+
+ warn qq/$table_class->belongs_to( '$other_relname' => '$other_class',/
+ . qq/$cond_printable);\n\n/
+ if $class->_loader_debug;
+
+ $table_class->belongs_to( $other_relname => $other_class, $cond);
+
+ warn qq/\# Has_many relationship\n/ if $class->_loader_debug;
+
+ warn qq/$other_class->has_many( '$table_relname' => '$table_class',/
+ . qq/$rev_cond_printable);\n\n/
+ . qq/);\n\n/
+ if $class->_loader_debug;
+
+ $other_class->has_many( $table_relname => $table_class, $rev_cond);
}
# Load and setup classes
foreach my $relid (keys %$rels) {
my $reltbl = $rels->{$relid}->{tbl};
my $cond = $rels->{$relid}->{cols};
- eval { $class->_loader_make_relations( $table, $reltbl, $cond ) };
+ eval { $class->_loader_make_cond_rel( $table, $reltbl, $cond ) };
warn qq/\# belongs_to_many failed "$@"\n\n/
if $@ && $class->_loader_debug;
}
# Grab reference
chomp $col;
- if( $col =~ /^(.*)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix ) {
+ 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+.*$//;
- }
+ 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];
- }
- }
- else {
- $cond = $cols;
- }
+ my $cond;
- eval { $class->_loader_make_relations( $table, $f_table, $cond ) };
- warn qq/\# belongs_to_many failed "$@"\n\n/
- if $@ && $class->_loader_debug;
+ 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;
}
}
}