Data-Model
view release on metacpan or search on metacpan
lib/Data/Model.pm view on Meta::CPAN
## ->set( modelname => [ keys ] => { key => value, ... } );
$key_array = shift;
} elsif (!ref($_[0])) {
## ->set( modelname => 'key' => { key => value, ... } );
$key_array = [ shift ];
} else {
# return;
}
# get columns
if ($columns) {
## nop
} elsif (ref($_[0]) eq 'HASH') {
## get hash columns data
my $hash = shift;
$columns = $schema->get_columns_hash_by_key_array_and_hash($hash, $key_array);
} else {
$columns = $schema->get_columns_hash_by_key_array_and_hash(+{}, $key_array);
}
# deflate
$schema->deflate($columns);
$key_array = $schema->get_key_array_by_hash( $columns );
# triggers
$schema->call_trigger('pre_save', $columns);
$schema->set_default($columns); # set default
$schema->call_trigger('pre_insert', $columns);
local $schema->{schema_obj} = $self;
my $method = $is_replace ? 'replace' : 'set';
my $result = $schema->{driver}->$method( $schema, $key_array => $columns, @_ );
return unless $result;
unless ($schema->{options}->{bare_row}) {
my $obj = $schema->new_obj($self, $result);
$schema->inflate($obj);
$schema->call_trigger('post_load', $obj);
return $obj;
}
return $result;
}
sub set_multi {
my $self = shift;
Carp::croak "The 'set_multi' method can not be performed during a transaction." if $self->{active_transaction};
}
sub _get_schema_by_row {
my($self, $row) = @_;
my $class = ref($row);
return unless $class;
my($klass, $model) = $class =~ /^(.+)::([^:]+)$/;
return unless (ref($self) || $self) eq $klass;
return $self->get_schema($model);
}
sub update {
my $self = shift;
Carp::croak "The 'update' method can not be performed during a transaction." if $self->{active_transaction};
my $row = shift;
return $self->update_direct($row, @_) unless ref($row) && $row->isa('Data::Model::Row');
my $schema = $self->_get_schema_by_row($row);
return unless $schema;
return unless @{ $schema->{key} } > 0; # not has key
return unless scalar(%{ $row->get_changed_columns });
my $columns = $row->get_columns;
my $changed_columns = $row->get_changed_columns;
my $old_columns = +{ %{ $columns }, %{ $changed_columns } };
if ($schema->has_deflate) {
# deflate
$schema->deflate($columns);
$schema->deflate($old_columns);
}
$schema->call_trigger('pre_save', $columns);
$schema->call_trigger('pre_update', $columns, $old_columns);
my $key_array = $schema->get_key_array_by_hash($columns);
my $old_key_array = $schema->get_key_array_by_hash($old_columns);
my $result = $schema->{driver}->update(
$schema, $old_key_array, $key_array, $old_columns, $columns, $changed_columns, @_
);
$row->{changed_cols} = +{};
return unless $result;
$row;
}
# $model->update_direct( model_name => 'key', +{ querys }, +{ update columns } );
# $model->update_direct( model_name => [qw/ key1 key2 /], +{ querys }, +{ update columns } );
# $model->update_direct( model_name => +{ querys }, +{ update columns } );
# direct_update get ããªãã§ç´æ¥ updateãã where ã®çµã¿ç«ã¦ã¯ get/delete ã¨åã
sub update_direct {
my $self = shift;
Carp::croak "The 'update_direct' method can not be performed during a transaction." if $self->{active_transaction};
my $model = shift;
my $schema = $self->get_schema($model);
return unless $schema;
my $query = $self->_get_query_args($schema, @_);
return unless @{ $query };
$schema->deflate($query->[2]);
$schema->call_trigger('pre_save', $query->[2]);
$schema->call_trigger('pre_update', $query->[2]);
local $schema->{schema_obj} = $self;
$schema->{driver}->update_direct( $schema, @{ $query } );
}
# $model->delete( model_name => 'key' );
# $model->delete( model_name => [qw/ key1 key2 /] );
sub delete {
my $self = shift;
Carp::croak "The 'delete' method can not be performed during a transaction." if $self->{active_transaction};
my $row = shift;
return $self->delete_direct($row, @_) unless ref($row) && $row->isa('Data::Model::Row');
my $schema = $self->_get_schema_by_row($row);
return unless $schema;
return unless @{ $schema->{key} } > 0; # not has key
my $columns = $row->get_columns;
my $key_array = $schema->get_key_array_by_hash($columns);
local $schema->{schema_obj} = $self;
$schema->{driver}->delete( $schema, $key_array, @_ );
}
sub delete_direct {
my $self = shift;
Carp::croak "The 'delete_direct' method can not be performed during a transaction." if $self->{active_transaction};
my $model = shift;
my $schema = $self->get_schema($model);
return unless $schema;
my $query = $self->_get_query_args($schema, @_);
return unless @{ $query };
local $schema->{schema_obj} = $self;
$schema->{driver}->delete( $schema, @{ $query } );
}
sub delete_multi {
my $self = shift;
Carp::croak "The 'delete_multi' method can not be performed during a transaction." if $self->{active_transaction};
}
# for transactions
sub txn_scope {
( run in 0.457 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )