<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package DBIx::Custom::Model;
use Object::Simple -base;

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

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

has [qw/dbi table ctime mtime bind_type join primary_key/],
  columns =&gt; sub { [] };

our $AUTOLOAD;

sub AUTOLOAD {
  my $self = shift;

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

  # Method
  $self-&gt;{_methods} ||= {};
  if (my $method = $self-&gt;{_methods}-&gt;{$mname}) {
    return $self-&gt;$method(@_)
  }
  elsif (my $dbi_method = $self-&gt;dbi-&gt;can($mname)) {
    $self-&gt;dbi-&gt;$dbi_method(@_);
  }
  elsif ($self-&gt;{dbh} &amp;&amp; (my $dbh_method = $self-&gt;dbh-&gt;can($mname))) {
    $self-&gt;dbi-&gt;dbh-&gt;$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/;
for my $method (@methods) {
  
  my $code =
       qq/sub {/ .
       qq/my \$self = shift;/ .
       qq/\$self-&gt;dbi-&gt;$method(/ .
           qq/\@_ % 2 ? shift : (),/;

  
  my @attrs = qw/table type primary_key bind_type/;
  my @insert_attrs = qw/created_at updated_at ctime mtime/;
  my @update_attrs = qw/updated_at 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-&gt;{$attr} ? ($attr =&gt; \$self-&gt;{$attr}) : (),";
  }
  
  $code .= qq/\@_);/ .
       qq/}/;
  
  no strict 'refs';
  *{__PACKAGE__ . "::$method"} = eval $code;
  croak $code if $@;
}

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

sub execute {
  my $self = shift;
  
  if ($ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE}) {
      $self-&gt;dbi-&gt;execute(@_);
  }
  else {
    _deprecate('0.24', "DBIx::Custom::Model execute method is DEPRECATED! " .
      "use DBIx::Custom execute method. " .
      "If you want to call DBIx::Custom execute method directory from model, " .
      "set \$ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE} to 1 " .
      "until DBIx::Custom::Model execute method is removed in the future." );
    
    return $self-&gt;dbi-&gt;execute(
      shift,
      shift,
      table =&gt; $self-&gt;table,
      bind_type =&gt; $self-&gt;bind_type,
      primary_key =&gt; $self-&gt;primary_key,
      type =&gt; $self-&gt;type,
      @_
    );    
  }
}

sub DESTROY { }

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

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

sub new {
  my $self = shift-&gt;SUPER::new(@_);
  
  # Check attribute names
  my @attrs = keys %$self;
  for my $attr (@attrs) {
    croak qq{"$attr" is invalid attribute name } . _subname
      unless $self-&gt;can($attr);
  }
  
  # Cache
  for my $attr (qw/dbi table created_at updated_at ctime mtime bind_type join primary_key/) {
    $self-&gt;$attr;
    $self-&gt;{$attr} = undef unless exists $self-&gt;{$attr};
  }
  $self-&gt;columns;
  
  return $self;
}

# DEPRECATED!
has 'filter';
has 'name';
has 'type';
has 'created_at';
has 'updated_at'; 

# DEPRECATED!
sub method {
  _deprecate('0.24', "method method is DEPRECATED! use helper instead");
  return shift-&gt;helper(@_);
}

1;

=head1 NAME

DBIx::Custom::Model - Model object

=head1 SYNOPSIS

use DBIx::Custom::Model;

my $model = DBIx::Custom::Model-&gt;new(table =&gt; 'books');

=head1 ATTRIBUTES

=head2 dbi

  my $dbi = $model-&gt;dbi;
  $model = $model-&gt;dbi($dbi);

L&lt;DBIx::Custom&gt; object.

=head2 ctime

  my $ctime = $model-&gt;ctime;
  $model = $model-&gt;ctime('created_time');

Create timestamp column, this is passed to C&lt;insert&gt; or C&lt;update&gt; method.

=head2 join

  my $join = $model-&gt;join;
  $model = $model-&gt;join(
    ['left outer join company on book.company_id = company.id']
  );
  
Join clause, this value is passed to C&lt;select&gt; method.

=head2 primary_key

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

Primary key,this is passed to C&lt;insert&gt;, C&lt;update&gt;,
C&lt;delete&gt;, and C&lt;select&gt; method.

=head2 table

  my $model = $model-&gt;table;
  $model = $model-&gt;table('book');

Table name, this is passed to C&lt;select&gt; method.

=head2 bind_type

  my $type = $model-&gt;bind_type;
  $model = $model-&gt;bind_type(['image' =&gt; DBI::SQL_BLOB]);
  
Database data type, this is used as type optioon of C&lt;insert&gt;, 
C&lt;update&gt;, C&lt;update_all&gt;, C&lt;delete&gt;, C&lt;delete_all&gt;,
and C&lt;select&gt; method

=head2 mtime

  my $mtime = $model-&gt;mtime;
  $model = $model-&gt;mtime('modified_time');

Updated timestamp column, this is passed to C&lt;update&gt; method.

=head1 METHODS

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

=head2 count

  my $count = $model-&gt;count;

Get rows count.

Options is same as C&lt;select&gt; method's ones.

=head2 delete

  $model-&gt;delete(...);
  
Same as C&lt;delete&gt; of L&lt;DBIx::Custom&gt; except that
you don't have to specify options if you set attribute in model.

=head2 delete_all

  $model-&gt;delete_all(...);
  
Same as C&lt;delete_all&gt; of L&lt;DBIx::Custom&gt; except that
you don't have to specify options if you set attribute in model.

=head2 insert

  $model-&gt;insert(...);
  
Same as C&lt;insert&gt; of L&lt;DBIx::Custom&gt; except that
you don't have to specify options if you set attribute in model.

=head2 helper

  $model-&gt;helper(
    update_or_insert =&gt; sub {
      my $self = shift;
      
      # ...
    },
    find_or_create   =&gt; sub {
      my $self = shift;
      
      # ...
    }
  );

Register helper. These helper is called directly from L&lt;DBIx::Custom::Model&gt; object.

  $model-&gt;update_or_insert;
  $model-&gt;find_or_create;

=head2 mycolumn

  my $column = $self-&gt;mycolumn;
  my $column = $self-&gt;mycolumn(book =&gt; ['author', 'title']);
  my $column = $self-&gt;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&lt;table&gt; attribute of the model is used.
If column names is omitted, C&lt;columns&gt; attribute of the model is used.

=head2 new

  my $model = DBIx::Custom::Model-&gt;new;

Create a L&lt;DBIx::Custom::Model&gt; object.

=head2 select

  $model-&gt;select(...);
  
Same as C&lt;select&gt; of L&lt;DBIx::Custom&gt; except that
you don't have to specify options if you set attribute in model.

=head2 update

  $model-&gt;update(...);
  
Same as C&lt;update&gt; of L&lt;DBIx::Custom&gt; except that
you don't have to specify options if you set attribute in model.

=head2 update_all

  $model-&gt;update_all(param =&gt; \%param);
  
Same as C&lt;update_all&gt; of L&lt;DBIx::Custom&gt; except that
you don't have to specify options if you set attribute in model.

=head2 update_or_insert

  $model-&gt;update_or_insert(...);
  
Same as C&lt;update&gt; of L&lt;DBIx::Custom&gt; except that
you don't have to specify options if you set attribute in model.

=cut
</pre></body></html>