DBIx-Custom

 view release on metacpan or  search on metacpan

lib/DBIx/Custom/Model.pm  view on Meta::CPAN

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

use Carp 'confess';
use DBIx::Custom::Util qw/_subname _deprecate/;

has [qw/dbi table name ctime mtime bind_type join/];
has columns => sub { [] };

our $AUTOLOAD;

my @methods = qw(insert update update_all delete delete_all select count);
for my $method (@methods) {
  
  my $code =
       qq/sub {/ .
       qq/my \$self = shift;/ .
       qq/\$self->dbi->$method(/ .
           qq/\@_ % 2 ? shift : (),/;

  
  my @attrs = qw/table type primary_key bind_type/;
  my @insert_attrs = qw/ctime mtime/;
  my @update_attrs = qw/mtime/;
  my @select_attrs = qw/join/;
  if ($method eq 'insert') { push @attrs, @insert_attrs }
  elsif ($method eq 'update') { push @attrs, @update_attrs }
  elsif (index($method, 'select') != -1 || $method eq 'count') {
    push @attrs, @select_attrs
  }
  
  for my $attr (@attrs) {
    $code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),";
  }
  
  $code .= qq/\@_);/ .
       qq/}/;
  
  no strict 'refs';
  *{__PACKAGE__ . "::$method"} = eval $code;
  confess $code if $@;
}

# DEPRECATED
sub primary_key {
  if (@_ == 1) {
    return $_[0]{'primary_key'};
  }
  $_[0]{'primary_key'} = $_[1];
  $_[0];
};

# DEPRECATED
sub update_or_insert {
  my ($self, $param, %opt) = @_;

  _deprecate('0.39', "DBIx::Custom::Model::update_or_insert method is DEPRECATED!");

  confess "update_or_insert method need primary_key and id option "
    unless (defined $opt{id} || defined $self->{id})
        && (defined $opt{primary_key} || defined $self->{primary_key});
  
  my $statement_opt = $opt{option} || {};
  my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all;
  if (@$rows == 0) {
    return $self->insert($param, %opt, %{$statement_opt->{insert} || {}});
  }
  elsif (@$rows == 1) {
    return $self->update($param, %opt, %{$statement_opt->{update} || {}});
  }
  else { confess "selected row must be one " . _subname }
}

# DEPRECATED
sub AUTOLOAD {
  my $self = shift;
  
  _deprecate('0.39', "DBIx::Custom::Model AUTOLOAD feature is DEPRECATED!");
  
  # 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 {
    confess qq{Can't locate object method "$mname" via "$package" }
      . _subname;
  }
}
sub DESTROY { }

# DEPRECATED
sub helper {
  my $self = shift;
  
  _deprecate('0.39', "DBIx::Custom::Model::helper method is DEPRECATED!");
  
  # Merge
  my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
  $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
  
  return $self;
}

sub mycolumn {



( run in 2.035 seconds using v1.01-cache-2.11-cpan-f56aa216473 )