Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #78, #79 And column method accept setter values #80

Merged
merged 6 commits into from
Oct 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Revision history for Perl extension Teng

0.16
- row column method accept setter values. (thaks yappo)
- rename Row class method _lazy_get_data to generate_column_accessor. (thaks yappo)
- FIXED: AUTOLOAD row column method's uncached bug. (thaks yappo)

0.15
- stop mode:fixup support. (thanks nihen)
- refactoring load_plugin. (thanks ktat)
Expand Down
14 changes: 13 additions & 1 deletion lib/Teng.pm
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ in your script.
id => 1,
}
);
$row->update({name => 'nekokak'});
$row->update({name => 'nekokak'}); # same do { $row->name('nekokak'); $row->update; }

$row = $teng->search_by_sql(q{SELECT id, name FROM user WHERE id = ?}, [ 1 ]);
$row->delete();
Expand Down Expand Up @@ -817,6 +817,18 @@ You can also call update on a row object:
my $row = $teng->single('user',{id => 1});
$row->update({name => 'nomaneko'});

You can use the set_column method:

my $row = $teng->single('user', {id => 1});
$row->set_column( name => 'yappo' );
$row->update;

you can column update by using column method:

my $row = $teng->single('user', {id => 1});
$row->name('yappo');
$row->update;

=item $delete_row_count = $teng->delete($table, \%delete_condition)

Deletes the specified record(s) from C<$table> and returns the number of rows deleted. You may optionally specify C<%delete_condition> to create a conditional delete query.
Expand Down
16 changes: 10 additions & 6 deletions lib/Teng/Row.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ sub new {
my ($class, $args) = @_;

my $self = bless {
_get_column_cached => {},
_dirty_columns => {},
_get_column_cached => {},
_dirty_columns => {},
_autoload_column_cache => {},
%$args,
}, $class;

Expand All @@ -19,12 +20,14 @@ sub new {
$self;
}

sub _lazy_get_data {
sub generate_column_accessor {
my ($x, $col) = @_;

return sub {
my $self = shift;

return $self->set_column( $col => @_ ) if @_;

# "Untrusted" means the row is set_column by scalarref.
# e.g.
# $row->set_column("date" => \"DATE()");
Expand All @@ -33,7 +36,7 @@ sub _lazy_get_data {
}
my $cache = $self->{_get_column_cached};
my $data = $cache->{$col};
if (! $data) {
if (! $data) {
$data = $cache->{$col} = $self->{table} ? $self->{table}->call_inflate($col, $self->get_column($col)) : $self->get_column($col);
}
return $data;
Expand Down Expand Up @@ -174,10 +177,11 @@ sub _where_cond {
}
}

sub AUTOLOAD{
# for +columns option by some search methods
sub AUTOLOAD {
my $self = shift;
my($method) = ($AUTOLOAD =~ /([^:']+$)/);
$self->_lazy_get_data($method)->($self);
($self->{_autoload_column_cache}{$method} ||= $self->generate_column_accessor($method))->($self);
}

### don't autoload this
Expand Down
2 changes: 1 addition & 1 deletion lib/Teng/Schema/Table.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sub new {
for my $col (@{$self->columns}) {
no strict 'refs';
unless ($row_class->can($col)) {
*{"$row_class\::$col"} = $row_class->_lazy_get_data($col);
*{"$row_class\::$col"} = $row_class->generate_column_accessor($col);
}
}
$self->row_class($row_class);
Expand Down
13 changes: 13 additions & 0 deletions t/001_basic/015_row_class.t
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,23 @@ subtest 'handle' => sub {
can_ok $row->handle, 'single';
};

subtest 'your row class AUTOLOAD' => sub {
my $row = $db_basic_row->single('mock_basic_row',{id => 1},{'+columns' => [\'id+10 as id_plus_ten']});
isa_ok $row, 'Mock::BasicRow::Row::MockBasicRow';
is $row->foo, 'foo';
is $row->id, 1;
is $row->name, 'perl';
is $row->id_plus_ten, 11;

ok $row->can('id');
ok ! $row->can('mock_basic_id');
};

subtest 'AUTOLOAD' => sub {
my $row = $db_basic->search_by_sql(q{select id as mock_basic_id from mock_basic where id = 1})->next;
isa_ok $row, 'Teng::Row';
is $row->mock_basic_id, 1;
ok ! $row->can('mock_basic_id');
};

subtest 'can not use (update|delete) method' => sub {
Expand Down
16 changes: 16 additions & 0 deletions t/002_common/002_update.t
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,21 @@ subtest 'empty update' => sub {
is $row->name, 'perl';
};

subtest 'update by setter column' => sub {
my $row = $db->single('mock_basic',{
id => 1,
});
is $row->name, 'perl';

$row->name('tora');
is $row->update, 1;
is $row->name, 'tora';

my $row2 = $db->single('mock_basic',{
id => 1,
});
is $row2->name, 'tora';
};

done_testing;