| 1 | package AutoSQL::DBSQL::ObjectAdaptor::Store; | ||
| 2 | use strict; | ||
| 3 | use AutoSQL::DBSQL::ObjectAdaptor::Abstract; | ||
| 4 | our @ISA=qw(AutoSQL::DBSQL::ObjectAdaptor::Abstract); | ||
| 5 | |||
| 6 | sub store { | ||
| 7 | my ($self, $obj)=@_; | ||
| 8 | my $verb=(defined $obj->dbID)?'_update':'_insert'; | ||
| 9 | $self->$verb($obj); | ||
| 10 | } | ||
| 11 | |||
| 12 | sub _insert { | ||
| 13 | my ($self, $obj, $fks)=@_; | ||
| 14 | |||
| 15 | my $model=$self->model; | ||
| 16 | my @fk_slots =(defined $fks and ref($fks)eq'HASH')?keys(%$fks):(); | ||
| 17 | # Filter out the useless one for this type, such as parent_id. | ||
| 18 | @fk_slots=grep{$_ ne 'parent_id'}@fk_slots; | ||
| 19 | # print "\@fk_slots: @fk_slots\n"; | ||
| 20 | while(my ($k, $v)=each %$fks){ | ||
| 21 | print "\t$k\t$v|\n"; | ||
| 22 | } | ||
| 23 | |||
| 24 | # STORE shared children, like Location to Person, first , before storing the o j. | ||
| 25 | # | ||
| 26 | my $table=$self->_table_name; | ||
| 27 | my @slots=$self->_slots; | ||
| 28 | |||
| 29 | my @total_slots; | ||
| 30 | |||
| 31 | push @total_slots, @slots, @fk_slots; | ||
| 32 | my @methods=@slots; | ||
| 33 | |||
| 34 | my $sql="INSERT $table (". join(',', @total_slots) .")\n"; | ||
| 35 | $sql .= "VALUES (". join(',', map{'?'}@total_slots) .")\n"; | ||
| 36 | $self->debug("Originally made in ". __PACKAGE__ ."\n$sql\n"); | ||
| 37 | my $sth=$self->prepare($sql); | ||
| 38 | |||
| 39 | { # For each obj | ||
| 40 | my @values=map{$obj->$_}@methods; | ||
| 41 | push @values, map{$fks->{$_}}@fk_slots; | ||
| 42 | # print "@values\n"; | ||
| 43 | # @values =map{"'$_'"}@values; | ||
| 44 | $self->debug("Originally made in ". __PACKAGE__ ."\nvalues:". join('|',@values). "|\n"); | ||
| 45 | $sth->execute(@values); | ||
| 46 | my $dbid = $sth->{mysql_insertid}; | ||
| 47 | $obj->dbID($dbid); # if $obj->can('dbid'); | ||
| 48 | $obj->adaptor($self); # if $obj->can('adaptor'); | ||
| 49 | |||
| 50 | # insert array slots. | ||
| 51 | foreach my $slot($model->get_array_slots){ | ||
| 52 | my $slot_plural=$model->schema->get_plural($slot); | ||
| 53 | my $get_method="get_$slot_plural"; | ||
| 54 | foreach my $v ($obj->$get_method){ | ||
| 55 | |||
| 56 | $self->__insert_array_slot($slot, $v, {parent_id=>$dbid}); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | # insert scalar children. | ||
| 60 | foreach my $child($model->get_scalar_children){ | ||
| 61 | my $v=$obj->$child; | ||
| 62 | next unless defined $v; | ||
| 63 | my $content = ($model->_classify_value_attribute($child))[2]; | ||
| 64 | $self->__insert_scalar_child($content, $v, {parent_id=>$dbid}); | ||
| 65 | } | ||
| 66 | # insert array children. | ||
| 67 | foreach my $child($model->get_array_children){ | ||
| 68 | my $child_plural=$model->schema->get_plural($child); | ||
| 69 | my $get_method="get_$child_plural"; | ||
| 70 | foreach my $v($obj->$get_method){ | ||
| 71 | my $content = ($model->_classify_value_attribute($child))[2]; | ||
| 72 | $self->__insert_scalar_child($content, $v, {parent_id=>$dbid}); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | # | ||
| 76 | # Before leaving this method, help to stored the un-stored friend | ||
| 77 | # foreach my $friend ($schema->find_friends($model->type)){ | ||
| 78 | # store the friend first then get the friend_id for later's joint table. | ||
| 79 | # | ||
| 80 | # } | ||
| 81 | return $dbid; | ||
| 82 | } | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | sub __insert_array_slot { | ||
| 87 | my($self, $slot, $value, $fks)=@_; | ||
| 88 | $self->throw("no fks or fks is not hash ref") | ||
| 89 | unless(defined $fks and ref($fks) eq'HASH'); | ||
| 90 | $self->throw("no parent_id in fks") unless exists $fks->{parent_id}; | ||
| 91 | my $parent_id = $fks->{parent_id}; | ||
| 92 | |||
| 93 | my $table=$self->_table_name; | ||
| 94 | my $primary_key_name=$self->_primary_key_name; | ||
| 95 | my $table_slot =$self->_slot_table_name($slot); | ||
| 96 | my $sql ="INSERT $table_slot ($slot, $primary_key_name)\n"; | ||
| 97 | $sql .="VALUES (?, ?)\n"; | ||
| 98 | my $sth=$self->prepare($sql); | ||
| 99 | $sth->execute($value, $parent_id); | ||
| 100 | my $dbid = $sth->{mysql_insertid}; | ||
| 101 | return $dbid; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub __insert_scalar_child { | ||
| 105 | my ($self, $slot, $value, $fks)=@_; | ||
| 106 | $self->throw("no fks or fks is not hash ref") | ||
| 107 | unless(defined $fks and ref($fks) eq'HASH'); | ||
| 108 | $self->throw("no parent_id in fks") unless exists $fks->{parent_id}; | ||
| 109 | my $parent_id = $fks->{parent_id}; | ||
| 110 | |||
| 111 | # print "parent_id: $parent_id\n"; | ||
| 112 | my $table_name=$self->_table_name; | ||
| 113 | my $fk="$table_name\_id"; | ||
| 114 | $fks->{$fk}=$parent_id; | ||
| 115 | my $foriegn_adaptor = $self->dbadaptor->get_object_adaptor($slot); | ||
| 116 | my $foreign_dbid = $foriegn_adaptor->_insert($value,$fks); | ||
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | sub __insert_array_child { | ||
| 121 | my ($self, $slot, $value, $fks)=@_; | ||
| 122 | |||
| 123 | } | ||
| 124 | sub _update { | ||
| 125 | my ($self, $obj)=@_; | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; |