33 |
return keys %$members; |
return keys %$members; |
34 |
} |
} |
35 |
|
|
36 |
sub defered_save |
sub defered_save { |
37 |
{ |
my ($self, $obj, $field, $storage) = @_; |
38 |
my ($self, $storage, $obj, $members, $coll_id) = @_; |
|
39 |
|
my $coll_id = $storage->export_object($obj); |
40 |
foreach my $member (keys %$members) |
|
41 |
{ |
my ($table, $coll_col, $item_col, $slot_col) = @{ $self }{ qw( table coll item slot ) }; |
42 |
next if tied($obj->{$member}); |
my $Q = $self->{quote}; |
43 |
next unless exists $obj->{$member} && defined $obj->{$member}; |
|
44 |
|
my $coll = $obj->{$field}; |
45 |
my $def = $members->{$member}; |
|
46 |
my ($table, $coll_col, $item_col, $slot_col) = @{ $def }{ qw( table coll item slot ) }; |
my $old_state = $self->get_load_state($storage, $obj, $field) || {}; |
47 |
my $Q = $def->{quote}; |
|
48 |
|
my %removed = %$old_state; |
49 |
my $coll = $obj->{$member}; |
delete @removed{ keys %$coll }; |
50 |
|
my @free = keys %removed; |
51 |
my $old_state = $self->get_load_state($storage, $obj, $member) || {}; |
|
52 |
|
my %new_state; |
53 |
my %removed = %$old_state; |
|
54 |
delete @removed{ keys %$coll }; |
foreach my $slot (keys %$coll) |
55 |
my @free = keys %removed; |
{ |
56 |
|
my $item_id = $storage->export_object($coll->{$slot}); |
57 |
my %new_state; |
|
58 |
|
if (exists $old_state->{$slot}) |
|
foreach my $slot (keys %$coll) |
|
59 |
{ |
{ |
60 |
my $item_id = $storage->id($coll->{$slot}); |
# key already exists |
61 |
|
|
62 |
if (exists $old_state->{$slot}) |
if ($item_id != $old_state->{$slot}) |
63 |
{ |
{ |
64 |
# key already exists |
# val has changed |
65 |
|
$storage->sql_do( |
66 |
if ($item_id != $old_state->{$slot}) |
"UPDATE $table SET $item_col = $item_id WHERE $coll_col = $coll_id AND $slot_col = $Q$slot$Q" ); |
|
{ |
|
|
# val has changed |
|
|
$storage->sql_do( |
|
|
"UPDATE $table SET $item_col = $item_id WHERE $coll_col = $coll_id AND $slot_col = $Q$slot$Q" ); |
|
|
} |
|
67 |
} |
} |
68 |
else |
} |
69 |
|
else |
70 |
|
{ |
71 |
|
# key does not exist |
72 |
|
|
73 |
|
if (@free) |
74 |
{ |
{ |
75 |
# key does not exist |
# recycle an existing line |
76 |
|
my $rslot = shift @free; |
77 |
if (@free) |
$storage->sql_do( |
78 |
{ |
"UPDATE $table SET $slot_col = $Q$slot$Q, $item_col = $item_id WHERE $coll_col = $coll_id AND $slot_col = $Q$rslot$Q" ); |
79 |
# recycle an existing line |
} |
80 |
my $rslot = shift @free; |
else |
81 |
$storage->sql_do( |
{ |
82 |
"UPDATE $table SET $slot_col = $Q$slot$Q, $item_col = $item_id WHERE $coll_col = $coll_id AND $slot_col = $Q$rslot$Q" ); |
# insert a new line |
83 |
} |
$storage->sql_do( |
84 |
else |
"INSERT INTO $table ($coll_col, $item_col, $slot_col) VALUES ($coll_id, $item_id, $Q$slot$Q)" ); |
|
{ |
|
|
# insert a new line |
|
|
$storage->sql_do( |
|
|
"INSERT INTO $table ($coll_col, $item_col, $slot_col) VALUES ($coll_id, $item_id, $Q$slot$Q)" ); |
|
|
} |
|
85 |
} |
} |
|
|
|
|
$new_state{$slot} = $item_id; |
|
|
|
|
|
} # foreach my $slot (keys %$coll) |
|
|
|
|
|
# remove lines in excess |
|
|
|
|
|
if (@free) |
|
|
{ |
|
|
@free = map { "$Q$_$Q" } @free if $Q; |
|
|
$storage->sql_do( "DELETE FROM $table WHERE $coll_col = $coll_id AND $slot_col IN (@free)" ); |
|
86 |
} |
} |
87 |
|
|
88 |
$self->set_load_state($storage, $obj, $member, \%new_state ); |
$new_state{$slot} = $item_id; |
89 |
$storage->tx_on_rollback( |
|
90 |
sub { $self->set_load_state($storage, $obj, $member, $old_state) } ); |
} # foreach my $slot (keys %$coll) |
91 |
} |
|
92 |
|
# remove lines in excess |
93 |
|
|
94 |
|
if (@free) |
95 |
|
{ |
96 |
|
@free = map { "$Q$_$Q" } @free if $Q; |
97 |
|
$storage->sql_do( "DELETE FROM $table WHERE $coll_col = $coll_id AND $slot_col IN (@free)" ); |
98 |
|
} |
99 |
|
|
100 |
|
$self->set_load_state($storage, $obj, $field, \%new_state ); |
101 |
|
$storage->tx_on_rollback( sub { $self->set_load_state($storage, $obj, $field, $old_state) } ); |
102 |
} |
} |
103 |
|
|
104 |
|
|
105 |
sub erase |
sub erase |
106 |
{ |
{ |
107 |
my ($self, $storage, $obj, $members, $coll_id) = @_; |
my ($self, $storage, $obj, $members, $coll_id) = @_; |
124 |
|
|
125 |
my $cursor = Tangram::CollCursor->new($storage, $def->{class}, $storage->{db}); |
my $cursor = Tangram::CollCursor->new($storage, $def->{class}, $storage->{db}); |
126 |
|
|
127 |
my $coll_id = $storage->id($obj); |
my $coll_id = $storage->export_object($obj); |
128 |
my $coll_tid = $storage->alloc_table; |
my $coll_tid = $storage->alloc_table; |
129 |
my $table = $def->{table}; |
my $table = $def->{table}; |
130 |
my $item_tid = $cursor->{-stored}->root_table; |
my $item_tid = $cursor->{TARGET}->object->root_table; |
131 |
my $coll_col = $def->{coll}; |
my $coll_col = $def->{coll}; |
132 |
my $item_col = $def->{item}; |
my $item_col = $def->{item}; |
133 |
my $slot_col = $def->{slot}; |
my $slot_col = $def->{slot}; |