| 1 | package BenchmarkAnything::Storage::Backend::SQL::Query::common; | ||
| 2 | our $AUTHORITY = 'cpan:TAPPER'; | ||
| 3 | # ABSTRACT: BenchmarkAnything::Storage::Backend::SQL - querying - backend base class | ||
| 4 | $BenchmarkAnything::Storage::Backend::SQL::Query::common::VERSION = '0.029'; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use base 'BenchmarkAnything::Storage::Backend::SQL::Query'; | ||
| 8 | |||
| 9 | use List::MoreUtils qw( any ); | ||
| 10 | |||
| 11 | my %h_used_selects; | ||
| 12 | 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 | sub _NOW { "CURRENT_TIMESTAMP" } | ||
| 21 | |||
| 22 | sub _FOR_UPDATE { "FOR UPDATE" } | ||
| 23 | |||
| 24 | sub default_columns { | ||
| 25 | return %h_default_columns; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub benchmark_operators { | ||
| 29 | return ( '=', '!=', 'like', 'not_like', 'is_empty', '<', '>', '<=', '>=' ); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub create_where_clause { | ||
| 33 | |||
| 34 | my ( $or_self, $s_column_name, $ar_value ) = @_; | ||
| 35 | |||
| 36 | my $s_where_clause = q##; | ||
| 37 | if ( $ar_value->[0] eq 'not like' or $ar_value->[0] eq 'not_like' ) { | ||
| 38 | $s_where_clause = "$s_column_name NOT LIKE ?"; | ||
| 39 | } | ||
| 40 | elsif ( $ar_value->[0] eq 'like' ) { | ||
| 41 | $s_where_clause = "$s_column_name LIKE ?"; | ||
| 42 | } | ||
| 43 | elsif ( $ar_value->[0] eq 'is_empty' ) { | ||
| 44 | my $empty_option = $ar_value->[1]; | ||
| 45 | use Data::Dumper; | ||
| 46 | warn "ar_value: ".Dumper($ar_value); | ||
| 47 | if (defined($ar_value->[1]) and $ar_value->[1] eq '0') { | ||
| 48 | # check that field is NOT EMPTY: | ||
| 49 | # [ "is_empty", "some_field_name", 0 ] | ||
| 50 | $s_where_clause = "$s_column_name IS NOT NULL AND $s_column_name != ''"; | ||
| 51 | |||
| 52 | } elsif (defined($ar_value->[1]) and $ar_value->[1] eq '2') { | ||
| 53 | # check that field is just EMPTY but exists (ie. not undefined/null): | ||
| 54 | # [ "is_empty", "some_field_name", 2 ] | ||
| 55 | $s_where_clause = "$s_column_name IS NULL"; | ||
| 56 | |||
| 57 | } elsif (defined($ar_value->[1]) and $ar_value->[1] eq '1') { # TODO: Does not work yet (sic, THE actual feature) | ||
| 58 | # check that field is EMPTY or UNDEFINED/NULL: | ||
| 59 | # [ "is_empty", "some_field_name", 1 ] | ||
| 60 | # [ "is_empty", "some_field_name" ] | ||
| 61 | $s_where_clause = "$s_column_name IS NULL OR $s_column_name = ''"; | ||
| 62 | |||
| 63 | } elsif (not defined($ar_value->[1])) { | ||
| 64 | # check that field is EMPTY or UNDEFINED/NULL: | ||
| 65 | # [ "is_empty", "some_field_name", 1 ] | ||
| 66 | # [ "is_empty", "some_field_name" ] | ||
| 67 | warn "unsupported 'is_empty' condition (undef). Interpreting as 'is_empty' condition (1)."; | ||
| 68 | |||
| 69 | } else { | ||
| 70 | # we might invent other semantics so we better warn about | ||
| 71 | # what could once become meaningful. | ||
| 72 | warn "unclear 'is_empty' condition (".$ar_value->[1]."). Interpreting as 'is_empty' condition (1)."; | ||
| 73 | $s_where_clause = "$s_column_name IS NULL OR $s_column_name = ''"; | ||
| 74 | } | ||
| 75 | warn "WHERE_CLAUSE: ".$s_where_clause."\n"; | ||
| 76 | warn "ar_value->[1]: ".$ar_value->[1]."\n"; | ||
| 77 | } | ||
| 78 | elsif ( | ||
| 79 | $ar_value->[0] eq '<' | ||
| 80 | || $ar_value->[0] eq '>' | ||
| 81 | || $ar_value->[0] eq '<=' | ||
| 82 | || $ar_value->[0] eq '>=' | ||
| 83 | ) { | ||
| 84 | $s_where_clause = "$s_column_name $ar_value->[0] ?"; | ||
| 85 | } | ||
| 86 | elsif ( $ar_value->[0] eq '=' ) { | ||
| 87 | if ( $#{$ar_value} > 1 ) { | ||
| 88 | $s_where_clause = "$s_column_name IN (" . (join ',', map {'?'} 2..@{$ar_value}) . ')'; | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | $s_where_clause = "$s_column_name = ?"; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | elsif ( $ar_value->[0] eq '!=' ) { | ||
| 95 | if ( $#{$ar_value} > 1 ) { | ||
| 96 | $s_where_clause = "$s_column_name NOT IN (" . (join ',', map {'?'} 2..@{$ar_value}) . ')'; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $s_where_clause = "$s_column_name != ?"; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | require Carp; | ||
| 104 | Carp::confess("unknown operator '$ar_value->[0]'"); | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | return $s_where_clause; | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | sub create_period_check { | ||
| 113 | |||
| 114 | my ( $or_self, $s_column, $dt_from, $dt_to ) = @_; | ||
| 115 | |||
| 116 | my @a_vals; | ||
| 117 | my $s_where; | ||
| 118 | if ( $dt_from ) { | ||
| 119 | if ( my ( $s_date, $s_time ) = $dt_from =~ /(\d{4}-\d{2}-\d{2})( \d{2}:\d{2}:\d{2})?/ ) { | ||
| 120 | $s_where .= "\nAND $s_column > ?"; | ||
| 121 | push @a_vals, $s_date . ( $s_time || ' 00:00:00' ); | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | require Carp; | ||
| 125 | Carp::confess(q#unknown date format for 'date_from'#); | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | if ( $dt_to ) { | ||
| 130 | if ( my ( $s_date, $s_time ) = $dt_to =~ /(\d{4}-\d{2}-\d{2})( \d{2}:\d{2}:\d{2})?/ ) { | ||
| 131 | $s_where .= "\nAND $s_column < ?"; | ||
| 132 | push @a_vals, $s_date . ( $s_time || ' 23:59:59' ); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | require Carp; | ||
| 136 | Carp::confess(q#unknown date format for 'date_to'#); | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | return { | ||
| 142 | vals => \@a_vals, | ||
| 143 | where => $s_where, | ||
| 144 | }; | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | sub select_benchmark_point_essentials { | ||
| 149 | |||
| 150 | my ( $or_self, @a_vals ) = @_; | ||
| 151 | |||
| 152 | return $or_self->execute_query( " | ||
| 153 | SELECT | ||
| 154 | b.bench, | ||
| 155 | bv.created_at, | ||
| 156 | bv.bench_value, | ||
| 157 | bv.bench_value_id, | ||
| 158 | bu.bench_unit | ||
| 159 | FROM | ||
| 160 | $or_self->{config}{tables}{benchmark_table} b | ||
| 161 | JOIN | ||
| 162 | $or_self->{config}{tables}{benchmark_value_table} bv | ||
| 163 | ON | ||
| 164 | b.bench_id = bv.bench_id | ||
| 165 | LEFT JOIN | ||
| 166 | $or_self->{config}{tables}{unit_table} bu | ||
| 167 | ON | ||
| 168 | b.bench_unit_id = bu.bench_unit_id | ||
| 169 | WHERE | ||
| 170 | bv.bench_value_id = ? | ||
| 171 | ; | ||
| 172 | ", @a_vals ); | ||
| 173 | |||
| 174 | } | ||
| 175 | |||
| 176 | sub select_complete_benchmark_point { | ||
| 177 | |||
| 178 | my ( $or_self, @a_vals ) = @_; | ||
| 179 | |||
| 180 | my $query = " | ||
| 181 | SELECT | ||
| 182 | bat.bench_additional_type, | ||
| 183 | bav.bench_additional_value | ||
| 184 | FROM | ||
| 185 | benchs b | ||
| 186 | JOIN | ||
| 187 | bench_values bv | ||
| 188 | ON | ||
| 189 | b.bench_id = bv.bench_id | ||
| 190 | JOIN | ||
| 191 | bench_additional_type_relations batr | ||
| 192 | ON | ||
| 193 | bv.bench_id = batr.bench_id | ||
| 194 | JOIN | ||
| 195 | bench_additional_types bat | ||
| 196 | ON | ||
| 197 | batr.bench_additional_type_id = bat.bench_additional_type_id | ||
| 198 | JOIN | ||
| 199 | bench_additional_relations bar | ||
| 200 | ON | ||
| 201 | bv.bench_value_id = bar.bench_value_id | ||
| 202 | JOIN | ||
| 203 | bench_additional_values bav | ||
| 204 | ON | ||
| 205 | bar.bench_additional_value_id = bav.bench_additional_value_id AND | ||
| 206 | bat.bench_additional_type_id = bav.bench_additional_type_id | ||
| 207 | WHERE | ||
| 208 | bv.bench_value_id = ? | ||
| 209 | ORDER BY | ||
| 210 | bat.bench_additional_type"; | ||
| 211 | return $or_self->execute_query( $query, @a_vals ); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub select_multiple_benchmark_points_additionals { | ||
| 215 | |||
| 216 | my ( $or_self, $i_start, $i_count ) = @_; | ||
| 217 | |||
| 218 | my $i_to = $i_start + $i_count; | ||
| 219 | |||
| 220 | my $query = " | ||
| 221 | SELECT | ||
| 222 | bv.bench_value_id, | ||
| 223 | bat.bench_additional_type, | ||
| 224 | bav.bench_additional_value | ||
| 225 | FROM | ||
| 226 | benchs b | ||
| 227 | JOIN | ||
| 228 | bench_values bv | ||
| 229 | ON | ||
| 230 | b.bench_id = bv.bench_id | ||
| 231 | JOIN | ||
| 232 | bench_additional_type_relations batr | ||
| 233 | ON | ||
| 234 | bv.bench_id = batr.bench_id | ||
| 235 | JOIN | ||
| 236 | bench_additional_types bat | ||
| 237 | ON | ||
| 238 | batr.bench_additional_type_id = bat.bench_additional_type_id | ||
| 239 | JOIN | ||
| 240 | bench_additional_relations bar | ||
| 241 | ON | ||
| 242 | bv.bench_value_id = bar.bench_value_id | ||
| 243 | JOIN | ||
| 244 | bench_additional_values bav | ||
| 245 | ON | ||
| 246 | bar.bench_additional_value_id = bav.bench_additional_value_id AND | ||
| 247 | bat.bench_additional_type_id = bav.bench_additional_type_id | ||
| 248 | WHERE | ||
| 249 | bv.bench_value_id >= ? AND | ||
| 250 | bv.bench_value_id < ? | ||
| 251 | ORDER BY | ||
| 252 | bat.bench_additional_type"; | ||
| 253 | return $or_self->execute_query( $query, $i_start, $i_to ); | ||
| 254 | } | ||
| 255 | |||
| 256 | sub select_multiple_benchmark_points_essentials { | ||
| 257 | |||
| 258 | my ( $or_self, $i_start, $i_count ) = @_; | ||
| 259 | |||
| 260 | my $i_to = $i_start + $i_count; | ||
| 261 | |||
| 262 | return $or_self->execute_query( " | ||
| 263 | SELECT | ||
| 264 | b.bench, | ||
| 265 | bv.created_at, | ||
| 266 | bv.bench_value, | ||
| 267 | bv.bench_value_id, | ||
| 268 | bu.bench_unit | ||
| 269 | FROM | ||
| 270 | $or_self->{config}{tables}{benchmark_table} b | ||
| 271 | JOIN | ||
| 272 | $or_self->{config}{tables}{benchmark_value_table} bv | ||
| 273 | ON | ||
| 274 | b.bench_id = bv.bench_id | ||
| 275 | LEFT JOIN | ||
| 276 | $or_self->{config}{tables}{unit_table} bu | ||
| 277 | ON | ||
| 278 | b.bench_unit_id = bu.bench_unit_id | ||
| 279 | WHERE | ||
| 280 | bv.bench_value_id >= ? AND | ||
| 281 | bv.bench_value_id < ? | ||
| 282 | ; | ||
| 283 | ", $i_start, $i_to ); | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | sub select_addtype_by_name { | ||
| 288 | |||
| 289 | my ( $or_self, @a_vals ) = @_; | ||
| 290 | |||
| 291 | return $or_self->execute_query( " | ||
| 292 | SELECT bench_additional_type_id | ||
| 293 | FROM $or_self->{config}{tables}{additional_type_table} | ||
| 294 | WHERE bench_additional_type = ? | ||
| 295 | ", @a_vals ); | ||
| 296 | |||
| 297 | } | ||
| 298 | |||
| 299 | sub select_min_subsume_type { | ||
| 300 | |||
| 301 | my ( $or_self, @a_vals ) = @_; | ||
| 302 | |||
| 303 | return $or_self->execute_query( " | ||
| 304 | SELECT bench_subsume_type_id | ||
| 305 | FROM $or_self->{config}{tables}{subsume_type_table} | ||
| 306 | ORDER BY bench_subsume_type_rank ASC | ||
| 307 | LIMIT 1 | ||
| 308 | " ); | ||
| 309 | |||
| 310 | } | ||
| 311 | |||
| 312 | sub select_subsume_type { | ||
| 313 | |||
| 314 | my ( $or_self, @a_vals ) = @_; | ||
| 315 | |||
| 316 | return $or_self->execute_query( " | ||
| 317 | SELECT | ||
| 318 | bench_subsume_type_id, | ||
| 319 | bench_subsume_type_rank, | ||
| 320 | datetime_strftime_pattern | ||
| 321 | FROM | ||
| 322 | $or_self->{config}{tables}{subsume_type_table} | ||
| 323 | WHERE | ||
| 324 | bench_subsume_type = ? | ||
| 325 | ", @a_vals ); | ||
| 326 | |||
| 327 | } | ||
| 328 | |||
| 329 | sub select_check_subsumed_values { | ||
| 330 | |||
| 331 | my ( $or_self, $hr_vals ) = @_; | ||
| 332 | |||
| 333 | if (! $hr_vals->{subsume_type_id} ) { | ||
| 334 | require Carp; | ||
| 335 | Carp::confess(q#required parameter 'subsume_type_id' is missing#); | ||
| 336 | return; | ||
| 337 | } | ||
| 338 | |||
| 339 | my $hr_period_check = $or_self->create_period_check( | ||
| 340 | 'bv.created_at', $hr_vals->{date_from}, $hr_vals->{date_to} | ||
| 341 | ); | ||
| 342 | |||
| 343 | return $or_self->execute_query( | ||
| 344 | " | ||
| 345 | SELECT | ||
| 346 | bv.bench_value_id | ||
| 347 | FROM | ||
| 348 | bench_values bv | ||
| 349 | JOIN bench_subsume_types bet | ||
| 350 | ON ( bv.bench_subsume_type_id = bet.bench_subsume_type_id ) | ||
| 351 | WHERE | ||
| 352 | bet.bench_subsume_type_rank > ( | ||
| 353 | SELECT beti.bench_subsume_type_rank | ||
| 354 | FROM bench_subsume_types beti | ||
| 355 | WHERE bench_subsume_type_id = ? | ||
| 356 | ) | ||
| 357 | $hr_period_check->{where} | ||
| 358 | LIMIT | ||
| 359 | 1 | ||
| 360 | ", | ||
| 361 | $hr_vals->{subsume_type_id}, | ||
| 362 | @{$hr_period_check->{vals}}, | ||
| 363 | ); | ||
| 364 | |||
| 365 | } | ||
| 366 | |||
| 367 | sub select_data_values_for_subsume { | ||
| 368 | |||
| 369 | my ( $or_self, $hr_vals ) = @_; | ||
| 370 | |||
| 371 | my $hr_period_check = $or_self->create_period_check( | ||
| 372 | 'bv.created_at', $hr_vals->{date_from}, $hr_vals->{date_to} | ||
| 373 | ); | ||
| 374 | |||
| 375 | my @a_addexclude_vals; | ||
| 376 | my $s_addexclude_where = q##; | ||
| 377 | if ( $hr_vals->{exclude_additionals} && @{$hr_vals->{exclude_additionals}} ) { | ||
| 378 | $s_addexclude_where = 'AND bav.bench_additional_type_id NOT IN (' . (join ',', map {'?'} @{$hr_vals->{exclude_additionals}}) . ')'; | ||
| 379 | push @a_addexclude_vals, @{$hr_vals->{exclude_additionals}}; | ||
| 380 | } | ||
| 381 | |||
| 382 | return $or_self->execute_query( | ||
| 383 | " | ||
| 384 | SELECT | ||
| 385 | b.bench_id, | ||
| 386 | bv.bench_value_id, | ||
| 387 | bv.created_at, | ||
| 388 | bv.bench_value, | ||
| 389 | bet.bench_subsume_type_rank, | ||
| 390 | GROUP_CONCAT( | ||
| 391 | bav.bench_additional_type_id, | ||
| 392 | '|', | ||
| 393 | bav.bench_additional_value_id | ||
| 394 | ORDER BY | ||
| 395 | bav.bench_additional_type_id, | ||
| 396 | bav.bench_additional_value_id | ||
| 397 | SEPARATOR | ||
| 398 | '-' | ||
| 399 | ) AS additionals | ||
| 400 | FROM | ||
| 401 | benchs b | ||
| 402 | JOIN bench_values bv | ||
| 403 | ON ( bv.bench_id = b.bench_id ) | ||
| 404 | JOIN bench_subsume_types bet | ||
| 405 | ON ( bet.bench_subsume_type_id = bv.bench_subsume_type_id ) | ||
| 406 | LEFT JOIN ( | ||
| 407 | bench_additional_relations bar | ||
| 408 | JOIN bench_additional_values bav | ||
| 409 | ON ( bav.bench_additional_value_id = bar.bench_additional_value_id ) | ||
| 410 | ) | ||
| 411 | ON ( | ||
| 412 | bar.active = 1 | ||
| 413 | AND bar.bench_value_id = bv.bench_value_id | ||
| 414 | $s_addexclude_where | ||
| 415 | ) | ||
| 416 | WHERE | ||
| 417 | b.active = 1 | ||
| 418 | AND bv.active = 1 | ||
| 419 | $hr_period_check->{where} | ||
| 420 | GROUP BY | ||
| 421 | bet.bench_subsume_type_rank, | ||
| 422 | b.bench_id, | ||
| 423 | bv.created_at, | ||
| 424 | bv.bench_value, | ||
| 425 | bv.bench_value_id | ||
| 426 | ORDER BY | ||
| 427 | b.bench_id, | ||
| 428 | additionals, | ||
| 429 | bv.created_at | ||
| 430 | ", | ||
| 431 | @a_addexclude_vals, | ||
| 432 | @{$hr_period_check->{vals}}, | ||
| 433 | ); | ||
| 434 | } | ||
| 435 | |||
| 436 | sub select_benchmark { | ||
| 437 | |||
| 438 | my ( $or_self, @a_vals ) = @_; | ||
| 439 | |||
| 440 | return $or_self->execute_query( " | ||
| 441 | SELECT bench_id | ||
| 442 | FROM $or_self->{config}{tables}{benchmark_table} | ||
| 443 | WHERE bench = ? | ||
| 444 | ", @a_vals ); | ||
| 445 | |||
| 446 | } | ||
| 447 | |||
| 448 | sub select_benchmark_names { | ||
| 449 | |||
| 450 | my ( $or_self, @a_vals ) = @_; | ||
| 451 | |||
| 452 | my $query = " | ||
| 453 | SELECT DISTINCT bench | ||
| 454 | FROM $or_self->{config}{tables}{benchmark_table}"; | ||
| 455 | $query .= " | ||
| 456 | WHERE bench LIKE ? " if @a_vals; | ||
| 457 | return $or_self->execute_query( $query, @a_vals ); | ||
| 458 | |||
| 459 | } | ||
| 460 | |||
| 461 | sub select_additional_keys { | ||
| 462 | |||
| 463 | my ( $or_self, @a_vals ) = @_; | ||
| 464 | |||
| 465 | my $query = " | ||
| 466 | SELECT DISTINCT bench_additional_type | ||
| 467 | FROM $or_self->{config}{tables}{additional_type_table}"; | ||
| 468 | $query .= " | ||
| 469 | WHERE bench_additional_type LIKE ? " if @a_vals; | ||
| 470 | return $or_self->execute_query( $query, @a_vals ); | ||
| 471 | |||
| 472 | } | ||
| 473 | |||
| 474 | sub select_additional_key_id { | ||
| 475 | |||
| 476 | my ( $or_self, @a_vals ) = @_; | ||
| 477 | |||
| 478 | return $or_self->execute_query( " | ||
| 479 | SELECT DISTINCT bench_additional_type_id | ||
| 480 | FROM $or_self->{config}{tables}{additional_type_table} | ||
| 481 | WHERE bench_additional_type = ? | ||
| 482 | ", @a_vals ); | ||
| 483 | |||
| 484 | } | ||
| 485 | |||
| 486 | sub select_count_datapoints { | ||
| 487 | my ( $or_self, @a_vals ) = @_; | ||
| 488 | |||
| 489 | return $or_self->execute_query( "SELECT COUNT(1) FROM $or_self->{config}{tables}{benchmark_value_table}" ); | ||
| 490 | } | ||
| 491 | |||
| 492 | sub select_count_metrics { | ||
| 493 | my ( $or_self, @a_vals ) = @_; | ||
| 494 | |||
| 495 | return $or_self->execute_query( "SELECT COUNT(1) FROM $or_self->{config}{tables}{benchmark_table}" ); | ||
| 496 | } | ||
| 497 | |||
| 498 | sub select_count_keys { | ||
| 499 | my ( $or_self, @a_vals ) = @_; | ||
| 500 | |||
| 501 | return $or_self->execute_query( "SELECT COUNT(1) FROM $or_self->{config}{tables}{additional_type_table}" ); | ||
| 502 | } | ||
| 503 | |||
| 504 | sub select_count_datapointkeys { | ||
| 505 | my ( $or_self, @a_vals ) = @_; | ||
| 506 | |||
| 507 | return $or_self->execute_query( "SELECT COUNT(1) FROM $or_self->{config}{tables}{additional_relation_table}" ); | ||
| 508 | } | ||
| 509 | |||
| 510 | sub select_unit { | ||
| 511 | |||
| 512 | my ( $or_self, @a_vals ) = @_; | ||
| 513 | |||
| 514 | return $or_self->execute_query( " | ||
| 515 | SELECT bench_unit_id | ||
| 516 | FROM $or_self->{config}{tables}{unit_table} | ||
| 517 | WHERE bench_unit = ? | ||
| 518 | ", @a_vals ); | ||
| 519 | |||
| 520 | } | ||
| 521 | |||
| 522 | sub select_addtype { | ||
| 523 | |||
| 524 | my ( $or_self, @a_vals ) = @_; | ||
| 525 | |||
| 526 | return $or_self->execute_query( " | ||
| 527 | SELECT bench_additional_type_id | ||
| 528 | FROM $or_self->{config}{tables}{additional_type_table} | ||
| 529 | WHERE bench_additional_type = ? | ||
| 530 | ", @a_vals ); | ||
| 531 | |||
| 532 | } | ||
| 533 | |||
| 534 | sub select_addvalue { | ||
| 535 | |||
| 536 | my ( $or_self, @a_vals ) = @_; | ||
| 537 | |||
| 538 | return $or_self->execute_query( " | ||
| 539 | SELECT bench_additional_value_id | ||
| 540 | FROM $or_self->{config}{tables}{additional_value_table} | ||
| 541 | WHERE bench_additional_type_id = ? AND bench_additional_value ".(defined($a_vals[1]) ? '= ?' : 'IS NULL')." | ||
| 542 | ", @a_vals ); | ||
| 543 | |||
| 544 | } | ||
| 545 | |||
| 546 | sub select_addtyperelation { | ||
| 547 | |||
| 548 | my ( $or_self, @a_vals ) = @_; | ||
| 549 | |||
| 550 | return $or_self->execute_query( " | ||
| 551 | SELECT bench_id, bench_additional_type_id, created_at | ||
| 552 | FROM $or_self->{config}{tables}{additional_type_relation_table} | ||
| 553 | WHERE bench_id = ? AND bench_additional_type_id = ? | ||
| 554 | ", @a_vals ); | ||
| 555 | |||
| 556 | } | ||
| 557 | |||
| 558 | sub insert_raw_bench_bundle { | ||
| 559 | |||
| 560 | my ( $or_self, @a_vals ) = @_; | ||
| 561 | |||
| 562 | return $or_self->execute_query( " | ||
| 563 | INSERT INTO raw_bench_bundles | ||
| 564 | (raw_bench_bundle_serialized) | ||
| 565 | VALUES ( ? ) | ||
| 566 | ", @a_vals ); | ||
| 567 | |||
| 568 | } | ||
| 569 | |||
| 570 | sub copy_benchmark_backup_value { | ||
| 571 | |||
| 572 | my ( $or_self, $hr_vals ) = @_; | ||
| 573 | |||
| 574 | for my $s_param (qw/ new_bench_value_id old_bench_value_id /) { | ||
| 575 | if (! $hr_vals->{$s_param} ) { | ||
| 576 | require Carp; | ||
| 577 | Carp::confess("missing parameter '$s_param'"); | ||
| 578 | return; | ||
| 579 | } | ||
| 580 | } | ||
| 581 | |||
| 582 | return $or_self->execute_query( " | ||
| 583 | INSERT INTO $or_self->{config}{tables}{benchmark_backup_value_table} | ||
| 584 | ( bench_value_id, bench_id, bench_subsume_type_id, bench_value, active, created_at ) | ||
| 585 | SELECT | ||
| 586 | ?, bench_id, bench_subsume_type_id, bench_value, active, created_at | ||
| 587 | FROM | ||
| 588 | $or_self->{config}{tables}{benchmark_value_table} | ||
| 589 | WHERE | ||
| 590 | bench_value_id = ? | ||
| 591 | ", @{$hr_vals}{qw/ new_bench_value_id old_bench_value_id /} ); | ||
| 592 | |||
| 593 | } | ||
| 594 | |||
| 595 | sub copy_benchmark_backup_additional_relations { | ||
| 596 | |||
| 597 | my ( $or_self, $hr_vals ) = @_; | ||
| 598 | |||
| 599 | for my $s_param (qw/ new_bench_value_id old_bench_value_id /) { | ||
| 600 | if (! $hr_vals->{$s_param} ) { | ||
| 601 | require Carp; | ||
| 602 | Carp::confess("missing parameter '$s_param'"); | ||
| 603 | return; | ||
| 604 | } | ||
| 605 | } | ||
| 606 | |||
| 607 | return $or_self->execute_query( " | ||
| 608 | INSERT INTO $or_self->{config}{tables}{backup_additional_relation_table} | ||
| 609 | ( bench_backup_value_id, bench_additional_value_id, active, created_at ) | ||
| 610 | SELECT | ||
| 611 | ?, bench_additional_value_id, active, created_at | ||
| 612 | FROM | ||
| 613 | $or_self->{config}{tables}{additional_relation_table} | ||
| 614 | WHERE | ||
| 615 | bench_value_id = ? | ||
| 616 | ", @{$hr_vals}{qw/ new_bench_value_id old_bench_value_id /} ); | ||
| 617 | |||
| 618 | } | ||
| 619 | |||
| 620 | sub update_benchmark_backup_value { | ||
| 621 | |||
| 622 | my ( $or_self, $hr_vals ) = @_; | ||
| 623 | |||
| 624 | return $or_self->execute_query( " | ||
| 625 | UPDATE $or_self->{config}{tables}{benchmark_backup_value_table} | ||
| 626 | SET bench_value_id = ? | ||
| 627 | WHERE bench_value_id = ? | ||
| 628 | ", @{$hr_vals}{qw/ | ||
| 629 | new_bench_value_id | ||
| 630 | old_bench_value_id | ||
| 631 | /} ); | ||
| 632 | |||
| 633 | } | ||
| 634 | |||
| 635 | sub start_processing_raw_bench_bundle { | ||
| 636 | |||
| 637 | my ( $or_self, @a_vals ) = @_; | ||
| 638 | |||
| 639 | return $or_self->execute_query( " | ||
| 640 | UPDATE raw_bench_bundles | ||
| 641 | SET processing = 1 | ||
| 642 | WHERE raw_bench_bundle_id = ? | ||
| 643 | ", @a_vals ); | ||
| 644 | |||
| 645 | } | ||
| 646 | |||
| 647 | sub start_processing_raw_bench_bundle2 { | ||
| 648 | |||
| 649 | my ( $or_self, @a_vals ) = @_; | ||
| 650 | |||
| 651 | return $or_self->execute_query( " | ||
| 652 | UPDATE raw_bench_bundles | ||
| 653 | SET processing = 1 | ||
| 654 | WHERE raw_bench_bundle_id IN (".join(',', ('?') x @a_vals).") | ||
| 655 | ", @a_vals ); | ||
| 656 | |||
| 657 | } | ||
| 658 | |||
| 659 | sub update_raw_bench_bundle_set_processed { | ||
| 660 | |||
| 661 | my ( $or_self, @a_vals ) = @_; | ||
| 662 | |||
| 663 | return $or_self->execute_query( " | ||
| 664 | UPDATE raw_bench_bundles | ||
| 665 | SET processed=1, | ||
| 666 | processing=0 | ||
| 667 | WHERE processed=0 AND | ||
| 668 | processing=1 AND | ||
| 669 | raw_bench_bundle_id = ? | ||
| 670 | ", @a_vals ); | ||
| 671 | |||
| 672 | } | ||
| 673 | |||
| 674 | sub update_raw_bench_bundle_set_processed2 { | ||
| 675 | |||
| 676 | my ( $or_self, @a_vals ) = @_; | ||
| 677 | |||
| 678 | return $or_self->execute_query( " | ||
| 679 | UPDATE raw_bench_bundles | ||
| 680 | SET processed=1, | ||
| 681 | processing=0 | ||
| 682 | WHERE processed=0 AND | ||
| 683 | processing=1 AND | ||
| 684 | raw_bench_bundle_id IN (".join(',', ('?') x @a_vals).") | ||
| 685 | ", @a_vals ); | ||
| 686 | |||
| 687 | } | ||
| 688 | |||
| 689 | sub update_raw_bench_bundle_set_processed3 { | ||
| 690 | |||
| 691 | my ( $or_self, @a_vals ) = @_; | ||
| 692 | |||
| 693 | return $or_self->execute_query( " | ||
| 694 | UPDATE raw_bench_bundles | ||
| 695 | SET processed=1, | ||
| 696 | processing=0 | ||
| 697 | WHERE raw_bench_bundle_id IN (".join(',', ('?') x @a_vals).") | ||
| 698 | ", @a_vals ); | ||
| 699 | |||
| 700 | } | ||
| 701 | |||
| 702 | # sub unlock_raw_bench_bundle { | ||
| 703 | |||
| 704 | # my ( $or_self, @a_vals ) = @_; | ||
| 705 | |||
| 706 | # return $or_self->execute_query( " | ||
| 707 | # UPDATE raw_bench_bundles | ||
| 708 | # SET processing = 0 | ||
| 709 | # WHERE processed = 0 AND | ||
| 710 | # processing = 1 AND | ||
| 711 | # raw_bench_bundle_id = ? | ||
| 712 | # ", @a_vals ); | ||
| 713 | # } | ||
| 714 | |||
| 715 | sub delete_benchmark_additional_relations { | ||
| 716 | |||
| 717 | my ( $or_self, @a_vals ) = @_; | ||
| 718 | |||
| 719 | return $or_self->execute_query( " | ||
| 720 | DELETE FROM $or_self->{config}{tables}{additional_relation_table} | ||
| 721 | WHERE bench_value_id = ? | ||
| 722 | ", @a_vals ); | ||
| 723 | |||
| 724 | } | ||
| 725 | |||
| 726 | sub delete_benchmark_value { | ||
| 727 | |||
| 728 | my ( $or_self, @a_vals ) = @_; | ||
| 729 | |||
| 730 | return $or_self->execute_query( " | ||
| 731 | DELETE FROM $or_self->{config}{tables}{benchmark_value_table} | ||
| 732 | WHERE bench_value_id = ? | ||
| 733 | ", @a_vals ); | ||
| 734 | |||
| 735 | } | ||
| 736 | |||
| 737 | # Garbage Collection | ||
| 738 | sub delete_processed_raw_bench_bundles { | ||
| 739 | |||
| 740 | my ( $or_self, @a_vals ) = @_; | ||
| 741 | |||
| 742 | return $or_self->execute_query( " | ||
| 743 | DELETE FROM raw_bench_bundles | ||
| 744 | WHERE processed=1 AND | ||
| 745 | processing=0 | ||
| 746 | ", @a_vals ); | ||
| 747 | |||
| 748 | } | ||
| 749 | |||
| 750 | sub copy_additional_values { | ||
| 751 | |||
| 752 | my ( $or_self, $hr_vals ) = @_; | ||
| 753 | |||
| 754 | for my $s_param (qw/ new_bench_value_id old_bench_value_id /) { | ||
| 755 | if (! $hr_vals->{$s_param} ) { | ||
| 756 | require Carp; | ||
| 757 | Carp::confess("missing parameter '$s_param'"); | ||
| 758 | return; | ||
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | return $or_self->execute_query( " | ||
| 763 | INSERT INTO $or_self->{config}{tables}{additional_relation_table} | ||
| 764 | ( bench_value_id, bench_additional_value_id, active, created_at ) | ||
| 765 | SELECT | ||
| 766 | ?, bench_additional_value_id, 1, @{[$or_self->_NOW]} | ||
| 767 | FROM | ||
| 768 | $or_self->{config}{tables}{additional_relation_table} | ||
| 769 | WHERE | ||
| 770 | bench_value_id = ? | ||
| 771 | ", @{$hr_vals}{qw/ new_bench_value_id old_bench_value_id /} ); | ||
| 772 | |||
| 773 | } | ||
| 774 | |||
| 775 | sub select_raw_bench_bundle_for_lock { | ||
| 776 | |||
| 777 | my ( $or_self, @a_vals ) = @_; | ||
| 778 | |||
| 779 | return $or_self->execute_query( " | ||
| 780 | SELECT raw_bench_bundle_id | ||
| 781 | FROM raw_bench_bundles | ||
| 782 | WHERE processed=0 AND | ||
| 783 | processing=0 | ||
| 784 | ORDER BY raw_bench_bundle_id ASC | ||
| 785 | LIMIT 1 | ||
| 786 | @{[$or_self->_FOR_UPDATE]} | ||
| 787 | ", @a_vals ); | ||
| 788 | } | ||
| 789 | |||
| 790 | sub select_raw_bench_bundle_for_lock2 { | ||
| 791 | |||
| 792 | my ( $or_self, $count, @a_vals ) = @_; | ||
| 793 | |||
| 794 | return $or_self->execute_query( " | ||
| 795 | SELECT raw_bench_bundle_id | ||
| 796 | FROM raw_bench_bundles | ||
| 797 | WHERE processed=0 AND | ||
| 798 | processing=0 | ||
| 799 | ORDER BY raw_bench_bundle_id ASC | ||
| 800 | LIMIT ? | ||
| 801 | @{[$or_self->_FOR_UPDATE]} | ||
| 802 | ", $count, @a_vals ); | ||
| 803 | } | ||
| 804 | |||
| 805 | sub select_raw_bench_bundle_for_processing { | ||
| 806 | |||
| 807 | my ( $or_self, @a_vals ) = @_; | ||
| 808 | |||
| 809 | return $or_self->execute_query( " | ||
| 810 | SELECT raw_bench_bundle_serialized | ||
| 811 | FROM raw_bench_bundles | ||
| 812 | WHERE raw_bench_bundle_id = ? | ||
| 813 | LIMIT 1 | ||
| 814 | @{[$or_self->_FOR_UPDATE]} | ||
| 815 | ", @a_vals ); | ||
| 816 | } | ||
| 817 | |||
| 818 | sub select_raw_bench_bundle_for_processing2 { | ||
| 819 | |||
| 820 | my ( $or_self, @a_vals ) = @_; | ||
| 821 | |||
| 822 | my $q = " | ||
| 823 | SELECT raw_bench_bundle_serialized | ||
| 824 | FROM raw_bench_bundles | ||
| 825 | WHERE raw_bench_bundle_id IN (".join(',', ('?') x @a_vals).") | ||
| 826 | ORDER BY raw_bench_bundle_id ASC | ||
| 827 | @{[$or_self->_FOR_UPDATE]} | ||
| 828 | "; | ||
| 829 | #print STDERR "q: $q\n"; | ||
| 830 | return $or_self->execute_query($q, @a_vals ); | ||
| 831 | } | ||
| 832 | |||
| 833 | 1; | ||
| 834 | |||
| 835 | __END__ |