| 1 | package Fey::Role::HasAliasName; | ||||||||||||||
| 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 | 25 | our $VERSION = '0.42'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 27 | ▶ | use Fey::Types qw( Bool Str ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 27 | ▶ | use MooseX::Role::Parameterized 0.04; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 25 | parameter 'generated_alias_prefix' => ( | |||||||||||||
| 14 | isa => Str, | ||||||||||||||
| 15 | required => 1, | ||||||||||||||
| 16 | ); | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 25 | parameter 'sql_needs_parens' => ( | |||||||||||||
| 19 | isa => Bool, | ||||||||||||||
| 20 | default => 0, | ||||||||||||||
| 21 | ); | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 25 | has 'alias_name' => ( | |||||||||||||
| 24 | is => 'rw', | ||||||||||||||
| 25 | isa => Str, | ||||||||||||||
| 26 | writer => 'set_alias_name', | ||||||||||||||
| 27 | ); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 25 | requires 'sql'; | |||||||||||||
| 30 | |||||||||||||||
| 31 | sub alias { | ||||||||||||||
| 32 | 1 | ▶ | $_[0]->set_alias_name( $_[1] ); | ||||||||||||
|
Subroutine
alias: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 33 | 1 | return $_[0]; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | sub sql_with_alias { | ||||||||||||||
| 37 | 12 | ▶ | $_[0]->_make_alias() | ||||||||||||
|
Subroutine
sql_with_alias: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 38 | unless $_[0]->alias_name(); | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 12 | my $sql = $_[0]->_sql_for_alias( $_[1] ); | |||||||||||||
| 41 | |||||||||||||||
| 42 | 12 | $sql .= ' AS '; | |||||||||||||
| 43 | 12 | $sql .= $_[1]->quote_identifier( $_[0]->alias_name() ); | |||||||||||||
| 44 | |||||||||||||||
| 45 | 12 | return $sql; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | sub sql_or_alias { | ||||||||||||||
| 49 | 29 | ▶ | return $_[1]->quote_identifier( $_[0]->alias_name() ) | ||||||||||||
|
Subroutine
sql_or_alias: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 50 | if $_[0]->alias_name(); | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 23 | return $_[0]->sql( $_[1] ); | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | role { | ||||||||||||||
| 56 | 158 | ▶ | my $p = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 57 | |||||||||||||||
| 58 | 158 | my $parens = $p->sql_needs_parens(); | |||||||||||||
| 59 | |||||||||||||||
| 60 | method _sql_for_alias => sub { | ||||||||||||||
| 61 | 12 | ▶ | my $sql = $_[0]->sql( $_[1] ); | ||||||||||||
|
Subroutine
_sql_for_alias: called CC=2 SCAR=7.1
| |||||||||||||||
| 62 | 12 | ▶ | $sql = "( $sql )" if $parens; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 63 | 12 | return $sql; | |||||||||||||
| 64 | 158 | }; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 158 | my $prefix = $p->generated_alias_prefix(); | |||||||||||||
| 67 | 158 | my $num = 0; | |||||||||||||
| 68 | |||||||||||||||
| 69 | method '_make_alias' => sub { | ||||||||||||||
| 70 | 10 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_make_alias: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 71 | 10 | $self->set_alias_name( $prefix . $num++ ); | |||||||||||||
| 72 | 158 | }; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 25 | }; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 25 | 1; | |||||||||||||
| 77 | |||||||||||||||
| 78 | # ABSTRACT: A role for objects that bring an alias with them | ||||||||||||||
| 79 | |||||||||||||||
| 80 | __END__ | ||||||||||||||
| 81 | |||||||||||||||
| 82 | =pod | ||||||||||||||
| 83 | |||||||||||||||
| 84 | =head1 NAME | ||||||||||||||
| 85 | |||||||||||||||
| 86 | Fey::Role::HasAliasName - A role for objects that bring an alias with them | ||||||||||||||
| 87 | |||||||||||||||
| 88 | =head1 VERSION | ||||||||||||||
| 89 | |||||||||||||||
| 90 | version 0.42 | ||||||||||||||
| 91 | |||||||||||||||
| 92 | =head1 SYNOPSIS | ||||||||||||||
| 93 | |||||||||||||||
| 94 | package My::Thing; | ||||||||||||||
| 95 | |||||||||||||||
| 96 | use Moose 0.90; | ||||||||||||||
| 97 | with 'Fey::Role::HasAliasName' | ||||||||||||||
| 98 | => { generated_alias_prefix => 'THING' }; | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||||||
| 101 | |||||||||||||||
| 102 | This role adds an C<alias_name> attribute to objects, as well as some | ||||||||||||||
| 103 | methods for making use of that alias. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =head1 PARAMETERS | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head2 generated_alias_prefix | ||||||||||||||
| 108 | |||||||||||||||
| 109 | The prefix that generated aliases will have, e.g. C<LITERAL>, | ||||||||||||||
| 110 | C<FUNCTION>, etc. Required. | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =head2 sql_needs_parens | ||||||||||||||
| 113 | |||||||||||||||
| 114 | If true, C<sql_with_alias()> will wrap the output of C<sql()> when | ||||||||||||||
| 115 | generating its own output. Default is false. | ||||||||||||||
| 116 | |||||||||||||||
| 117 | =head1 METHODS | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =head2 $obj->alias_name() | ||||||||||||||
| 120 | |||||||||||||||
| 121 | Returns the current alias name, if any. | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =head2 $obj->set_alias_name() | ||||||||||||||
| 124 | |||||||||||||||
| 125 | $obj->set_alias_name('my object'); | ||||||||||||||
| 126 | |||||||||||||||
| 127 | Sets the current alias name. | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =head2 $obj->alias() | ||||||||||||||
| 130 | |||||||||||||||
| 131 | $obj->alias('my object')->do_something_else(...); | ||||||||||||||
| 132 | |||||||||||||||
| 133 | Sets the current alias name, then returns the object. | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =head2 $obj->sql_with_alias() | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =head2 $obj->sql_or_alias() | ||||||||||||||
| 138 | |||||||||||||||
| 139 | Returns the appropriate SQL snippet. C<sql_with_alias> will generate | ||||||||||||||
| 140 | an alias if one has not been set (using C<generated_alias_prefix>, | ||||||||||||||
| 141 | above). | ||||||||||||||
| 142 | |||||||||||||||
| 143 | =head1 BUGS | ||||||||||||||
| 144 | |||||||||||||||
| 145 | See L<Fey> for details on how to report bugs. | ||||||||||||||
| 146 | |||||||||||||||
| 147 | =head1 AUTHOR | ||||||||||||||
| 148 | |||||||||||||||
| 149 | Dave Rolsky <autarch@urth.org> | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 152 | |||||||||||||||
| 153 | This software is Copyright (c) 2011 - 2015 by Dave Rolsky. | ||||||||||||||
| 154 | |||||||||||||||
| 155 | This is free software, licensed under: | ||||||||||||||
| 156 | |||||||||||||||
| 157 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||
| 158 | |||||||||||||||
| 159 | =cut | ||||||||||||||