| 1 | package Fey::Literal::Term; | ||||||||||||||||||||||||||||
| 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( Bool LiteralTermArg ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 27 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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', 'Fey::Role::Selectable', | |||||||||||||||||||||||||||
| 17 | 'Fey::Role::Orderable', 'Fey::Role::Groupable', | ||||||||||||||||||||||||||||
| 18 | 'Fey::Role::IsLiteral'; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 27 | has 'term' => ( | |||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||
| 22 | isa => LiteralTermArg, | ||||||||||||||||||||||||||||
| 23 | required => 1, | ||||||||||||||||||||||||||||
| 24 | coerce => 1, | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 27 | has can_have_alias => ( | |||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||
| 29 | isa => Bool, | ||||||||||||||||||||||||||||
| 30 | default => 1, | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 27 | with 'Fey::Role::HasAliasName' => { generated_alias_prefix => 'TERM' }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub BUILDARGS { | ||||||||||||||||||||||||||||
| 36 | 13 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 13 | return { term => [@_] }; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub sql { | ||||||||||||||||||||||||||||
| 42 | 19 | ▶ | my ( $self, $dbh ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
sql: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | return join( | ||||||||||||||||||||||||||||
| 45 | '', | ||||||||||||||||||||||||||||
| 46 | map { | ||||||||||||||||||||||||||||
| 47 | 24 | ▶ | blessed($_) && $_->can('sql_or_alias') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($_) and $_->can("sql_or_alias")
MC/DC100%2/2
builtin::blessed($_) and $_->can("sql_or_alias")
builtin::blessed($_)
$_->can("sql_or_alias")
Truth table100%3/3
A and B
A= builtin::blessed($_)B= $_->can("sql_or_alias")
| |||||||||||||||||||||||||||||
| 48 | ? $_->sql_or_alias($dbh) | ||||||||||||||||||||||||||||
| 49 | : $_ | ||||||||||||||||||||||||||||
| 50 | 19 | } @{ $self->term() } | |||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # XXX - this bit of wackness is necessary because MX::Role::Parameterized | ||||||||||||||||||||||||||||
| 55 | # doesn't support -alias or -excludes, but we want to provide our own version | ||||||||||||||||||||||||||||
| 56 | # of sql_with_alias. | ||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 27 | my $meta = __PACKAGE__->meta(); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 27 | my $method = $meta->remove_method('sql_with_alias'); | |||||||||||||||||||||||||||
| 61 | 27 | $meta->add_method( _han_sql_with_alias => $method ); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | my $sql_with_alias = sub { | ||||||||||||||||||||||||||||
| 64 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
sql_with_alias: called CC=2 SCAR=9.2
Subroutine
__ANON__: not called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 65 | 3 | my $dbh = shift; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | return $self->can_have_alias() | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 68 | ? $self->_han_sql_with_alias($dbh) | ||||||||||||||||||||||||||||
| 69 | : $self->sql($dbh); | ||||||||||||||||||||||||||||
| 70 | 27 | }; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 27 | $meta->add_method( sql_with_alias => $sql_with_alias ); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | before 'set_alias_name' => sub { | ||||||||||||||||||||||||||||
| 76 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 3 | ▶ | croak 'This term cannot have an alias' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | unless $self->can_have_alias(); | ||||||||||||||||||||||||||||
| 80 | 27 | }; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 27 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 27 | 1; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # ABSTRACT: Represents a literal term in a SQL statement | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =pod | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 NAME | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Fey::Literal::Term - Represents a literal term in a SQL statement | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 VERSION | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | version 0.43 | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | my $term = Fey::Literal::Term->new(@anything) | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | This class represents a literal term in a SQL statement. A "term" in this | ||||||||||||||||||||||||||||
| 107 | module means a literal SQL snippet that will be used verbatim, without | ||||||||||||||||||||||||||||
| 108 | quoting. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | This allows you to create SQL for almost any expression, for example | ||||||||||||||||||||||||||||
| 111 | C<EXTRACT( DOY FROM TIMESTAMP "User.creation_date" )>, which is a valid Postgres | ||||||||||||||||||||||||||||
| 112 | expression. This would be created like this: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | my $term = | ||||||||||||||||||||||||||||
| 115 | Fey::Literal::Term->new | ||||||||||||||||||||||||||||
| 116 | ( 'DOY FROM TIMESTAMP ', $column ); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | my $function = Fey::Literal::Function->new( 'EXTRACT', $term ); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | This ability to insert arbitrary strings into a SQL statement is meant | ||||||||||||||||||||||||||||
| 121 | to be used as a back-door to support any sort of SQL snippet not | ||||||||||||||||||||||||||||
| 122 | otherwise supported by the core Fey classes in a more direct manner. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | This module is a subclass of C<Fey::Literal>. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 METHODS | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | This class provides the following methods: | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 Fey::Literal::Term->new(@fragments) | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | This method creates a new C<Fey::Literal::Term> object representing | ||||||||||||||||||||||||||||
| 135 | the term passed to the constructor. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | More than one argument may be given; they will all be joined together | ||||||||||||||||||||||||||||
| 138 | in the generated SQL. For example: | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | my $term = Fey::Literal::Term->new( $column, '::text' ); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | The arguments can be plain scalars, objects with a C<sql_or_alias()> | ||||||||||||||||||||||||||||
| 143 | method (columns, tables, etc.) or any object which is overloaded (the | ||||||||||||||||||||||||||||
| 144 | assumption being it that it overloads stringification). | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 $term->term() | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Returns the array reference of fragments passed to the constructor. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 $term->can_have_alias() | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 $term->set_can_have_alias() | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | If this attribute is explicitly set to a false value, then then the | ||||||||||||||||||||||||||||
| 155 | SQL-generating methods below will never include an alias. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 $term->id() | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | The id for a term is uniquely identifies the term. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 $term->sql() | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 $term->sql_with_alias() | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 $term->sql_or_alias() | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Returns the appropriate SQL snippet. If the term contains any Fey objects, | ||||||||||||||||||||||||||||
| 168 | their C<sql_or_alias()> method is called to generate their part of the term. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 DETAILS OF SQL GENERATION | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | A term generates SQL by taking each of the elements passed to its constructor | ||||||||||||||||||||||||||||
| 173 | and concatenating them. If the element is an object with a C<sql_or_alias()> | ||||||||||||||||||||||||||||
| 174 | method, that method will be called to generate SQL. Otherwise, the element is | ||||||||||||||||||||||||||||
| 175 | just used as-is. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | If C<< $term->can_have_alias() >> is false, then calling any of the three | ||||||||||||||||||||||||||||
| 178 | SQL-generating methods is always equivalent to calling C<< $term->sql() >>. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 ROLES | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | This class does the C<Fey::Role::Selectable>, C<Fey::Role::Comparable>, | ||||||||||||||||||||||||||||
| 183 | C<Fey::Role::Groupable>, and C<Fey::Role::Orderable> roles. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Of course, the contents of a given term may not really allow for any | ||||||||||||||||||||||||||||
| 186 | of these things, but having this class do these roles means you can | ||||||||||||||||||||||||||||
| 187 | freely use a term object in any part of a SQL snippet. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 BUGS | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | See L<Fey> for details on how to report bugs. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | This software is Copyright (c) 2011 - 2015 by Dave Rolsky. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||