File Coverage

blib/lib/Fey/SQL.pm
Criterion Covered Total %
statement 8 10 80.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 12 14 85.7


line stmt bran cond sub pod time code
1             package Fey::SQL;
2             BEGIN {
3 1     1   26647 $Fey::SQL::VERSION = '0.40';
4             }
5              
6 1     1   41 use strict;
  1         2  
  1         37  
7 1     1   5 use warnings;
  1         2  
  1         40  
8              
9 1     1   601 use Fey::SQL::Delete;
  0            
  0            
10             use Fey::SQL::Insert;
11             use Fey::SQL::Select;
12             use Fey::SQL::Update;
13             use Fey::SQL::Where;
14             use Fey::SQL::Union;
15             use Fey::SQL::Intersect;
16             use Fey::SQL::Except;
17             use Fey::Types;
18              
19             sub new_delete {
20             shift;
21             return Fey::SQL::Delete->new(@_);
22             }
23              
24             sub new_insert {
25             shift;
26             return Fey::SQL::Insert->new(@_);
27             }
28              
29             sub new_select {
30             shift;
31             return Fey::SQL::Select->new(@_);
32             }
33              
34             sub new_update {
35             shift;
36             return Fey::SQL::Update->new(@_);
37             }
38              
39             sub new_where {
40             shift;
41             return Fey::SQL::Where->new(@_);
42             }
43              
44             sub new_union {
45             shift;
46             return Fey::SQL::Union->new(@_);
47             }
48              
49             sub new_intersect {
50             shift;
51             return Fey::SQL::Intersect->new(@_);
52             }
53              
54             sub new_except {
55             shift;
56             return Fey::SQL::Except->new(@_);
57             }
58              
59             1;
60              
61             # ABSTRACT: Documentation on SQL generation with Fey and SQL object factory
62              
63              
64              
65             =pod
66              
67             =head1 NAME
68              
69             Fey::SQL - Documentation on SQL generation with Fey and SQL object factory
70              
71             =head1 VERSION
72              
73             version 0.40
74              
75             =head1 SYNOPSIS
76              
77             my $sql = Fey::SQL->new_select();
78              
79             $sql->select( @columns );
80              
81             =head1 DESCRIPTION
82              
83             This module mostly exists to provide documentation and a factory
84             interface for making SQL statement objects.
85              
86             For convenience, loading this module loads all of the C<Fey::SQL::*>
87             classes, such as L<Fey::SQL::Select>, L<Fey::SQL::Delete>, etc.
88              
89             =head1 METHODS
90              
91             This class acts as a factory for the various SQL statement classes,
92             such as L<Fey::SQL::Select> or L<Fey::SQL::Update>. This is simply
93             sugar which makes it easy to replace C<Fey::SQL> with a subclass,
94             either for your application or for a specific DBMS.
95              
96             =head2 Fey::SQL->new_select()
97              
98             Returns a new C<Fey::SQL::Select> object.
99              
100             =head2 Fey::SQL->new_insert()
101              
102             Returns a new C<Fey::SQL::Insert> object.
103              
104             =head2 Fey::SQL->new_update()
105              
106             Returns a new C<Fey::SQL::Update> object.
107              
108             =head2 Fey::SQL->new_delete()
109              
110             Returns a new C<Fey::SQL::Delete> object.
111              
112             =head2 Fey::SQL->new_where()
113              
114             Returns a new C<Fey::SQL::Where> object.
115              
116             =head2 Fey::SQL->new_union()
117              
118             Returns a new C<Fey::SQL::Union> object.
119              
120             =head2 Fey::SQL->new_intersect()
121              
122             Returns a new C<Fey::SQL::Intersect> object.
123              
124             =head2 Fey::SQL->new_except()
125              
126             Returns a new C<Fey::SQL::Except> object.
127              
128             =head1 CREATING SQL
129              
130             This documentation covers the clauses in SQL queries which are shared
131             across different types of queries, including C<WHERE>, C<ORDER BY>,
132             and C<LIMIT>. For SQL clauses that are specific to one type of query,
133             see the appropriate subclass. For example, for C<SELECT> clauses, see
134             the L<Fey::SQL::Select> class documentation.
135              
136             =head2 WHERE Clauses
137              
138             Many types of queries allow C<WHERE> clauses via the a C<where()>
139             method. The method accepts several different types of parameters:
140              
141             =head3 Comparisons
142              
143             Comparing a column to a given value ...
144              
145             # WHERE Part.size = $value}
146             $sql->where( $size, '=', $value );
147              
148             # WHERE Part.size = AVG(Part.size);
149             $sql->where( $size, '=', $avg_size_function );
150              
151             # WHERE Part.size = ?
152             $sql->where( $size, '=', $placeholder );
153              
154             # WHERE User.user_id = Message.user_id
155             $sql->where( $user_id, '=', $other_user_id );
156              
157             The left-hand side of a conditional does not need to be a column
158             object, it could be a function or anything that produces valid SQL.
159              
160             my $length = Fey::Literal::Function->new( 'LENGTH', $name );
161             # WHERE LENGTH(Part.name) = 10
162             $sql->where( $length, '=', 10 );
163              
164             The second parameter in a conditional can be any comparison operator that
165             produces valid SQL:
166              
167             # WHERE Message.body LIKE 'hello%'
168             $sql->where( $body, 'LIKE', 'hello%' );
169              
170             # WHERE Part.quantity > 10
171             $sql->where( $quantity, '>', 10 );
172              
173             If you use a comparison operator like C<BETWEEN> or C<(NOT) IN>, you
174             can pass more than three parameters to C<where()>.
175              
176             # WHERE Part.size BETWEEN 4 AND 10
177             $sql->where( $size, 'BETWEEN', 4, 10 );
178              
179             # WHERE User.user_id IN (1, 2, 7, 9)
180             $sql->where( $user_id, 'IN', 1, 2, 7, 9 );
181              
182             You can also pass a subselect when using C<IN>.
183              
184             my $select = $sql->select(...);
185              
186             # WHERE User.user_id IN ( SELECT user_id FROM ... )
187             $sql->where( $user_id, 'IN', $select );
188              
189             If you use C<=>, C<!=>, or C<< <> >> as the comparison and the
190             right-hand side is C<undef>, then the generated query will use C<IS
191             NULL> or C<IS NOT NULL>, as appropriate:
192              
193             # WHERE Part.name IS NULL
194             $sql->where( $name, '=', undef );
195              
196             # WHERE Part.name IS NOT NULL
197             $sql->where( $name, '!=', undef );
198              
199             Note that if you use a placeholder object in this case, then the query
200             will not be transformed into an C<IS (NOT) NULL> expression, since the
201             value of the placeholder is not known when the SQL is being generated.
202              
203             You can also use C<and()> instead of where if you like the look ...
204              
205             $sql->where( $size, '=', $value )
206             ->and ( $quantity, '>', 10 );
207              
208             The C<and()> method is just sugar, since by default, multiple calls to
209             C<where()> end up concatenated with an C<AND> in the resulting SQL.
210              
211             =head3 Boolean AND/OR
212              
213             You can pass the strings "and" and "or" to the C<where()> method in
214             order to create complex boolean conditions. When you call C<where()>
215             with multiple comparisons in a row, an implicit "and" is added between
216             each one.
217              
218             # WHERE Part.size > 10 OR Part.size = 5
219             $sql->where( $size, '>', 10 );
220             $sql->where( 'or' );
221             $sql->where( $size, '=', 5 );
222              
223             # WHERE Part.size > 10 AND Part.size < 20
224             $sql->where( $size, '>', 10 );
225             # there is an implicit $sql->where( 'and' ) here ...
226             $sql->where( $size, '<', 10 );
227              
228             =head3 What Comparison Operators Are Valid?
229              
230             Basically, any operator should work, and there is no check that a particular operator is valid.
231              
232             Some operators are special-cased, specifically C<BETWEEN>, C<IN>, and C<NOT
233             IN>. If you use C<BETWEEN> as the operator, you are expected to pass I<two>
234             items after it. If you use C<IN> or C<NOT IN>, you can pass as many items as
235             you need to on the right hand side.
236              
237             =head3 What Can Be Compared?
238              
239             When you call C<where()> to do a comparison, you can pass any of the following
240             types of things:
241              
242             =over 4
243              
244             =item * An object which has an C<is_comparable()> method that returns true
245              
246             This includes objects which do the L<Fey::ColumnLike> role: L<Fey::Column> and
247             L<Fey::Column::Alias>. A column only returns true for C<is_comparable()> when
248             it is actually attached to a table.
249              
250             Objects which do the L<Fey::Role::Comaprable> role: L<Fey::SQL::Select>,
251             L<Fey::SQL::Union>, L<Fey::SQL::Intersection>, and L<Fey::SQL::Except> always
252             return true for C<is_comparable()>.
253              
254             If you try to compare something to something that returns a data set, you must
255             be using an equality comparison operator (C<=>, C<!=>, etc), C<IN>, or, C<NOT
256             IN>.
257              
258             Also, all L<Fey::Literal> subclasses return true for C<is_comparable()>:
259             L<Fey::Literal::Function>, L<Fey::Literal::Null>, L<Fey::Literal::Number>,
260             L<Fey::Literal::String>, and L<Fey::Literal::Term>.
261              
262             Finally, you can pass a L<Fey::Placeholder> object.
263              
264             =item * An unblessed non-reference scalar
265              
266             This can be C<undef>, a string, or a number. This scalar will be passed to C<<
267             Fey::Literal->new_from_scalar() >> and converted into an appropriate
268             L<Fey::Literal> object.
269              
270             =item * An object which returns true for C<overload::Overloaded($object)>
271              
272             This will be stringified (C<$object .= q{}>) and passed to C<<
273             Fey::Literal->new_from_scalar() >>.
274              
275             =back
276              
277             =head3 NULL In Comparisons
278              
279             Fey does the right thing for NULLs used in equality comparisons, generating
280             C<IS NULL> and C<IS NOT NULL> as appropriate.
281              
282             =head2 Subgroups
283              
284             You can pass the strings "(" and ")" to the C<where()> method in order
285             to create subgroups.
286              
287             # WHERE Part.size > 10
288             # AND ( User.name = 'Widget'
289             # OR
290             # User.name = 'Grommit' )
291             $sql->where( $size, '>', 10 );
292             $sql->where( '(' );
293             $sql->where( $name, '=', 'Widget' );
294             $sql->where( 'or' );
295             $sql->where( $name, '=', 'Grommit' );
296             $sql->where( ')' );
297              
298             =head2 ORDER BY Clauses
299              
300             Many types of queries allow C<ORDER BY> clauses via the C<order_by()>
301             method. This method accepts a list of items. The items in the list may
302             be columns, functions, terms, or sort directions ("ASC" or
303             "DESC"). The sort direction can also specify "NULLS FIRST" or "NULLS
304             LAST".
305              
306             # ORDER BY Part.size
307             $sql->order_by( $size );
308              
309             # ORDER BY Part.size DESC
310             $sql->order_by( $size, 'DESC' );
311              
312             # ORDER BY Part.size DESC, Part.name ASC
313             $sql->order_by( $size, 'DESC', $name, 'ASC' );
314              
315             # ORDER BY Part.size ASC NULLS FIRST
316             $sql->order_by( $size, 'ASC NULLS FIRST' );
317              
318             my $length = Fey::Literal::Function->new( 'LENGTH', $name );
319             # ORDER BY LENGTH( Part.name ) ASC
320             $sql->order_by( $length, 'ASC' );
321              
322             If you pass a function literal to the C<order_by()> method and the
323             literal was used previously in the select clause, then an alias is
324             used in the C<ORDER BY> clause.
325              
326             my $length = Fey::Literal::Function->new( 'LENGTH', $name );
327             $sql->select($length);
328              
329             # SELECT LENGTH(Part.name) AS FUNCTION0 ...
330             # ORDER BY FUNCTION0 ASC
331             $sql->order_by( $length, 'ASC' );
332              
333             =head2 LIMIT Clauses
334              
335             Many types of queries allow C<LIMIT> clauses via the C<limit()>
336             method. This method accepts two parameters, with the second being
337             optional.
338              
339             The first parameter is the number of items. The second, optional
340             parameter, is the offset for the limit clause.
341              
342             # LIMIT 10
343             $sql->limit( 10 );
344              
345             # LIMIT 10 OFFSET 20
346             $sql->limit( 10, 20 );
347              
348             # OFFSET 20
349             $sql->limit( undef, 20 );
350              
351             =head2 Bind Parameters
352              
353             By default, whenever you pass a non-object value where a placeholder
354             could go, the SQL class replaces this with a placeholder and stores
355             the value as a bind parameter. This applies to things like C<WHERE>
356             and C<HAVING> clauses, as well as the C<VALUES> clause of an
357             C<INSERT>, and the C<SET> clause of an C<UPDATE>.
358              
359             You can retrieve the bind parameters by calling C<<
360             $sql->bind_params() >>. These will be returned in the proper order for
361             passing to C<DBI>'s C<execute()> method.
362              
363             If you do not want values automatically converted to placeholders, you
364             can turn this behavior off by setting C<auto_placeholders> to a false
365             value when creating the object:
366              
367             my $select = Fey::SQL->new_select( auto_placeholders => 0 );
368              
369             In this case, values will be quoted as needed and inserted directly
370             into the generated SQL.
371              
372             =head2 Cloning
373              
374             Every SQL object has a C<clone()> method. This is useful if you want
375             to have an object that you use as the base for multiple queries.
376              
377             my $user_select = Fey::SQL->new_select( $user_table )
378             ->from( $user_table);
379              
380             my $select_new =
381             $user_select->clone()
382             ->where( $creation_column, '>=', $six_months_ago );
383              
384             my $select_old
385             $user_select->clone()
386             ->where( $creation_column, '<', $six_months_ago );
387              
388             =head2 Overloaded Objects as Parameters
389              
390             Any method which accepts a plain scalar can also take an overloaded
391             object that overloads stringification or numification. This includes
392             C<WHERE> clause comparisons, C<VALUES> in an C<INSERT>, and C<SET>
393             clauses in an C<UPDATE>.
394              
395             =head1 BUGS
396              
397             See L<Fey> for details on how to report bugs.
398              
399             =head1 AUTHOR
400              
401             Dave Rolsky <autarch@urth.org>
402              
403             =head1 COPYRIGHT AND LICENSE
404              
405             This software is Copyright (c) 2011 by Dave Rolsky.
406              
407             This is free software, licensed under:
408              
409             The Artistic License 2.0 (GPL Compatible)
410              
411             =cut
412              
413              
414             __END__
415