DBIx::Custom::Model - Model


DBIx-Custom documentation Contained in the DBIx-Custom distribution.

Index


Code Index:

NAME

Top

DBIx::Custom::Model - Model

SYNOPSIS

Top

use DBIx::Custom::Table;

my $table = DBIx::Custom::Model->new(table => 'books');

ATTRIBUTES

Top

dbi

    my $dbi = $model->dbi;
    $model = $model->dbi($dbi);

DBIx::Custom object.

join

    my $join = $model->join;
    $model = $model->join(
        ['left outer join company on book.company_id = company.id']
    );

Join clause, this value is passed to select method.

primary_key

    my $primary_key = $model->primary_key;
    $model = $model->primary_key(['id', 'number']);

Primary key,this is passed to insert, update, delete, and select method.

table

    my $table = $model->table;
    $model = $model->table('book');

Table name, this is passed to select method.

bind_type

    my $type = $model->bind_type;
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);

Database data type, this is used as type optioon of insert, update, update_all, delete, delete_all, select, and execute method

METHODS

Top

DBIx::Custom::Model inherits all methods from Object::Simple, and you can use all methods of DBIx::Custom and DBI and implements the following new ones.

delete

    $table->delete(...);

Same as delete of DBIx::Custom except that you don't have to specify table option.

delete_all

    $table->delete_all(...);

Same as delete_all of DBIx::Custom except that you don't have to specify table option.

insert

    $table->insert(...);

Same as insert of DBIx::Custom except that you don't have to specify table option.

method

    $model->method(
        update_or_insert => sub {
            my $self = shift;

            # ...
        },
        find_or_create   => sub {
            my $self = shift;

            # ...
    );

Register method. These method is called directly from DBIx::Custom::Model object.

    $model->update_or_insert;
    $model->find_or_create;

mycolumn

    my $column = $self->mycolumn;
    my $column = $self->mycolumn(book => ['author', 'title']);
    my $column = $self->mycolumn(['author', 'title']);

Create column clause for myself. The follwoing column clause is created.

    book.author as author,
    book.title as title

If table name is ommited, table attribute of the model is used. If column names is omitted, columns attribute of the model is used.

new

    my $table = DBIx::Custom::Table->new;

Create a DBIx::Custom::Table object.

select

    $table->select(...);

Same as select of DBIx::Custom except that you don't have to specify table option.

update

    $table->update(...);

Same as update of DBIx::Custom except that you don't have to specify table option.

update_all

    $table->update_all(param => \%param);

Same as update_all of DBIx::Custom except that you don't have to specify table name.


DBIx-Custom documentation Contained in the DBIx-Custom distribution.

package DBIx::Custom::Model;
use Object::Simple -base;

use Carp 'croak';
use DBIx::Custom::Util '_subname';

# Carp trust relationship
push @DBIx::Custom::CARP_NOT, __PACKAGE__;

has [qw/dbi table/],
    bind_type => sub { [] },
    columns => sub { [] },
    join => sub { [] },
    primary_key => sub { [] };

our $AUTOLOAD;

sub AUTOLOAD {
    my $self = shift;

    # Method name
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;

    # Method
    $self->{_methods} ||= {};
    if (my $method = $self->{_methods}->{$mname}) {
        return $self->$method(@_)
    }
    elsif (my $dbi_method = $self->dbi->can($mname)) {
        $self->dbi->$dbi_method(@_);
    }
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
        $self->dbi->dbh->$dbh_method(@_);
    }
    else {
        croak qq{Can't locate object method "$mname" via "$package" }
            . _subname;
    }
}

my @methods = qw/insert insert_at update update_at update_all
                 delete delete_at delete_all select select_at/;
foreach my $method (@methods) {

    my $code = sub {
        my $self = shift;

        my @args = (
            table => $self->table,
            bind_type => $self->bind_type,
            primary_key => $self->primary_key,
            type => $self->type, # DEPRECATED!
        );
        push @args, (join => $self->join) if $method =~ /^select/;
        unshift @args, shift if @_ % 2;
        
        $self->dbi->$method(@args, @_);
    };
    
    no strict 'refs';
    my $class = __PACKAGE__;
    *{"${class}::$method"} = $code;
}

sub DESTROY { }

sub method {
    my $self = shift;
    
    # Merge
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
    
    return $self;
}

sub mycolumn {
    my $self = shift;
    my $table = shift unless ref $_[0];
    my $columns = shift;
    
    $table ||= $self->table || '';
    $columns ||= $self->columns;
    
    return $self->dbi->mycolumn($table, $columns);
}

sub new {
    my $self = shift->SUPER::new(@_);
    
    # Check attribute names
    my @attrs = keys %$self;
    foreach my $attr (@attrs) {
        croak qq{"$attr" is invalid attribute name } . _subname
          unless $self->can($attr);
    }
    
    return $self;
}

# DEPRECATED!
has filter => sub { [] };
has 'name';
has type => sub { [] };

1;