| 1 | package Tapper::Benchmark::Query::SQLite; | ||||||||||||||||||||||||||||||||||
| 2 | 1 | our $AUTHORITY = 'cpan:TAPPER'; | |||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Tapper::Benchmark - querying - SQLite backend | ||||||||||||||||||||||||||||||||||
| 4 | 1 | $Tapper::Benchmark::Query::SQLite::VERSION = '5.0.0'; | |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use base 'Tapper::Benchmark::Query::common'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use List::MoreUtils qw( any ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 1 | my %h_used_selects; | |||||||||||||||||||||||||||||||||
| 12 | 1 | my %h_default_columns = ( | |||||||||||||||||||||||||||||||||
| 13 | 'NAME' => 'b.bench', | ||||||||||||||||||||||||||||||||||
| 14 | 'UNIT' => 'bu.bench_unit', | ||||||||||||||||||||||||||||||||||
| 15 | 'VALUE' => 'bv.bench_value', | ||||||||||||||||||||||||||||||||||
| 16 | 'VALUE_ID' => 'bv.bench_value_id', | ||||||||||||||||||||||||||||||||||
| 17 | 'CREATED' => 'bv.created_at', | ||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub _NOW { "CURRENT_TIMESTAMP" } | ||||||||||||||||||||||||||||||||
|
Subroutine
_NOW: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub _FOR_UPDATE { "" } | ||||||||||||||||||||||||||||||||
|
Subroutine
_FOR_UPDATE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub select_benchmark_values { | ||||||||||||||||||||||||||||||||
|
Subroutine
select_benchmark_values: not called CC=38 SCAR=73.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | 0 | my ( $or_self, $hr_search ) = @_; | |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | # clear selected columns | ||||||||||||||||||||||||||||||||||
| 29 | 0 | $h_used_selects{$or_self} = {}; | |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | # deep copy hash | ||||||||||||||||||||||||||||||||||
| 32 | 0 | require JSON::XS; | |||||||||||||||||||||||||||||||||
| 33 | 0 | $hr_search = JSON::XS::decode_json( | |||||||||||||||||||||||||||||||||
| 34 | JSON::XS::encode_json( $hr_search ) | ||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | my ( | ||||||||||||||||||||||||||||||||||
| 38 | 0 | $s_limit, | |||||||||||||||||||||||||||||||||
| 39 | $s_offset, | ||||||||||||||||||||||||||||||||||
| 40 | $s_order_by, | ||||||||||||||||||||||||||||||||||
| 41 | @a_select, | ||||||||||||||||||||||||||||||||||
| 42 | @a_from, | ||||||||||||||||||||||||||||||||||
| 43 | @a_from_vals, | ||||||||||||||||||||||||||||||||||
| 44 | @a_where, | ||||||||||||||||||||||||||||||||||
| 45 | @a_where_vals, | ||||||||||||||||||||||||||||||||||
| 46 | ) = ( | ||||||||||||||||||||||||||||||||||
| 47 | q##, | ||||||||||||||||||||||||||||||||||
| 48 | q##, | ||||||||||||||||||||||||||||||||||
| 49 | q##, | ||||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | # limit clause | ||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ( $hr_search->{limit} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( $hr_search->{limit} =~ /^\d+$/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | 0 | $s_limit = "LIMIT $hr_search->{limit}"; | |||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||||||||
| 58 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 59 | 0 | Carp::confess("invalid limit value '$hr_search->{limit}'"); | |||||||||||||||||||||||||||||||||
| 60 | 0 | return; | |||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | # offset clause | ||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $hr_search->{offset} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ( $hr_search->{offset} =~ /^\d+$/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 67 | 0 | $s_offset = "OFFSET $hr_search->{offset}"; | |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||||||
| 70 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 71 | 0 | Carp::confess("invalid offset value '$hr_search->{offset}'"); | |||||||||||||||||||||||||||||||||
| 72 | 0 | return; | |||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | # where clause | ||||||||||||||||||||||||||||||||||
| 77 | 0 | my $i_counter = 0; | |||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ( $hr_search->{where} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 0 | for my $ar_where ( @{$hr_search->{where}} ) { | |||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( any { $ar_where->[1] eq $_ } keys %h_default_columns ) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 82 | 0 | my $s_column = splice( @{$ar_where}, 1, 1 ); | |||||||||||||||||||||||||||||||||
| 83 | 0 | push @a_where, $or_self->create_where_clause( $h_default_columns{$s_column}, $ar_where ); | |||||||||||||||||||||||||||||||||
| 84 | 0 | push @a_where_vals , @{$ar_where}[1..$#{$ar_where}]; | |||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||||||||
| 87 | 0 | my $s_additional_type = splice( @{$ar_where}, 1, 1 ); | |||||||||||||||||||||||||||||||||
| 88 | 0 | my $hr_additional_type = $or_self | |||||||||||||||||||||||||||||||||
| 89 | ->select_addtype_by_name( $s_additional_type ) | ||||||||||||||||||||||||||||||||||
| 90 | ->fetchrow_hashref() | ||||||||||||||||||||||||||||||||||
| 91 | ; | ||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( !$hr_additional_type || !$hr_additional_type->{bench_additional_type_id} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$hr_additional_type and $hr_additional_type->{"bench_additional_type_id"}
MC/DC0%0/2
$hr_additional_type and $hr_additional_type->{"bench_additional_type_id"}
$hr_additional_type
$hr_additional_type->{"bench_additional_type_id"}
Truth table0%0/3
A and B
A= $hr_additional_typeB= $hr_additional_type->{"bench_additional_type_id"}
| |||||||||||||||||||||||||||||||||||
| 93 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 94 | 0 | Carp::confess("benchmark additional value '$s_additional_type' not exists"); | |||||||||||||||||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | 0 | push @a_from, " | |||||||||||||||||||||||||||||||||
| 98 | JOIN ( | ||||||||||||||||||||||||||||||||||
| 99 | $or_self->{config}{tables}{additional_relation_table} bar$i_counter | ||||||||||||||||||||||||||||||||||
| 100 | JOIN $or_self->{config}{tables}{additional_value_table} bav$i_counter | ||||||||||||||||||||||||||||||||||
| 101 | ON ( bav$i_counter.bench_additional_value_id = bar$i_counter.bench_additional_value_id ) | ||||||||||||||||||||||||||||||||||
| 102 | ) | ||||||||||||||||||||||||||||||||||
| 103 | ON ( | ||||||||||||||||||||||||||||||||||
| 104 | bar$i_counter.bench_value_id = bv.bench_value_id | ||||||||||||||||||||||||||||||||||
| 105 | AND bav$i_counter.bench_additional_type_id = ? | ||||||||||||||||||||||||||||||||||
| 106 | ) | ||||||||||||||||||||||||||||||||||
| 107 | "; | ||||||||||||||||||||||||||||||||||
| 108 | 0 | push @a_from_vals, $hr_additional_type->{bench_additional_type_id}; | |||||||||||||||||||||||||||||||||
| 109 | 0 | push @a_where, $or_self->create_where_clause( "bav$i_counter.bench_additional_value", $ar_where ); | |||||||||||||||||||||||||||||||||
| 110 | 0 | push @a_where_vals , @{$ar_where}[1..$#{$ar_where}]; | |||||||||||||||||||||||||||||||||
| 111 | 0 | $i_counter++; | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | # select clause | ||||||||||||||||||||||||||||||||||
| 117 | 0 | my $b_aggregate_all = 0; | |||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ( $hr_search->{select} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 119 | 0 | for my $i_counter ( 0..$#{$hr_search->{select}} ) { | |||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( ref $hr_search->{select}[$i_counter] ne 'ARRAY' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 121 | 0 | $hr_search->{select}[$i_counter] = ['',$hr_search->{select}[$i_counter]]; | |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||
| 123 | ▶ | elsif ( !$b_aggregate_all && $hr_search->{select}[$i_counter][0] ne q## ) { | |||||||||||||||||||||||||||||||||
|
Condition0%0/3
not $b_aggregate_all and $hr_search->{"select"}[$i_counter][0] ne ""
MC/DC0%0/2
not $b_aggregate_all and $hr_search->{"select"}[$i_counter][0] ne ""
not $b_aggregate_all
$hr_search->{"select"}[$i_counter][0] ne ""
Truth table0%0/3
not(A) and B
A= not $b_aggregate_allB= $hr_search->{"select"}[$i_counter][0] ne ""
| |||||||||||||||||||||||||||||||||||
| 124 | 0 | $b_aggregate_all = 1; | |||||||||||||||||||||||||||||||||
| 125 | 0 | for my $s_clause (qw/ order_by limit offset /) { | |||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ( $hr_search->{$s_clause} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 127 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 128 | 0 | Carp::confess("cannot use '$s_clause' with aggregation"); | |||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | push @{$hr_search->{select} ||= []}, map {['',$_]} keys %h_default_columns; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$hr_search->{"select"} ||= []
MC/DC0%0/1
$hr_search->{"select"} ||= []
$hr_search->{"select"}
Truth table0%0/2
A
A= $hr_search->{"select"}
| |||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 0 | for my $ar_select ( @{$hr_search->{select}} ) { | |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | 0 | my ( $s_column, $s_select ) = $or_self->create_select_column( | |||||||||||||||||||||||||||||||||
| 139 | $ar_select, $i_counter, $b_aggregate_all, | ||||||||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ( $s_select ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | 0 | push @a_select, $s_select; | |||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ( $s_column ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | 0 | my $hr_additional_type = $or_self | |||||||||||||||||||||||||||||||||
| 149 | ->select_addtype_by_name( $s_column ) | ||||||||||||||||||||||||||||||||||
| 150 | ->fetchrow_hashref() | ||||||||||||||||||||||||||||||||||
| 151 | ; | ||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ( !$hr_additional_type || !$hr_additional_type->{bench_additional_type_id} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$hr_additional_type and $hr_additional_type->{"bench_additional_type_id"}
MC/DC0%0/2
$hr_additional_type and $hr_additional_type->{"bench_additional_type_id"}
$hr_additional_type
$hr_additional_type->{"bench_additional_type_id"}
Truth table0%0/3
A and B
A= $hr_additional_typeB= $hr_additional_type->{"bench_additional_type_id"}
| |||||||||||||||||||||||||||||||||||
| 153 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 154 | 0 | Carp::confess("benchmark additional value '$s_column' not exists"); | |||||||||||||||||||||||||||||||||
| 155 | 0 | return; | |||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | 0 | push @a_from_vals, $hr_additional_type->{bench_additional_type_id}; | |||||||||||||||||||||||||||||||||
| 159 | 0 | push @a_from, " | |||||||||||||||||||||||||||||||||
| 160 | LEFT JOIN ( | ||||||||||||||||||||||||||||||||||
| 161 | $or_self->{config}{tables}{additional_relation_table} bar$i_counter | ||||||||||||||||||||||||||||||||||
| 162 | JOIN $or_self->{config}{tables}{additional_value_table} bav$i_counter | ||||||||||||||||||||||||||||||||||
| 163 | ON ( bav$i_counter.bench_additional_value_id = bar$i_counter.bench_additional_value_id ) | ||||||||||||||||||||||||||||||||||
| 164 | ) | ||||||||||||||||||||||||||||||||||
| 165 | ON ( | ||||||||||||||||||||||||||||||||||
| 166 | bar$i_counter.bench_value_id = bv.bench_value_id | ||||||||||||||||||||||||||||||||||
| 167 | AND bav$i_counter.bench_additional_type_id = ? | ||||||||||||||||||||||||||||||||||
| 168 | ) | ||||||||||||||||||||||||||||||||||
| 169 | "; | ||||||||||||||||||||||||||||||||||
| 170 | 0 | $i_counter++; | |||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | # order_by clause | ||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ( $hr_search->{order_by} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 178 | 0 | my @a_order_by_possible = keys %h_default_columns; | |||||||||||||||||||||||||||||||||
| 179 | 0 | my @a_order_by_direction = qw/ ASC DESC /; | |||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if ( $hr_search->{select} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 181 | 0 | push @a_order_by_possible, map { $_->[1] } @{$hr_search->{select}}; | |||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||
| 183 | 0 | my @a_order_by; | |||||||||||||||||||||||||||||||||
| 184 | 0 | for my $order_column ( @{$hr_search->{order_by}} ) { | |||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ( ref $order_column ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if ( any { $order_column->[0] eq $_ } @a_order_by_possible ) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if ( any { $order_column->[1] eq $_ } @a_order_by_direction ) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 188 | 0 | my $s_numeric_cast = q##; | |||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if ( $order_column->[2] && $order_column->[2]{numeric} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$order_column->[2] and $order_column->[2]{"numeric"}
MC/DC0%0/2
$order_column->[2] and $order_column->[2]{"numeric"}
$order_column->[2]
$order_column->[2]{"numeric"}
Truth table0%0/3
A and B
A= $order_column->[2]B= $order_column->[2]{"numeric"}
| |||||||||||||||||||||||||||||||||||
| 190 | 0 | $s_numeric_cast = '0 + '; | |||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if ( any { $order_column->[0] eq $_ } keys %h_default_columns ) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 193 | 0 | push @a_order_by, "$s_numeric_cast$h_default_columns{$order_column->[0]} $order_column->[1]"; | |||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||
| 195 | else { | ||||||||||||||||||||||||||||||||||
| 196 | 0 | push @a_order_by, "$s_numeric_cast$order_column->[0] $order_column->[1]"; | |||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||
| 199 | else { | ||||||||||||||||||||||||||||||||||
| 200 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 201 | 0 | Carp::confess("unknown order by direction '$order_column->[1]'"); | |||||||||||||||||||||||||||||||||
| 202 | 0 | return; | |||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||
| 205 | else { | ||||||||||||||||||||||||||||||||||
| 206 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 207 | 0 | Carp::confess("unknown order by column '$order_column->[0]'"); | |||||||||||||||||||||||||||||||||
| 208 | 0 | return; | |||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||
| 211 | else { | ||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | if ( any { $order_column eq $_ } @a_order_by_possible ) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | if ( any { $order_column eq $_ } keys %h_default_columns ) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | 0 | push @a_order_by, "$h_default_columns{$order_column} ASC"; | |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||||||||||||||||
| 217 | 0 | push @a_order_by, "$order_column ASC"; | |||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||
| 220 | else { | ||||||||||||||||||||||||||||||||||
| 221 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 222 | 0 | Carp::confess("unknown order by column '$order_column'"); | |||||||||||||||||||||||||||||||||
| 223 | 0 | return; | |||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||
| 227 | 0 | $s_order_by = 'ORDER BY ' . (join ', ', @a_order_by) | |||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||
| 230 | # replace placeholders inside of raw sql where clause | ||||||||||||||||||||||||||||||||||
| 231 | 0 | my $s_raw_where = $hr_search->{where_sql}; | |||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | if ( $s_raw_where ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 233 | 0 | $s_raw_where =~ s/ | |||||||||||||||||||||||||||||||||
| 234 | \$\{(.+?)\} | ||||||||||||||||||||||||||||||||||
| 235 | / | ||||||||||||||||||||||||||||||||||
| 236 | $h_used_selects{$or_self}{$1} | ||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | ? $h_used_selects{$or_self}{$1} | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 238 | : die "column '$1' not exists in SELECT clause" | ||||||||||||||||||||||||||||||||||
| 239 | /gex; | ||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||
| 242 | return $or_self->execute_query( | ||||||||||||||||||||||||||||||||||
| 243 | " | ||||||||||||||||||||||||||||||||||
| 244 | SELECT | ||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | " . ( join ",\n", map {"$_"} @a_select ) . " | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 246 | FROM | ||||||||||||||||||||||||||||||||||
| 247 | $or_self->{config}{tables}{benchmark_table} b | ||||||||||||||||||||||||||||||||||
| 248 | JOIN $or_self->{config}{tables}{benchmark_value_table} bv | ||||||||||||||||||||||||||||||||||
| 249 | ON ( bv.bench_id = b.bench_id ) | ||||||||||||||||||||||||||||||||||
| 250 | LEFT JOIN $or_self->{config}{tables}{unit_table} bu | ||||||||||||||||||||||||||||||||||
| 251 | ON ( bu.bench_unit_id = b.bench_unit_id ) | ||||||||||||||||||||||||||||||||||
| 252 | " . ( join "\n", @a_from ) . " | ||||||||||||||||||||||||||||||||||
| 253 | WHERE | ||||||||||||||||||||||||||||||||||
| 254 | b.active = 1 | ||||||||||||||||||||||||||||||||||
| 255 | AND bv.active = 1 | ||||||||||||||||||||||||||||||||||
| 256 | " . | ||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | ( @a_where ? join "\n", map { "AND $_" } @a_where : q## ) . | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 258 | ( $s_raw_where ? " $s_raw_where" : q## ) . | ||||||||||||||||||||||||||||||||||
| 259 | " | ||||||||||||||||||||||||||||||||||
| 260 | $s_order_by | ||||||||||||||||||||||||||||||||||
| 261 | $s_limit | ||||||||||||||||||||||||||||||||||
| 262 | $s_offset | ||||||||||||||||||||||||||||||||||
| 263 | ", | ||||||||||||||||||||||||||||||||||
| 264 | @a_from_vals, | ||||||||||||||||||||||||||||||||||
| 265 | @a_where_vals, | ||||||||||||||||||||||||||||||||||
| 266 | ); | ||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | sub create_select_column { | ||||||||||||||||||||||||||||||||
|
Subroutine
create_select_column: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||
| 272 | 0 | my ( $or_self, $ar_select, $i_counter, $b_aggregate_all ) = @_; | |||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||
| 274 | 0 | my $s_aggr_func = q##; | |||||||||||||||||||||||||||||||||
| 275 | 0 | my ( $s_aggr, $s_column ) = @{$ar_select}; | |||||||||||||||||||||||||||||||||
| 276 | 0 | my $s_return_select = q##; | |||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||
| 278 | AGGR: { | ||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | if ( $s_aggr eq q## ) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 280 | # aggregate all columns if a single column is aggregated | ||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if ( $b_aggregate_all ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 282 | 0 | $s_aggr = $or_self->{config}{default_aggregation}; | |||||||||||||||||||||||||||||||||
| 283 | 0 | redo AGGR; | |||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||
| 285 | 0 | $s_return_select = '${COLUMN}'; | |||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||
| 287 | elsif ( $s_aggr eq 'min' ) { | ||||||||||||||||||||||||||||||||||
| 288 | 0 | $s_return_select = 'MIN( ${COLUMN} )'; | |||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||
| 290 | elsif ( $s_aggr eq 'max' ) { | ||||||||||||||||||||||||||||||||||
| 291 | 0 | $s_return_select = 'MAX( ${COLUMN} )'; | |||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||
| 293 | elsif ( $s_aggr eq 'avg' ) { | ||||||||||||||||||||||||||||||||||
| 294 | 0 | $s_return_select = 'AVG( ${COLUMN} )'; | |||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||
| 296 | # Geometric Mean, unsupported in SQLite due to lack of EXP(), | ||||||||||||||||||||||||||||||||||
| 297 | # see http://stackoverflow.com/questions/13190064/how-to-find-power-of-a-number-in-sqlite | ||||||||||||||||||||||||||||||||||
| 298 | # | ||||||||||||||||||||||||||||||||||
| 299 | # elsif ( $s_aggr eq 'gem' ) { | ||||||||||||||||||||||||||||||||||
| 300 | # $s_return_select = 'EXP( SUM( LOG( ${COLUMN} ) ) / COUNT( ${COLUMN} ) )'; | ||||||||||||||||||||||||||||||||||
| 301 | # } | ||||||||||||||||||||||||||||||||||
| 302 | elsif ( $s_aggr eq 'sum' ) { | ||||||||||||||||||||||||||||||||||
| 303 | 0 | $s_return_select = 'SUM( ${COLUMN} )'; | |||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||
| 305 | elsif ( $s_aggr eq 'cnt' ) { | ||||||||||||||||||||||||||||||||||
| 306 | 0 | $s_return_select = 'COUNT( ${COLUMN} )'; | |||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||
| 308 | elsif ( $s_aggr eq 'cnd' ) { | ||||||||||||||||||||||||||||||||||
| 309 | 0 | $s_return_select = 'COUNT( DISTINCT ${COLUMN} )'; | |||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||
| 311 | else { | ||||||||||||||||||||||||||||||||||
| 312 | 0 | require Carp; | |||||||||||||||||||||||||||||||||
| 313 | 0 | Carp::confess("unknown aggregate function '$s_aggr'"); | |||||||||||||||||||||||||||||||||
| 314 | 0 | return; | |||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||
| 316 | } # AGGR | ||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||
| 318 | 0 | my ( $s_return_column ); | |||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | my $s_replace_as = $s_aggr ? $s_aggr . "_$s_column" : $s_column; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | if ( $h_used_selects{$or_self}{$s_replace_as} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 322 | 0 | return; | |||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | if ( any { $s_column eq $_ } keys %h_default_columns ) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 325 | 0 | $h_used_selects{$or_self}{$s_replace_as} = $h_default_columns{$s_column}; | |||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||
| 327 | else { | ||||||||||||||||||||||||||||||||||
| 328 | 0 | $s_return_column = $s_column; | |||||||||||||||||||||||||||||||||
| 329 | 0 | $h_used_selects{$or_self}{$s_replace_as} = "bav$i_counter.bench_additional_value"; | |||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||
| 332 | 0 | $s_return_select =~ s/\$\{COLUMN\}/$h_used_selects{$or_self}{$s_replace_as}/g; | |||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | 0 | return ( $s_return_column, "$s_return_select AS '$s_replace_as'", ); | |||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | sub insert_addtyperelation { | ||||||||||||||||||||||||||||||||
|
Subroutine
insert_addtyperelation: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||
| 340 | 0 | my ( $or_self, @a_vals ) = @_; | |||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||
| 342 | 0 | return $or_self->execute_query( " | |||||||||||||||||||||||||||||||||
| 343 | INSERT OR IGNORE INTO $or_self->{config}{tables}{additional_type_relation_table} | ||||||||||||||||||||||||||||||||||
| 344 | ( bench_id, bench_additional_type_id, created_at ) | ||||||||||||||||||||||||||||||||||
| 345 | VALUES | ||||||||||||||||||||||||||||||||||
| 346 | 0 | ( ?, ?, @{[$or_self->_NOW]} ) | |||||||||||||||||||||||||||||||||
| 347 | ", @a_vals ); | ||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | sub insert_unit { | ||||||||||||||||||||||||||||||||
|
Subroutine
insert_unit: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | 0 | my ( $or_self, @a_vals ) = @_; | |||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||
| 355 | 0 | return $or_self->execute_query( " | |||||||||||||||||||||||||||||||||
| 356 | INSERT INTO $or_self->{config}{tables}{unit_table} | ||||||||||||||||||||||||||||||||||
| 357 | ( bench_unit, created_at ) | ||||||||||||||||||||||||||||||||||
| 358 | VALUES | ||||||||||||||||||||||||||||||||||
| 359 | 0 | ( ?, @{[$or_self->_NOW]} ) | |||||||||||||||||||||||||||||||||
| 360 | ", @a_vals ); | ||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | sub insert_benchmark { | ||||||||||||||||||||||||||||||||
|
Subroutine
insert_benchmark: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||
| 366 | 0 | my ( $or_self, @a_vals ) = @_; | |||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||
| 368 | 0 | return $or_self->execute_query( " | |||||||||||||||||||||||||||||||||
| 369 | INSERT INTO $or_self->{config}{tables}{benchmark_table} | ||||||||||||||||||||||||||||||||||
| 370 | ( bench, bench_unit_id, active, created_at ) | ||||||||||||||||||||||||||||||||||
| 371 | VALUES | ||||||||||||||||||||||||||||||||||
| 372 | 0 | ( ?, ?, 1, @{[$or_self->_NOW]} ) | |||||||||||||||||||||||||||||||||
| 373 | ", @a_vals ); | ||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | sub insert_benchmark_value { | ||||||||||||||||||||||||||||||||
|
Subroutine
insert_benchmark_value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||
| 379 | 0 | my ( $or_self, @a_vals ) = @_; | |||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||
| 381 | 0 | return $or_self->execute_query( " | |||||||||||||||||||||||||||||||||
| 382 | INSERT OR IGNORE INTO $or_self->{config}{tables}{benchmark_value_table} | ||||||||||||||||||||||||||||||||||
| 383 | ( bench_id, bench_subsume_type_id, bench_value, active, created_at ) | ||||||||||||||||||||||||||||||||||
| 384 | VALUES | ||||||||||||||||||||||||||||||||||
| 385 | 0 | ( ?, ?, ?, 1, @{[$or_self->_NOW]} ) | |||||||||||||||||||||||||||||||||
| 386 | ", @a_vals ); | ||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||
| 390 | 0 | ▶ | sub insert_addtype { | ||||||||||||||||||||||||||||||||
|
Subroutine
insert_addtype: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||
| 392 | 0 | my ( $or_self, @a_vals ) = @_; | |||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||
| 394 | 0 | return $or_self->execute_query( " | |||||||||||||||||||||||||||||||||
| 395 | INSERT INTO $or_self->{config}{tables}{additional_type_table} | ||||||||||||||||||||||||||||||||||
| 396 | ( bench_additional_type, created_at ) | ||||||||||||||||||||||||||||||||||
| 397 | VALUES | ||||||||||||||||||||||||||||||||||
| 398 | 0 | ( ?, @{[$or_self->_NOW]} ) | |||||||||||||||||||||||||||||||||
| 399 | ", @a_vals ); | ||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | sub insert_addvalue { | ||||||||||||||||||||||||||||||||
|
Subroutine
insert_addvalue: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||
| 405 | 0 | my ( $or_self, @a_vals ) = @_; | |||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||
| 407 | 0 | return $or_self->execute_query( " | |||||||||||||||||||||||||||||||||
| 408 | INSERT INTO $or_self->{config}{tables}{additional_value_table} | ||||||||||||||||||||||||||||||||||
| 409 | ( bench_additional_type_id, bench_additional_value, created_at ) | ||||||||||||||||||||||||||||||||||
| 410 | VALUES | ||||||||||||||||||||||||||||||||||
| 411 | 0 | ( ?, ?, @{[$or_self->_NOW]} ) | |||||||||||||||||||||||||||||||||
| 412 | ", @a_vals ); | ||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | sub insert_addvaluerelation { | ||||||||||||||||||||||||||||||||
|
Subroutine
insert_addvaluerelation: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||
| 418 | 0 | my ( $or_self, @a_vals ) = @_; | |||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||
| 420 | 0 | return $or_self->execute_query( " | |||||||||||||||||||||||||||||||||
| 421 | INSERT OR IGNORE INTO $or_self->{config}{tables}{additional_relation_table} | ||||||||||||||||||||||||||||||||||
| 422 | ( bench_value_id, bench_additional_value_id, active, created_at ) | ||||||||||||||||||||||||||||||||||
| 423 | VALUES | ||||||||||||||||||||||||||||||||||
| 424 | 0 | ( ?, ?, 1, @{[$or_self->_NOW]} ) | |||||||||||||||||||||||||||||||||
| 425 | ", @a_vals ); | ||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||
| 429 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||
| 431 | __END__ | ||||||||||||||||||||||||||||||||||