| 1 | package BenchmarkAnything::Storage::Backend::SQL::Query; | ||
| 2 | our $AUTHORITY = 'cpan:TAPPER'; | ||
| 3 | # ABSTRACT: BenchmarkAnything::Storage::Backend::SQL - querying - base class | ||
| 4 | $BenchmarkAnything::Storage::Backend::SQL::Query::VERSION = '0.029'; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Digest::MD5; | ||
| 8 | #use Try::Tiny; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | |||
| 12 | my ( $s_self, $hr_atts ) = @_; | ||
| 13 | |||
| 14 | my $or_self = bless {}, $s_self; | ||
| 15 | |||
| 16 | for my $s_key (qw/ config dbh /) { | ||
| 17 | if (! $hr_atts->{$s_key} ) { | ||
| 18 | require Carp; | ||
| 19 | Carp::confess("missing parameter '$s_key'"); | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | $or_self->{dbh} = $hr_atts->{dbh}; | ||
| 24 | $or_self->{debug} = $hr_atts->{debug} || 0; | ||
| 25 | $or_self->{config} = $hr_atts->{config}; | ||
| 26 | |||
| 27 | return $or_self; | ||
| 28 | |||
| 29 | } | ||
| 30 | |||
| 31 | sub execute_query { | ||
| 32 | |||
| 33 | my ( $or_self, $s_statement, @a_vals ) = @_; | ||
| 34 | |||
| 35 | if ( $or_self->{debug} ) { | ||
| 36 | warn $s_statement . ' (' . (join ',', @a_vals) . ')'; | ||
| 37 | } | ||
| 38 | |||
| 39 | local $or_self->{dbh}{RaiseError} = 1; | ||
| 40 | local $or_self->{dbh}{PrintError} = 0; | ||
| 41 | |||
| 42 | my $s_key = Digest::MD5::md5($s_statement); | ||
| 43 | if ( $or_self->{prepared}{$s_key} ) { | ||
| 44 | $or_self->{prepared}{$s_key}->finish(); | ||
| 45 | } | ||
| 46 | $or_self->{prepared}{$s_key} = $or_self->{dbh}->prepare( $s_statement ); | ||
| 47 | $or_self->{prepared}{$s_key}->execute( @a_vals ); | ||
| 48 | |||
| 49 | # try/catch - this should better be handled in upper levels | ||
| 50 | # try { | ||
| 51 | # $or_self->{prepared}{$s_key}->execute( @a_vals ); | ||
| 52 | # } | ||
| 53 | # catch { | ||
| 54 | # warn("CATCH"); | ||
| 55 | # if (/Deadlock found when trying to get lock/) { | ||
| 56 | # warn("IGNORED DEADLOCK"); | ||
| 57 | # } else { | ||
| 58 | # Carp::cluck("INNER UNKNOWN {{{\n$_\n}}}\n"); | ||
| 59 | # die $_; | ||
| 60 | # } | ||
| 61 | # }; | ||
| 62 | |||
| 63 | return $or_self->{prepared}{$s_key}; | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | sub last_insert_id { | ||
| 68 | |||
| 69 | my ( $or_self, $s_table, $s_column ) = @_; | ||
| 70 | |||
| 71 | return $or_self->{dbh}->last_insert_id( | ||
| 72 | undef, undef, $s_table, $s_column, | ||
| 73 | ); | ||
| 74 | |||
| 75 | } | ||
| 76 | |||
| 77 | sub start_transaction { | ||
| 78 | |||
| 79 | my ( $or_self ) = @_; | ||
| 80 | |||
| 81 | local $or_self->{dbh}{RaiseError} = 1; | ||
| 82 | |||
| 83 | eval { | ||
| 84 | $or_self->{old_AutoCommit} = $or_self->{dbh}{AutoCommit}; | ||
| 85 | $or_self->{dbh}{AutoCommit} = 0; | ||
| 86 | }; | ||
| 87 | if ( $@ ) { | ||
| 88 | if ( $or_self->{debug} ) { | ||
| 89 | require Carp; | ||
| 90 | Carp::cluck('Transactions not supported by your database'); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $or_self->{transaction_supported} = 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | return 1; | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | sub finish_transaction { | ||
| 102 | |||
| 103 | my ( $or_self, $s_error, $hr_opts ) = @_; | ||
| 104 | |||
| 105 | if ( $or_self->{transaction_supported} ) { | ||
| 106 | |||
| 107 | local $or_self->{dbh}{RaiseError} = 1; | ||
| 108 | |||
| 109 | if ( $s_error ) { | ||
| 110 | require Carp; | ||
| 111 | Carp::cluck("transaction failed: $s_error") unless $hr_opts->{silent}; | ||
| 112 | $or_self->{dbh}->rollback(); | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | $or_self->{dbh}->commit(); | ||
| 116 | } | ||
| 117 | |||
| 118 | $or_self->{dbh}{AutoCommit} = $or_self->{old_AutoCommit}; | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | return 1; | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | __END__ |