| 1 | # Copyright (c) 2016 Timm Murray | ||||||||||||||||||||||||||||
| 2 | # All rights reserved. | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Redistribution and use in source and binary forms, with or without | ||||||||||||||||||||||||||||
| 5 | # modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # * Redistributions of source code must retain the above copyright notice, | ||||||||||||||||||||||||||||
| 8 | # this list of conditions and the following disclaimer. | ||||||||||||||||||||||||||||
| 9 | # * Redistributions in binary form must reproduce the above copyright | ||||||||||||||||||||||||||||
| 10 | # notice, this list of conditions and the following disclaimer in the | ||||||||||||||||||||||||||||
| 11 | # documentation and/or other materials provided with the distribution. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||||||||||||||||||||||||
| 14 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||||||||||||||||||||||||
| 15 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||||||||||||||||||||||||||
| 16 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||||||||||||||||||||||||||||
| 17 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||||||||||||||||||||||||||||
| 18 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||||||||||||||||||||||||||||
| 19 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||||||||||||||||||||||||||||
| 20 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||||||||||||||||||||||||||||
| 21 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||||||||||||||||||||||||||||
| 22 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||||||||||||||||||||||||||
| 23 | # POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 24 | package SQL::Functional; | ||||||||||||||||||||||||||||
| 25 | 32 | $SQL::Functional::VERSION = '0.2'; | |||||||||||||||||||||||||||
| 26 | 32 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 32 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 32 | ▶ | use SQL::Functional::AndClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 32 | ▶ | use SQL::Functional::DeleteClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 32 | ▶ | use SQL::Functional::DistinctClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 32 | ▶ | use SQL::Functional::FromClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 32 | ▶ | use SQL::Functional::FunctionClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 32 | ▶ | use SQL::Functional::GroupByClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 32 | ▶ | use SQL::Functional::InsertClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 32 | ▶ | use SQL::Functional::JoinClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 32 | ▶ | use SQL::Functional::LimitClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 32 | ▶ | use SQL::Functional::LiteralClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 32 | ▶ | use SQL::Functional::MatchClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 32 | ▶ | use SQL::Functional::NullClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 32 | ▶ | use SQL::Functional::OrClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 32 | ▶ | use SQL::Functional::OrderByClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 32 | ▶ | use SQL::Functional::PlaceholderClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 32 | ▶ | use SQL::Functional::SelectClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 32 | ▶ | use SQL::Functional::SetClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 32 | ▶ | use SQL::Functional::TableClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 32 | ▶ | use SQL::Functional::TruncateClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 32 | ▶ | use SQL::Functional::UpdateClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 32 | ▶ | use SQL::Functional::ValuesClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 32 | ▶ | use SQL::Functional::VerbatimClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 32 | ▶ | use SQL::Functional::WhereClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 32 | ▶ | use SQL::Functional::WrapClause; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 32 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 32 | our @ISA = qw{ Exporter }; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 32 | our @EXPORT_OK = qw{ | |||||||||||||||||||||||||||
| 56 | SELECT | ||||||||||||||||||||||||||||
| 57 | star | ||||||||||||||||||||||||||||
| 58 | field | ||||||||||||||||||||||||||||
| 59 | col | ||||||||||||||||||||||||||||
| 60 | FROM | ||||||||||||||||||||||||||||
| 61 | WHERE | ||||||||||||||||||||||||||||
| 62 | match | ||||||||||||||||||||||||||||
| 63 | match_verbatim | ||||||||||||||||||||||||||||
| 64 | op | ||||||||||||||||||||||||||||
| 65 | table | ||||||||||||||||||||||||||||
| 66 | ORDER_BY | ||||||||||||||||||||||||||||
| 67 | DESC | ||||||||||||||||||||||||||||
| 68 | INNER_JOIN | ||||||||||||||||||||||||||||
| 69 | JOIN | ||||||||||||||||||||||||||||
| 70 | LEFT_JOIN | ||||||||||||||||||||||||||||
| 71 | LEFT_OUTER_JOIN | ||||||||||||||||||||||||||||
| 72 | RIGHT_JOIN | ||||||||||||||||||||||||||||
| 73 | RIGHT_OUTER_JOIN | ||||||||||||||||||||||||||||
| 74 | FULL_JOIN | ||||||||||||||||||||||||||||
| 75 | FULL_OUTER_JOIN | ||||||||||||||||||||||||||||
| 76 | OUTER_JOIN | ||||||||||||||||||||||||||||
| 77 | SUBSELECT | ||||||||||||||||||||||||||||
| 78 | AND | ||||||||||||||||||||||||||||
| 79 | OR | ||||||||||||||||||||||||||||
| 80 | INSERT | ||||||||||||||||||||||||||||
| 81 | INTO | ||||||||||||||||||||||||||||
| 82 | VALUES | ||||||||||||||||||||||||||||
| 83 | UPDATE | ||||||||||||||||||||||||||||
| 84 | SET | ||||||||||||||||||||||||||||
| 85 | DELETE | ||||||||||||||||||||||||||||
| 86 | wrap | ||||||||||||||||||||||||||||
| 87 | IS_NULL | ||||||||||||||||||||||||||||
| 88 | IS_NOT_NULL | ||||||||||||||||||||||||||||
| 89 | GROUP_BY | ||||||||||||||||||||||||||||
| 90 | LIMIT | ||||||||||||||||||||||||||||
| 91 | TRUNCATE | ||||||||||||||||||||||||||||
| 92 | DISTINCT | ||||||||||||||||||||||||||||
| 93 | func | ||||||||||||||||||||||||||||
| 94 | literal | ||||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | 32 | our @EXPORT = @EXPORT_OK; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # ABSTRACT: Create SQL programmatically | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub SELECT ($$@) | ||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 27 | ▶ | my ($fields, @clauses) = @_; | ||||||||||||||||||||||||||
|
Subroutine
SELECT: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 27 | my @fields; | |||||||||||||||||||||||||||
| 105 | 27 | my $is_distinct = 0; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 27 | ▶ | if( ref $fields eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 108 | @fields = map { | ||||||||||||||||||||||||||||
| 109 | 12 | ▶ | (ref($_) && $_->isa( 'SQL::Functional::FieldClause' )) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_ and $_->isa("SQL::Functional::FieldClause")
MC/DC50%1/2
ref $_ and $_->isa("SQL::Functional::FieldClause")
ref $_
$_->isa("SQL::Functional::FieldClause")
Truth table66%2/3
A and B
A= ref $_B= $_->isa("SQL::Functional::FieldClause")
| |||||||||||||||||||||||||||||
| 110 | ? $_ | ||||||||||||||||||||||||||||
| 111 | : SQL::Functional::FieldClause->new({ | ||||||||||||||||||||||||||||
| 112 | name => $_, | ||||||||||||||||||||||||||||
| 113 | }); | ||||||||||||||||||||||||||||
| 114 | } @$fields; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | elsif( $fields->isa( 'SQL::Functional::FieldClause' ) ) { | ||||||||||||||||||||||||||||
| 117 | 13 | @fields = ($fields); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | elsif( $fields->isa( 'SQL::Functional::DistinctClause' ) ) { | ||||||||||||||||||||||||||||
| 120 | 1 | @fields = ($fields->fields); | |||||||||||||||||||||||||||
| 121 | 1 | $is_distinct = 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||
| 124 | 1 | @fields = ( SQL::Functional::FieldClause->new({ | |||||||||||||||||||||||||||
| 125 | name => $fields, | ||||||||||||||||||||||||||||
| 126 | }) ); | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 27 | my $clause = SQL::Functional::SelectClause->new( | |||||||||||||||||||||||||||
| 130 | fields => \@fields, | ||||||||||||||||||||||||||||
| 131 | clauses => \@clauses, | ||||||||||||||||||||||||||||
| 132 | is_distinct => $is_distinct, | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | 27 | return ($clause->to_string, $clause->get_params); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub star () | ||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | 15 | ▶ | return field( '*' ); | ||||||||||||||||||||||||||
|
Subroutine
star: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | sub field ($) | ||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||
| 144 | 17 | ▶ | my ($name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
field: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 17 | return SQL::Functional::FieldClause->new({ | |||||||||||||||||||||||||||
| 146 | name => $name, | ||||||||||||||||||||||||||||
| 147 | }); | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 32 | *col = \&field; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub table($) | ||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||
| 153 | 40 | ▶ | my ($name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
table: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 40 | my $table = SQL::Functional::TableClause->new({ | |||||||||||||||||||||||||||
| 155 | name => $name, | ||||||||||||||||||||||||||||
| 156 | }); | ||||||||||||||||||||||||||||
| 157 | 40 | return $table; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 32 | *INTO = \&table; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub FROM (@) | ||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 33 | ▶ | my (@tables) = @_; | ||||||||||||||||||||||||||
|
Subroutine
FROM: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | my @table_objs = map { | ||||||||||||||||||||||||||||
| 165 | 33 | my $result; | |||||||||||||||||||||||||||
| 166 | 34 | ▶ | if( ref $_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 8 | $result = $_; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 26 | $result = table $_; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 34 | $result; | |||||||||||||||||||||||||||
| 174 | } @tables; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 33 | my $clause = SQL::Functional::FromClause->new({ | |||||||||||||||||||||||||||
| 177 | tables => \@table_objs | ||||||||||||||||||||||||||||
| 178 | }); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 33 | return $clause; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | sub WHERE ($) | ||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||
| 185 | 20 | ▶ | my ($clause) = @_; | ||||||||||||||||||||||||||
|
Subroutine
WHERE: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 20 | my @params = $clause->params; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 20 | my $where = SQL::Functional::WhereClause->new({ | |||||||||||||||||||||||||||
| 189 | params => \@params, | ||||||||||||||||||||||||||||
| 190 | sub_clause => $clause, | ||||||||||||||||||||||||||||
| 191 | }); | ||||||||||||||||||||||||||||
| 192 | 20 | return $where; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | sub match($$$) | ||||||||||||||||||||||||||||
| 196 | { | ||||||||||||||||||||||||||||
| 197 | 28 | ▶ | my ($field, $op, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 28 | ▶ | my $field_obj = ref($field) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | ? $field | ||||||||||||||||||||||||||||
| 201 | : SQL::Functional::FieldClause->new({ | ||||||||||||||||||||||||||||
| 202 | name => $field, | ||||||||||||||||||||||||||||
| 203 | }); | ||||||||||||||||||||||||||||
| 204 | 28 | ▶ | my $clause_value = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $value and $value->does("SQL::Functional::Clause")
MC/DC50%1/2
ref $value and $value->does("SQL::Functional::Clause")
ref $value
$value->does("SQL::Functional::Clause")
Truth table66%2/3
A and B
A= ref $valueB= $value->does("SQL::Functional::Clause")
| |||||||||||||||||||||||||||||
| 205 | ref($value) && $value->does( 'SQL::Functional::Clause' ) | ||||||||||||||||||||||||||||
| 206 | ? $value | ||||||||||||||||||||||||||||
| 207 | : SQL::Functional::PlaceholderClause->new({ | ||||||||||||||||||||||||||||
| 208 | value => $value, | ||||||||||||||||||||||||||||
| 209 | }); | ||||||||||||||||||||||||||||
| 210 | 28 | my $clause = SQL::Functional::MatchClause->new({ | |||||||||||||||||||||||||||
| 211 | field => $field_obj, | ||||||||||||||||||||||||||||
| 212 | op => $op, | ||||||||||||||||||||||||||||
| 213 | value => $clause_value, | ||||||||||||||||||||||||||||
| 214 | }); | ||||||||||||||||||||||||||||
| 215 | 28 | return $clause; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 32 | *op = \&match; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | sub match_verbatim($$$) | ||||||||||||||||||||||||||||
| 220 | { | ||||||||||||||||||||||||||||
| 221 | 1 | ▶ | my ($field, $op, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
match_verbatim: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 1 | ▶ | my $field_obj = ref($field) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | ? $field | ||||||||||||||||||||||||||||
| 225 | : SQL::Functional::FieldClause->new({ | ||||||||||||||||||||||||||||
| 226 | name => $field, | ||||||||||||||||||||||||||||
| 227 | }); | ||||||||||||||||||||||||||||
| 228 | 1 | ▶ | my $clause_value = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $value and $value->does("SQL::Functional::Clause")
MC/DC0%0/2
ref $value and $value->does("SQL::Functional::Clause")
ref $value
$value->does("SQL::Functional::Clause")
Truth table33%1/3
A and B
A= ref $valueB= $value->does("SQL::Functional::Clause")
| |||||||||||||||||||||||||||||
| 229 | ref($value) && $value->does( 'SQL::Functional::Clause' ) | ||||||||||||||||||||||||||||
| 230 | ? $value | ||||||||||||||||||||||||||||
| 231 | : SQL::Functional::VerbatimClause->new({ | ||||||||||||||||||||||||||||
| 232 | value => $value, | ||||||||||||||||||||||||||||
| 233 | }); | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 1 | my $clause = SQL::Functional::MatchClause->new({ | |||||||||||||||||||||||||||
| 236 | field => $field_obj, | ||||||||||||||||||||||||||||
| 237 | op => $op, | ||||||||||||||||||||||||||||
| 238 | value => $clause_value, | ||||||||||||||||||||||||||||
| 239 | }); | ||||||||||||||||||||||||||||
| 240 | 1 | return $clause; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | sub ORDER_BY($;@) | ||||||||||||||||||||||||||||
| 244 | { | ||||||||||||||||||||||||||||
| 245 | 2 | ▶ | my (@fields) = @_; | ||||||||||||||||||||||||||
|
Subroutine
ORDER_BY: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 246 | 2 | my $clause = SQL::Functional::OrderByClause->new({ | |||||||||||||||||||||||||||
| 247 | fields => \@fields, | ||||||||||||||||||||||||||||
| 248 | }); | ||||||||||||||||||||||||||||
| 249 | 2 | return $clause; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | sub DESC($) | ||||||||||||||||||||||||||||
| 253 | { | ||||||||||||||||||||||||||||
| 254 | 1 | ▶ | my ($field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DESC: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 255 | # TODO should this be an object? It'd be consistent with everything | ||||||||||||||||||||||||||||
| 256 | # else to make it one. Is there an argument besides consistency? | ||||||||||||||||||||||||||||
| 257 | # Seems just fine like this so far . . . | ||||||||||||||||||||||||||||
| 258 | 1 | return "$field DESC"; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | sub INNER_JOIN($$$) | ||||||||||||||||||||||||||||
| 262 | { | ||||||||||||||||||||||||||||
| 263 | 3 | ▶ | my ($table, $field1, $field2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
INNER_JOIN: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 264 | 3 | my $clause = SQL::Functional::JoinClause->new( | |||||||||||||||||||||||||||
| 265 | table => $table, | ||||||||||||||||||||||||||||
| 266 | field1 => $field1, | ||||||||||||||||||||||||||||
| 267 | field2 => $field2, | ||||||||||||||||||||||||||||
| 268 | type => 'inner', | ||||||||||||||||||||||||||||
| 269 | ); | ||||||||||||||||||||||||||||
| 270 | 3 | return $clause; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | sub LEFT_JOIN($$$) | ||||||||||||||||||||||||||||
| 274 | { | ||||||||||||||||||||||||||||
| 275 | 1 | ▶ | my ($table, $field1, $field2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
LEFT_JOIN: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 276 | 1 | my $clause = SQL::Functional::JoinClause->new( | |||||||||||||||||||||||||||
| 277 | table => $table, | ||||||||||||||||||||||||||||
| 278 | field1 => $field1, | ||||||||||||||||||||||||||||
| 279 | field2 => $field2, | ||||||||||||||||||||||||||||
| 280 | type => 'left', | ||||||||||||||||||||||||||||
| 281 | ); | ||||||||||||||||||||||||||||
| 282 | 1 | return $clause; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | sub RIGHT_JOIN($$$) | ||||||||||||||||||||||||||||
| 286 | { | ||||||||||||||||||||||||||||
| 287 | 1 | ▶ | my ($table, $field1, $field2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
RIGHT_JOIN: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 288 | 1 | my $clause = SQL::Functional::JoinClause->new( | |||||||||||||||||||||||||||
| 289 | table => $table, | ||||||||||||||||||||||||||||
| 290 | field1 => $field1, | ||||||||||||||||||||||||||||
| 291 | field2 => $field2, | ||||||||||||||||||||||||||||
| 292 | type => 'right', | ||||||||||||||||||||||||||||
| 293 | ); | ||||||||||||||||||||||||||||
| 294 | 1 | return $clause; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | sub FULL_JOIN($$$) | ||||||||||||||||||||||||||||
| 298 | { | ||||||||||||||||||||||||||||
| 299 | 1 | ▶ | my ($table, $field1, $field2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
FULL_JOIN: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 300 | 1 | my $clause = SQL::Functional::JoinClause->new( | |||||||||||||||||||||||||||
| 301 | table => $table, | ||||||||||||||||||||||||||||
| 302 | field1 => $field1, | ||||||||||||||||||||||||||||
| 303 | field2 => $field2, | ||||||||||||||||||||||||||||
| 304 | type => 'full', | ||||||||||||||||||||||||||||
| 305 | ); | ||||||||||||||||||||||||||||
| 306 | 1 | return $clause; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 32 | *JOIN = \&INNER_JOIN; | |||||||||||||||||||||||||||
| 310 | 32 | *LEFT_OUTER_JOIN = \&LEFT_JOIN; | |||||||||||||||||||||||||||
| 311 | 32 | *RIGHT_OUTER_JOIN = \&RIGHT_JOIN; | |||||||||||||||||||||||||||
| 312 | 32 | *FULL_OUTER_JOIN = \&FULL_JOIN; | |||||||||||||||||||||||||||
| 313 | 32 | *OUTER_JOIN = \&FULL_JOIN; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | sub SUBSELECT($$@) | ||||||||||||||||||||||||||||
| 316 | { | ||||||||||||||||||||||||||||
| 317 | 4 | ▶ | my ($fields, @clauses) = @_; | ||||||||||||||||||||||||||
|
Subroutine
SUBSELECT: called CC=5 SCAR=17.1
Pod
documented
| |||||||||||||||||||||||||||||
| 318 | 4 | my @fields; | |||||||||||||||||||||||||||
| 319 | 4 | ▶ | if( ref $fields eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 320 | @fields = map { | ||||||||||||||||||||||||||||
| 321 | 2 | ▶ | (ref($_) && $_->isa( 'SQL::Functional::FieldClause' )) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_ and $_->isa("SQL::Functional::FieldClause")
MC/DC0%0/2
ref $_ and $_->isa("SQL::Functional::FieldClause")
ref $_
$_->isa("SQL::Functional::FieldClause")
Truth table33%1/3
A and B
A= ref $_B= $_->isa("SQL::Functional::FieldClause")
| |||||||||||||||||||||||||||||
| 322 | ? $_ | ||||||||||||||||||||||||||||
| 323 | : SQL::Functional::FieldClause->new({ | ||||||||||||||||||||||||||||
| 324 | name => $_, | ||||||||||||||||||||||||||||
| 325 | }); | ||||||||||||||||||||||||||||
| 326 | } @$fields; | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | elsif( $fields->isa( 'SQL::Functional::FieldClause' ) ) { | ||||||||||||||||||||||||||||
| 329 | 2 | @fields = ($fields); | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | else { | ||||||||||||||||||||||||||||
| 332 | 0 | @fields = ( SQL::Functional::FieldClause->new({ | |||||||||||||||||||||||||||
| 333 | name => $fields, | ||||||||||||||||||||||||||||
| 334 | }) ); | ||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 4 | my $clause = SQL::Functional::SelectClause->new({ | |||||||||||||||||||||||||||
| 338 | fields => \@fields, | ||||||||||||||||||||||||||||
| 339 | clauses => \@clauses, | ||||||||||||||||||||||||||||
| 340 | }); | ||||||||||||||||||||||||||||
| 341 | 4 | return $clause; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | sub AND | ||||||||||||||||||||||||||||
| 345 | { | ||||||||||||||||||||||||||||
| 346 | 3 | ▶ | my (@clauses) = @_; | ||||||||||||||||||||||||||
|
Subroutine
AND: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 347 | 3 | my $clause = SQL::Functional::AndClause->new({ | |||||||||||||||||||||||||||
| 348 | clauses => \@clauses, | ||||||||||||||||||||||||||||
| 349 | }); | ||||||||||||||||||||||||||||
| 350 | 3 | return $clause; | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | sub OR | ||||||||||||||||||||||||||||
| 354 | { | ||||||||||||||||||||||||||||
| 355 | 3 | ▶ | my (@clauses) = @_; | ||||||||||||||||||||||||||
|
Subroutine
OR: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 356 | 3 | my $clause = SQL::Functional::OrClause->new({ | |||||||||||||||||||||||||||
| 357 | clauses => \@clauses, | ||||||||||||||||||||||||||||
| 358 | }); | ||||||||||||||||||||||||||||
| 359 | 3 | return $clause; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | sub INSERT ($$$) | ||||||||||||||||||||||||||||
| 363 | { | ||||||||||||||||||||||||||||
| 364 | 2 | ▶ | my ($into, $fields, $values) = @_; | ||||||||||||||||||||||||||
|
Subroutine
INSERT: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 365 | 2 | ▶ | my $clause = SQL::Functional::InsertClause->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | into => $into, | ||||||||||||||||||||||||||||
| 367 | fields => ref $fields ? $fields : [$fields], | ||||||||||||||||||||||||||||
| 368 | values => $values, | ||||||||||||||||||||||||||||
| 369 | ); | ||||||||||||||||||||||||||||
| 370 | 2 | return ($clause->to_string, $clause->get_params); | |||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | sub VALUES ($) | ||||||||||||||||||||||||||||
| 374 | { | ||||||||||||||||||||||||||||
| 375 | 1 | ▶ | my ($values) = @_; | ||||||||||||||||||||||||||
|
Subroutine
VALUES: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 376 | 1 | my $clause = SQL::Functional::ValuesClause->new( | |||||||||||||||||||||||||||
| 377 | params => $values, | ||||||||||||||||||||||||||||
| 378 | ); | ||||||||||||||||||||||||||||
| 379 | 1 | return $clause; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | sub UPDATE ($$;$) | ||||||||||||||||||||||||||||
| 383 | { | ||||||||||||||||||||||||||||
| 384 | 2 | ▶ | my ($table, $set, $where) = @_; | ||||||||||||||||||||||||||
|
Subroutine
UPDATE: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 385 | 2 | my $clause = SQL::Functional::UpdateClause->new( | |||||||||||||||||||||||||||
| 386 | table => $table, | ||||||||||||||||||||||||||||
| 387 | set => $set, | ||||||||||||||||||||||||||||
| 388 | where => $where, | ||||||||||||||||||||||||||||
| 389 | ); | ||||||||||||||||||||||||||||
| 390 | 2 | return ($clause->to_string, $clause->get_params); | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | sub SET | ||||||||||||||||||||||||||||
| 394 | { | ||||||||||||||||||||||||||||
| 395 | 2 | ▶ | my (@clauses) = @_; | ||||||||||||||||||||||||||
|
Subroutine
SET: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 396 | 2 | my $clause = SQL::Functional::SetClause->new( | |||||||||||||||||||||||||||
| 397 | clauses => \@clauses, | ||||||||||||||||||||||||||||
| 398 | ); | ||||||||||||||||||||||||||||
| 399 | 2 | return $clause; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | sub DELETE ($;$) | ||||||||||||||||||||||||||||
| 403 | { | ||||||||||||||||||||||||||||
| 404 | 2 | ▶ | my ($from, $where) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 405 | 2 | my $clause = SQL::Functional::DeleteClause->new( | |||||||||||||||||||||||||||
| 406 | from => $from, | ||||||||||||||||||||||||||||
| 407 | where => $where, | ||||||||||||||||||||||||||||
| 408 | ); | ||||||||||||||||||||||||||||
| 409 | 2 | return ($clause->to_string, $clause->get_params); | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | sub wrap ($) | ||||||||||||||||||||||||||||
| 413 | { | ||||||||||||||||||||||||||||
| 414 | 3 | ▶ | my ($clause) = @_; | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 415 | 3 | my $wrap = SQL::Functional::WrapClause->new({ | |||||||||||||||||||||||||||
| 416 | clause => $clause, | ||||||||||||||||||||||||||||
| 417 | }); | ||||||||||||||||||||||||||||
| 418 | 3 | return $wrap; | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | sub IS_NULL ($) | ||||||||||||||||||||||||||||
| 422 | { | ||||||||||||||||||||||||||||
| 423 | 1 | ▶ | my ($field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
IS_NULL: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 424 | 1 | ▶ | my $field_clause = ref $field | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | ? $field | ||||||||||||||||||||||||||||
| 426 | : SQL::Functional::FieldClause->new({ | ||||||||||||||||||||||||||||
| 427 | name => $field, | ||||||||||||||||||||||||||||
| 428 | }); | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 1 | my $clause = SQL::Functional::NullClause->new({ | |||||||||||||||||||||||||||
| 431 | field => $field_clause, | ||||||||||||||||||||||||||||
| 432 | }); | ||||||||||||||||||||||||||||
| 433 | 1 | return $clause; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | sub IS_NOT_NULL ($) | ||||||||||||||||||||||||||||
| 437 | { | ||||||||||||||||||||||||||||
| 438 | 1 | ▶ | my ($field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
IS_NOT_NULL: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 439 | 1 | ▶ | my $field_clause = ref $field | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 440 | ? $field | ||||||||||||||||||||||||||||
| 441 | : SQL::Functional::FieldClause->new({ | ||||||||||||||||||||||||||||
| 442 | name => $field, | ||||||||||||||||||||||||||||
| 443 | }); | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 1 | my $clause = SQL::Functional::NullClause->new( | |||||||||||||||||||||||||||
| 446 | field => $field_clause, | ||||||||||||||||||||||||||||
| 447 | not => 1, | ||||||||||||||||||||||||||||
| 448 | ); | ||||||||||||||||||||||||||||
| 449 | 1 | return $clause; | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | sub GROUP_BY ($) | ||||||||||||||||||||||||||||
| 453 | { | ||||||||||||||||||||||||||||
| 454 | 1 | ▶ | my ($field) = @_; | ||||||||||||||||||||||||||
|
Subroutine
GROUP_BY: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 455 | 1 | ▶ | my $field_clause = ref $field | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | ? $field | ||||||||||||||||||||||||||||
| 457 | : SQL::Functional::FieldClause->new({ | ||||||||||||||||||||||||||||
| 458 | name => $field, | ||||||||||||||||||||||||||||
| 459 | }); | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 1 | my $clause = SQL::Functional::GroupByClause->new({ | |||||||||||||||||||||||||||
| 462 | field => $field_clause, | ||||||||||||||||||||||||||||
| 463 | }); | ||||||||||||||||||||||||||||
| 464 | 1 | return $clause; | |||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | sub LIMIT ($) | ||||||||||||||||||||||||||||
| 468 | { | ||||||||||||||||||||||||||||
| 469 | 1 | ▶ | my ($num) = @_; | ||||||||||||||||||||||||||
|
Subroutine
LIMIT: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 470 | 1 | my $limit = SQL::Functional::LimitClause->new({ | |||||||||||||||||||||||||||
| 471 | num => $num, | ||||||||||||||||||||||||||||
| 472 | }); | ||||||||||||||||||||||||||||
| 473 | 1 | return $limit; | |||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | sub TRUNCATE($) | ||||||||||||||||||||||||||||
| 477 | { | ||||||||||||||||||||||||||||
| 478 | 1 | ▶ | my ($table) = @_; | ||||||||||||||||||||||||||
|
Subroutine
TRUNCATE: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 479 | 1 | my $trunc = SQL::Functional::TruncateClause->new({ | |||||||||||||||||||||||||||
| 480 | table => $table, | ||||||||||||||||||||||||||||
| 481 | }); | ||||||||||||||||||||||||||||
| 482 | 1 | return ($trunc->to_string, $trunc->get_params); | |||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | sub DISTINCT ($) | ||||||||||||||||||||||||||||
| 486 | { | ||||||||||||||||||||||||||||
| 487 | 1 | ▶ | my ($fields) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DISTINCT: called CC=5 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 488 | 1 | my @fields; | |||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 1 | ▶ | if( ref $fields eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 491 | @fields = map { | ||||||||||||||||||||||||||||
| 492 | 1 | ▶ | (ref($_) && $_->isa( 'SQL::Functional::FieldClause' )) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_ and $_->isa("SQL::Functional::FieldClause")
MC/DC0%0/2
ref $_ and $_->isa("SQL::Functional::FieldClause")
ref $_
$_->isa("SQL::Functional::FieldClause")
Truth table33%1/3
A and B
A= ref $_B= $_->isa("SQL::Functional::FieldClause")
| |||||||||||||||||||||||||||||
| 493 | ? $_ | ||||||||||||||||||||||||||||
| 494 | : SQL::Functional::FieldClause->new({ | ||||||||||||||||||||||||||||
| 495 | name => $_, | ||||||||||||||||||||||||||||
| 496 | }); | ||||||||||||||||||||||||||||
| 497 | } @$fields; | ||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | elsif( $fields->isa( 'SQL::Functional::FieldClause' ) ) { | ||||||||||||||||||||||||||||
| 500 | 0 | @fields = ($fields); | |||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||
| 502 | else { | ||||||||||||||||||||||||||||
| 503 | 0 | @fields = ( SQL::Functional::FieldClause->new({ | |||||||||||||||||||||||||||
| 504 | name => $fields, | ||||||||||||||||||||||||||||
| 505 | }) ); | ||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | 1 | my $clause = SQL::Functional::DistinctClause->new({ | |||||||||||||||||||||||||||
| 509 | fields => \@fields, | ||||||||||||||||||||||||||||
| 510 | }); | ||||||||||||||||||||||||||||
| 511 | 1 | return $clause; | |||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | sub func ($;@) | ||||||||||||||||||||||||||||
| 515 | { | ||||||||||||||||||||||||||||
| 516 | 1 | ▶ | my ($func_name, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
func: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | 1 | my $clause = SQL::Functional::FunctionClause->new({ | |||||||||||||||||||||||||||
| 519 | name => $func_name, | ||||||||||||||||||||||||||||
| 520 | args => \@args, | ||||||||||||||||||||||||||||
| 521 | }); | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | 1 | return $clause; | |||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | sub literal ($) | ||||||||||||||||||||||||||||
| 527 | { | ||||||||||||||||||||||||||||
| 528 | 0 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||
|
Subroutine
literal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 529 | 0 | my $clause = SQL::Functional::LiteralClause->new({ | |||||||||||||||||||||||||||
| 530 | literal => $str, | ||||||||||||||||||||||||||||
| 531 | }); | ||||||||||||||||||||||||||||
| 532 | 0 | return $clause; | |||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | 32 | 1; | |||||||||||||||||||||||||||
| 537 | __END__ | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | =head1 NAME | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | SQL::Functional - Create SQL programmatically | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | my ($select, @select_params) = SELECT star, | ||||||||||||||||||||||||||||
| 547 | FROM( 'foo' ), | ||||||||||||||||||||||||||||
| 548 | WHERE match( 'bar', '=', 1 ); | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | # Run through DBI | ||||||||||||||||||||||||||||
| 551 | my $sth = $dbh->prepare_cached( $select ) or die $dbh->errstr; | ||||||||||||||||||||||||||||
| 552 | $sth->execute( @select_params ) or die $sth->errstr; | ||||||||||||||||||||||||||||
| 553 | my $results = $sth->fetchall_arrayref; | ||||||||||||||||||||||||||||
| 554 | $sth->finish; | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | my ($insert, @insert_params) = INSERT INTO 'foo', | ||||||||||||||||||||||||||||
| 557 | [ | ||||||||||||||||||||||||||||
| 558 | 'bar', | ||||||||||||||||||||||||||||
| 559 | ], | ||||||||||||||||||||||||||||
| 560 | VALUES [ | ||||||||||||||||||||||||||||
| 561 | 1, | ||||||||||||||||||||||||||||
| 562 | ]; | ||||||||||||||||||||||||||||
| 563 | my ($update, @update_params) = UPDATE 'foo', SET( | ||||||||||||||||||||||||||||
| 564 | op( 'bar', '=', 1 ), | ||||||||||||||||||||||||||||
| 565 | op( 'baz', '=', 2 ), | ||||||||||||||||||||||||||||
| 566 | ), | ||||||||||||||||||||||||||||
| 567 | WHERE match( 'qux', '=', 3 ); | ||||||||||||||||||||||||||||
| 568 | my ($delete, @delete_params) = DELETE FROM( 'foo' ), | ||||||||||||||||||||||||||||
| 569 | WHERE match( 'bar', '=', 1 ); | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | Builds SQL programmatically through a function-based interface. | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | Generally, functions mapping to SQL keywords (like C<SELECT> or C<FROM>) are | ||||||||||||||||||||||||||||
| 578 | uppercase. Additional functions (like C<match> and C<table>) are lowercase. | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | =head2 Rawwrrr, why are you polluting my namespace?!! | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | Functions gotta go somewhere. If you want to keep it out of your top level | ||||||||||||||||||||||||||||
| 583 | namespace, but also want to keep things short, try this: | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | package Q; | ||||||||||||||||||||||||||||
| 586 | use SQL::Functional; | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | package main; | ||||||||||||||||||||||||||||
| 589 | my ($sql, @params) = Q::SELECT Q::star, Q::FROM 'foo'; | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | Not exactly pretty, but it works. | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | =head2 Top-level Functions | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | These return a list. The first element is the generated SQL. The rest are the | ||||||||||||||||||||||||||||
| 596 | bind params. | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | =head3 SELECT | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | =head3 INSERT | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | =head3 UPDATE | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | =head3 DELETE | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | =head2 Helper Functions | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | These are used to build the statement. Their return values eventually make | ||||||||||||||||||||||||||||
| 609 | their way to one of the top-level functions above. | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | =head3 star | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | A star, like the one you would use to say C<SELECT * FROM . . .>. | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | =head3 field | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | Creates a L<SQL::Functional::FieldClause> and returns it. You pass in the | ||||||||||||||||||||||||||||
| 618 | name of a field, like the names of columns.. | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | =head3 col | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | Alias for C<field()>. | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | =head3 FROM | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | FROM(qw{ foo bar baz }) | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | Creates a L<SQL::Functional::FromClause> and returns it. You pass in a list of | ||||||||||||||||||||||||||||
| 629 | tables as a string name. Alternatively, they can also be specified by a | ||||||||||||||||||||||||||||
| 630 | L<SQL::Functional::TableClause> object, which just so happens to be returned | ||||||||||||||||||||||||||||
| 631 | by the C<table> function: | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | my $foo_tbl = table 'foo'; | ||||||||||||||||||||||||||||
| 634 | my $from_clause = FROM $foo_tbl; | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | =head3 WHERE | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | Creates a L<SQL::Functional::WhereClause> and returns it. You pass in a | ||||||||||||||||||||||||||||
| 639 | single sub clause. Since the C<and> and C<or> functions are a single clause | ||||||||||||||||||||||||||||
| 640 | that take other clauses, you can chain these together to create your full | ||||||||||||||||||||||||||||
| 641 | C<WHERE> clause. | ||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | my $where_clause = WHERE AND( | ||||||||||||||||||||||||||||
| 644 | match( 'bar', '=', 1 ), | ||||||||||||||||||||||||||||
| 645 | OR( | ||||||||||||||||||||||||||||
| 646 | match( 'baz', '=', 2 ), | ||||||||||||||||||||||||||||
| 647 | match( 'qux', '=', 3 ), | ||||||||||||||||||||||||||||
| 648 | ), | ||||||||||||||||||||||||||||
| 649 | ); | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | =head3 match | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | Creates a L<SQL::Functional::MatchClause> and returns it. This is how you | ||||||||||||||||||||||||||||
| 654 | would setup SQL phrases like C<baz = 1> in your C<WHERE>. It takes a | ||||||||||||||||||||||||||||
| 655 | string, an SQL operator, and a value to match. "Raw" values will always be set | ||||||||||||||||||||||||||||
| 656 | as a bind parameter. Alternatively, if the value is an object that does the | ||||||||||||||||||||||||||||
| 657 | L<SQL::Functional::Clause> role, it will be added as-is. | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | =head3 op | ||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||
| 661 | Alias for C<match>. The wording of C<match> tends to look better inside | ||||||||||||||||||||||||||||
| 662 | C<SELECT ... WHERE ...> statements, while C<op> tends to be better inside | ||||||||||||||||||||||||||||
| 663 | C<UPDATE ... SET ...> statements. | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | =head3 match_verbatim | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | Creates a L<SQL::Functional::MatchClause> and returns it. The difference | ||||||||||||||||||||||||||||
| 668 | between this and C<match> is that the third argument is used directly, | ||||||||||||||||||||||||||||
| 669 | rather than a placeholder. This is useful, for example, when you want to | ||||||||||||||||||||||||||||
| 670 | do a join like: | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | WHERE foo.id = bar.foo_id | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | Don't know what's wrong with C<INNER JOIN>, but some people prefer this syntax. | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | =head3 table | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | Creates a L<SQL::Functional::TableClause> and returns it. Takes a string for | ||||||||||||||||||||||||||||
| 679 | the name of the table. | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | If you'd like to alias your table name, you can create it with this function, | ||||||||||||||||||||||||||||
| 682 | and then call the C<as> method on the object. For instance: | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | my $foo_tbl = table 'foo'; | ||||||||||||||||||||||||||||
| 685 | my $bar_tbl = table 'bar'; | ||||||||||||||||||||||||||||
| 686 | $foo_tbl->as( 'f' ); | ||||||||||||||||||||||||||||
| 687 | $bar_tbl->as( 'b' ); | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | my ($sql, @sql_params) = SELECT [ | ||||||||||||||||||||||||||||
| 690 | $foo_tbl->field( 'qux' ), | ||||||||||||||||||||||||||||
| 691 | $foo_tbl->field( 'quux' ), | ||||||||||||||||||||||||||||
| 692 | $bar_tbl->field( 'quuux' ), | ||||||||||||||||||||||||||||
| 693 | ], | ||||||||||||||||||||||||||||
| 694 | FROM( $foo_tbl, $bar_tbl ), | ||||||||||||||||||||||||||||
| 695 | ... | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | The calls to the C<field> method will include the table alias. | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | =head3 INTO | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | Alias for C<table>. The wording here is better for C<INESRT> statements. | ||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||
| 703 | =head3 ORDER_BY | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | ORDER_BY 'foo', 'bar'; | ||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||
| 707 | Creates a L<SQL::Functional::OrderByClause> and returns it. Takes a list of | ||||||||||||||||||||||||||||
| 708 | fields, with sorting being done in the order given. See C<DESC> for | ||||||||||||||||||||||||||||
| 709 | sorting a field in decending order. | ||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | =head3 DESC | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | ORDER_BY 'foo', DESC 'bar'; | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | Used with C<ORDER_BY> to set a field to sort in decending order. | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | =head3 INNER_JOIN | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | INNER_JOIN( $table, $field1, $field2 ); | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | Creates a L<SQL::Functional::JoinClause> with C<join_type = 'inner'> and | ||||||||||||||||||||||||||||
| 722 | returns it. The first argument, (C<$table>), is an | ||||||||||||||||||||||||||||
| 723 | C<SQL::Functional::TableClause> object, which is the table being joined. The | ||||||||||||||||||||||||||||
| 724 | second argument is the field on the main table that will be checked. The third | ||||||||||||||||||||||||||||
| 725 | argument is the field on the joined table. | ||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||
| 727 | =head3 JOIN | ||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||
| 729 | Alias for C<INNER_JOIN>. | ||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||
| 731 | =head3 LEFT_JOIN | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | LEFT_JOIN( $table, $field1, $field2 ); | ||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | Creates a L<SQL::Functional::JoinClause> with C<join_type = 'left'> and | ||||||||||||||||||||||||||||
| 736 | returns it. The first argument, (C<$table>), is an | ||||||||||||||||||||||||||||
| 737 | C<SQL::Functional::TableClause> object, which is the table being joined. The | ||||||||||||||||||||||||||||
| 738 | second argument is the field on the main table that will be checked. The third | ||||||||||||||||||||||||||||
| 739 | argument is the field on the joined table. | ||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | =head3 LEFT_OUTER_JOIN | ||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||
| 743 | Alias for C<LEFT_JOIN>. | ||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||
| 745 | =head3 RIGHT_JOIN | ||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | RIGHT_JOIN( $table, $field1, $field2 ); | ||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||
| 749 | Creates a L<SQL::Functional::JoinClause> with C<join_type = 'right'> and | ||||||||||||||||||||||||||||
| 750 | returns it. The first argument, (C<$table>), is an | ||||||||||||||||||||||||||||
| 751 | C<SQL::Functional::TableClause> object, which is the table being joined. The | ||||||||||||||||||||||||||||
| 752 | second argument is the field on the main table that will be checked. The third | ||||||||||||||||||||||||||||
| 753 | argument is the field on the joined table. | ||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||
| 755 | =head3 RIGHT_OUTER_JOIN | ||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||
| 757 | Alias for C<RIGHT_JOIN>. | ||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||
| 759 | =head3 FULL_JOIN | ||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | FULL_JOIN( $table, $field1, $field2 ); | ||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | Creates a L<SQL::Functional::JoinClause> with C<join_type = 'full'> and | ||||||||||||||||||||||||||||
| 764 | returns it. The first argument, (C<$table>), is an | ||||||||||||||||||||||||||||
| 765 | C<SQL::Functional::TableClause> object, which is the table being joined. The | ||||||||||||||||||||||||||||
| 766 | second argument is the field on the main table that will be checked. The third | ||||||||||||||||||||||||||||
| 767 | argument is the field on the joined table. | ||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||
| 769 | =head3 FULL_OUTER_JOIN | ||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | Alias for C<FULL_JOIN>. | ||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | =head3 SUBSELECT | ||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||
| 775 | Creates a L<SQL::Functional::SubSelectClause> object and returns it. Takes | ||||||||||||||||||||||||||||
| 776 | the same arguments as C<SELECT>, but returns the clause object rather than | ||||||||||||||||||||||||||||
| 777 | the SQL string and bind params. | ||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||
| 779 | =head3 AND | ||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | Creates a L<SQL::Functional::AndClause> and returns it. Takes a series of | ||||||||||||||||||||||||||||
| 782 | clauses (generally created by C<match>/C<op>), which will be joined with | ||||||||||||||||||||||||||||
| 783 | C<AND>'s. You can pass in as many clauses as you want, and even nest in | ||||||||||||||||||||||||||||
| 784 | C<OR> clauses: | ||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | WHERE AND( | ||||||||||||||||||||||||||||
| 787 | match( 'bar', '=', 1 ), | ||||||||||||||||||||||||||||
| 788 | OR( | ||||||||||||||||||||||||||||
| 789 | match( 'baz', '=', 2 ), | ||||||||||||||||||||||||||||
| 790 | match( 'qux', '=', 3 ), | ||||||||||||||||||||||||||||
| 791 | ), | ||||||||||||||||||||||||||||
| 792 | match( 'foo', '=', 4 ), | ||||||||||||||||||||||||||||
| 793 | ); | ||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||
| 795 | =head3 OR | ||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||
| 797 | Creates a L<SQL::Functional::OrClause> and returns it. Takes a series of | ||||||||||||||||||||||||||||
| 798 | clauses (generally created by C<match>/C<op>), which will be joined with | ||||||||||||||||||||||||||||
| 799 | C<OR>'s. You can pass in as many clauses as you want, and even nest in | ||||||||||||||||||||||||||||
| 800 | C<AND> clauses: | ||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||
| 802 | WHERE OR( | ||||||||||||||||||||||||||||
| 803 | match( 'bar', '=', 1 ), | ||||||||||||||||||||||||||||
| 804 | AND( | ||||||||||||||||||||||||||||
| 805 | match( 'baz', '=', 2 ), | ||||||||||||||||||||||||||||
| 806 | match( 'qux', '=', 3 ), | ||||||||||||||||||||||||||||
| 807 | ), | ||||||||||||||||||||||||||||
| 808 | match( 'foo', '=', 4 ), | ||||||||||||||||||||||||||||
| 809 | ); | ||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | =head3 VALUES | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | Creates a L<SQL::Functional::ValuesCaluse> and returns it. Takes an arrayref | ||||||||||||||||||||||||||||
| 814 | of values, which will become bind variables. | ||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||
| 816 | =head3 SET | ||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||
| 818 | Creates a L<SQL::Functional::SetClause> and returns it. Takes a list of | ||||||||||||||||||||||||||||
| 819 | L<SQL::Functional::MatchClause> objects, which you can make with C<op> | ||||||||||||||||||||||||||||
| 820 | (or C<match>). | ||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | =head3 wrap | ||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||
| 824 | Creates a L<SQL::Functional::WrapClause> and returns it. Takes a clause | ||||||||||||||||||||||||||||
| 825 | as an argument. | ||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||
| 827 | This is used when you need to wrap a portion of the SQL in parens. For | ||||||||||||||||||||||||||||
| 828 | instance, subqueries in a C<SELECT> statement need this: | ||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||
| 830 | SELECT * FROM foo WHERE bar IN (SELECT id FROM bar); | ||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||
| 832 | Which you could build like this: | ||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||
| 834 | SELECT star, | ||||||||||||||||||||||||||||
| 835 | FROM( 'foo' ), | ||||||||||||||||||||||||||||
| 836 | WHERE match( 'bar', 'IN', wrap( | ||||||||||||||||||||||||||||
| 837 | SUBSELECT ['id'], FROM 'bar' | ||||||||||||||||||||||||||||
| 838 | )); | ||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||
| 840 | On the other hand, C<INSERT> statements with subqueries don't take parens: | ||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||
| 842 | INSERT INTO foo (bar) SELECT id from bar; | ||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||
| 844 | In which case you I<don't> need to use C<wrap()>: | ||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||
| 846 | INSERT INTO 'foo', [ 'bar' ], | ||||||||||||||||||||||||||||
| 847 | SUBSELECT ['id'], FROM( 'baz' ), WHERE match( 'qux', '=', 1 ); | ||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||
| 849 | =head3 IS_NULL | ||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||
| 851 | Creates a L<SQL::Functional::NullClause> and returns it. Takes a field to | ||||||||||||||||||||||||||||
| 852 | check as being null. | ||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||
| 854 | =head3 IS_NOT_NULL | ||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||
| 856 | Creates a L<SQL::Functional::NullClause> and returns it. Takes a field to | ||||||||||||||||||||||||||||
| 857 | check as being not null. | ||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||
| 859 | =head3 LIMIT | ||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||
| 861 | Creates a L<SQL::Functional::LimitClause> and returns it. Takes a number | ||||||||||||||||||||||||||||
| 862 | that limits the number of rows returned. | ||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||
| 864 | =head3 TRUNCATE | ||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||
| 866 | Creates a L<SQL::Functional::TruncateClause> and returns it. Takes a table | ||||||||||||||||||||||||||||
| 867 | name. | ||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||
| 869 | =head3 DISTINCT | ||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||
| 871 | Creates a L<SQL::Functional::DistinictClause> and returns it. Takes a list of | ||||||||||||||||||||||||||||
| 872 | fields, which will be listed as C<DISTINCT> rows in the SQL. | ||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||
| 874 | =head3 func | ||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||
| 876 | Creates a L<SQL::Functional::FunctionClause> and returns it. Takes a | ||||||||||||||||||||||||||||
| 877 | function name, followed by any parameters. | ||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||
| 879 | =head3 literal | ||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||
| 881 | Not yet implemented. | ||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||
| 883 | When it is, it will create a L<SQL::Functional::LiteralClause> and returns it. | ||||||||||||||||||||||||||||
| 884 | Takes a string which will be put literally into the final SQL, rather than as a | ||||||||||||||||||||||||||||
| 885 | placeholder. | ||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||
| 887 | =head1 WRITING EXTENSIONS | ||||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||
| 889 | C<SQL::Functional> can be easily extended for new SQL clauses using the | ||||||||||||||||||||||||||||
| 890 | L<SQL::Functional::Clause> Moose role. See the documentation on that | ||||||||||||||||||||||||||||
| 891 | module to get started. | ||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||
| 893 | =head1 WHY ANOTHER WAY TO WRITE SQL? | ||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||
| 895 | I should preface this section by saying that I'm not trying to insult the | ||||||||||||||||||||||||||||
| 896 | developers of C<SQL::Abstract> or C<DBIx::Class>. They've obviously worked | ||||||||||||||||||||||||||||
| 897 | hard to create successful and widely used libraries for a very common task. | ||||||||||||||||||||||||||||
| 898 | Perl is better for what they've accomplished. That said, I think they're stuck | ||||||||||||||||||||||||||||
| 899 | in an object-oriented way of thinking in a problem space that could be expressed | ||||||||||||||||||||||||||||
| 900 | more naturally with functions. | ||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||
| 902 | Existing ways of making database calls fall into one of three approaches: | ||||||||||||||||||||||||||||
| 903 | direct string manipulation by hand, an object interface that outputs SQL | ||||||||||||||||||||||||||||
| 904 | (as in L<SQL::Abstract>), or an object-relation mapper (L<DBIx::Class>). | ||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||
| 906 | Direct string manipulation is fine when your database is almost trivial; just | ||||||||||||||||||||||||||||
| 907 | a few tables and straightforward relationships. The C<SQL::Abstract> approach | ||||||||||||||||||||||||||||
| 908 | can handle slightly more complicated databases, but it tends to break down into | ||||||||||||||||||||||||||||
| 909 | esoteric, unintuitive syntax when things get really tough. Good object | ||||||||||||||||||||||||||||
| 910 | relational mappers can make some very complicated things easy, but there comes | ||||||||||||||||||||||||||||
| 911 | a point where you still need hand-optimized SQL. | ||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||
| 913 | What we end up with is that direct string manipulation is the way to go for | ||||||||||||||||||||||||||||
| 914 | both trivial I<and> difficult cases. The middle ground is held by libraries | ||||||||||||||||||||||||||||
| 915 | that write the SQL for you. | ||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||
| 917 | If we look at C<SQL::Abstract>'s documentation (most of the examples below | ||||||||||||||||||||||||||||
| 918 | are directly copied from there), we see quite a few places where it's | ||||||||||||||||||||||||||||
| 919 | hamfisting the syntax in order to get increasingly complicated features to work. | ||||||||||||||||||||||||||||
| 920 | Here's an example of using direct SQL to set a date column: | ||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||
| 922 | my %where = ( | ||||||||||||||||||||||||||||
| 923 | date_entered => { '>' => \["to_date(?, 'MM/DD/YYYY')", "11/26/2008"] }, | ||||||||||||||||||||||||||||
| 924 | date_expires => { '<' => \"now()" } | ||||||||||||||||||||||||||||
| 925 | ); | ||||||||||||||||||||||||||||
| 926 | # Becomes: | ||||||||||||||||||||||||||||
| 927 | # WHERE date_entered > to_date(?, 'MM/DD/YYYY') AND date_expires < now() | ||||||||||||||||||||||||||||
| 928 | # With '11/26/2008' in the bind vars. | ||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||
| 930 | Why are we taking a reference to a scalar, or worse, a reference to an array | ||||||||||||||||||||||||||||
| 931 | reference? Quite simply because C<SQL::Abstract> has to do everything in terms | ||||||||||||||||||||||||||||
| 932 | of arguments to methods on objects, and this is a way to twist Perl's syntax to | ||||||||||||||||||||||||||||
| 933 | get the result you want. | ||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||
| 935 | Switching between C<AND> and C<OR> operations gets complicated as well. | ||||||||||||||||||||||||||||
| 936 | You can do statements separated by C<AND> like this: | ||||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||
| 938 | my %where = ( | ||||||||||||||||||||||||||||
| 939 | user => 'nwiger', | ||||||||||||||||||||||||||||
| 940 | status => { '!=', 'completed', -not_like => 'pending%' } | ||||||||||||||||||||||||||||
| 941 | ); | ||||||||||||||||||||||||||||
| 942 | # Becomes: WHERE user = ? AND status != ? AND status NOT LIKE ? | ||||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||
| 944 | Or separated by C<OR> like this: | ||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||
| 946 | my %where = ( | ||||||||||||||||||||||||||||
| 947 | status => { '=', ['assigned', 'in-progress', 'pending'] } | ||||||||||||||||||||||||||||
| 948 | ); | ||||||||||||||||||||||||||||
| 949 | # Becomes: WHERE status = ? OR status = ? OR status = ? | ||||||||||||||||||||||||||||
| 950 | |||||||||||||||||||||||||||||
| 951 | So hashrefs give us C<AND> and arrayrefs give us C<OR>, which is already rather | ||||||||||||||||||||||||||||
| 952 | arbitrary. On top of that, we run into the problem of unique keys in hashes. | ||||||||||||||||||||||||||||
| 953 | That means the syntax can't be extended in the what would otherwise be the | ||||||||||||||||||||||||||||
| 954 | obvious way: | ||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||
| 956 | my %where = ( | ||||||||||||||||||||||||||||
| 957 | status => { '!=' => 2, '!=' => 1 } | ||||||||||||||||||||||||||||
| 958 | ); | ||||||||||||||||||||||||||||
| 959 | # Doesn't work, second '!=' clobbers the first | ||||||||||||||||||||||||||||
| 960 | |||||||||||||||||||||||||||||
| 961 | Instead, the syntax has to be further extended in less and less natural ways: | ||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||
| 963 | my %where = ( | ||||||||||||||||||||||||||||
| 964 | status => [ -and => {'!=', 2}, | ||||||||||||||||||||||||||||
| 965 | {'!=', 1} ]; | ||||||||||||||||||||||||||||
| 966 | ); | ||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||
| 968 | At which point things start to look like an Abstract Syntax Tree. A point which | ||||||||||||||||||||||||||||
| 969 | reminds me of an | ||||||||||||||||||||||||||||
| 970 | L<old post from BrowserUK on Perlmonks|http://perlmonks.org/?node_id=510249>: | ||||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||
| 972 | LISP has virtually no syntax (aside from all the parens), so whan you write | ||||||||||||||||||||||||||||
| 973 | LISP code, you are essentially writing an AST (abstract syntax tree). | ||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||
| 975 | Which has stuck with me ever since I read it. We can represent Abstract | ||||||||||||||||||||||||||||
| 976 | Syntax Trees very naturally with functions. For instance, Lisp might | ||||||||||||||||||||||||||||
| 977 | handle the multiple C<AND> statement like this: | ||||||||||||||||||||||||||||
| 978 | |||||||||||||||||||||||||||||
| 979 | (WHERE | ||||||||||||||||||||||||||||
| 980 | (AND | ||||||||||||||||||||||||||||
| 981 | (match 'status' '!=' 2) | ||||||||||||||||||||||||||||
| 982 | (match 'status' '!=' 1) | ||||||||||||||||||||||||||||
| 983 | ) | ||||||||||||||||||||||||||||
| 984 | ) | ||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||
| 986 | Which looks at least vaguely like direct SQL, while avoiding some of the easy | ||||||||||||||||||||||||||||
| 987 | syntax errors and other cumbersome issues that you get with verbatim SQL | ||||||||||||||||||||||||||||
| 988 | strings. | ||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||
| 990 | With the right function definitions, we can get pretty close to the Lisp | ||||||||||||||||||||||||||||
| 991 | example in Perl: | ||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||
| 993 | WHERE AND( | ||||||||||||||||||||||||||||
| 994 | match( 'status', '!=', 2 ), | ||||||||||||||||||||||||||||
| 995 | match( 'status', '!=', 1 ), | ||||||||||||||||||||||||||||
| 996 | ) | ||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||
| 998 | You could copy-and-paste that in an email to a DBA with only a short explanation | ||||||||||||||||||||||||||||
| 999 | of what's going on. It's not too far from the AST that an SQL parser might | ||||||||||||||||||||||||||||
| 1000 | create internally. | ||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||
| 1002 | As it happens, C<SQL::Functional> uses a lot of objects to pass data around | ||||||||||||||||||||||||||||
| 1003 | between functions. Objects aren't bad, they just aren't always the right tool. | ||||||||||||||||||||||||||||
| 1004 | Objects and functions can be used in harmony, and it's wonderful that Perl | ||||||||||||||||||||||||||||
| 1005 | allows you to do both without getting in your way. | ||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||
| 1007 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||
| 1009 | =over 4 | ||||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||
| 1011 | =item * L<SQL::Abstract> | ||||||||||||||||||||||||||||
| 1012 | |||||||||||||||||||||||||||||
| 1013 | =item * L<DBIx::Class> | ||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||
| 1015 | =item * L<DBI> | ||||||||||||||||||||||||||||
| 1016 | |||||||||||||||||||||||||||||
| 1017 | =back | ||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||
| 1019 | Also, all the classes at C<SQL::Functional::*Clause>. | ||||||||||||||||||||||||||||
| 1020 | |||||||||||||||||||||||||||||
| 1021 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||
| 1023 | Copyright (c) 2016 Timm Murray | ||||||||||||||||||||||||||||
| 1024 | All rights reserved. | ||||||||||||||||||||||||||||
| 1025 | |||||||||||||||||||||||||||||
| 1026 | Redistribution and use in source and binary forms, with or without | ||||||||||||||||||||||||||||
| 1027 | modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||
| 1029 | * Redistributions of source code must retain the above copyright notice, | ||||||||||||||||||||||||||||
| 1030 | this list of conditions and the following disclaimer. | ||||||||||||||||||||||||||||
| 1031 | * Redistributions in binary form must reproduce the above copyright | ||||||||||||||||||||||||||||
| 1032 | notice, this list of conditions and the following disclaimer in the | ||||||||||||||||||||||||||||
| 1033 | documentation and/or other materials provided with the distribution. | ||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||
| 1035 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||||||||||||||||||||||||
| 1036 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||||||||||||||||||||||||
| 1037 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||||||||||||||||||||||||||
| 1038 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||||||||||||||||||||||||||||
| 1039 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||||||||||||||||||||||||||||
| 1040 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||||||||||||||||||||||||||||
| 1041 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||||||||||||||||||||||||||||
| 1042 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||||||||||||||||||||||||||||
| 1043 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||||||||||||||||||||||||||||
| 1044 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||||||||||||||||||||||||||
| 1045 | POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||
| 1047 | =cut | ||||||||||||||||||||||||||||