1 |
ulpfr |
10 |
# -*- Mode: Perl -*- |
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: Sun Nov 22 18:44:40 1998 |
8 |
|
|
# Language : CPerl |
9 |
|
|
# Update Count : 55 |
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($fld, |
95 |
|
|
$self->{Plain}->{$fld}, |
96 |
|
|
$self->{Raw}->{$fld}, |
97 |
|
|
); |
98 |
|
|
my ($key, $val); |
99 |
|
|
while (($key, $val) = each %r) { |
100 |
|
|
if (exists $result{$key}) { |
101 |
|
|
$result{$key} += $val; |
102 |
|
|
} else { |
103 |
|
|
$result{$key} = $val; |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
%result; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
sub hilight { |
111 |
|
|
my $self = shift; |
112 |
|
|
$self->{Table}->hilight($_[0], $self->{Plain}, $self->{Raw}) |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
sub flatten { |
116 |
|
|
my $self = shift; |
117 |
|
|
#print STDERR "WAIT::Query::Base::flatten($self)\n"; |
118 |
|
|
$self->clone() |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
package WAIT::Query::bin; |
122 |
|
|
|
123 |
|
|
sub new { |
124 |
|
|
my $type = shift; |
125 |
|
|
my $self = [@_]; |
126 |
|
|
|
127 |
|
|
#print STDERR "WAIT::Query::bin::new $type $self\n"; |
128 |
|
|
bless $self, ref($type) || $type; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
sub flatten { |
132 |
|
|
my $self = shift; |
133 |
|
|
#print STDERR "WAIT::Query::bin::flatten($self)\n"; |
134 |
|
|
$self->[0]->flatten->merge($self->[1]->flatten) |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
sub hilight { |
138 |
|
|
my $self = shift; |
139 |
|
|
my $query = $self->flatten(); |
140 |
|
|
|
141 |
|
|
$query->hilight(@_); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
package WAIT::Query::and; |
145 |
|
|
|
146 |
|
|
@ISA = qw(WAIT::Query::bin); |
147 |
|
|
|
148 |
|
|
sub execute { |
149 |
|
|
my $self = shift; |
150 |
|
|
my %ra = $self->[0]->execute(); |
151 |
|
|
my %rb = $self->[1]->execute(); |
152 |
|
|
|
153 |
|
|
#print STDERR "WAIT::Query::and::execute\n"; |
154 |
|
|
for (keys %ra) { |
155 |
|
|
if (exists $rb{$_}) { |
156 |
|
|
$ra{$_} *= $rb{$_}; |
157 |
|
|
delete $ra{$_} if $ra{$_} <= 0; |
158 |
|
|
} else { |
159 |
|
|
delete $ra{$_}; |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
%ra; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
|
166 |
|
|
sub merge { |
167 |
|
|
#print STDERR "WAIT::Query::and::merge(@_)\n"; |
168 |
|
|
new WAIT::Query::or @_; # XXX |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
package WAIT::Query::or; |
172 |
|
|
|
173 |
|
|
@ISA = qw(WAIT::Query::bin); |
174 |
|
|
|
175 |
|
|
sub execute { |
176 |
|
|
my $self = shift; |
177 |
|
|
my %ra = $self->[0]->execute(); |
178 |
|
|
my %rb = $self->[1]->execute(); |
179 |
|
|
|
180 |
|
|
for (keys %ra) { |
181 |
|
|
if (exists $rb{$_}) { |
182 |
|
|
$ra{$_} += $rb{$_} |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
for (keys %rb) { |
186 |
|
|
unless (exists $ra{$_}) { |
187 |
|
|
$ra{$_} = $rb{$_} |
188 |
|
|
} |
189 |
|
|
} |
190 |
|
|
%ra; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
|
194 |
|
|
sub merge { |
195 |
|
|
my $self = shift; |
196 |
|
|
|
197 |
|
|
if (ref($_[0]) eq 'WAIT::Query::Base') { |
198 |
|
|
$self->[0] = $self->[0]->merge($_[0]); |
199 |
|
|
} else { |
200 |
|
|
new WAIT::Query::or $self, @_; # XXX |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
package WAIT::Query::not; |
205 |
|
|
|
206 |
|
|
@ISA = qw(WAIT::Query::and WAIT::Query::bin); |
207 |
|
|
|
208 |
|
|
sub execute { |
209 |
|
|
my $self = shift; |
210 |
|
|
my %ra = $self->[0]->execute(); |
211 |
|
|
my %rb = $self->[1]->execute(); |
212 |
|
|
|
213 |
|
|
for (keys %ra) { |
214 |
|
|
if (exists $rb{$_}) { |
215 |
|
|
if (exists $ra{$_}) { |
216 |
|
|
$ra{$_} -= $rb{$_}; |
217 |
|
|
delete $ra{$_} if $ra{$_} <= 0; |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
%ra; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
package WAIT::Query::Raw; |
226 |
|
|
use strict; |
227 |
|
|
use Carp; |
228 |
|
|
|
229 |
|
|
sub new { |
230 |
|
|
my $type = shift; |
231 |
|
|
my $self = shift; |
232 |
|
|
|
233 |
|
|
$self = {} unless defined $self; |
234 |
|
|
bless $self, ref($type) || $type; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
sub clone { |
238 |
|
|
my $self = shift; |
239 |
|
|
my %copy; |
240 |
|
|
|
241 |
|
|
for (keys %$self) { |
242 |
|
|
$copy{$_} = [@{$self->{$_}}]; |
243 |
|
|
} |
244 |
|
|
$self->new(\%copy); |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
# Modifies first argument |
248 |
|
|
sub merge { |
249 |
|
|
my $self = shift; |
250 |
|
|
my $other = shift; |
251 |
|
|
|
252 |
|
|
croak "$other is not at 'WAIT::Query'" unless ref($other) =~ /^WAIT::Query/; |
253 |
|
|
for (keys %$other) { |
254 |
|
|
if (exists $self->{$_}) { |
255 |
|
|
push @{$self->{$_}}, @{$other->{$_}} |
256 |
|
|
} else { |
257 |
|
|
$self->{$_} = $other->{$_}; |
258 |
|
|
} |
259 |
|
|
} |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
1; |