| 1 | package DBIx::Class::Async::SelectNormaliser; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 100 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 100 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 100 | ▶ | use version; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 100 | our $VERSION = qv('v1.2.0'); | |||||||||||||||||||
| 8 | 100 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | DBIx::Class::Async::SelectNormaliser - Normalise C<-ident> clauses in ResultSet C<select> attributes | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | Version v1.2.0 | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | use DBIx::Class::Async::SelectNormaliser; | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | # Inline normalisation before passing attrs to search() | ||||||||||||||||||||
| 23 | my ($clean_select, $clean_as) = DBIx::Class::Async::SelectNormaliser->normalise( | ||||||||||||||||||||
| 24 | select => [ { -ident => 'me.status', -as => 'current_status' } ], | ||||||||||||||||||||
| 25 | as => [], | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | my $rs = $schema->resultset('Order')->search({}, { | ||||||||||||||||||||
| 29 | select => $clean_select, | ||||||||||||||||||||
| 30 | as => $clean_as, | ||||||||||||||||||||
| 31 | }); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # Or call normalise_attrs() directly on a complete attrs hashref | ||||||||||||||||||||
| 34 | my $attrs = DBIx::Class::Async::SelectNormaliser->normalise_attrs({ | ||||||||||||||||||||
| 35 | select => [ | ||||||||||||||||||||
| 36 | 'me.id', | ||||||||||||||||||||
| 37 | { -ident => 'me.status', -as => 'current_status' }, | ||||||||||||||||||||
| 38 | { -ident => 'me.created', -as => 'created_at' }, | ||||||||||||||||||||
| 39 | { count => 'me.id', -as => 'total' }, # function form -- left unchanged | ||||||||||||||||||||
| 40 | ], | ||||||||||||||||||||
| 41 | as => [ 'id' ], # partially specified -- filled in from -as | ||||||||||||||||||||
| 42 | where => { active => 1 }, | ||||||||||||||||||||
| 43 | order_by => 'me.id', | ||||||||||||||||||||
| 44 | }); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | my $rs = $schema->resultset('Order')->search($attrs->{where}, $attrs); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head2 The problem | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | L<DBIx::Class> supports a C<-ident> operator in C<where>, C<order_by>, and | ||||||||||||||||||||
| 53 | C<group_by> clauses to force a value to be treated as a SQL identifier (column | ||||||||||||||||||||
| 54 | or table name) rather than a literal string or a function call: | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # In a where clause -- works correctly | ||||||||||||||||||||
| 57 | $rs->search({ 'me.status' => { -ident => 'other_table.status' } }); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | However, C<-ident> in the C<select> attribute does B<not> work: | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | # Broken -- produces: SELECT -IDENT(me.status) AS current_status | ||||||||||||||||||||
| 62 | $rs->search({}, { | ||||||||||||||||||||
| 63 | select => [ { -ident => 'me.status', -as => 'current_status' } ], | ||||||||||||||||||||
| 64 | }); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | The C<select> attribute is processed by a different code path inside | ||||||||||||||||||||
| 67 | L<SQL::Abstract> -- specifically C<_select_field> -- which does not recognise | ||||||||||||||||||||
| 68 | C<-ident> as a special sigil and instead treats it as a function name. The | ||||||||||||||||||||
| 69 | hash key C<-ident> becomes the function, its value becomes the argument, and | ||||||||||||||||||||
| 70 | the result is the literal string C<-IDENT(me.status)> in the SQL output, | ||||||||||||||||||||
| 71 | which is a syntax error on every database. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head2 Why not fix upstream? | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | Extending C<select> to support C<-ident> in L<DBIx::Class> or | ||||||||||||||||||||
| 76 | L<SQL::Abstract::More> is non-trivial: | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =over 4 | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =item * | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | C<select> hashrefs are already used for function calls: | ||||||||||||||||||||
| 83 | C<< { count => 'me.id', -as => 'cnt' } >>. Adding C<-ident> as a special | ||||||||||||||||||||
| 84 | sigil inside the same hashref form requires distinguishing | ||||||||||||||||||||
| 85 | C<< { -ident => 'col', -as => 'alias' } >> (identifier alias) from | ||||||||||||||||||||
| 86 | C<< { func => 'col', -as => 'alias' } >> (function call) without introducing | ||||||||||||||||||||
| 87 | ambiguity. A column named C<ident> would be indistinguishable from the | ||||||||||||||||||||
| 88 | operator. | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =item * | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | The C<select>/C<as> separation is deliberate DBIC design: C<select> is a list | ||||||||||||||||||||
| 93 | of SQL expressions and C<as> is a parallel list of Perl-side aliases. Adding | ||||||||||||||||||||
| 94 | inline C<-as> to C<select> as well (which DBIC already supports for functions) | ||||||||||||||||||||
| 95 | and now C<-ident> would create three overlapping ways to alias a column, all | ||||||||||||||||||||
| 96 | with subtly different semantics. | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =item * | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Changing this in C<SQL::Abstract> would affect all DBIC users and all other | ||||||||||||||||||||
| 101 | consumers of SQL::Abstract, requiring deprecation cycles and backwards | ||||||||||||||||||||
| 102 | compatibility guarantees that are beyond the scope of a single distribution. | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =back | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =head2 The solution: pre-processing in DBIx::Class::Async | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Rather than patching upstream, this module pre-processes the C<select> and | ||||||||||||||||||||
| 109 | C<as> attributes before they reach L<SQL::Abstract>. Any | ||||||||||||||||||||
| 110 | C<< { -ident => $col, -as => $alias } >> hashref is rewritten to its canonical | ||||||||||||||||||||
| 111 | DBIC form: a bare column name string in C<select> and a corresponding entry in | ||||||||||||||||||||
| 112 | C<as>. All other forms -- bare strings, function hashrefs, literal SQL | ||||||||||||||||||||
| 113 | references -- are left completely unchanged. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | This approach: | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =over 4 | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =item * | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | B<Requires no upstream changes.> The transformation happens entirely in | ||||||||||||||||||||
| 122 | DBIx::Class::Async before the attrs touch SQL::Abstract. | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =item * | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | B<Is transparent to callers.> Application code that already uses the | ||||||||||||||||||||
| 127 | canonical C<select>/C<as> form is unaffected. Callers who prefer the | ||||||||||||||||||||
| 128 | C<-ident> form get intuitive, correct behaviour. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =item * | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | B<Is safe to compose.> Function hashrefs (C<< { count => 'me.id' } >>) are | ||||||||||||||||||||
| 133 | detected by the absence of C<-ident> and passed through untouched, so all | ||||||||||||||||||||
| 134 | existing query patterns continue to work. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =item * | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | B<Is explicit about intent.> C<-ident> says clearly "this is a column | ||||||||||||||||||||
| 139 | name, not a function and not a literal string", which is useful documentation | ||||||||||||||||||||
| 140 | in itself. | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =back | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =head2 Integration Points | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | This module is called from two places in DBIx::Class::Async: | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =over 4 | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =item C<DBIx::Class::Async::ResultSet::search()> | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Before building the payload for the worker, C<search()> calls | ||||||||||||||||||||
| 153 | L</normalise_attrs> on the incoming attrs hashref. This means all ResultSet | ||||||||||||||||||||
| 154 | operations that flow through C<search> (C<all>, C<count>, C<update>, etc.) | ||||||||||||||||||||
| 155 | benefit automatically. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =item C<DBIx::Class::Async::ResultSet::search_rs()> | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | The same normalisation is applied when building a new ResultSet object, so | ||||||||||||||||||||
| 160 | chained searches also produce correct SQL. | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =back | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 Interaction with C<-as> in function hashrefs | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | DBIC supports an inline C<-as> inside function hashrefs: | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | { count => 'me.id', -as => 'total' } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | This module does B<not> touch that form. The C<-as> key is only consumed when | ||||||||||||||||||||
| 171 | it appears alongside C<-ident>. In a function hashref, C<-as> is already | ||||||||||||||||||||
| 172 | handled correctly by DBIC and SQL::Abstract and must not be extracted into the | ||||||||||||||||||||
| 173 | C<as> array, because DBIC expects the alias to come from the function hashref | ||||||||||||||||||||
| 174 | itself in that case. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head2 Partial C<as> arrays | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | The incoming C<as> array may be shorter than C<select>, absent entirely, or | ||||||||||||||||||||
| 179 | partially specified. This module fills in missing entries from C<-as> values | ||||||||||||||||||||
| 180 | found in the C<select> items. Entries already present in C<as> take priority | ||||||||||||||||||||
| 181 | over any C<-as> in the corresponding C<select> item, preserving the behaviour | ||||||||||||||||||||
| 182 | of callers who specify both. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head1 METHODS | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head2 normalise_attrs | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | my $clean_attrs = DBIx::Class::Async::SelectNormaliser->normalise_attrs(\%attrs); | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | Accepts a complete ResultSet attrs hashref. If the hashref contains a | ||||||||||||||||||||
| 193 | C<select> key, rewrites any C<< { -ident => $col, -as => $alias } >> items to | ||||||||||||||||||||
| 194 | bare column strings and populates C<as> accordingly. All other keys in the | ||||||||||||||||||||
| 195 | attrs hashref (C<where>, C<order_by>, C<join>, etc.) are passed through | ||||||||||||||||||||
| 196 | unchanged. | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | Returns a B<new> hashref -- the input is never modified in place. | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | If C<select> is absent or contains no C<-ident> items, the returned hashref is | ||||||||||||||||||||
| 201 | a shallow copy of the input with no further changes. | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =cut | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 620 | ▶ | sub normalise_attrs { | ||||||||||||||||||
|
Subroutine
normalise_attrs: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 206 | 620 | my ($class, $attrs) = @_; | |||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 620 | ▶ | return $attrs unless ref $attrs eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 209 | 620 | ▶ | return $attrs unless exists $attrs->{select}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | # Shallow copy -- we only touch select and as, leave everything else | ||||||||||||||||||||
| 212 | 10 | my %out = %$attrs; | |||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | my ($clean_select, $clean_as) = $class->normalise( | ||||||||||||||||||||
| 215 | select => $attrs->{select}, | ||||||||||||||||||||
| 216 | 10 | ▶ | as => $attrs->{as} // [], | ||||||||||||||||||
|
Condition100%2/2
$attrs->{"as"} // []
MC/DC100%1/1
$attrs->{"as"} // []
$attrs->{"as"}
Truth table100%2/2
A
A= $attrs->{"as"}
| |||||||||||||||||||||
| 217 | ); | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 10 | $out{select} = $clean_select; | |||||||||||||||||||
| 220 | 10 | $out{as} = $clean_as; | |||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 10 | return \%out; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head2 normalise | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | my ($clean_select, $clean_as) = DBIx::Class::Async::SelectNormaliser->normalise( | ||||||||||||||||||||
| 228 | select => \@select_items, | ||||||||||||||||||||
| 229 | as => \@as_items, # may be empty or shorter than select | ||||||||||||||||||||
| 230 | ); | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | Lower-level method. Accepts C<select> and C<as> arrays directly and returns | ||||||||||||||||||||
| 233 | two new arrayrefs. | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | Each item in C<select> is inspected: | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =over 4 | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =item C<< { -ident => $col } >> or C<< { -ident => $col, -as => $alias } >> | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | Rewritten to the bare column string C<$col> in C<< $clean_select >>. If | ||||||||||||||||||||
| 242 | C<-as> is present and the corresponding position in the incoming C<as> array | ||||||||||||||||||||
| 243 | is not already set, the alias is placed into C<< $clean_as >>. | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =item Any other form | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | Passed through to C<< $clean_select >> unchanged. If the corresponding | ||||||||||||||||||||
| 248 | position in the incoming C<as> array is set, it is preserved in | ||||||||||||||||||||
| 249 | C<< $clean_as >>. Otherwise the C<< $clean_as >> slot is left as C<undef> | ||||||||||||||||||||
| 250 | (DBIC omits C<undef> alias entries). | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | =back | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | The two returned arrays are always the same length. | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | =cut | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 23 | ▶ | sub normalise { | ||||||||||||||||||
|
Subroutine
normalise: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||
| 259 | 23 | my ($class, %args) = @_; | |||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | 23 | ▶ | my $select = $args{select} // []; | ||||||||||||||||||
|
Condition50%1/2
$args{"select"} // []
MC/DC0%0/1
$args{"select"} // []
$args{"select"}
Truth table50%1/2
A
A= $args{"select"}
| |||||||||||||||||||||
| 262 | 23 | ▶ | my $as = $args{as} // []; | ||||||||||||||||||
|
Condition50%1/2
$args{"as"} // []
MC/DC0%0/1
$args{"as"} // []
$args{"as"}
Truth table50%1/2
A
A= $args{"as"}
| |||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | # Normalise select to an arrayref -- callers sometimes pass a bare scalar | ||||||||||||||||||||
| 265 | # (single column) or a hashref (single function/ident expression). | ||||||||||||||||||||
| 266 | 23 | ▶ | $select = [ $select ] unless ref $select eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | 23 | my (@clean_select, @clean_as); | |||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | 23 | for my $i ( 0 .. $#$select ) { | |||||||||||||||||||
| 271 | 30 | my $item = $select->[$i]; | |||||||||||||||||||
| 272 | 30 | my $alias = $as->[$i]; # may be undef if as is shorter than select | |||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | 30 | ▶ | if ( _is_ident_hashref(undef, $item) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 275 | # Rewrite { -ident => $col, -as => $alias } to a bare column string. | ||||||||||||||||||||
| 276 | # The caller's $as entry takes priority if already set. | ||||||||||||||||||||
| 277 | 12 | push @clean_select, $item->{'-ident'}; | |||||||||||||||||||
| 278 | 12 | ▶ | push @clean_as, defined($alias) ? $alias : $item->{'-as'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 279 | } | ||||||||||||||||||||
| 280 | else { | ||||||||||||||||||||
| 281 | # All other forms: bare string, function hashref, literal SQL ref -- | ||||||||||||||||||||
| 282 | # pass through untouched. | ||||||||||||||||||||
| 283 | 18 | push @clean_select, $item; | |||||||||||||||||||
| 284 | 18 | push @clean_as, $alias; | |||||||||||||||||||
| 285 | } | ||||||||||||||||||||
| 286 | } | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | # Preserve any trailing as[] entries that extend beyond the select list. | ||||||||||||||||||||
| 289 | # These are unusual (DBIC ignores them) but we return them unchanged so | ||||||||||||||||||||
| 290 | # the caller gets back exactly what they passed in. | ||||||||||||||||||||
| 291 | 23 | ▶ | if ( @$as > @$select ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 292 | 1 | push @clean_as, @{$as}[ scalar(@$select) .. $#$as ]; | |||||||||||||||||||
| 293 | } | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | 23 | return (\@clean_select, \@clean_as); | |||||||||||||||||||
| 296 | } | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | # | ||||||||||||||||||||
| 299 | # | ||||||||||||||||||||
| 300 | # Private Helpers | ||||||||||||||||||||
| 301 | |||||||||||||||||||||
| 302 | =head2 _is_ident_hashref | ||||||||||||||||||||
| 303 | |||||||||||||||||||||
| 304 | my $bool = _is_ident_hashref($item); | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | Returns true if C<$item> is a hashref with a C<-ident> key. Returns false for | ||||||||||||||||||||
| 307 | everything else, including function hashrefs like C<< { count => 'me.id' } >> | ||||||||||||||||||||
| 308 | which happen to also contain a C<-as> key. | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | The check is intentionally minimal: we only require the presence of C<-ident>. | ||||||||||||||||||||
| 311 | The C<-as> key is optional (the caller may specify aliases via the C<as> array | ||||||||||||||||||||
| 312 | instead). | ||||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | =cut | ||||||||||||||||||||
| 315 | |||||||||||||||||||||
| 316 | 40 | ▶ | sub _is_ident_hashref { | ||||||||||||||||||
|
Subroutine
_is_ident_hashref: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 317 | 40 | my (undef, $item) = @_; | |||||||||||||||||||
| 318 | 40 | ▶ | return ref $item eq 'HASH' && exists $item->{'-ident'}; | ||||||||||||||||||
|
Condition100%3/3
ref $item eq 'HASH' && exists $item->{'-ident'}
MC/DC100%2/2
ref $item eq 'HASH' && exists $item->{'-ident'}
ref $item eq 'HASH'
exists $item->{'-ident'}
Truth table100%3/3
A && B
A= ref $item eq 'HASH'B= exists $item->{'-ident'}
| |||||||||||||||||||||
| 319 | } | ||||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | =head1 EXAMPLES | ||||||||||||||||||||
| 322 | |||||||||||||||||||||
| 323 | =head2 Basic identifier aliasing | ||||||||||||||||||||
| 324 | |||||||||||||||||||||
| 325 | # Before normalisation (would produce broken SQL): | ||||||||||||||||||||
| 326 | select => [ { -ident => 'me.status', -as => 'current_status' } ] | ||||||||||||||||||||
| 327 | |||||||||||||||||||||
| 328 | # After normalisation (correct canonical DBIC form): | ||||||||||||||||||||
| 329 | select => [ 'me.status' ], | ||||||||||||||||||||
| 330 | as => [ 'current_status' ], | ||||||||||||||||||||
| 331 | |||||||||||||||||||||
| 332 | =head2 Mixed select list | ||||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | # Input | ||||||||||||||||||||
| 335 | select => [ | ||||||||||||||||||||
| 336 | 'me.id', | ||||||||||||||||||||
| 337 | { -ident => 'me.status', -as => 'current_status' }, | ||||||||||||||||||||
| 338 | { count => 'me.id', -as => 'total' }, # function -- untouched | ||||||||||||||||||||
| 339 | { -ident => 'me.created' }, # no inline alias | ||||||||||||||||||||
| 340 | ], | ||||||||||||||||||||
| 341 | as => [ 'id', undef, 'total', 'created_at' ], # as takes priority | ||||||||||||||||||||
| 342 | |||||||||||||||||||||
| 343 | # Output | ||||||||||||||||||||
| 344 | select => [ 'me.id', 'me.status', { count => 'me.id', -as => 'total' }, 'me.created' ], | ||||||||||||||||||||
| 345 | as => [ 'id', 'current_status', 'total', 'created_at' ], | ||||||||||||||||||||
| 346 | |||||||||||||||||||||
| 347 | # Note: slot 1 uses 'current_status' from -as (incoming as[1] was undef). | ||||||||||||||||||||
| 348 | # Note: slot 3 uses 'created_at' from the as array (it was already set), | ||||||||||||||||||||
| 349 | # ignoring the missing -as in the -ident item. | ||||||||||||||||||||
| 350 | |||||||||||||||||||||
| 351 | =head2 Caller-specified as takes priority | ||||||||||||||||||||
| 352 | |||||||||||||||||||||
| 353 | # Input | ||||||||||||||||||||
| 354 | select => [ { -ident => 'me.col', -as => 'from_ident' } ], | ||||||||||||||||||||
| 355 | as => [ 'from_as_array' ], | ||||||||||||||||||||
| 356 | |||||||||||||||||||||
| 357 | # Output | ||||||||||||||||||||
| 358 | select => [ 'me.col' ], | ||||||||||||||||||||
| 359 | as => [ 'from_as_array' ], # as array wins | ||||||||||||||||||||
| 360 | |||||||||||||||||||||
| 361 | =head2 Scalar select (single column, not an array) | ||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | # Input -- normalise() accepts a bare scalar or hashref too | ||||||||||||||||||||
| 364 | select => { -ident => 'me.status', -as => 'current_status' }, | ||||||||||||||||||||
| 365 | as => [], | ||||||||||||||||||||
| 366 | |||||||||||||||||||||
| 367 | # Output | ||||||||||||||||||||
| 368 | select => [ 'me.status' ], | ||||||||||||||||||||
| 369 | as => [ 'current_status' ], | ||||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | =head1 SEE ALSO | ||||||||||||||||||||
| 372 | |||||||||||||||||||||
| 373 | =over 4 | ||||||||||||||||||||
| 374 | |||||||||||||||||||||
| 375 | =item L<DBIx::Class::ResultSet/select> | ||||||||||||||||||||
| 376 | |||||||||||||||||||||
| 377 | DBIC documentation for the C<select> ResultSet attribute. | ||||||||||||||||||||
| 378 | |||||||||||||||||||||
| 379 | =item L<DBIx::Class::ResultSet/as> | ||||||||||||||||||||
| 380 | |||||||||||||||||||||
| 381 | DBIC documentation for the C<as> ResultSet attribute. | ||||||||||||||||||||
| 382 | |||||||||||||||||||||
| 383 | =item L<SQL::Abstract> | ||||||||||||||||||||
| 384 | |||||||||||||||||||||
| 385 | The underlying SQL generation library. The C<_select_field> method is the | ||||||||||||||||||||
| 386 | code path that does B<not> handle C<-ident>. | ||||||||||||||||||||
| 387 | |||||||||||||||||||||
| 388 | =back | ||||||||||||||||||||
| 389 | |||||||||||||||||||||
| 390 | =head1 AUTHOR | ||||||||||||||||||||
| 391 | |||||||||||||||||||||
| 392 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||
| 393 | |||||||||||||||||||||
| 394 | =head1 REPOSITORY | ||||||||||||||||||||
| 395 | |||||||||||||||||||||
| 396 | L<https://github.com/manwar/DBIx-Class-Async> | ||||||||||||||||||||
| 397 | |||||||||||||||||||||
| 398 | =head1 BUGS | ||||||||||||||||||||
| 399 | |||||||||||||||||||||
| 400 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/DBIx-Class-Async/issues>. | ||||||||||||||||||||
| 401 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||
| 402 | bug as I make changes. | ||||||||||||||||||||
| 403 | |||||||||||||||||||||
| 404 | =head1 SUPPORT | ||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 407 | |||||||||||||||||||||
| 408 | perldoc DBIx::Class::Async::SelectNormaliser | ||||||||||||||||||||
| 409 | |||||||||||||||||||||
| 410 | You can also look for information at: | ||||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | =over 4 | ||||||||||||||||||||
| 413 | |||||||||||||||||||||
| 414 | =item * BUG Report | ||||||||||||||||||||
| 415 | |||||||||||||||||||||
| 416 | L<https://github.com/manwar/DBIx-Class-Async/issues> | ||||||||||||||||||||
| 417 | |||||||||||||||||||||
| 418 | =item * CPAN Ratings | ||||||||||||||||||||
| 419 | |||||||||||||||||||||
| 420 | L<http://cpanratings.perl.org/d/DBIx-Class-Async> | ||||||||||||||||||||
| 421 | |||||||||||||||||||||
| 422 | =item * Search MetaCPAN | ||||||||||||||||||||
| 423 | |||||||||||||||||||||
| 424 | L<https://metacpan.org/dist/DBIx-Class-Async/> | ||||||||||||||||||||
| 425 | |||||||||||||||||||||
| 426 | =back | ||||||||||||||||||||
| 427 | |||||||||||||||||||||
| 428 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 429 | |||||||||||||||||||||
| 430 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||
| 431 | |||||||||||||||||||||
| 432 | This program is free software; you can redistribute it and / or modify it under | ||||||||||||||||||||
| 433 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||||||||||||||||||||
| 434 | license at: | ||||||||||||||||||||
| 435 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||
| 436 | Any use, modification, and distribution of the Standard or Modified Versions is | ||||||||||||||||||||
| 437 | governed by this Artistic License.By using, modifying or distributing the Package, | ||||||||||||||||||||
| 438 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||||||||||||||||||||
| 439 | not accept this license. | ||||||||||||||||||||
| 440 | If your Modified Version has been derived from a Modified Version made by someone | ||||||||||||||||||||
| 441 | other than you,you are nevertheless required to ensure that your Modified Version | ||||||||||||||||||||
| 442 | complies with the requirements of this license. | ||||||||||||||||||||
| 443 | This license does not grant you the right to use any trademark, service mark, | ||||||||||||||||||||
| 444 | tradename, or logo of the Copyright Holder. | ||||||||||||||||||||
| 445 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||||||||||||||||||||
| 446 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||||||||||||||||||||
| 447 | Package with respect to any patent claims licensable by the Copyright Holder that | ||||||||||||||||||||
| 448 | are necessarily infringed by the Package. If you institute patent litigation | ||||||||||||||||||||
| 449 | (including a cross-claim or counterclaim) against any party alleging that the | ||||||||||||||||||||
| 450 | Package constitutes direct or contributory patent infringement,then this Artistic | ||||||||||||||||||||
| 451 | License to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||
| 452 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||||||||||||||||||||
| 453 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||||||||||||||||||||
| 454 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||||||||||||||||||||
| 455 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||||||||||||||||||||
| 456 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||||||||||||||||||||
| 457 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||||||||||||||||||||
| 458 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||
| 459 | |||||||||||||||||||||
| 460 | =cut | ||||||||||||||||||||
| 461 | |||||||||||||||||||||
| 462 | 100 | 1; # End of DBIx::Class::Async::SelectNormaliser | |||||||||||||||||||