| 1 | 28 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package DBIx::Squirrel::Result; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use Digest::SHA qw(sha1_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use DBIx::Squirrel::Errors qw(croakf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 28 | ▶ | use DBIx::Squirrel::Util qw(install_sub is_name); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 28 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Croak at the line that called DBIx::Squirrel. | ||||||||||||||||||||||||||||
| 13 | 22 | our @CARP_NOT = @DBIx::Squirrel::Errors::INTERNAL; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Each row class made, with the position of each of its columns' names. | ||||||||||||||||||||||||||||
| 16 | 22 | my %position_of; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Return a column's value, by its name, in any case, or its position, | ||||||||||||||||||||||||||||
| 19 | # from 0. | ||||||||||||||||||||||||||||
| 20 | 23 | ▶ | sub get_column ($self, $column) { | ||||||||||||||||||||||||||
|
Subroutine
get_column: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | my $n | ||||||||||||||||||||||||||||
| 22 | = $column =~ m( \A \d+ \z )xa | ||||||||||||||||||||||||||||
| 23 | ? $column | ||||||||||||||||||||||||||||
| 24 | 23 | ▶ | : $position_of{ref $self}{lc $column}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 23 | ▶ | croakf 'Expected a column of the row (%s)', $column | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $n or $n >= @$self
MC/DC100%2/2
not defined $n or $n >= @$self
not defined $n
$n >= @$self
Truth table100%3/3
not(A) or B
A= not defined $nB= $n >= @$self
| |||||||||||||||||||||||||||||
| 26 | if !defined $n || $n >= @$self; | ||||||||||||||||||||||||||||
| 27 | 20 | return $self->[$n]; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # For DBIx::Squirrel::Results: return the row class, a subclass of the | ||||||||||||||||||||||||||||
| 31 | # invocant, for rows with the columns named, making it the first time, | ||||||||||||||||||||||||||||
| 32 | # with an accessor for each column. | ||||||||||||||||||||||||||||
| 33 | 43 | ▶ | sub class_for ($base, $names) { | ||||||||||||||||||||||||||
|
Subroutine
class_for: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 43 | my $class = sprintf '%s::Row_%s', $base, | |||||||||||||||||||||||||||
| 35 | substr(sha1_hex(join "\0", @$names), 0, 16); | ||||||||||||||||||||||||||||
| 36 | 43 | ▶ | return $class if $position_of{$class}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 22 | my %position; | |||||||||||||||||||||||||||
| 38 | 22 | for my $n (reverse 0 .. $#$names) { | |||||||||||||||||||||||||||
| 39 | 36 | $position{$names->[$n]} = $n; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||
| 42 | 28 | ▶ | no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 22 | @{ "${class}::ISA" } = ($base); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 22 | for my $name (keys %position) { | |||||||||||||||||||||||||||
| 46 | 35 | ▶ | next if !is_name($name) || $base->can($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not is_name($name) or $base->can($name)
MC/DC100%2/2
not is_name($name) or $base->can($name)
not is_name($name)
$base->can($name)
Truth table100%3/3
not(A) or B
A= not is_name($name)B= $base->can($name)
| |||||||||||||||||||||||||||||
| 47 | 30 | my $n = $position{$name}; | |||||||||||||||||||||||||||
| 48 | # Without a signature, as it's called for each column read, and | ||||||||||||||||||||||||||||
| 49 | # a signature costs about 20 ns a call (roadmap 2026-0028). | ||||||||||||||||||||||||||||
| 50 | 30 | ▶ | install_sub("${class}::$name", sub { $_[0][$n] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
n: called CC=1 SCAR=0.0
Subroutine
id: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 22 | $position_of{$class} = \%position; | |||||||||||||||||||||||||||
| 53 | 22 | return $class; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 22 | 1; | |||||||||||||||||||||||||||
| 57 | __END__ | ||||||||||||||||||||||||||||