Data-Model
view release on metacpan or search on metacpan
lib/Data/Model/Driver/DBI.pm view on Meta::CPAN
}
undef $sth;
$columns;
}
# update
sub _update {
my($self, $schema, $changed_columns, $columns, $where_sql, $pre_bind, $pre_bind_column) = @_;
my @bind;
my @bind_column;
my @set;
for my $column (keys %{ $changed_columns }) {
my $val = $columns->{$column};
if (ref($val) eq 'SCALAR') {
push @set, "$column = " . ${ $val };
} elsif (!ref($val)) {
push @set, "$column = ?";
push @bind, $val;
push @bind_column, $column;
} else {
Carp::confess 'No references other than a SCALAR reference can use a update column';
}
}
push @bind, @{ $pre_bind };
push @bind_column, @{ $pre_bind_column };
# bind_params
my @params;
for my $i (1..scalar(@bind)) {
push @params, [ $bind_column[$i - 1], $bind[$i - 1] ];
}
my $sql = 'UPDATE ' . $schema->model . ' SET ' . join(', ', @set) . ' ' . $where_sql;
my $sth;
eval {
my $dbh = $self->rw_handle;
$self->start_query($sql, \@bind);
$sth = $dbh->prepare_cached($sql);
$self->bind_params($schema, \@params, $sth);
$sth->execute;
$sth->finish;
$self->end_query($sth);
};
if ($@) {
$self->_stack_trace($sth, $sql, \@params, $@);
}
if (wantarray) {
my @ret = $sth->rows;
undef $sth;
return @ret;
} else {
my $ret = $sth->rows;
undef $sth;
return $ret;
}
}
sub update {
my($self, $schema, $old_key, $key, $old_columns, $columns, $changed_columns, %args) = @_;
my $stmt = Data::Model::SQL->new;
$self->add_key_to_where($stmt, $schema->key, $old_key);
my $where_sql = $stmt->as_sql_where;
return unless $where_sql;
return $self->_update($schema, $changed_columns, $columns, $where_sql, $stmt->bind, $stmt->bind_column);
}
sub update_direct {
my($self, $schema, $key, $query, $columns, %args) = @_;
my $index_query = delete $query->{index};
my $stmt = Data::Model::SQL->new(%{ $query });
$self->add_key_to_where($stmt, $schema->key, $key) if $key;
$self->add_index_to_where($schema, $stmt, $index_query) if $index_query;
my $where_sql = $stmt->as_sql_where;
return unless $where_sql;
return $self->_update($schema, $columns, $columns, $where_sql, $stmt->bind, $stmt->bind_column);
}
# delete
sub delete {
my($self, $schema, $key, $columns, %args) = @_;
$columns->{from} = [ $schema->model ];
my $index_query = delete $columns->{index};
my $stmt = Data::Model::SQL->new(%{ $columns });
$self->add_key_to_where($stmt, $schema->key, $key) if $key;
$self->add_index_to_where($schema, $stmt, $index_query) if $index_query;
# bind_params
my @params;
for my $i (1..scalar(@{ $stmt->bind })) {
push @params, [ $stmt->bind_column->[$i - 1], $stmt->bind->[$i - 1] ];
}
my $sql = "DELETE " . $stmt->as_sql;
my $sth;
eval {
my $dbh = $self->rw_handle;
$self->start_query($sql, $stmt->bind);
$sth = $dbh->prepare_cached($sql);
$self->bind_params($schema, \@params, $sth);
$sth->execute;
$sth->finish;
$self->end_query($sth);
};
if ($@) {
$self->_stack_trace($sth, $sql, $stmt->bind, $@);
}
if (wantarray) {
my @ret = $sth->rows;
undef $sth;
return @ret;
} else {
my $ret = $sth->rows;
undef $sth;
return $ret;
}
}
# for schema
sub _as_sql_hook {
my $self = shift;
$self->dbd->_as_sql_hook(@_);
}
( run in 0.512 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )