File Coverage

blib/lib/Fey/Literal/Function.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             package Fey::Literal::Function;
2             BEGIN {
3 1     1   19 $Fey::Literal::Function::VERSION = '0.40';
4             }
5              
6 1     1   5 use strict;
  1         2  
  1         26  
7 1     1   5 use warnings;
  1         9  
  1         33  
8 1     1   5 use namespace::autoclean;
  1         2  
  1         6  
9              
10 1     1   586 use Fey::Types qw( ArrayRefOfFunctionArgs Str );
  0            
  0            
11             use Scalar::Util qw( blessed );
12              
13             use Moose;
14             use MooseX::SemiAffordanceAccessor;
15             use MooseX::StrictConstructor;
16              
17             with 'Fey::Role::Comparable',
18             'Fey::Role::Selectable',
19             'Fey::Role::Orderable',
20             'Fey::Role::Groupable' => { -excludes => 'is_groupable' },
21             'Fey::Role::IsLiteral';
22              
23             with 'Fey::Role::HasAliasName' => { generated_alias_prefix => 'FUNCTION' };
24              
25             has 'function' => (
26             is => 'ro',
27             isa => Str,
28             required => 1,
29             );
30              
31             has 'args' => (
32             is => 'ro',
33             isa => ArrayRefOfFunctionArgs,
34             default => sub { [] },
35             coerce => 1,
36             );
37              
38             sub BUILDARGS {
39             my $class = shift;
40              
41             return {
42             function => shift,
43             args => [@_],
44             };
45             }
46              
47             sub sql {
48             my $sql = $_[0]->function();
49             $sql .= '(';
50              
51             $sql .= (
52             join ', ',
53             map { $_->sql( $_[1] ) } @{ $_[0]->args() }
54             );
55             $sql .= ')';
56             }
57              
58             sub is_groupable { $_[0]->alias_name() ? 1 : 0 }
59              
60             __PACKAGE__->meta()->make_immutable();
61              
62             1;
63              
64             # ABSTRACT: Represents a literal function in a SQL statement
65              
66              
67              
68             =pod
69              
70             =head1 NAME
71              
72             Fey::Literal::Function - Represents a literal function in a SQL statement
73              
74             =head1 VERSION
75              
76             version 0.40
77              
78             =head1 SYNOPSIS
79              
80             my $function = Fey::Literal::Function->new( 'LENGTH', $column );
81              
82             =head1 DESCRIPTION
83              
84             This class represents a literal function in a SQL statement, such as
85             C<NOW()> or C<LENGTH(User.username)>.
86              
87             =head1 INHERITANCE
88              
89             This module is a subclass of C<Fey::Literal>.
90              
91             =head1 METHODS
92              
93             This class provides the following methods:
94              
95             =head2 Fey::Literal::Function->new( $function, @args )
96              
97             This method creates a new C<Fey::Literal::Function> object.
98              
99             It requires at least one argument, which is the name of the SQL
100             function that this literal represents. It can accept any number of
101             additional optional arguments. These arguments must be either scalars,
102             literals, or columns which belong to a table.
103              
104             Any scalars passed in as arguments will be passed in turn to C<<
105             Fey::Literal->new_from_scalar() >>.
106              
107             =head2 $function->set_alias_name($alias)
108              
109             Use this to explicitly set a function's alias name for use in SQL. If
110             you don't set this it will be autogenerated as needed.
111              
112             =head2 $function->function()
113              
114             The function's name, as passed to the constructor.
115              
116             =head2 $function->args()
117              
118             Returns an array reference of the function's arguments, as passed to
119             the constructor.
120              
121             =head2 $function->id()
122              
123             The id for a function is uniquely identifies the function.
124              
125             =head2 $function->sql()
126              
127             =head2 $function->sql_with_alias()
128              
129             =head2 $function->sql_or_alias()
130              
131             Returns the appropriate SQL snippet.
132              
133             Calling C<< $function->sql_with_alias() >> causes a unique alias for
134             the function to be created.
135              
136             =head1 ROLES
137              
138             This class does the C<Fey::Role::Selectable>, C<Fey::Role::Comparable>,
139             C<Fey::Role::Groupable>, C<Fey::Role::Orderable>, and
140             C<Fey::Role::HasAliasName> roles.
141              
142             This class overrides the C<is_groupable()> and C<is_orderable()>
143             methods so that they only return true if the C<<
144             $function->sql_with_alias() >> has been called previously. This
145             function is called when a function is used in the C<SELECT> clause of
146             a query. A function must be used in a C<SELECT> in order to be used in
147             a C<GROUP BY> or C<ORDER BY> clause.
148              
149             =head1 BUGS
150              
151             See L<Fey> for details on how to report bugs.
152              
153             =head1 AUTHOR
154              
155             Dave Rolsky <autarch@urth.org>
156              
157             =head1 COPYRIGHT AND LICENSE
158              
159             This software is Copyright (c) 2011 by Dave Rolsky.
160              
161             This is free software, licensed under:
162              
163             The Artistic License 2.0 (GPL Compatible)
164              
165             =cut
166              
167              
168             __END__
169