line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# This module is for creating complex select queries with |
2
|
|
|
|
|
|
|
# Relations. |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Relations::Query; |
5
|
|
|
|
|
|
|
require Exporter; |
6
|
|
|
|
|
|
|
require Relations; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
3797
|
use Relations; |
|
1
|
|
|
|
|
2057
|
|
|
1
|
|
|
|
|
189
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# You can run this file through either pod2man or pod2html to produce pretty |
11
|
|
|
|
|
|
|
# documentation in manual or html file format (these utilities are part of the |
12
|
|
|
|
|
|
|
# Perl 5 distribution). |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Copyright 2001 GAF-3 Industries, Inc. All rights reserved. |
15
|
|
|
|
|
|
|
# Written by George A. Fitch III (aka Gaffer), gaf3@gaf3.com |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# This program is free software, you can redistribute it and/or modify it under |
18
|
|
|
|
|
|
|
# the same terms as Perl istelf |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Do the version thing |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$Relations::Query::VERSION='0.93'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
@EXPORT = qw( |
27
|
|
|
|
|
|
|
new |
28
|
|
|
|
|
|
|
to_string |
29
|
|
|
|
|
|
|
to_text |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
33
|
|
|
|
|
|
|
new |
34
|
|
|
|
|
|
|
clone |
35
|
|
|
|
|
|
|
add |
36
|
|
|
|
|
|
|
set |
37
|
|
|
|
|
|
|
get |
38
|
|
|
|
|
|
|
get_add |
39
|
|
|
|
|
|
|
get_set |
40
|
|
|
|
|
|
|
to_string |
41
|
|
|
|
|
|
|
to_text |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
%EXPORT_TAGS = (); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Be strict |
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1320
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
### Creates a Relations::Query object. It takes |
53
|
|
|
|
|
|
|
### info for each part of the query, and stores |
54
|
|
|
|
|
|
|
### it into the new object. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub new { |
57
|
|
|
|
|
|
|
|
58
|
7
|
|
|
7
|
1
|
41
|
my ($type) = shift; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Get all the arguments passed, which are named |
61
|
|
|
|
|
|
|
# for their part of the query. |
62
|
|
|
|
|
|
|
|
63
|
7
|
|
|
|
|
34
|
my ($select, |
64
|
|
|
|
|
|
|
$from, |
65
|
|
|
|
|
|
|
$where, |
66
|
|
|
|
|
|
|
$group_by, |
67
|
|
|
|
|
|
|
$having, |
68
|
|
|
|
|
|
|
$order_by, |
69
|
|
|
|
|
|
|
$limit) = rearrange(['SELECT', |
70
|
|
|
|
|
|
|
'FROM', |
71
|
|
|
|
|
|
|
'WHERE', |
72
|
|
|
|
|
|
|
'GROUP_BY', |
73
|
|
|
|
|
|
|
'HAVING', |
74
|
|
|
|
|
|
|
'ORDER_BY', |
75
|
|
|
|
|
|
|
'LIMIT'],@_); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Create the hash to hold all the vars |
78
|
|
|
|
|
|
|
# for this object. |
79
|
|
|
|
|
|
|
|
80
|
7
|
|
|
|
|
276
|
my $self = {}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Bless it with the type sent (I think this |
83
|
|
|
|
|
|
|
# makes it a full fledged object) |
84
|
|
|
|
|
|
|
|
85
|
7
|
|
|
|
|
17
|
bless $self, $type; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Add the sent info into the hash |
88
|
|
|
|
|
|
|
|
89
|
7
|
|
|
|
|
20
|
$self->{'select'} = as_clause($select); |
90
|
7
|
|
|
|
|
154
|
$self->{'from'} = as_clause($from); |
91
|
7
|
|
|
|
|
123
|
$self->{'where'} = equals_clause($where); |
92
|
7
|
|
|
|
|
94
|
$self->{'group_by'} = comma_clause($group_by); |
93
|
7
|
|
|
|
|
84
|
$self->{'having'} = equals_clause($having); |
94
|
7
|
|
|
|
|
98
|
$self->{'order_by'} = comma_clause($order_by); |
95
|
7
|
|
|
|
|
85
|
$self->{'limit'} = comma_clause($limit); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Give thyself |
98
|
|
|
|
|
|
|
|
99
|
7
|
|
|
|
|
83
|
return $self; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
### Creates a copy of a current query. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub clone { |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Get the self reference first |
110
|
|
|
|
|
|
|
|
111
|
3
|
|
|
3
|
1
|
11
|
my ($self) = shift; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Create a new query object using this query |
114
|
|
|
|
|
|
|
# object's info |
115
|
|
|
|
|
|
|
|
116
|
3
|
|
|
|
|
12
|
my ($clone) = new Relations::Query($self->{'select'}, |
117
|
|
|
|
|
|
|
$self->{'from'}, |
118
|
|
|
|
|
|
|
$self->{'where'}, |
119
|
|
|
|
|
|
|
$self->{'group_by'}, |
120
|
|
|
|
|
|
|
$self->{'having'}, |
121
|
|
|
|
|
|
|
$self->{'order_by'}, |
122
|
|
|
|
|
|
|
$self->{'limit'}); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Return the new object |
125
|
|
|
|
|
|
|
|
126
|
3
|
|
|
|
|
7
|
return $clone; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
### Gets the query for the object in string form. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub get { |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Get the self reference first |
137
|
|
|
|
|
|
|
|
138
|
10
|
|
|
10
|
1
|
94
|
my($self) = shift; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Create an array to hold the query pieces |
141
|
|
|
|
|
|
|
|
142
|
10
|
|
|
|
|
16
|
my @query = (); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Add info where appropriate. |
145
|
|
|
|
|
|
|
|
146
|
10
|
50
|
|
|
|
44
|
push @query, "select $self->{'select'}" if length($self->{'select'}); |
147
|
10
|
100
|
|
|
|
33
|
push @query, "from $self->{'from'}" if length($self->{'from'}); |
148
|
10
|
100
|
|
|
|
32
|
push @query, "where $self->{'where'}" if length($self->{'where'}); |
149
|
10
|
100
|
|
|
|
29
|
push @query, "group by $self->{'group_by'}" if length($self->{'group_by'}); |
150
|
10
|
100
|
|
|
|
27
|
push @query, "having $self->{'having'}" if length($self->{'having'}); |
151
|
10
|
100
|
|
|
|
27
|
push @query, "order by $self->{'order_by'}" if length($self->{'order_by'}); |
152
|
10
|
100
|
|
|
|
51
|
push @query, "limit $self->{'limit'}" if length($self->{'limit'}); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# Return the info, delimitted by a space. |
155
|
|
|
|
|
|
|
|
156
|
10
|
|
|
|
|
69
|
return join ' ', @query; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
### Adds data to the existing clauses of the query. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub add { |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
# Get the self reference first |
167
|
|
|
|
|
|
|
|
168
|
2
|
|
|
2
|
1
|
21
|
my ($self) = shift; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# Get all the other arguments passed |
171
|
|
|
|
|
|
|
|
172
|
2
|
|
|
|
|
12
|
my ($select, |
173
|
|
|
|
|
|
|
$from, |
174
|
|
|
|
|
|
|
$where, |
175
|
|
|
|
|
|
|
$group_by, |
176
|
|
|
|
|
|
|
$having, |
177
|
|
|
|
|
|
|
$order_by, |
178
|
|
|
|
|
|
|
$limit) = rearrange(['SELECT', |
179
|
|
|
|
|
|
|
'FROM', |
180
|
|
|
|
|
|
|
'WHERE', |
181
|
|
|
|
|
|
|
'GROUP_BY', |
182
|
|
|
|
|
|
|
'HAVING', |
183
|
|
|
|
|
|
|
'ORDER_BY', |
184
|
|
|
|
|
|
|
'LIMIT'],@_); |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# Concatente info into the self hash, prefixing it if there's |
187
|
|
|
|
|
|
|
# already something there, only if something's actually been |
188
|
|
|
|
|
|
|
# sent. |
189
|
|
|
|
|
|
|
|
190
|
2
|
|
|
|
|
166
|
$self->{'select'} = add_as_clause($self->{'select'},$select); |
191
|
2
|
|
|
|
|
66
|
$self->{'from'} = add_as_clause($self->{'from'},$from); |
192
|
2
|
|
|
|
|
94
|
$self->{'where'} = add_equals_clause($self->{'where'},$where); |
193
|
2
|
|
|
|
|
56
|
$self->{'group_by'} = add_comma_clause($self->{'group_by'},$group_by); |
194
|
2
|
|
|
|
|
46
|
$self->{'having'} = add_equals_clause($self->{'having'},$having); |
195
|
2
|
|
|
|
|
51
|
$self->{'order_by'} = add_comma_clause($self->{'order_by'},$order_by); |
196
|
2
|
|
|
|
|
54
|
$self->{'limit'} = add_comma_clause($self->{'limit'},$limit); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
### Sets the existing settings of a query. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub set { |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# Get the self reference first |
207
|
|
|
|
|
|
|
|
208
|
3
|
|
|
3
|
1
|
36
|
my ($self) = shift; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# Get all the other arguments passed, which |
211
|
|
|
|
|
|
|
# are named for their part of the query. |
212
|
|
|
|
|
|
|
|
213
|
3
|
|
|
|
|
14
|
my ($select, |
214
|
|
|
|
|
|
|
$from, |
215
|
|
|
|
|
|
|
$where, |
216
|
|
|
|
|
|
|
$group_by, |
217
|
|
|
|
|
|
|
$having, |
218
|
|
|
|
|
|
|
$order_by, |
219
|
|
|
|
|
|
|
$limit) = rearrange(['SELECT', |
220
|
|
|
|
|
|
|
'FROM', |
221
|
|
|
|
|
|
|
'WHERE', |
222
|
|
|
|
|
|
|
'GROUP_BY', |
223
|
|
|
|
|
|
|
'HAVING', |
224
|
|
|
|
|
|
|
'ORDER_BY', |
225
|
|
|
|
|
|
|
'LIMIT'],@_); |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
# Put info into the self hash, only if something's actually been |
228
|
|
|
|
|
|
|
# sent. |
229
|
|
|
|
|
|
|
|
230
|
3
|
|
|
|
|
232
|
$self->{'select'} = set_as_clause($self->{'select'},$select); |
231
|
3
|
|
|
|
|
62
|
$self->{'from'} = set_as_clause($self->{'from'},$from); |
232
|
3
|
|
|
|
|
78
|
$self->{'where'} = set_equals_clause($self->{'where'},$where); |
233
|
3
|
|
|
|
|
49
|
$self->{'group_by'} = set_comma_clause($self->{'group_by'},$group_by); |
234
|
3
|
|
|
|
|
74
|
$self->{'having'} = set_equals_clause($self->{'having'},$having); |
235
|
3
|
|
|
|
|
47
|
$self->{'order_by'} = set_comma_clause($self->{'order_by'},$order_by); |
236
|
3
|
|
|
|
|
65
|
$self->{'limit'} = set_comma_clause($self->{'limit'},$limit); |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
### Gets the string form of the query object, and accepts |
243
|
|
|
|
|
|
|
### extra info to temporarily add on to the current |
244
|
|
|
|
|
|
|
### clause. The added info will be in the returned string, |
245
|
|
|
|
|
|
|
### but will not be stored in the query object. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub get_add { |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# Get the self reference first |
250
|
|
|
|
|
|
|
|
251
|
1
|
|
|
1
|
1
|
20
|
my ($self) = shift; |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
# Create a clone of ourselves |
254
|
|
|
|
|
|
|
|
255
|
1
|
|
|
|
|
4
|
my ($get_add) = $self->clone(); |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
# Add the stuff sent to our clone |
258
|
|
|
|
|
|
|
|
259
|
1
|
|
|
|
|
5
|
$get_add->add(@_); |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
# Return our fattened clone's query |
262
|
|
|
|
|
|
|
|
263
|
1
|
|
|
|
|
24
|
return $get_add->get(); |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
### Gets the string form of the query object, and accepts |
270
|
|
|
|
|
|
|
### extra info to temporarily overwrite the current |
271
|
|
|
|
|
|
|
### clause. The set info will be in the returned string, |
272
|
|
|
|
|
|
|
### but will not be stored in the query object. |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
sub get_set { |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
# Get the self reference first |
277
|
|
|
|
|
|
|
|
278
|
1
|
|
|
1
|
1
|
17
|
my ($self) = shift; |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
# Create a clone of ourselves |
281
|
|
|
|
|
|
|
|
282
|
1
|
|
|
|
|
4
|
my ($get_set) = $self->clone(); |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
# Set the stuff sent to our clone |
285
|
|
|
|
|
|
|
|
286
|
1
|
|
|
|
|
4
|
$get_set->set(@_); |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
# Return our altered clone's query |
289
|
|
|
|
|
|
|
|
290
|
1
|
|
|
|
|
8
|
return $get_set->get(); |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
### Takes a hash ref, Relations::Query object, or string |
297
|
|
|
|
|
|
|
### and returns a string. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
sub to_string { |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
# Get the query sent |
302
|
|
|
|
|
|
|
|
303
|
4
|
|
|
4
|
1
|
28
|
my ($query) = shift; |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
# If we were sent a hash reference, create a new |
306
|
|
|
|
|
|
|
# Relations::Query object. |
307
|
|
|
|
|
|
|
|
308
|
4
|
100
|
|
|
|
18
|
$query = new Relations::Query($query) if ref($query) eq 'HASH'; |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
# If we were sent a query object, get the query |
311
|
|
|
|
|
|
|
# string from it. |
312
|
|
|
|
|
|
|
|
313
|
4
|
100
|
|
|
|
16
|
$query = $query->get() if ref($query) eq 'Relations::Query'; |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
# Return the query string |
316
|
|
|
|
|
|
|
|
317
|
4
|
|
|
|
|
14
|
return $query; |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
### Returns text info about the Relations::Query |
324
|
|
|
|
|
|
|
### object. Useful for debugging and export purposes. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
sub to_text { |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
# Know thyself |
329
|
|
|
|
|
|
|
|
330
|
0
|
|
|
0
|
1
|
|
my ($self) = shift; |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
# Get the indenting string and current |
333
|
|
|
|
|
|
|
# indenting amount. |
334
|
|
|
|
|
|
|
|
335
|
0
|
|
|
|
|
|
my ($string,$current) = @_; |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
# Calculate the ident amount so we don't |
338
|
|
|
|
|
|
|
# do it a bazillion times. |
339
|
|
|
|
|
|
|
|
340
|
0
|
|
|
|
|
|
my $indent = ($string x $current); |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
# Create a text string to hold everything |
343
|
|
|
|
|
|
|
|
344
|
0
|
|
|
|
|
|
my $text = ''; |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
# 411 |
347
|
|
|
|
|
|
|
|
348
|
0
|
|
|
|
|
|
$text .= $indent . "Relations::Query: $self\n\n"; |
349
|
0
|
|
|
|
|
|
$text .= $indent . "Select: $self->{select}\n"; |
350
|
0
|
|
|
|
|
|
$text .= $indent . "From: $self->{from}\n"; |
351
|
0
|
|
|
|
|
|
$text .= $indent . "Where: $self->{where}\n"; |
352
|
0
|
|
|
|
|
|
$text .= $indent . "Group By: $self->{group_by}\n"; |
353
|
0
|
|
|
|
|
|
$text .= $indent . "Having: $self->{having}\n"; |
354
|
0
|
|
|
|
|
|
$text .= $indent . "Order By: $self->{order_by}\n"; |
355
|
0
|
|
|
|
|
|
$text .= $indent . "Limit: $self->{limit}\n"; |
356
|
|
|
|
|
|
|
|
357
|
0
|
|
|
|
|
|
$text .= "\n"; |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
# Return the text |
360
|
|
|
|
|
|
|
|
361
|
0
|
|
|
|
|
|
return $text; |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
$Relations::Query::VERSION; |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
__END__ |