| 1 | 1 | ▶ | use 5.008003; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | package LINQ::Database::Util; | ||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.000_002'; | |||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | sub selection_to_sql { | ||||||||||||||||||||||
| 13 | 6 | ▶ | my ( $selection, $name_quoter ) = ( shift, @_ ); | ||||||||||||||||||||
|
Subroutine
selection_to_sql: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | 6 | ▶ | return unless blessed( $selection ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 16 | 6 | ▶ | return unless $selection->isa( 'LINQ::FieldSet::Selection' ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 17 | 6 | ▶ | return if $selection->seen_asterisk; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | $name_quoter ||= sub { | ||||||||||||||||||||||
| 20 | 4 | ▶ | my $name = shift; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 21 | 4 | return sprintf( '"%s"', quotemeta( $name ) ); | |||||||||||||||||||||
| 22 | 6 | ▶ | }; | ||||||||||||||||||||
|
Condition66%2/3
$name_quoter ||= sub {
MC/DC0%0/2
$name_quoter ||= sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
$name_quoter
sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
Truth table66%2/3
A ||= B
A= $name_quoterB= sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
| |||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | 6 | my @cols; | |||||||||||||||||||||
| 25 | 6 | for my $field ( @{ $selection->fields } ) { | |||||||||||||||||||||
| 26 | 8 | my $orig_name = $field->value; | |||||||||||||||||||||
| 27 | 8 | my $aliased = $field->name; | |||||||||||||||||||||
| 28 | 8 | ▶ | return if ref( $orig_name ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 29 | # uncoverable branch true | ||||||||||||||||||||||
| 30 | 8 | ▶ | return if !defined( $aliased ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | 8 | push @cols, $name_quoter->( $orig_name ); | |||||||||||||||||||||
| 33 | } #/ for my $field ( @{ $self...}) | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | 6 | return join( q[, ], @cols ); | |||||||||||||||||||||
| 36 | } #/ sub _sql_selection | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | sub assertion_to_sql { | ||||||||||||||||||||||
| 39 | 5 | ▶ | my ( $assertion, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
assertion_to_sql: called CC=8 SCAR=23.6
Pod
undocumented
| |||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | 5 | ▶ | return unless blessed( $assertion ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | $name_quoter ||= sub { | ||||||||||||||||||||||
| 44 | 2 | ▶ | my $name = shift; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 45 | 2 | return sprintf( '"%s"', quotemeta( $name ) ); | |||||||||||||||||||||
| 46 | 5 | ▶ | }; | ||||||||||||||||||||
|
Condition66%2/3
$name_quoter ||= sub {
MC/DC0%0/2
$name_quoter ||= sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
$name_quoter
sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
Truth table66%2/3
A ||= B
A= $name_quoterB= sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
| |||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | $value_quoter ||= sub { | ||||||||||||||||||||||
| 49 | 2 | ▶ | my $name = shift; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 50 | 2 | return sprintf( '"%s"', quotemeta( $name ) ); | |||||||||||||||||||||
| 51 | 5 | ▶ | }; | ||||||||||||||||||||
|
Condition66%2/3
$value_quoter ||= sub {
MC/DC0%0/2
$value_quoter ||= sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
$value_quoter
sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
Truth table66%2/3
A ||= B
A= $value_quoterB= sub {
my $name = shift();
return sprintf('"%s"', quotemeta $name);
}
| |||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | 5 | ▶ | if ( $assertion->isa( 'LINQ::FieldSet::Assertion::AND' ) ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 54 | 0 | return _assertion_to_sql_AND( $assertion, $name_quoter, $value_quoter ); | |||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||
| 56 | elsif ( $assertion->isa( 'LINQ::FieldSet::Assertion::OR' ) ) { | ||||||||||||||||||||||
| 57 | 0 | return _assertion_to_sql_OR( $assertion, $name_quoter, $value_quoter ); | |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||
| 59 | elsif ( $assertion->isa( 'LINQ::FieldSet::Assertion::NOT' ) ) { | ||||||||||||||||||||||
| 60 | 0 | return _assertion_to_sql_NOT( $assertion, $name_quoter, $value_quoter ); | |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||
| 62 | elsif ( $assertion->isa( 'LINQ::FieldSet::Assertion' ) ) { | ||||||||||||||||||||||
| 63 | 5 | return _assertion_to_sql_FIELDSET( $assertion, $name_quoter, $value_quoter ); | |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | |||||||||||||||||||||||
| 67 | sub _assertion_to_sql_AND { | ||||||||||||||||||||||
| 68 | 0 | ▶ | my ( $assertion, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_AND: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | 0 | ▶ | my $left = assertion_to_sql( $assertion->left, $name_quoter, $value_quoter ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 71 | or return; | ||||||||||||||||||||||
| 72 | 0 | ▶ | my $right = assertion_to_sql( $assertion->right, $name_quoter, $value_quoter ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 73 | or return; | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | 0 | return "($left) AND ($right)"; | |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | sub _assertion_to_sql_OR { | ||||||||||||||||||||||
| 79 | 0 | ▶ | my ( $assertion, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_OR: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | 0 | ▶ | my $left = assertion_to_sql( $assertion->left, $name_quoter, $value_quoter ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 82 | or return; | ||||||||||||||||||||||
| 83 | 0 | ▶ | my $right = assertion_to_sql( $assertion->right, $name_quoter, $value_quoter ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 84 | or return; | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 0 | return "($left) OR ($right)"; | |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | sub _assertion_to_sql_NOT { | ||||||||||||||||||||||
| 90 | 0 | ▶ | my ( $assertion, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_NOT: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | 0 | ▶ | my $left = assertion_to_sql( $assertion->left, $name_quoter, $value_quoter ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 93 | or return; | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | 0 | return "NOT ($left)"; | |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | sub _assertion_to_sql_FIELDSET { | ||||||||||||||||||||||
| 99 | 5 | ▶ | my ( $assertion, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_FIELDSET: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 5 | my @fields; | |||||||||||||||||||||
| 102 | 5 | for my $field ( @{ $assertion->fields } ) { | |||||||||||||||||||||
| 103 | 5 | ▶ | my $field_sql = _assertion_to_sql_FIELD( $field, $name_quoter, $value_quoter ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 104 | or return; | ||||||||||||||||||||||
| 105 | 4 | push @fields, "($field_sql)"; | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | 4 | join " AND ", @fields; | |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | sub _assertion_to_sql_FIELD { | ||||||||||||||||||||||
| 112 | 5 | ▶ | my ( $field, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_FIELD: called CC=8 SCAR=23.7
| |||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | 5 | ▶ | return if ref( $field->value ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 115 | 5 | my $result; | |||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | 5 | ▶ | if ( exists $field->params->{is} ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 118 | 4 | $result = _assertion_to_sql_FIELD_IS( @_ ); | |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | elsif ( exists $field->params->{in} ) { | ||||||||||||||||||||||
| 121 | 0 | $result = _assertion_to_sql_FIELD_IN( @_ ); | |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | elsif ( exists $field->params->{like} ) { | ||||||||||||||||||||||
| 124 | 0 | $result = _assertion_to_sql_FIELD_LIKE( @_ ); | |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | elsif ( exists $field->params->{to} ) { | ||||||||||||||||||||||
| 127 | 0 | $result = _assertion_to_sql_FIELD_TO( @_ ); | |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | 5 | ▶ | return unless defined $result; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | 4 | ▶ | if ( exists $field->params->{nix} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 133 | 0 | return "NOT ($result)"; | |||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | 4 | return $result; | |||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | sub _assertion_to_sql_FIELD_IS { | ||||||||||||||||||||||
| 140 | 4 | ▶ | my ( $field, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_FIELD_IS: called CC=4 SCAR=15.5
| |||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | 4 | ▶ | my $cmp = $field->params->{cmp} || '=='; | ||||||||||||||||||||
|
Condition50%1/2
$field->params->{'cmp'} || '=='
MC/DC0%0/1
$field->params->{'cmp'} || '=='
$field->params->{'cmp'}
Truth table50%1/2
A
A= $field->params->{'cmp'}
| |||||||||||||||||||||||
| 143 | 4 | ▶ | if ( $cmp eq '!=' ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 144 | 0 | $cmp = '<>'; # SQL syntax <> Perl syntax | |||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | my $wrapper = $field->params->{nocase} | ||||||||||||||||||||||
| 148 | 0 | ▶ | ? sub { sprintf( 'LOWER(%s)', $_[0] ) } | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 149 | 4 | ▶ | : sub { $_[0] }; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | return sprintf( | ||||||||||||||||||||||
| 152 | '%s %s %s', | ||||||||||||||||||||||
| 153 | $wrapper->( $name_quoter->( $field->value ) ), | ||||||||||||||||||||||
| 154 | $cmp, | ||||||||||||||||||||||
| 155 | 4 | $wrapper->( $value_quoter->( $field->params->{is} ) ), | |||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||
| 158 | |||||||||||||||||||||||
| 159 | sub _assertion_to_sql_FIELD_IN { | ||||||||||||||||||||||
| 160 | 0 | ▶ | my ( $field, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_FIELD_IN: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | return sprintf( | ||||||||||||||||||||||
| 163 | '%s IN (%s)', | ||||||||||||||||||||||
| 164 | $name_quoter->( $field->value ), | ||||||||||||||||||||||
| 165 | join( | ||||||||||||||||||||||
| 166 | q[, ], | ||||||||||||||||||||||
| 167 | 0 | map $value_quoter->( $_ ), @{ $field->params->{to} }, | |||||||||||||||||||||
| 168 | ), | ||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||
| 171 | |||||||||||||||||||||||
| 172 | sub _assertion_to_sql_FIELD_LIKE { | ||||||||||||||||||||||
| 173 | 0 | ▶ | my ( $field, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_FIELD_LIKE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | my $wrapper = $field->params->{nocase} | ||||||||||||||||||||||
| 176 | 0 | ▶ | ? sub { sprintf( 'LOWER(%s)', $_[0] ) } | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 177 | 0 | ▶ | : sub { $_[0] }; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | return sprintf( | ||||||||||||||||||||||
| 180 | '%s LIKE %s', | ||||||||||||||||||||||
| 181 | $wrapper->( $name_quoter->( $field->value ) ), | ||||||||||||||||||||||
| 182 | 0 | $wrapper->( $value_quoter->( $field->params->{like} ) ), | |||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||
| 185 | |||||||||||||||||||||||
| 186 | sub _assertion_to_sql_FIELD_TO { | ||||||||||||||||||||||
| 187 | 0 | ▶ | my ( $field, $name_quoter, $value_quoter ) = ( @_ ); | ||||||||||||||||||||
|
Subroutine
_assertion_to_sql_FIELD_TO: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | 0 | ▶ | my $cmp = $field->params->{cmp} || '=='; | ||||||||||||||||||||
|
Condition0%0/2
$field->params->{'cmp'} || '=='
MC/DC0%0/1
$field->params->{'cmp'} || '=='
$field->params->{'cmp'}
Truth table0%0/2
A
A= $field->params->{'cmp'}
| |||||||||||||||||||||||
| 190 | 0 | ▶ | if ( $cmp eq '!=' ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 191 | 0 | $cmp = '<>'; # SQL syntax <> Perl syntax | |||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||
| 193 | |||||||||||||||||||||||
| 194 | my $wrapper = $field->params->{nocase} | ||||||||||||||||||||||
| 195 | 0 | ▶ | ? sub { sprintf( 'LOWER(%s)', $_[0] ) } | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 196 | 0 | ▶ | : sub { $_[0] }; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||
| 197 | |||||||||||||||||||||||
| 198 | return sprintf( | ||||||||||||||||||||||
| 199 | '%s %s %s', | ||||||||||||||||||||||
| 200 | $wrapper->( $name_quoter->( $field->value ) ), | ||||||||||||||||||||||
| 201 | $cmp, | ||||||||||||||||||||||
| 202 | 0 | $wrapper->( $name_quoter->( $field->params->{to} ) ), | |||||||||||||||||||||
| 203 | ); | ||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||
| 205 | |||||||||||||||||||||||
| 206 | 1 | 1; | |||||||||||||||||||||