DBIx-Skinny
view release on metacpan or search on metacpan
lib/DBIx/Skinny.pm view on Meta::CPAN
opt_table_info => $table,
}
);
$obj->setup;
$obj;
}
sub _last_insert_id {
my ($class, $table) = @_;
my $dbh = $class->dbh;
my $driver = $class->_attributes->{driver_name};
if ( $driver eq 'mysql' ) {
return $dbh->{mysql_insertid};
} elsif ( $driver eq 'Pg' ) {
return $dbh->last_insert_id( undef, undef, undef, undef,{ sequence => join( '_', $table, 'id', 'seq' ) } );
} elsif ( $driver eq 'SQLite' ) {
return $dbh->func('last_insert_rowid');
} elsif ( $driver eq 'Oracle' ) {
return;
} else {
Carp::croak "Don't know how to get last insert id for $driver";
}
}
*create = \*insert;
sub insert {
my ($class, $table, $args) = @_;
my $schema = $class->schema;
$class->call_schema_trigger('pre_insert', $schema, $table, $args);
my $obj = $class->_insert_or_replace(0, $table, $args);
$class->call_schema_trigger('post_insert', $schema, $table, $obj);
$obj;
}
sub replace {
my ($class, $table, $args) = @_;
my $schema = $class->schema;
$class->call_schema_trigger('pre_insert', $schema, $table, $args);
my $obj = $class->_insert_or_replace(1, $table, $args);
$class->call_schema_trigger('post_insert', $schema, $table, $obj);
$obj;
}
sub bulk_insert {
my ($class, $table, $args) = @_;
my $code = $class->_attributes->{dbd}->can('bulk_insert') or Carp::croak "dbd don't provide bulk_insert method";
$code->($class, $table, $args);
}
sub update {
my ($class, $table, $args, $where) = @_;
my $schema = $class->schema;
$class->call_schema_trigger('pre_update', $schema, $table, $args);
my $values = {};
for my $col (keys %{$args}) {
$values->{$col} = $schema->call_deflate($col, $args->{$col});
}
my ($columns, $bind_columns, undef) = $class->_set_columns($values, 0);
my $stmt = $class->resultset;
$class->_add_where($stmt, $where);
my @where_values = map {[$_ => $stmt->where_values->{$_}]} @{$stmt->bind_col};
push @{$bind_columns}, @where_values;
my $sql = "UPDATE $table SET " . join(', ', @$columns) . ' ' . $stmt->as_sql_where;
my $sth = $class->_execute($sql, $bind_columns, $table);
my $rows = $sth->rows;
$class->_close_sth($sth);
$class->call_schema_trigger('post_update', $schema, $table, $rows);
return $rows;
}
sub update_by_sql {
my ($class, $sql, $bind) = @_;
Carp::carp( 'update_by_sql has been deprecated. Please use $skinny->do($sql, undef, @bind)' );
$class->do($sql, undef, @$bind);
}
sub delete {
my ($class, $table, $where) = @_;
my $schema = $class->schema;
$class->call_schema_trigger('pre_delete', $schema, $table, $where);
my $stmt = $class->resultset(
{
from => [$table],
}
);
$class->_add_where($stmt, $where);
my $sql = "DELETE " . $stmt->as_sql;
my @where_values = map {[$_ => $stmt->where_values->{$_}]} @{$stmt->bind_col};
my $sth = $class->_execute($sql, \@where_values, $table);
my $rows = $sth->rows;
$class->call_schema_trigger('post_delete', $schema, $table, $rows);
$class->_close_sth($sth);
$rows;
}
sub delete_by_sql {
my ($class, $sql, $bind) = @_;
Carp::carp( 'delete_by_sql has been deprecated. Please use $skinny->do($sql, undef, @bind)' );
$class->do($sql, undef, @$bind);
}
*find_or_insert = \*find_or_create;
sub find_or_create {
my ($class, $table, $args) = @_;
my $row = $class->single($table, $args);
return $row if $row;
$class->insert($table, $args)->refetch;
}
sub _add_where {
my ($class, $stmt, $where) = @_;
for my $col (keys %{$where}) {
$stmt->add_where($col => $where->{$col});
}
}
sub _execute {
my ($class, $stmt, $args, $table) = @_;
my ($sth, $bind);
if ($table) {
$bind = [map {(ref $_->[1]) eq 'ARRAY' ? @{$_->[1]} : $_->[1]} @$args];
$class->profiler($stmt, $bind);
( run in 0.589 second using v1.01-cache-2.11-cpan-9581c071862 )