| 1 | package AutoSQL::DBSQL::ObjectAdaptor::Fetch; | ||
| 2 | use strict; | ||
| 3 | use AutoSQL::DBSQL::ObjectAdaptor::Abstract; | ||
| 4 | our @ISA=qw(AutoSQL::DBSQL::ObjectAdaptor::Abstract); | ||
| 5 | |||
| 6 | sub fetch_by_dbID { | ||
| 7 | my ($self, $dbid)=@_; | ||
| 8 | my $where = $self->_primary_key_name .' = ?'; | ||
| 9 | my @objs = $self->_fetch_by_where($where, [$dbid]); | ||
| 10 | return $objs[0]; | ||
| 11 | } | ||
| 12 | |||
| 13 | sub fetch_by_FK { | ||
| 14 | my ($self, $fks)=@_; | ||
| 15 | my @fk_slots=grep!/parent_id/, | ||
| 16 | (defined $fks and ref($fks)eq'HASH')?keys(%$fks):(); | ||
| 17 | my $where=join ' and ', map {"$_=?"} @fk_slots; | ||
| 18 | my @values=map{$fks->{$_}}@fk_slots; | ||
| 19 | return $self->_fetch_by_where($where, \@values); | ||
| 20 | } | ||
| 21 | |||
| 22 | # examples to use this method: fetch_by_dbID, fetch_by_condition, | ||
| 23 | # PersonAdaptor::fetch_by_first_name | ||
| 24 | # | ||
| 25 | sub _fetch_by_where { | ||
| 26 | my ($self, $where, $values)=@_; | ||
| 27 | |||
| 28 | my $model=$self->model; | ||
| 29 | my $table=$self->_table_name; | ||
| 30 | my @slots=$self->_slots; | ||
| 31 | push @slots, 'dbid'; | ||
| 32 | my @columns = map {($_ eq'dbid')?$self->_primary_key_name:$_} @slots; | ||
| 33 | my $sql = 'SELECT '. join(', ', @columns) .' FROM '. $self->_table_name; | ||
| 34 | $sql .= " WHERE $where"; | ||
| 35 | |||
| 36 | my $sth=$self->prepare($sql); | ||
| 37 | $sth->execute(@$values); | ||
| 38 | my @objs; | ||
| 39 | my $vp=$self->factory->make_module($self->model->type); | ||
| 40 | while(my @array = $sth->fetchrow_array){ | ||
| 41 | my @args; | ||
| 42 | my $dbid; | ||
| 43 | for(my $i=0; $i<@slots; $i++){ | ||
| 44 | push @args, "-".$slots[$i] => $array[$i]; | ||
| 45 | $dbid = $array[$i] if $slots[$i] eq 'dbid'; | ||
| 46 | |||
| 47 | } | ||
| 48 | push @args, -dbID => $dbid; | ||
| 49 | push @args, -adaptor => $self; | ||
| 50 | # Then fetch array slots, and children. | ||
| 51 | foreach my $slot($model->get_array_slots){ | ||
| 52 | my $slot_plural=$model->schema->get_plural($slot); | ||
| 53 | my @slots=$self->__fetch_array_slot($slot, {parent_id=>$dbid}); | ||
| 54 | push @args, "-$slot_plural", \@slots; | ||
| 55 | } | ||
| 56 | my $fks={ | ||
| 57 | $self->_primary_key_name => $dbid, | ||
| 58 | parent_id=>$dbid | ||
| 59 | }; | ||
| 60 | foreach my $child($model->get_scalar_children){ | ||
| 61 | my $content = ($model->_classify_value_attribute($child))[2]; | ||
| 62 | my @children=$self->__fetch_scalar_child($content, $fks); | ||
| 63 | $self->warn("scalar child has more than one element") | ||
| 64 | if(scalar(@children) > 1); | ||
| 65 | push @args, '-'.$child, $children[0] if(scalar(@children)>0); | ||
| 66 | } | ||
| 67 | foreach my $child($model->get_array_children){ | ||
| 68 | my $content = ($model->_classify_value_attribute($child))[2]; | ||
| 69 | my @children=$self->__fetch_scalar_child($content, $fks); | ||
| 70 | push @args, '-'. $model->schema->get_plural($child), \@children; | ||
| 71 | } | ||
| 72 | push @objs, $vp->new(@args); | ||
| 73 | } | ||
| 74 | return @objs; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub __fetch_array_slot { | ||
| 78 | my ($self, $slot, $fks)=@_; | ||
| 79 | my $parent_id=$fks->{parent_id}; | ||
| 80 | my $primary_key_name=$self->_primary_key_name; | ||
| 81 | my $table_slot =$self->_slot_table_name($slot); | ||
| 82 | |||
| 83 | my $sql="SELECT $slot FROM $table_slot WHERE $primary_key_name=?"; | ||
| 84 | my $sth=$self->prepare($sql); | ||
| 85 | $sth->execute($parent_id); | ||
| 86 | my @slots; | ||
| 87 | while(my @array=$sth->fetchrow_array){ | ||
| 88 | push @slots, $array[0]; | ||
| 89 | } | ||
| 90 | return @slots; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub __fetch_scalar_child { | ||
| 94 | my ($self, $slot, $fks)=@_; | ||
| 95 | my $parent_id=$fks->{parent_id}; | ||
| 96 | my $primary_key_name=$self->_primary_key_name; | ||
| 97 | $fks->{$primary_key_name}=$parent_id; | ||
| 98 | my $child_adaptor=$self->dbadaptor->get_object_adaptor($slot); | ||
| 99 | my @children=$child_adaptor->fetch_by_FK($fks); | ||
| 100 | return @children; | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; |