| 1 | package Class::DBI::Loader::mysql::Grok; | ||
| 2 | our $VERSION = '0.19'; | ||
| 3 | |||
| 4 | package Class::DBI::Loader::mysql; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Lingua::EN::Inflect qw(PL); | ||
| 9 | use Class::DBI::Loader 0.22; | ||
| 10 | use Class::DBI::Loader::mysql 0.22; | ||
| 11 | use Time::Piece::MySQL 0.05; | ||
| 12 | |||
| 13 | no warnings 'redefine'; | ||
| 14 | |||
| 15 | sub _relationships { | ||
| 16 | my $self = shift; print "Entering Grok::_relationships\n" if $self->debug; | ||
| 17 | my @tables = $self->tables; | ||
| 18 | my $dbh = $self->find_class( $tables[0] )->db_Main; | ||
| 19 | |||
| 20 | # keys are table names, | ||
| 21 | # values are hrefs where keys | ||
| 22 | # are column names and values are pri & type | ||
| 23 | my %tables = (); | ||
| 24 | for my $table ($self->tables) { | ||
| 25 | my $sth = $dbh->prepare(qq[ SELECT * FROM $table WHERE 0=1 ]); | ||
| 26 | $sth->execute; | ||
| 27 | for(my $i = 0; $i < @{$sth->{NAME}}; $i++) { # MySQL-specific | ||
| 28 | $tables{$table}->{$sth->{NAME}->[$i]} = { | ||
| 29 | type => $sth->{mysql_type_name}->[$i], | ||
| 30 | pri => $sth->{mysql_is_pri_key}->[$i], | ||
| 31 | # uni => ($sth->{mysql_is_key}->[$i] && ! $sth->{mysql_is_pri_key}->[$i]), # unique | ||
| 32 | }; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | # for each table, go through the columns to see if they refer to another table | ||
| 37 | # | ||
| 38 | for my $subject_table (@tables) { print "$subject_table\n" if $self->debug; | ||
| 39 | for my $column (sort keys %{$tables{$subject_table}}) { | ||
| 40 | my $subject_class = $self->find_class($subject_table); | ||
| 41 | $subject_class->autoinflate(dates => 'Time::Piece'); | ||
| 42 | my $object_class = $self->find_class($column); | ||
| 43 | if($tables{$subject_table}->{$column}->{type} eq 'time') { print "\t$subject_class->has_a($column, Time::Piece) time\n" if $self->debug; | ||
| 44 | $subject_class->has_a($column, 'Time::Piece', | ||
| 45 | inflate => sub { Time::Piece->strptime(shift(),'%H:%M:%S') }, | ||
| 46 | deflate => sub { shift->strftime('%H:%M:%S') }, | ||
| 47 | ); | ||
| 48 | # this points to another table; don't point to this table | ||
| 49 | # | ||
| 50 | } elsif($tables{$column} and $subject_table ne $column) { print "\t$column matches another table\n" if $self->debug; | ||
| 51 | |||
| 52 | # referring column is a primary key and this is NOT a _ref table: liner notes | ||
| 53 | # load all of the _other_ columns in $subject_table into $object_class | ||
| 54 | if($tables{$subject_table}->{$column}->{pri} && $subject_table !~ /_ref$/i) { | ||
| 55 | for my $col (keys %{$tables{$subject_table}}) { | ||
| 56 | |||
| 57 | # this is the primary column, so we can skip it | ||
| 58 | # | ||
| 59 | next if $col eq $column; print "\t\t$object_class->might_have($subject_table.'_'.$col, $subject_class, $col)\n" if $self->debug; | ||
| 60 | $object_class->might_have($subject_table.'_'.$col, $subject_class, $col); # might_have | ||
| 61 | } | ||
| 62 | } else { print "\t\t$subject_class->has_a($column, $object_class)\n" if $self->debug; | ||
| 63 | # this is a non-primary column so there's a has_a relationship here | ||
| 64 | # | ||
| 65 | $subject_class->has_a($column, $object_class); # has_a | ||
| 66 | |||
| 67 | # as for the has_many in the reverse direction, is this mapping or simple? | ||
| 68 | # | ||
| 69 | if($subject_table =~ /_ref$/i) { # $subject_table is a mapping table | ||
| 70 | my($other_column) = grep { $tables{$_} && $column ne $_ } # not the key, and not this col | ||
| 71 | keys %{$tables{$subject_table}}; # get the column which points to the mapped table | ||
| 72 | my $plural = PL($other_column); print "\t\t$object_class->has_many($plural, [ $subject_class, $other_column ] )\n" if $self->debug; | ||
| 73 | $object_class->has_many($plural, [ $subject_class, $other_column ]); # has_many | ||
| 74 | } else { # simple has_many | ||
| 75 | my $plural = PL($subject_table); print "\t\t$object_class->has_many($plural, $subject_class)\n" if $self->debug; | ||
| 76 | $object_class->has_many($plural, $subject_class); # has_many | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | 1; | ||
| 86 | __END__ |