| 1 | package Fey::Literal::Function; | ||||||||||
| 2 | |||||||||||
| 3 | 27 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 27 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 27 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 27 | our $VERSION = '0.43'; | |||||||||
| 8 | |||||||||||
| 9 | 27 | ▶ | use Fey::Types qw( ArrayRefOfFunctionArgs Str ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 27 | ▶ | use Scalar::Util qw( blessed ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 27 | ▶ | use Moose 2.1200; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 27 | ▶ | use MooseX::SemiAffordanceAccessor 0.03; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 27 | ▶ | use MooseX::StrictConstructor 0.13; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 27 | 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 | 27 | with 'Fey::Role::HasAliasName' => { generated_alias_prefix => 'FUNCTION' }; | |||||||||
| 23 | |||||||||||
| 24 | 27 | has 'function' => ( | |||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | isa => Str, | ||||||||||
| 27 | required => 1, | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | has 'args' => ( | ||||||||||
| 31 | is => 'ro', | ||||||||||
| 32 | isa => ArrayRefOfFunctionArgs, | ||||||||||
| 33 | 0 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 34 | 27 | coerce => 1, | |||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | sub BUILDARGS { | ||||||||||
| 38 | 67 | ▶ | my $class = shift; | ||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 39 | |||||||||||
| 40 | return { | ||||||||||
| 41 | 67 | function => shift, | |||||||||
| 42 | args => [@_], | ||||||||||
| 43 | }; | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub sql { | ||||||||||
| 47 | 19 | ▶ | my $sql = $_[0]->function(); | ||||||||
|
Subroutine
sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 48 | 19 | $sql .= '('; | |||||||||
| 49 | |||||||||||
| 50 | $sql .= ( | ||||||||||
| 51 | join ', ', | ||||||||||
| 52 | 19 | map { $_->sql( $_[1] ) } @{ $_[0]->args() } | |||||||||
| 53 | ); | ||||||||||
| 54 | 19 | $sql .= ')'; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | sub is_groupable { $_[0]->alias_name() ? 1 : 0 } | ||||||||
|
Subroutine
is_groupable: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 27 | __PACKAGE__->meta()->make_immutable(); | |||||||||
| 60 | |||||||||||
| 61 | 27 | 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.43 | ||||||||||
| 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 | ||||||||||