Skip to content

Commit

Permalink
Made getting last_insert_id more abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Jan 30, 2010
1 parent 8fa81b5 commit 62441a5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
53 changes: 29 additions & 24 deletions lib/Async/ORM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -393,37 +393,42 @@ sub create {

warn "$sql" if DEBUG;

$dbh->exec(
"$sql" => [@values] => sub {
my ($dbh, $rows, $rv) = @_;
if (my $auto_increment = $self->schema->auto_increment) {
my $table = $self->schema->table;

return $cb->($dbh) unless $rv;
$dbh->exec_and_get_last_insert_id(
$table,
$auto_increment,
"$sql" => [@values] => sub {
my ($dbh, $id, $rv) = @_;

$self->is_in_db(1);
$self->is_modified(0);
return $cb->($dbh) unless $rv;

if (my $auto_increment = $self->schema->auto_increment) {
my $table = $self->schema->table;
$dbh->func(
last_insert_id =>
[undef, undef, $table, $auto_increment] => sub {
my ($dbh, $id, $handle_error) = @_;
$self->column($auto_increment => $id);

$self->column($auto_increment => $id);
$self->is_modified(0);
$self->is_in_db(1);
$self->is_modified(0);

#return $cb->($dbh, $self);
$self->_create_related(
$dbh => sub {
my ($dbh) = @_;
$self->_create_related(
$dbh => sub {
my ($dbh) = @_;

return $cb->($dbh, $self);
}
);
return $cb->($dbh, $self);
}
);
}
else {
);
}
else {
$dbh->exec(
"$sql" => [@values] => sub {
my ($dbh, $rows, $rv) = @_;

return $cb->($dbh) unless $rv;

$self->is_in_db(1);
$self->is_modified(0);

$self->_create_related(
$dbh => sub {
my ($dbh) = @_;
Expand All @@ -432,8 +437,8 @@ sub create {
}
);
}
}
);
);
}
}

sub load {
Expand Down
25 changes: 25 additions & 0 deletions lib/Async/ORM/DBI/Abstract.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ sub pass { @_ > 1 ? $_[0]->{pass} = $_[1] : $_[0]->{pass} }
sub attr { @_ > 1 ? $_[0]->{attr} = $_[1] : $_[0]->{attr} }
sub dbh { @_ > 1 ? $_[0]->{dbh} = $_[1] : $_[0]->{dbh} }

sub exec_and_get_last_insert_id {
my $self = shift;
my ($table, $auto_increment, $sql, $args, $cb) = @_;

($cb, $args) = ($args, []) unless $cb;

$self->exec(
$sql,
$args => sub {
my ($self, $rows, $rv) = @_;

return $cb->($self) unless $rv;

$self->func(
last_insert_id => [undef, undef, $table, $auto_increment] =>
sub {
my ($self, $id, $error) = @_;

$cb->($self, $id, $error ? 0 : 1);
}
);
}
);
}

1;
__END__
Expand Down

0 comments on commit 62441a5

Please sign in to comment.