DBIx-ResultSet
view release on metacpan or search on metacpan
lib/DBIx/ResultSet.pm view on Meta::CPAN
my ($self) = @_;
if ($self->clauses->{page}) {
$self->clauses->{limit} = $self->pager->entries_per_page();
$self->clauses->{offset} = $self->pager->skipped();
}
return;
}
sub _do_select {
my ($self, $fields) = @_;
$self->_set_pager();
my $clauses = $self->clauses();
return $self->abstract->select(
$self->table(), $fields, $self->where(),
$clauses->{order_by},
$clauses->{limit},
$clauses->{offset},
);
}
=head1 MANIPULATION METHODS
These methods create, change, or remove data.
=head2 insert
$users_rs->insert(
{ user_name=>'bob2003', email=>'bob@example.com' }, # fields to insert
);
# Executes: INSERT INTO users (user_name, email) VALUES (?, ?);
Creates and executes an INSERT statement.
=cut
sub insert {
my ($self, $fields) = @_;
my ($sql, @bind) = $self->abstract->insert( $self->table(), $fields );
$self->_dbi_execute( 'do', $sql, \@bind );
return;
}
=head2 update
$users_rs->update(
{ phone => '555-1234' }, # fields to update
);
# Executes: UPDATE users SET phone = ?;
$users_rs->search({ is_admin=>1 })->update({ phone=>'555-1234 });
# Executes: UPDATE users SET phone = ? WHERE is_admin = ?;
Creates and executes an UPDATE statement.
=cut
sub update {
my ($self, $fields) = @_;
my ($sql, @bind) = $self->abstract->update( $self->table(), $fields, $self->where() );
$self->_dbi_execute( 'do', $sql, \@bind );
return;
}
=head2 delete
# Delete all users!
$users_rs->delete();
# Executes: DELETE FROM users;
# Or just the ones that are disabled.
users_rs->search({status=>0})->delete();
# Executes: DELETE FROM users WHERE status = 0;
Creates and executes a DELETE statement.
=cut
sub delete {
my ($self) = @_;
my ($sql, @bind) = $self->abstract->delete( $self->table(), $self->where() );
$self->_dbi_execute( 'do', $sql, \@bind );
return;
}
=head2 auto_pk
$users_rs->insert({ user_name=>'jdoe' });
my $user_id = $users_rs->auto_pk();
# Executes (MySQL): SELECT LAST_INSERT_ID();
# Executes (SQLite): SELECT LAST_INSERT_ROWID();
# etc...
Currently only MySQL and SQLite are supported. Oracle support will
be added soon, and other databases making their way in as needed.
=cut
sub auto_pk {
my ($self) = @_;
return $self->connector->_auto_pk( $self->table() );
}
=head1 RETRIEVAL METHODS
These methods provide common shortcuts for retrieving data.
=head2 array_row
my $user = $users_rs->search({ user_id => 32 })->array_row(
['created', 'email', 'phone'], # optional, fields to retrieve
);
print $user->[1]; # email
Creates and executes a SELECT statement and then returns an
array reference. The array will contain only the first row
that is retrieved, so you'll normally be doing this on a
resultset that has already been limited to a single row by
( run in 1.649 second using v1.01-cache-2.11-cpan-39bf76dae61 )