| 1 | package Mojo::Pg::Che::Database; | ||
| 2 | |||
| 3 | #~ use Mojo::Base 'Mojo::EventEmitter'; | ||
| 4 | use Mojo::Base 'Mojo::Pg::Database'; | ||
| 5 | use Carp qw(croak shortmess); | ||
| 6 | use DBD::Pg ':async'; | ||
| 7 | |||
| 8 | use Mojo::Pg::Che::Results; | ||
| 9 | use Mojo::Pg::Transaction; | ||
| 10 | |||
| 11 | |||
| 12 | my $handler_err = sub {$_[0] = shortmess $_[0]; 0;}; | ||
| 13 | has handler_err => sub {$handler_err}; | ||
| 14 | has results_class => 'Mojo::Pg::Che::Results'; | ||
| 15 | has debug => 0; | ||
| 16 | |||
| 17 | my $PKG = __PACKAGE__; | ||
| 18 | |||
| 19 | sub query { shift->select(@_) } | ||
| 20 | |||
| 21 | sub execute_sth { | ||
| 22 | my ($self, $sth,) = map shift, 1..2; | ||
| 23 | |||
| 24 | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||
| 25 | |||
| 26 | #~ croak 'Previous async query has not finished' | ||
| 27 | #~ if $self->dbh->{pg_async_status} == 1; | ||
| 28 | |||
| 29 | croak 'Non-blocking query already in progress' | ||
| 30 | if $self->{waiting}; | ||
| 31 | |||
| 32 | local $sth->{HandleError} = $self->handler_err; | ||
| 33 | |||
| 34 | eval {$sth->execute(@_)}#binds | ||
| 35 | or die "Bad statement: ", $@;#, $sth->{Statement}; | ||
| 36 | |||
| 37 | # Blocking | ||
| 38 | unless ($cb) {# | ||
| 39 | $self->_notifications; | ||
| 40 | return $self->results_class->new(db => $self, sth => $sth); | ||
| 41 | } | ||
| 42 | |||
| 43 | # Non-blocking | ||
| 44 | $self->{waiting} = {cb => $cb, sth => $sth}; | ||
| 45 | $self->_watch; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub execute_string { | ||
| 49 | my ($self, $query, $attrs,) = map shift, 1..3; | ||
| 50 | |||
| 51 | my $dbh = $self->dbh; | ||
| 52 | |||
| 53 | my $sth = $self->prepare($query, $attrs,); | ||
| 54 | |||
| 55 | return $self->execute_sth($sth, @_); | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | sub prepare { | ||
| 60 | my ($self, $query, $attrs,) = @_; | ||
| 61 | |||
| 62 | my $dbh = $self->dbh; | ||
| 63 | |||
| 64 | my $sth = delete $attrs->{Cached} | ||
| 65 | ? $dbh->prepare_cached($query, $attrs, 3) | ||
| 66 | : $dbh->prepare($query, $attrs); | ||
| 67 | |||
| 68 | return $sth; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub prepare_cached { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | my $sth = $self->dbh->prepare_cached(@_); | ||
| 75 | |||
| 76 | return $sth; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub tx { shift->begin } | ||
| 80 | sub begin { | ||
| 81 | my $self = shift; | ||
| 82 | return $self->{tx} | ||
| 83 | if $self->{tx}; | ||
| 84 | |||
| 85 | my $tx = $self->{tx} = Mojo::Pg::Transaction->new(db => $self); | ||
| 86 | return $tx; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub commit { | ||
| 90 | my $self = shift; | ||
| 91 | my $tx = delete $self->{tx} | ||
| 92 | or return; | ||
| 93 | $tx->commit; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub rollback { | ||
| 97 | my $self = shift; | ||
| 98 | delete $self->{tx};# DESTROY | ||
| 99 | } | ||
| 100 | |||
| 101 | my @DBH_METHODS = qw( | ||
| 102 | select | ||
| 103 | selectrow_array | ||
| 104 | selectrow_arrayref | ||
| 105 | selectrow_hashref | ||
| 106 | selectall_arrayref | ||
| 107 | selectall_array | ||
| 108 | selectall_hashref | ||
| 109 | selectcol_arrayref | ||
| 110 | do | ||
| 111 | ); | ||
| 112 | |||
| 113 | for my $method (@DBH_METHODS) { | ||
| 114 | no strict 'refs'; | ||
| 115 | no warnings 'redefine'; | ||
| 116 | *{"${PKG}::$method"} = sub { shift->_DBH_METHOD($method, @_) }; | ||
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | sub _DBH_METHOD { | ||
| 121 | my ($self, $method) = (shift, shift); | ||
| 122 | my ($sth, $query) = ref $_[0] ? (shift, undef) : (undef, shift); | ||
| 123 | |||
| 124 | my @to_fetch = (); | ||
| 125 | |||
| 126 | push @to_fetch, shift # $key_field | ||
| 127 | if $method eq 'selectall_hashref' && ! ref $_[0]; | ||
| 128 | |||
| 129 | my $attrs = shift || {}; | ||
| 130 | |||
| 131 | $to_fetch[0] = delete $attrs->{KeyField} | ||
| 132 | if exists $attrs->{KeyField}; | ||
| 133 | |||
| 134 | for (qw(Slice MaxRows)) { | ||
| 135 | push @to_fetch, delete $attrs->{$_} | ||
| 136 | if exists $attrs->{$_}; | ||
| 137 | } | ||
| 138 | $to_fetch[0] = delete $attrs->{Columns} | ||
| 139 | if exists $attrs->{Columns}; | ||
| 140 | |||
| 141 | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||
| 142 | |||
| 143 | $attrs->{pg_async} = PG_ASYNC | ||
| 144 | if $cb;## || delete $attrs->{Async}; | ||
| 145 | |||
| 146 | $sth->{pg_async} = PG_ASYNC | ||
| 147 | if $sth && $attrs->{pg_async}; | ||
| 148 | |||
| 149 | $sth ||= $self->prepare($query, $attrs); | ||
| 150 | |||
| 151 | my @bind = @_; | ||
| 152 | |||
| 153 | my @result = $self->execute_sth($sth, @bind, $cb ? ($cb) : ());# | ||
| 154 | |||
| 155 | (my $fetch_method = $method) =~ s/select/fetch/; | ||
| 156 | |||
| 157 | return $result[0]->$fetch_method(@to_fetch) | ||
| 158 | if ref $result[0] eq $self->results_class && $result[0]->can($fetch_method); | ||
| 159 | |||
| 160 | return wantarray ? @result : shift @result; | ||
| 161 | |||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | sub DESTROY {# copy/paste Mojo::Pg::Database + rollback | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | $self->rollback; | ||
| 169 | |||
| 170 | my $waiting = $self->{waiting}; | ||
| 171 | $waiting->{cb}($self, 'Premature connection close', undef) if $waiting->{cb}; | ||
| 172 | |||
| 173 | return unless (my $pg = $self->pg) && (my $dbh = $self->dbh); | ||
| 174 | $pg->_enqueue($dbh) | ||
| 175 | #~ and ($self->debug && say STDERR "DESTROY $dbh") | ||
| 176 | unless $dbh->{private_mojo_no_reuse}; | ||
| 177 | |||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | 1; | ||
| 182 | |||
| 183 | __END__ |