File Coverage

blib/lib/Fey/Literal/Function.pm
Criterion Covered Total %
statement 33 33 100.0
branch 2 2 100.0
condition n/a
subroutine 11 11 100.0
pod 2 3 66.6
total 48 49 97.9


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