1 |
# -*- Mode: Cperl -*- |
2 |
# Query.pm -- |
3 |
# ITIID : $ITI$ $Header $__Header$ |
4 |
# Author : Ulrich Pfeifer |
5 |
# Created On : Fri Sep 13 13:05:52 1996 |
6 |
# Last Modified By: Ulrich Pfeifer |
7 |
# Last Modified On: Fri Apr 14 16:27:01 2000 |
8 |
# Language : CPerl |
9 |
# Update Count : 57 |
10 |
# Status : Unknown, Use with caution! |
11 |
# |
12 |
# Copyright (c) 1996-1997, Ulrich Pfeifer |
13 |
# |
14 |
|
15 |
package WAIT::Query::Base; |
16 |
|
17 |
sub new { |
18 |
my $type = shift; |
19 |
my $table = shift; |
20 |
my $self = {Table => $table}; |
21 |
|
22 |
bless $self, ref($type) || $type; |
23 |
if (@_) { |
24 |
$self->add(@_); |
25 |
} else { |
26 |
$self; |
27 |
} |
28 |
} |
29 |
|
30 |
sub add { |
31 |
my ($self, $fldorref, %parm) = @_; |
32 |
my @fld = (ref $fldorref)?@$fldorref:$fldorref; |
33 |
my $fld; |
34 |
|
35 |
for $fld (@fld) { |
36 |
if (defined $parm{Plain}) { |
37 |
if (defined $self->{Plain}->{$fld}) { |
38 |
$self->{Plain}->{$fld} .= ' ' . $parm{Plain}; |
39 |
} else { |
40 |
$self->{Plain}->{$fld} = $parm{Plain}; |
41 |
} |
42 |
} |
43 |
if (defined $parm{Raw}) { |
44 |
if (defined $self->{Raw}->{$fld}) { |
45 |
$self->{Raw}->{$fld}->merge($parm{Raw}); |
46 |
} else { |
47 |
$self->{Raw}->{$fld} = $parm{Raw}; |
48 |
} |
49 |
} |
50 |
} |
51 |
$self; |
52 |
} |
53 |
|
54 |
sub merge { |
55 |
my ($self, $other) = @_; |
56 |
my $fld; |
57 |
|
58 |
if (ref($self) ne ref($other)) { |
59 |
return $other->merge($self); |
60 |
} |
61 |
for $fld (keys %{$other->{Plain}}) { |
62 |
$self->add($fld, Plain => $other->{Plain}->{$fld}); |
63 |
} |
64 |
for $fld (keys %{$other->{Raw}}) { |
65 |
$self->add($fld, Raw => $other->{Raw}->{$fld}); |
66 |
} |
67 |
|
68 |
$self; |
69 |
} |
70 |
|
71 |
sub clone { |
72 |
my $self = shift; |
73 |
my %copy; |
74 |
my $fld; |
75 |
|
76 |
for $fld (keys %{$self->{Plain}}) { |
77 |
$copy{Plain}->{$fld} = $self->{Plain}->{$fld}; |
78 |
} |
79 |
for $fld (keys %{$self->{Raw}}) { |
80 |
next unless defined $self->{Raw}->{$fld}; # XXX bug elsewere |
81 |
$copy{Raw}->{$fld} = $self->{Raw}->{$fld}->clone; |
82 |
} |
83 |
|
84 |
$self; |
85 |
} |
86 |
|
87 |
sub execute { |
88 |
my $self = shift; |
89 |
my $tb = $self->{Table}; |
90 |
my %result; |
91 |
my $fld; |
92 |
|
93 |
for $fld (keys %{$self->{Plain}}, keys %{$self->{Raw}}) { |
94 |
%r = $tb->search( |
95 |
{ attr => $fld, |
96 |
cont => $self->{Plain}->{$fld}, |
97 |
raw => $self->{Raw}->{$fld}, |
98 |
@_ |
99 |
} |
100 |
); |
101 |
my ($key, $val); |
102 |
while (($key, $val) = each %r) { |
103 |
if (exists $result{$key}) { |
104 |
$result{$key} += $val; |
105 |
} else { |
106 |
$result{$key} = $val; |
107 |
} |
108 |
} |
109 |
} |
110 |
%result; |
111 |
} |
112 |
|
113 |
sub hilight { |
114 |
my $self = shift; |
115 |
$self->{Table}->hilight($_[0], $self->{Plain}, $self->{Raw}) |
116 |
} |
117 |
|
118 |
sub flatten { |
119 |
my $self = shift; |
120 |
#print STDERR "WAIT::Query::Base::flatten($self)\n"; |
121 |
$self->clone() |
122 |
} |
123 |
|
124 |
package WAIT::Query::bin; |
125 |
|
126 |
sub new { |
127 |
my $type = shift; |
128 |
my $self = [@_]; |
129 |
|
130 |
#print STDERR "WAIT::Query::bin::new $type $self\n"; |
131 |
bless $self, ref($type) || $type; |
132 |
} |
133 |
|
134 |
sub flatten { |
135 |
my $self = shift; |
136 |
#print STDERR "WAIT::Query::bin::flatten($self)\n"; |
137 |
$self->[0]->flatten->merge($self->[1]->flatten) |
138 |
} |
139 |
|
140 |
sub hilight { |
141 |
my $self = shift; |
142 |
my $query = $self->flatten(); |
143 |
|
144 |
$query->hilight(@_); |
145 |
} |
146 |
|
147 |
package WAIT::Query::and; |
148 |
|
149 |
@ISA = qw(WAIT::Query::bin); |
150 |
|
151 |
sub execute { |
152 |
my $self = shift; |
153 |
my %ra = $self->[0]->execute(); |
154 |
my %rb = $self->[1]->execute(); |
155 |
|
156 |
#print STDERR "WAIT::Query::and::execute\n"; |
157 |
for (keys %ra) { |
158 |
if (exists $rb{$_}) { |
159 |
$ra{$_} *= $rb{$_}; |
160 |
delete $ra{$_} if $ra{$_} <= 0; |
161 |
} else { |
162 |
delete $ra{$_}; |
163 |
} |
164 |
} |
165 |
%ra; |
166 |
} |
167 |
|
168 |
|
169 |
sub merge { |
170 |
#print STDERR "WAIT::Query::and::merge(@_)\n"; |
171 |
new WAIT::Query::or @_; # XXX |
172 |
} |
173 |
|
174 |
package WAIT::Query::or; |
175 |
|
176 |
@ISA = qw(WAIT::Query::bin); |
177 |
|
178 |
sub execute { |
179 |
my $self = shift; |
180 |
my %ra = $self->[0]->execute(); |
181 |
my %rb = $self->[1]->execute(); |
182 |
|
183 |
for (keys %ra) { |
184 |
if (exists $rb{$_}) { |
185 |
$ra{$_} += $rb{$_} |
186 |
} |
187 |
} |
188 |
for (keys %rb) { |
189 |
unless (exists $ra{$_}) { |
190 |
$ra{$_} = $rb{$_} |
191 |
} |
192 |
} |
193 |
%ra; |
194 |
} |
195 |
|
196 |
|
197 |
sub merge { |
198 |
my $self = shift; |
199 |
|
200 |
if (ref($_[0]) eq 'WAIT::Query::Base') { |
201 |
$self->[0] = $self->[0]->merge($_[0]); |
202 |
} else { |
203 |
new WAIT::Query::or $self, @_; # XXX |
204 |
} |
205 |
} |
206 |
|
207 |
package WAIT::Query::not; |
208 |
|
209 |
@ISA = qw(WAIT::Query::and WAIT::Query::bin); |
210 |
|
211 |
sub execute { |
212 |
my $self = shift; |
213 |
my %ra = $self->[0]->execute(); |
214 |
my %rb = $self->[1]->execute(); |
215 |
|
216 |
for (keys %ra) { |
217 |
if (exists $rb{$_}) { |
218 |
if (exists $ra{$_}) { |
219 |
$ra{$_} -= $rb{$_}; |
220 |
delete $ra{$_} if $ra{$_} <= 0; |
221 |
} |
222 |
} |
223 |
} |
224 |
|
225 |
%ra; |
226 |
} |
227 |
|
228 |
package WAIT::Query::Raw; |
229 |
use strict; |
230 |
use Carp; |
231 |
|
232 |
sub new { |
233 |
my $type = shift; |
234 |
my $self = shift; |
235 |
|
236 |
$self = {} unless defined $self; |
237 |
bless $self, ref($type) || $type; |
238 |
} |
239 |
|
240 |
sub clone { |
241 |
my $self = shift; |
242 |
my %copy; |
243 |
|
244 |
for (keys %$self) { |
245 |
$copy{$_} = [@{$self->{$_}}]; |
246 |
} |
247 |
$self->new(\%copy); |
248 |
} |
249 |
|
250 |
# Modifies first argument |
251 |
sub merge { |
252 |
my $self = shift; |
253 |
my $other = shift; |
254 |
|
255 |
croak "$other is not at 'WAIT::Query'" unless ref($other) =~ /^WAIT::Query/; |
256 |
for (keys %$other) { |
257 |
if (exists $self->{$_}) { |
258 |
push @{$self->{$_}}, @{$other->{$_}} |
259 |
} else { |
260 |
$self->{$_} = $other->{$_}; |
261 |
} |
262 |
} |
263 |
} |
264 |
|
265 |
1; |