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 count/; foreach my $method (@methods) { my $code = sub { my $self = shift; my $args = [qw/table bind_type primary_key type/]; push @$args, 'join' if $method =~ /^select/; $self->call_dbi($method, {args => $args}, @_); }; no strict 'refs'; my $class = __PACKAGE__; *{"${class}::$method"} = $code; } sub call_dbi { my $self = shift; my $method = shift; my $options = shift; my $arg_names = $options->{args}; my @args; push @args, ($_ => $self->$_) for @$arg_names; unshift @args, shift if @_ % 2; return $self->dbi->$method(@args, @_); } sub execute { my $self = shift; push @_, ($_ => $self->$_) for qw/table bind_type primary_key type/; return $self->dbi->execute(@_); } 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'; has 'name'; has type => sub { [] }; 1; =head1 NAME DBIx::Custom::Model - Model =head1 SYNOPSIS use DBIx::Custom::Model; my $model = DBIx::Custom::Model->new(table => 'books'); =head1 ATTRIBUTES =head2 C my $dbi = $model->dbi; $model = $model->dbi($dbi); L object. =head2 C my $join = $model->join; $model = $model->join( ['left outer join company on book.company_id = company.id'] ); Join clause, this value is passed to C method. =head2 C my $model = $model->table; $model = $model->table('book'); Table name, this is passed to C, and C method =head1 METHODS L inherits all methods from L, and you can use all methods of L and L and implements the following new ones. =head2 C EXPERIMENTAL $model->call_dbi('insert', {args => ['table', 'primary_key' 'bind_type']}, @_) Call L(or subclass) method. you can add attribute values of model to arguments by C option. Generally this method is used when you want to added dbi method to model. sub insert { shift->call_dbi('insert', {args => ['table', 'primary_key' 'bind_type']}, @_); } =head2 C EXPERIMENTAL my $count = $model->count; Get rows count. Options is same as C
and C option. =head2 C $model->delete_all(...); Same as C of L except that you don't have to specify C
and C option. =head2 C $model->execute(...); Same as C of L except that you don't have to specify C
and C option. =head2 C $model->insert(...); Same as C of L except that you don't have to specify C
and C option. =head2 C $model->method( update_or_insert => sub { my $self = shift; # ... }, find_or_create => sub { my $self = shift; # ... ); Register method. These method is called directly from L object. $model->update_or_insert; $model->find_or_create; =head2 C 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, C
attribute of the model is used. If column names is omitted, C attribute of the model is used. =head2 C my $model = DBIx::Custom::Model->new; Create a L object. =head2 C of L except that you don't have to specify C
, C and C option. =head2 C $model->update(...); Same as C of L except that you don't have to specify C
and C option. =head2 C $model->update_all(param => \%param); Same as C of L except that you don't have to specify C
and C option. =cut