| 1 | # -*-cperl-*- | ||
| 2 | # | ||
| 3 | # DBD::Patroni::st - Statement class for DBD::Patroni | ||
| 4 | # | ||
| 5 | # Copyright (c) 2025 Xavier Guimard | ||
| 6 | # | ||
| 7 | # You may distribute under the terms of either the GNU General Public | ||
| 8 | # License or the Artistic License, as specified in the Perl README file. | ||
| 9 | |||
| 10 | package DBD::Patroni::st; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | |||
| 15 | use DBD::Patroni; | ||
| 16 | |||
| 17 | our $imp_data_size = 0; | ||
| 18 | |||
| 19 | sub execute { | ||
| 20 | my ( $sth, @bind ) = @_; | ||
| 21 | my $dbh = $sth->{Database}; | ||
| 22 | my $target = $sth->{patroni_target}; | ||
| 23 | |||
| 24 | return DBD::Patroni::_with_retry( | ||
| 25 | $dbh, | ||
| 26 | $target, | ||
| 27 | sub { | ||
| 28 | my $real_sth = $sth->{patroni_real_sth}; | ||
| 29 | |||
| 30 | # Check if statement handle is still valid | ||
| 31 | unless ( $real_sth | ||
| 32 | && $real_sth->{Database} | ||
| 33 | && $real_sth->{Database}{Active} ) | ||
| 34 | { | ||
| 35 | # Re-prepare statement after reconnection | ||
| 36 | my $handle = | ||
| 37 | $target eq 'replica' | ||
| 38 | ? $dbh->{patroni_replica_dbh} | ||
| 39 | : $dbh->{patroni_leader_dbh}; | ||
| 40 | $sth->{patroni_real_sth} = | ||
| 41 | $handle->prepare( $sth->{patroni_statement} ); | ||
| 42 | $real_sth = $sth->{patroni_real_sth}; | ||
| 43 | } | ||
| 44 | my $rv = $real_sth->execute(@bind); | ||
| 45 | |||
| 46 | # Check for connection errors that need retry | ||
| 47 | unless ( defined $rv ) { | ||
| 48 | my $err = $real_sth->errstr; | ||
| 49 | if ( $err && DBD::Patroni::_is_connection_error($err) ) { | ||
| 50 | die $err; # Will trigger retry | ||
| 51 | } | ||
| 52 | |||
| 53 | # Propagate error to our dbh | ||
| 54 | $dbh->set_err( $real_sth->err, $err, $real_sth->state ); | ||
| 55 | } | ||
| 56 | |||
| 57 | # Copy NUM_OF_FIELDS to our sth for DBI | ||
| 58 | if ( $real_sth->{NUM_OF_FIELDS} ) { | ||
| 59 | $sth->STORE( 'NUM_OF_FIELDS', $real_sth->{NUM_OF_FIELDS} ); | ||
| 60 | } | ||
| 61 | return $rv; | ||
| 62 | } | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub fetch { | ||
| 67 | my $sth = shift; | ||
| 68 | return unless $sth->{patroni_real_sth}; | ||
| 69 | return $sth->{patroni_real_sth}->fetch; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub fetchrow_array { | ||
| 73 | my $sth = shift; | ||
| 74 | return unless $sth->{patroni_real_sth}; | ||
| 75 | return $sth->{patroni_real_sth}->fetchrow_array; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub fetchrow_arrayref { | ||
| 79 | my $sth = shift; | ||
| 80 | return unless $sth->{patroni_real_sth}; | ||
| 81 | return $sth->{patroni_real_sth}->fetchrow_arrayref; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub fetchrow_hashref { | ||
| 85 | my $sth = shift; | ||
| 86 | return unless $sth->{patroni_real_sth}; | ||
| 87 | return $sth->{patroni_real_sth}->fetchrow_hashref(@_); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub fetchall_arrayref { | ||
| 91 | my $sth = shift; | ||
| 92 | return unless $sth->{patroni_real_sth}; | ||
| 93 | return $sth->{patroni_real_sth}->fetchall_arrayref(@_); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub fetchall_hashref { | ||
| 97 | my $sth = shift; | ||
| 98 | return unless $sth->{patroni_real_sth}; | ||
| 99 | return $sth->{patroni_real_sth}->fetchall_hashref(@_); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub finish { | ||
| 103 | my $sth = shift; | ||
| 104 | return $sth->{patroni_real_sth}->finish if $sth->{patroni_real_sth}; | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub rows { | ||
| 109 | my $sth = shift; | ||
| 110 | return $sth->{patroni_real_sth}->rows if $sth->{patroni_real_sth}; | ||
| 111 | return -1; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub bind_param { | ||
| 115 | my $sth = shift; | ||
| 116 | return unless $sth->{patroni_real_sth}; | ||
| 117 | return $sth->{patroni_real_sth}->bind_param(@_); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub bind_param_inout { | ||
| 121 | my $sth = shift; | ||
| 122 | return unless $sth->{patroni_real_sth}; | ||
| 123 | return $sth->{patroni_real_sth}->bind_param_inout(@_); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub bind_col { | ||
| 127 | my $sth = shift; | ||
| 128 | return unless $sth->{patroni_real_sth}; | ||
| 129 | return $sth->{patroni_real_sth}->bind_col(@_); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub bind_columns { | ||
| 133 | my $sth = shift; | ||
| 134 | return unless $sth->{patroni_real_sth}; | ||
| 135 | return $sth->{patroni_real_sth}->bind_columns(@_); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub STORE { | ||
| 139 | my ( $sth, $attr, $val ) = @_; | ||
| 140 | |||
| 141 | if ( $attr =~ /^patroni_/ ) { | ||
| 142 | $sth->{$attr} = $val; | ||
| 143 | return 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | return $sth->SUPER::STORE( $attr, $val ); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub FETCH { | ||
| 150 | my ( $sth, $attr ) = @_; | ||
| 151 | |||
| 152 | if ( $attr =~ /^patroni_/ ) { | ||
| 153 | return $sth->{$attr}; | ||
| 154 | } | ||
| 155 | |||
| 156 | # Delegate to real sth for common attributes | ||
| 157 | if ( $sth->{patroni_real_sth} ) { | ||
| 158 | my $real_sth = $sth->{patroni_real_sth}; | ||
| 159 | if ( $attr eq 'NAME' || $attr eq 'NAME_lc' || $attr eq 'NAME_uc' ) { | ||
| 160 | return $real_sth->{$attr}; | ||
| 161 | } | ||
| 162 | if ( $attr eq 'TYPE' || $attr eq 'PRECISION' || $attr eq 'SCALE' ) { | ||
| 163 | return $real_sth->{$attr}; | ||
| 164 | } | ||
| 165 | if ( $attr eq 'NULLABLE' || $attr eq 'NUM_OF_FIELDS' ) { | ||
| 166 | return $real_sth->{$attr}; | ||
| 167 | } | ||
| 168 | if ( $attr eq 'NUM_OF_PARAMS' ) { | ||
| 169 | return $real_sth->{$attr}; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | return $sth->SUPER::FETCH($attr); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub DESTROY { | ||
| 177 | my $sth = shift; | ||
| 178 | $sth->{patroni_real_sth}->finish if $sth->{patroni_real_sth}; | ||
| 179 | } | ||
| 180 | |||
| 181 | 1; |