| 1 | 9 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package DBIx::Squirrel::Stages::Rows; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use experimental qw(builtin); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use builtin qw(refaddr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Ref::Util qw(is_plain_arrayref is_plain_hashref); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use DBIx::Squirrel::Context qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use DBIx::Squirrel::Errors qw(croakf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use DBIx::Squirrel::Gather qw(gather); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use namespace::clean -except => 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 9 | our @EXPORT = qw(pluck as_hash); ## no critic (Modules::ProhibitAutomaticExportation) | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 9 | our @EXPORT_OK = @EXPORT; | |||||||||||||||||||||||||||
| 20 | 9 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Croak at the line that called DBIx::Squirrel. | ||||||||||||||||||||||||||||
| 23 | 9 | our @CARP_NOT = @DBIx::Squirrel::Errors::INTERNAL; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # The column pluck has when it's given none: a reference no caller can | ||||||||||||||||||||||||||||
| 26 | # give, so pluck() is told from pluck(undef), which croaks, while the | ||||||||||||||||||||||||||||
| 27 | # signature still croaks for more than one. | ||||||||||||||||||||||||||||
| 28 | 9 | my $ONLY = \'the only column'; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Return a stage giving one column of each row, by its name, in any | ||||||||||||||||||||||||||||
| 32 | # case, or its position, from 0, whether the row is an array, a hash or | ||||||||||||||||||||||||||||
| 33 | # a row object; or, given none, each row's only column. | ||||||||||||||||||||||||||||
| 34 | 58 | ▶ | sub pluck ($column = $ONLY) { | ||||||||||||||||||||||||||
|
Subroutine
pluck: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 58 | ▶ | return _pluck_only() | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $column and builtin::refaddr($column) == builtin::refaddr($ONLY)
MC/DC50%1/2
ref $column and builtin::refaddr($column) == builtin::refaddr($ONLY)
ref $column
builtin::refaddr($column) == builtin::refaddr($ONLY)
Truth table66%2/3
A and B
A= ref $columnB= builtin::refaddr($column) == builtin::refaddr($ONLY)
| |||||||||||||||||||||||||||||
| 36 | if ref $column && refaddr($column) == refaddr($ONLY); | ||||||||||||||||||||||||||||
| 37 | 47 | ▶ | croakf 'Expected a column, by name or position' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $column and length $column
MC/DC100%2/2
defined $column and length $column
defined $column
length $column
Truth table100%3/3
A and B
A= defined $columnB= length $column
| |||||||||||||||||||||||||||||
| 38 | unless defined $column && length $column; | ||||||||||||||||||||||||||||
| 39 | 45 | my $by_position = $column =~ m( \A \d+ \z )xa; | |||||||||||||||||||||||||||
| 40 | 67 | ▶ | return gather { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 67 | my $key; # the array index or hash key, found once a run | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Without a signature, as it's called for each row: one costs | ||||||||||||||||||||||||||||
| 44 | # about 19 ns a call. | ||||||||||||||||||||||||||||
| 45 | 152 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 46 | 152 | ▶ | return $_[0]->get_column($column) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | if $_[0] isa 'DBIx::Squirrel::Result'; | ||||||||||||||||||||||||||||
| 48 | 139 | ▶ | if (is_plain_arrayref($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 71 | ▶ | $key //= $by_position ? $column : _position_of($column); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key //= $by_position ? $column : _position_of($column)
MC/DC100%2/2
$key //= $by_position ? $column : _position_of($column)
$key
$by_position ? $column : _position_of($column)
Truth table100%3/3
A //= B
A= $keyB= $by_position ? $column : _position_of($column)
| |||||||||||||||||||||||||||||
| 50 | croakf 'Expected a column of the row (%s)', $column | ||||||||||||||||||||||||||||
| 51 | 65 | ▶ | if $key >= @{ $_[0] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 64 | return $_[0][$key]; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 68 | ▶ | if (is_plain_hashref($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 67 | ▶ | $key //= _key_of($_[0], $column, $by_position); | ||||||||||||||||||||||||||
|
Condition100%3/3
$key //= _key_of($_[0], $column, $by_position)
MC/DC100%2/2
$key //= _key_of($_[0], $column, $by_position)
$key
_key_of($_[0], $column, $by_position)
Truth table100%3/3
A //= B
A= $keyB= _key_of($_[0], $column, $by_position)
| |||||||||||||||||||||||||||||
| 56 | 65 | return $_[0]{$key}; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 1 | croakf 'Expected a row: an array, a hash or a row object'; | |||||||||||||||||||||||||||
| 59 | 67 | }; | |||||||||||||||||||||||||||
| 60 | 45 | }; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Return a stage giving each row's only column, counted from the row, so | ||||||||||||||||||||||||||||
| 64 | # it needs no statement; a row of more columns croaks. | ||||||||||||||||||||||||||||
| 65 | 11 | ▶ | sub _pluck_only () { | ||||||||||||||||||||||||||
|
Subroutine
_pluck_only: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | # Without a signature, as it's called for each row, as for pluck. A | ||||||||||||||||||||||||||||
| 67 | # row object is an array too. | ||||||||||||||||||||||||||||
| 68 | 22 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 69 | 22 | ▶ | if (is_plain_arrayref($_[0]) || $_[0] isa 'DBIx::Squirrel::Result') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
Ref::Util::XS::is_plain_arrayref($_[0]) or $_[0] isa "DBIx::Squirrel::Result"
MC/DC100%2/2
Ref::Util::XS::is_plain_arrayref($_[0]) or $_[0] isa "DBIx::Squirrel::Result"
Ref::Util::XS::is_plain_arrayref($_[0])
$_[0] isa "DBIx::Squirrel::Result"
Truth table100%3/3
A or B
A= Ref::Util::XS::is_plain_arrayref($_[0])B= $_[0] isa "DBIx::Squirrel::Result"
| |||||||||||||||||||||||||||||
| 70 | 3 | croakf 'Expected one column to pluck, not %d', scalar @{ $_[0] } | |||||||||||||||||||||||||||
| 71 | 16 | ▶ | unless @{ $_[0] } == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 13 | return $_[0][0]; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 6 | ▶ | if (is_plain_hashref($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | croakf 'Expected one column to pluck, not %d', scalar keys %{ $_[0] } | |||||||||||||||||||||||||||
| 76 | 5 | ▶ | unless keys %{ $_[0] } == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 4 | return (values %{ $_[0] })[0]; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 1 | croakf 'Expected a row: an array, a hash or a row object'; | |||||||||||||||||||||||||||
| 80 | 11 | }; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Return a stage making each row a hash, keyed by the column names as | ||||||||||||||||||||||||||||
| 85 | # fetchrow_hashref keys them: arrays and row objects are made into one, | ||||||||||||||||||||||||||||
| 86 | # and hashes are passed on as they are. Given column names, the hash has | ||||||||||||||||||||||||||||
| 87 | # only those columns, each found in any case: a hash is made anew. | ||||||||||||||||||||||||||||
| 88 | 29 | ▶ | sub as_hash (@columns) { | ||||||||||||||||||||||||||
|
Subroutine
as_hash: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 29 | ▶ | croakf 'Expected a column name' if grep { !defined || !length } @columns; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_ && length $_
MC/DC100%2/2
defined $_ && length $_
defined $_
length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 90 | 27 | ▶ | return gather { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 27 | my($names, $at, $keys); # found once a run: names, positions, keys | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Without a signature, as for pluck. | ||||||||||||||||||||||||||||
| 94 | 48 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 95 | 48 | ▶ | if (is_plain_hashref($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 7 | ▶ | return $_[0] unless @columns; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 5 | ▶ | $keys //= [map { _key_of($_[0], $_, 0) } @columns]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$keys //= [map({_key_of($_[0], $_, 0);} @columns)]
MC/DC100%1/1
$keys //= [map({_key_of($_[0], $_, 0);} @columns)]
$keys
Truth table100%2/2
A
A= $keys
| |||||||||||||||||||||||||||||
| 98 | 4 | my %hash; | |||||||||||||||||||||||||||
| 99 | 4 | @hash{@$keys} = @{ $_[0] }{@$keys}; | |||||||||||||||||||||||||||
| 100 | 4 | return \%hash; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 41 | ▶ | croakf 'Expected a row: an array, a hash or a row object' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
Ref::Util::XS::is_plain_arrayref($_[0]) or $_[0] isa "DBIx::Squirrel::Result"
MC/DC100%2/2
Ref::Util::XS::is_plain_arrayref($_[0]) or $_[0] isa "DBIx::Squirrel::Result"
Ref::Util::XS::is_plain_arrayref($_[0])
$_[0] isa "DBIx::Squirrel::Result"
Truth table100%3/3
A or B
A= Ref::Util::XS::is_plain_arrayref($_[0])B= $_[0] isa "DBIx::Squirrel::Result"
| |||||||||||||||||||||||||||||
| 103 | unless is_plain_arrayref($_[0]) | ||||||||||||||||||||||||||||
| 104 | || $_[0] isa 'DBIx::Squirrel::Result'; | ||||||||||||||||||||||||||||
| 105 | 40 | ▶ | $names //= [_column_names()]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$names //= [_column_names()]
MC/DC100%1/1
$names //= [_column_names()]
$names
Truth table100%2/2
A
A= $names
| |||||||||||||||||||||||||||||
| 106 | 37 | my %hash; | |||||||||||||||||||||||||||
| 107 | 37 | ▶ | if (@columns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 15 | ▶ | $at //= [map { _last_position_in($names, $_) } @columns]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$at //= [map({_last_position_in($names, $_);} @columns)]
MC/DC100%1/1
$at //= [map({_last_position_in($names, $_);} @columns)]
$at
Truth table100%2/2
A
A= $at
| |||||||||||||||||||||||||||||
| 109 | 14 | @hash{@$names[@$at]} = @{ $_[0] }[@$at]; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||
| 112 | 22 | @hash{@$names} = @{ $_[0] }; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 36 | return \%hash; | |||||||||||||||||||||||||||
| 115 | 27 | }; | |||||||||||||||||||||||||||
| 116 | 27 | }; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Return the names of the columns of the statement whose rows the | ||||||||||||||||||||||||||||
| 120 | # stages are running on. | ||||||||||||||||||||||||||||
| 121 | 49 | ▶ | sub _column_names () { | ||||||||||||||||||||||||||
|
Subroutine
_column_names: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 122 | 49 | ▶ | my $run = DBIx::Squirrel::Context::statement_context() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | or croakf 'Expected a statement to read, for the column names'; | ||||||||||||||||||||||||||||
| 124 | 42 | return @{ $run->{columns} }; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Return the position of a column, by its name: as it's written, or else | ||||||||||||||||||||||||||||
| 128 | # in any case. | ||||||||||||||||||||||||||||
| 129 | 26 | ▶ | sub _position_of ($column) { | ||||||||||||||||||||||||||
|
Subroutine
_position_of: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 130 | 26 | my @names = _column_names(); | |||||||||||||||||||||||||||
| 131 | 22 | my($n) = grep { $names[$_] eq $column } 0 .. $#names; | |||||||||||||||||||||||||||
| 132 | 22 | ▶ | ($n) = grep { lc $names[$_] eq lc $column } 0 .. $#names | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | unless defined $n; | ||||||||||||||||||||||||||||
| 134 | 22 | ▶ | croakf 'Expected a column of the row (%s)', $column unless defined $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 20 | return $n; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Return the position of a column among the names, by its name: as it's | ||||||||||||||||||||||||||||
| 139 | # written, or else in any case. Of two columns of that name, the last, | ||||||||||||||||||||||||||||
| 140 | # as fetchrow_hashref keeps the last of them in its hash. | ||||||||||||||||||||||||||||
| 141 | 8 | ▶ | sub _last_position_in ($names, $column) { | ||||||||||||||||||||||||||
|
Subroutine
_last_position_in: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 142 | 8 | my($n) = reverse grep { $names->[$_] eq $column } 0 .. $#$names; | |||||||||||||||||||||||||||
| 143 | 8 | ▶ | ($n) = reverse grep { lc $names->[$_] eq lc $column } 0 .. $#$names | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | unless defined $n; | ||||||||||||||||||||||||||||
| 145 | 8 | ▶ | croakf 'Expected a column of the row (%s)', $column unless defined $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 7 | return $n; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Return the key of a column in a hash row, by its name: as it's written, | ||||||||||||||||||||||||||||
| 150 | # or else in any case; or by its position. | ||||||||||||||||||||||||||||
| 151 | 32 | ▶ | sub _key_of ($row, $column, $by_position) { | ||||||||||||||||||||||||||
|
Subroutine
_key_of: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 152 | 32 | my $key; | |||||||||||||||||||||||||||
| 153 | 32 | ▶ | if ($by_position) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 154 | 2 | $key = (_column_names())[$column]; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | elsif (exists $row->{$column}) { | ||||||||||||||||||||||||||||
| 157 | 25 | $key = $column; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | else { | ||||||||||||||||||||||||||||
| 160 | 5 | ($key) = grep { lc $_ eq lc $column } sort keys %$row; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 32 | ▶ | croakf 'Expected a column of the row (%s)', $column unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 29 | return $key; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 9 | 1; | |||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||