DBIx-OO

 view release on metacpan or  search on metacpan

lib/DBIx/OO.pm  view on Meta::CPAN

The C<$limit> and C<$offset> arguments are added by DBIx::OO and allow you
to limit/paginate your query.

UPDATE 0.0.7:

Certain queries are difficult to express in SQL::Abstract syntax.  The
search accepts a literal WHERE clause too, but until version 0.0.7
there was no way to specify bind variables.  For example, now you can
do this:

    @admins = Users->search("mode & ? <> 0 and created > ?",
                            undef, undef, undef,
                            MODE_FLAGS->{admin},
                            strftime('%Y-%m-%d', localtime)).

In order to pass bind variables, you must pass order, limit and offset
(give undef if you don't care about them) and add your bind variables
immediately after.

=cut

sub search {
    my $class = __T(shift);
    my ($where, $order, $limit, $offset) = @_;
    splice @_, 0, 4;
    my $sa = $class->get_sql_abstract;
    my $cols = $class->_get_columns([ 'P', 'E' ]);
    my ($sql, @bind) = $sa->select($class->table, $cols, $where, $order, $limit, $offset);
    if (@_) {
        push @bind, @_;
    }
    my $sth = $class->_run_sql($sql, \@bind);
    my @ret = ();
    while (my $row = $sth->fetchrow_arrayref) {
        my $obj = $class->new;
        @{$obj->{values}}{@$cols} = @$row;
        push @ret, $obj;
    }
    return wantarray ? @ret : \@ret;
}

=head2 C<retrieve_all()>

retrieve_all() is an alias to search() -- since with no arguments it
fetches all objects.

=cut

*retrieve_all = *search;

=head2 C<update>

    $u->set(first_name => 'Foo',
            last_name => 'Bar');
    $u->update;

Saves any modified columns to the database.

=cut

sub update {
    my $class = shift;
    if (ref $class) {
        $class->_do_update;
    } else {
        my ($fieldvals, $where) = @_;
        my $sa = $class->get_sql_abstract;
        my ($sql, @bind) = $sa->update($class->table, $fieldvals, $where);
        $class->_run_sql($sql, \@bind);
    }
}

=head2 C<delete>

    $u = Users->retrieve('foo');
    $u->delete;

Removes the object's record from the database.  Note that the Perl
object remains intact and you can actually revive it (if you're not
losing it) using undelete().

=cut

sub delete {
    my ($self, $where) = @_;
    my ($sql, @bind);
    my $sa = $self->get_sql_abstract;
    if (!defined $where) {
        # we're deleting one object
        ($sql, @bind) = $sa->delete($self->table, $self->_get_pk_where);
    } else {
        # deleting multiple objects at once
        ($sql, @bind) = $sa->delete($self->table, $where);
    }
    $self->_run_sql($sql, \@bind);
}

=head2 C<undelete>

    $u = Users->retrieve('foo');
    $u->delete;    # record's gone
    $u->undelete;  # resurrected

This function can "ressurect" an object that has been deleted (that
is, it re-INSERT-s the record into the database), provided that you
still have a reference to the object.  I'm not sure how useful it is,
but it helped me test the delete() function. :-)

Other (unuseful) thing you can do with it is manually emulating the
create() function:

    $u = new Users;
    $u->{values}{id} = 'foo';
    $u->first_name('Foo');
    $u->last_name('Bar');
    $u->undelete;

Note we can't call the column accessors, nor use set/get, before we
have a primary key.

This method is not too useful in itself, but it helps understanding



( run in 1.048 second using v1.01-cache-2.11-cpan-5a3173703d6 )