AnyEvent-MySQL
view release on metacpan or search on metacpan
lib/AnyEvent/MySQL.pm view on Meta::CPAN
$sth->execute(1, sub {
warn "executed! $_[0]";
$end3->send($_[0]);
});
my $fth = $end3->recv;
my $end4 = AE::cv;
$fth->bind_col(2, \my $a, sub {
warn $_[0];
});
my $fetch; $fetch = sub {
$fth->fetch(sub {
if( $_[0] ) {
warn "Get! $a";
$fetch->();
}
else {
warn "Get End!";
undef $fetch;
$end4->send;
}
});
}; $fetch->();
#$fth->bind_columns(\my($a, $b), sub {
# warn $_[0];
# warn $AnyEvent::MySQL::errstr;
#});
#my $fetch; $fetch = sub {
# $fth->fetch(sub {
# if( $_[0] ) {
# warn "Get! ($a, $b)";
# $fetch->();
# }
# else {
lib/AnyEvent/MySQL.pm view on Meta::CPAN
Non-blocking get the value immediately
=cut
sub last_insert_id {
$_[0]{mysql_insertid};
}
sub _do {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my($rev_dir, $dbh, $statement, $attr, @bind_values) = @_;
if( $dbh->{_}[ATTRi]{ReadOnly} && $statement !~ /^\s*(?:show|select|set\s+names)\s+/i ){
_report_error($dbh, 'do', 1227, 'unable to perform write queries on a ReadOnly handle');
$cb->();
return;
}
my @args = ($dbh, [TXN_TASK, sub {
my $next_act = shift;
AnyEvent::MySQL::Imp::send_packet($dbh->{_}[HDi], 0, AnyEvent::MySQL::Imp::COM_QUERY, _text_prepare($statement, @bind_values));
AnyEvent::MySQL::Imp::recv_response($dbh->{_}[HDi], sub {
eval {
if( $_[0]==AnyEvent::MySQL::Imp::RES_OK ) {
$dbh->{mysql_insertid} = $_[2];
$cb->($_[1]);
}
elsif( $_[0]==AnyEvent::MySQL::Imp::RES_ERROR ) {
_report_error($dbh, 'do', $_[1], $_[3]);
$cb->();
}
lib/AnyEvent/MySQL.pm view on Meta::CPAN
}, $cb, 0]);
if( $rev_dir ) {
_unshift_task(@args);
}
else {
_push_task(@args);
}
}
=head2 $dbh->do($statement, [\%attr, [@bind_values,]] [$cb->($rv)])
=cut
sub do {
unshift @_, 0;
&_do;
}
=head2 $dbh->pre_do($statement, [\%attr, [@bind_values,]] [$cb->($rv)])
This method is like $dbh->do except that $dbh->pre_do will unshift
job into the queue instead of push.
This method is for the initializing actions in the AnyEvent::MySQL->connect's callback
=cut
sub pre_do {
unshift @_, 1;
&_do;
}
=head2 $dbh->selectall_arrayref($statement, [\%attr, [@bind_values,]] $cb->($ary_ref))
=cut
sub selectall_arrayref {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my($dbh, $statement, $attr, @bind_values) = @_;
_push_task($dbh, [TXN_TASK, sub {
my $next_act = shift;
AnyEvent::MySQL::Imp::send_packet($dbh->{_}[HDi], 0, AnyEvent::MySQL::Imp::COM_QUERY, _text_prepare($statement, @bind_values));
AnyEvent::MySQL::Imp::recv_response($dbh->{_}[HDi], sub {
eval {
if( $_[0]==AnyEvent::MySQL::Imp::RES_OK ) {
$dbh->{mysql_insertid} = $_[2];
$cb->([]);
}
elsif( $_[0]==AnyEvent::MySQL::Imp::RES_ERROR ) {
_report_error($dbh, 'selectall_arrayref', $_[1], $_[3]);
$cb->();
}
else {
$cb->($_[2]);
}
};
$next_act->();
});
}, $cb, 0]);
}
=head2 $dbh->selectall_hashref($statement, [$key_field|\@key_field], [\%attr, [@bind_values,]] $cb->($hash_ref))
=cut
sub selectall_hashref {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my($dbh, $statement, $key_field) = splice @_, 0, 3;
my @key_field;
if( ref($key_field) eq 'ARRAY' ) {
@key_field = @$key_field;
lib/AnyEvent/MySQL.pm view on Meta::CPAN
unshift @_, $key_field;
@key_field = ();
}
elsif( defined($key_field) ) {
@key_field = ($key_field);
}
else {
@key_field = ();
}
my($attr, @bind_values) = @_;
_push_task($dbh, [TXN_TASK, sub {
my $next_act = shift;
AnyEvent::MySQL::Imp::send_packet($dbh->{_}[HDi], 0, AnyEvent::MySQL::Imp::COM_QUERY, _text_prepare($statement, @bind_values));
AnyEvent::MySQL::Imp::recv_response($dbh->{_}[HDi], sub {
eval {
if( $_[0]==AnyEvent::MySQL::Imp::RES_OK ) {
$dbh->{mysql_insertid} = $_[2];
if( @key_field ) {
$cb->({});
}
else {
$cb->([]);
}
lib/AnyEvent/MySQL.pm view on Meta::CPAN
}
}
$cb->($res);
}
};
$next_act->();
});
}, $cb, 0]);
}
=head2 $dbh->selectcol_arrayref($statement, [\%attr, [@bind_values,]] $cb->($ary_ref))
=cut
sub selectcol_arrayref {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my($dbh, $statement, $attr, @bind_values) = @_;
$attr ||= {};
my @columns = map { $_-1 } @{ $attr->{Columns} || [1] };
_push_task($dbh, [TXN_TASK, sub {
my $next_act = shift;
AnyEvent::MySQL::Imp::send_packet($dbh->{_}[HDi], 0, AnyEvent::MySQL::Imp::COM_QUERY, _text_prepare($statement, @bind_values));
AnyEvent::MySQL::Imp::recv_response($dbh->{_}[HDi], sub {
eval {
if( $_[0]==AnyEvent::MySQL::Imp::RES_OK ) {
$dbh->{mysql_insertid} = $_[2];
$cb->([]);
}
elsif( $_[0]==AnyEvent::MySQL::Imp::RES_ERROR ) {
_report_error($dbh, 'selectcol_arrayref', $_[1], $_[3]);
$cb->();
}
lib/AnyEvent/MySQL.pm view on Meta::CPAN
map { $r->[$_] } @columns
} @{$_[2]};
$cb->(\@res);
}
};
$next_act->();
});
}, $cb, 0]);
}
=head2 $dbh->selectrow_array($statement, [\%attr, [@bind_values,]], $cb->(@row_ary))
=cut
sub selectrow_array {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my($dbh, $statement, $attr, @bind_values) = @_;
_push_task($dbh, [TXN_TASK, sub {
my $next_act = shift;
AnyEvent::MySQL::Imp::send_packet($dbh->{_}[HDi], 0, AnyEvent::MySQL::Imp::COM_QUERY, _text_prepare($statement, @bind_values));
AnyEvent::MySQL::Imp::recv_response($dbh->{_}[HDi], sub {
eval {
if( $_[0]==AnyEvent::MySQL::Imp::RES_OK ) {
$dbh->{mysql_insertid} = $_[2];
$cb->();
}
elsif( $_[0]==AnyEvent::MySQL::Imp::RES_ERROR ) {
_report_error($dbh, 'selectrow_array', $_[1], $_[3]);
$cb->();
}
else {
$cb->($_[2][0] ? @{$_[2][0]} : ());
}
};
$next_act->();
});
}, $cb, 0]);
}
=head2 $dbh->selectrow_arrayref($statement, [\%attr, [@bind_values,]], $cb->($ary_ref))
=cut
sub selectrow_arrayref {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my($dbh, $statement, $attr, @bind_values) = @_;
_push_task($dbh, [TXN_TASK, sub {
my $next_act = shift;
AnyEvent::MySQL::Imp::send_packet($dbh->{_}[HDi], 0, AnyEvent::MySQL::Imp::COM_QUERY, _text_prepare($statement, @bind_values));
AnyEvent::MySQL::Imp::recv_response($dbh->{_}[HDi], sub {
eval {
if( $_[0]==AnyEvent::MySQL::Imp::RES_OK ) {
$dbh->{mysql_insertid} = $_[2];
$cb->(undef);
}
elsif( $_[0]==AnyEvent::MySQL::Imp::RES_ERROR ) {
_report_error($dbh, 'selectrow_arrayref', $_[1], $_[3]);
$cb->(undef);
}
else {
$cb->($_[2][0]);
}
};
$next_act->();
});
}, $cb, 0]);
}
=head2 $dbh->selectrow_hashref($statement, [\%attr, [@bind_values,]], $cb->($hash_ref))
=cut
sub selectrow_hashref {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my($dbh, $statement, $attr, @bind_values) = @_;
_push_task($dbh, [TXN_TASK, sub {
my $next_act = shift;
AnyEvent::MySQL::Imp::send_packet($dbh->{_}[HDi], 0, AnyEvent::MySQL::Imp::COM_QUERY, _text_prepare($statement, @bind_values));
AnyEvent::MySQL::Imp::recv_response($dbh->{_}[HDi], sub {
eval {
if( $_[0]==AnyEvent::MySQL::Imp::RES_OK ) {
$dbh->{mysql_insertid} = $_[2];
$cb->(undef);
}
elsif( $_[0]==AnyEvent::MySQL::Imp::RES_ERROR ) {
_report_error($dbh, 'selectrow_hashref', $_[1], $_[3]);
$cb->(undef);
}
lib/AnyEvent/MySQL.pm view on Meta::CPAN
sub new {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my($class, $dbh, $statement) = @_;
my $sth = bless [], $class;
$sth->[DBHi] = $dbh;
$sth->[STATEMENTi] = $statement;
return $sth;
}
=head2 $sth->execute(@bind_values, [\%attr,] [$cb->($fth/$rv)])
=cut
sub execute {
my $cb = ref($_[-1]) eq 'CODE' ? pop : \&AnyEvent::MySQL::_empty_cb;
my $attr = ref($_[-1]) eq 'HASH' ? pop : {};
my($sth, @bind_values) = @_;
my $dbh = $sth->[DBHi];
AnyEvent::MySQL::db::_push_task($dbh, [AnyEvent::MySQL::db::TXN_TASK, sub {
my $next_act = shift;
my $execute = sub {
AnyEvent::MySQL::Imp::do_execute_param($dbh->{_}[AnyEvent::MySQL::db::HDi], $sth->[IDi], \@bind_values, $sth->[PARAMi]);
AnyEvent::MySQL::Imp::recv_response($dbh->{_}[AnyEvent::MySQL::db::HDi], execute => 1, sub {
eval {
if( $_[0]==AnyEvent::MySQL::Imp::RES_OK ) {
$cb->($_[1]);
}
elsif( $_[0]==AnyEvent::MySQL::Imp::RES_RESULT ) {
$cb->(AnyEvent::MySQL::ft->new($sth->[FIELDi], $_[2]));
}
elsif( $_[0]==AnyEvent::MySQL::Imp::RES_ERROR ) {
AnyEvent::MySQL::db::_report_error($dbh, 'execute', $_[1], $_[3]);
lib/AnyEvent/MySQL.pm view on Meta::CPAN
sub new {
my($class, $field_set, $data_set) = @_;
my $fth = bless [], $class;
$fth->[FIELDi] = $field_set;
$fth->[DATAi] = $data_set;
return $fth;
}
=head2 $rc = $fth->bind_columns(@list_of_refs_to_vars_to_bind, [$cb->($rc)])
=cut
sub bind_columns {
my $cb = ref($_[-1]) eq 'CODE' ? pop : undef;
my $fth = shift;
my @list_of_refs_to_vars_to_bind = @_;
if( !@{$fth->[DATAi]} ) {
$cb->(1) if $cb;
return 1;
}
elsif( @{$fth->[DATAi][0]} == @list_of_refs_to_vars_to_bind ) {
$fth->[BINDi] = \@list_of_refs_to_vars_to_bind;
$cb->(1) if $cb;
return 1;
}
else {
$cb->() if $cb;
return;
}
}
=head2 $rc = $fth->bind_col($col_num, \$col_variable, [$cb->($rc)])
=cut
sub bind_col {
my $cb = ref($_[-1]) eq 'CODE' ? pop : undef;
my($fth, $col_num, $col_ref) = @_;
if( !@{$fth->[DATAi]} ) {
$cb->(1) if $cb;
return 1;
}
elsif( 0<=$col_num && $col_num<=$#{$fth->[DATAi][0]} ) {
$fth->[BINDi][$col_num] = $col_ref;
$cb->(1) if $cb;
lib/AnyEvent/MySQL.pm view on Meta::CPAN
}
=head2 $rv = $fth->fetch([$cb->($rv)])
=cut
sub fetch {
my $cb = ref($_[-1]) eq 'CODE' ? pop : undef;
my $fth = shift;
if( $fth->[BINDi] && $fth->[DATAi] && @{$fth->[DATAi]} ) {
my $bind = $fth->[BINDi];
my $row = shift @{$fth->[DATAi]};
for(my $i=0; $i<@$row; ++$i) {
${$bind->[$i]} = $row->[$i] if $bind->[$i];
}
$cb->(1) if $cb;
return 1;
}
else {
$cb->() if $cb;
return;
}
}
( run in 0.827 second using v1.01-cache-2.11-cpan-2398b32b56e )