1 |
# (c) Sound Object Logic 2000-2001 |
2 |
|
3 |
# not implemented yet |
4 |
|
5 |
__END__ |
6 |
|
7 |
package Tangram::IntrHash; |
8 |
|
9 |
use base qw( Tangram::AbstractHash ); |
10 |
|
11 |
use Carp; |
12 |
|
13 |
sub reschema |
14 |
{ |
15 |
my ($self, $members, $class, $schema) = @_; |
16 |
|
17 |
foreach my $member (keys %$members) |
18 |
{ |
19 |
my $def = $members->{$member}; |
20 |
|
21 |
unless (ref($def)) |
22 |
{ |
23 |
$def = { class => $def }; |
24 |
$members->{$member} = $def; |
25 |
} |
26 |
|
27 |
$def->{coll} ||= $class . "_$member"; |
28 |
$def->{slot} ||= $class . "_$member" . "_slot"; |
29 |
|
30 |
$schema->{classes}{$def->{class}}{stateless} = 0; |
31 |
} |
32 |
|
33 |
return keys %$members; |
34 |
} |
35 |
|
36 |
sub defered_save |
37 |
{ |
38 |
use integer; |
39 |
|
40 |
my ($self, $storage, $obj, $members, $coll_id) = @_; |
41 |
|
42 |
my $classes = $storage->{schema}{classes}; |
43 |
my $old_states = $storage->{scratch}{ref($self)}{$coll_id}; |
44 |
|
45 |
foreach my $member (keys %$members) |
46 |
{ |
47 |
next if tied $obj->{$member}; |
48 |
next unless exists $obj->{$member} && defined $obj->{$member}; |
49 |
|
50 |
my $def = $members->{$member}; |
51 |
my $item_classdef = $classes->{$def->{class}}; |
52 |
my $table = $item_classdef->{table} or die; |
53 |
my $item_col = $def->{coll}; |
54 |
my $slot_col = $def->{slot}; |
55 |
|
56 |
my $coll_id = $storage->id($obj); |
57 |
my $coll = $obj->{$member}; |
58 |
my $coll_size = @$coll; |
59 |
|
60 |
my @new_state = (); |
61 |
|
62 |
my $old_state = $old_states->{$member}; |
63 |
my $old_size = $old_state ? @$old_state : 0; |
64 |
|
65 |
my %removed; |
66 |
@removed{ @$old_state } = () if $old_state; |
67 |
|
68 |
my $slot = 0; |
69 |
|
70 |
while ($slot < $coll_size) |
71 |
{ |
72 |
my $item_id = $storage->id( $coll->[$slot] ) || die; |
73 |
|
74 |
$storage->sql_do("UPDATE $table SET $item_col = $coll_id, $slot_col = $slot WHERE id = $item_id") |
75 |
unless $slot < $old_size && $item_id eq $old_state->[$slot]; |
76 |
|
77 |
push @new_state, $item_id; |
78 |
delete $removed{$item_id}; |
79 |
++$slot; |
80 |
} |
81 |
|
82 |
if (keys %removed) |
83 |
{ |
84 |
my $removed = join(' ', keys %removed); |
85 |
$storage->sql_do("UPDATE $table SET $item_col = NULL, $slot_col = NULL WHERE id IN ($removed)"); |
86 |
} |
87 |
|
88 |
$old_states->{$member} = \@new_state; |
89 |
|
90 |
$storage->tx_on_rollback( sub { $old_states->{$member} = $old_state } ); |
91 |
} |
92 |
} |
93 |
|
94 |
sub erase |
95 |
{ |
96 |
my ($self, $storage, $obj, $members, $coll_id) = @_; |
97 |
|
98 |
foreach my $member (keys %$members) |
99 |
{ |
100 |
next if tied $obj->{$member}; |
101 |
|
102 |
my $def = $members->{$member}; |
103 |
my $item_classdef = $storage->{schema}{$def->{class}}; |
104 |
my $table = $item_classdef->{table} || $def->{class}; |
105 |
my $item_col = $def->{coll}; |
106 |
my $slot_col = $def->{slot}; |
107 |
|
108 |
my $sql = "UPDATE $table SET $item_col = NULL, $slot_col = NULL WHERE $item_col = $coll_id"; |
109 |
$storage->sql_do($sql); |
110 |
} |
111 |
} |
112 |
|
113 |
sub cursor |
114 |
{ |
115 |
my ($self, $def, $storage, $obj, $member) = @_; |
116 |
|
117 |
my $cursor = Tangram::CollCursor->new($storage, $def->{class}, $storage->{db}); |
118 |
|
119 |
my $item_col = $def->{coll}; |
120 |
my $slot_col = $def->{slot}; |
121 |
|
122 |
my $coll_id = $storage->id($obj); |
123 |
my $tid = $cursor->{-stored}->{table_hash}{$def->{class}}; # ->leaf_table; |
124 |
$cursor->{-coll_cols} = "t$tid.$slot_col"; |
125 |
$cursor->{-coll_where} = "t$tid.$item_col = $coll_id"; |
126 |
|
127 |
return $cursor; |
128 |
} |
129 |
|
130 |
sub query_expr |
131 |
{ |
132 |
my ($self, $obj, $members, $tid) = @_; |
133 |
map { Tangram::IntrCollExpr->new($obj, $_); } values %$members; |
134 |
} |
135 |
|
136 |
sub remote_expr |
137 |
{ |
138 |
my ($self, $obj, $tid) = @_; |
139 |
Tangram::IntrCollExpr->new($obj, $self); |
140 |
} |
141 |
|
142 |
sub prefetch |
143 |
{ |
144 |
my ($self, $storage, $def, $coll, $class, $member, $filter) = @_; |
145 |
|
146 |
my $ritem = $storage->remote($def->{class}); |
147 |
|
148 |
my $prefetch = $storage->{PREFETCH}{$class}{$member} ||= {}; # weakref |
149 |
|
150 |
my $cursor = Tangram::Cursor->new($storage, $ritem, $storage->{db}); |
151 |
|
152 |
my $includes = $coll->{$member}->includes($ritem); |
153 |
$includes &= $filter if $filter; |
154 |
|
155 |
# also retrieve collection-side id and index of elmt in sequence |
156 |
|
157 |
$cursor->retrieve($coll->{id}, |
158 |
Tangram::Expr->new("t$ritem->{object}{table_hash}{$def->{class}}.$def->{slot}", Tangram::Integer->instance() ); |
159 |
|
160 |
$cursor->select($includes); |
161 |
|
162 |
while (my $item = $cursor->current) |
163 |
{ |
164 |
my ($coll_id, $slot) = $cursor->residue; |
165 |
$prefetch->{$coll_id}[$slot] = $item; |
166 |
$cursor->next; |
167 |
} |
168 |
} |
169 |
|
170 |
$Tangram::Schema::TYPES{iarray} = Tangram::IntrHash->new; |
171 |
|
172 |
1; |