Permalink
Browse files

support '+columns' for single, search, lookup and search_with_pager

  • Loading branch information...
nihen committed Dec 20, 2011
1 parent 64b342a commit a95561230faebea6e0d84fc1a7c626684f290607
View
@@ -431,9 +431,14 @@ sub search {
Carp::croak("No such table $table_name");
}
+ my $columns = $opt->{'+columns'}
+ ? [@{$table->{columns}}, @{$opt->{'+columns'}}]
+ : ($opt->{columns} || $table->{columns})
+ ;
+
my ($sql, @binds) = $self->{sql_builder}->select(
$table_name,
- ($opt->{columns} || $table->{columns}),
+ $columns,
$where,
$opt
);
@@ -469,9 +474,14 @@ sub single {
my $table = $self->{schema}->get_table( $table_name );
Carp::croak("No such table $table_name") unless $table;
+ my $columns = $opt->{'+columns'}
+ ? [@{$table->{columns}}, @{$opt->{'+columns'}}]
+ : ($opt->{columns} || $table->{columns})
+ ;
+
my ($sql, @binds) = $self->{sql_builder}->select(
$table_name,
- ($opt->{columns} || $table->{columns}),
+ $columns,
$where,
$opt
);
View
@@ -13,9 +13,10 @@ sub lookup {
my @sorted_keys = sort keys %$where;
+ my $columns = _get_select_columns($table, $opt);
my $cond = join ' AND ', map {"$_ = ?"} @sorted_keys;
my $sql = sprintf('SELECT %s FROM %s WHERE %s %s',
- join(',', @{$opt->{columns} || $table->{columns}}),
+ join(',', @{$columns}),
$table_name,
$cond,
$opt->{for_update} ? 'FOR UPDATE' : '',
@@ -37,6 +38,27 @@ sub lookup {
}
);
}
+sub _get_select_columns {
+ my ($table, $opt) = @_;
+
+ my $columns;
+ if ( $opt->{'+columns'} ) {
+ $columns = [
+ @{$table->{columns}},
+ (map { ref $_ eq 'SCALAR' ? $$_ : $_ } @{$opt->{'+columns'}})
+ ];
+ }
+ elsif ( $opt->{columns} ) {
+ $columns = [
+ map { ref $_ eq 'SCALAR' ? $$_ : $_ } @{$opt->{columns}}
+ ];
+ }
+ else {
+ $columns = $table->{columns};
+ }
+
+ return $columns;
+}
1;
__END__
View
@@ -20,9 +20,14 @@ sub search_with_pager {
Carp::croak("missing mandatory parameter: $_") unless exists $opt->{$_};
}
+ my $columns = $opt->{'+columns'}
+ ? [@{$table->{columns}}, @{$opt->{'+columns'}}]
+ : ($opt->{columns} || $table->{columns})
+ ;
+
my ($sql, @binds) = $self->sql_builder->select(
$table_name,
- ($opt->{columns} || $table->{columns}),
+ $columns,
$where,
+{
%$opt,
@@ -16,9 +16,14 @@ sub search_with_pager {
my $page = $opt->{page};
my $rows = $opt->{rows};
+ my $columns = $opt->{'+columns'}
+ ? [@{$table->{columns}}, @{$opt->{'+columns'}}]
+ : ($opt->{columns} || $table->{columns})
+ ;
+
my ($sql, @binds) = $self->sql_builder->select(
$table_name,
- ($opt->{columns} || $table->{columns}),
+ $columns,
$where,
+{
%$opt,
View
@@ -47,6 +47,17 @@ subtest 'simple_with_columns' => sub {
ok $pager->has_next, 'has_next';
is $pager->prev_page, undef;
};
+subtest 'simple_with_+columns' => sub {
+ my ($rows, $pager) = $db->search_with_pager(mock_basic => {}, {'+columns' => [\'id+20 as calc'], rows => 3, page => 1});
+ is join(',', map { $_->id } @$rows), '1,2,3';
+ is join(',', map { $_->calc } @$rows), '21,22,23';
+ is $pager->entries_per_page(), 3;
+ is $pager->entries_on_this_page(), 3;
+ is $pager->current_page(), 1;
+ is $pager->next_page, 2, 'next_page';
+ ok $pager->has_next, 'has_next';
+ is $pager->prev_page, undef;
+};
done_testing;
View
@@ -43,6 +43,21 @@ subtest 'lookup_with_columns' => sub {
id => 2,
};
};
+subtest 'lookup_with_+columns' => sub {
+ $db_basic->insert('mock_basic', => +{
+ id => 3,
+ name => 'python',
+ });
+
+ my $row = $db_basic->lookup('mock_basic', +{id => 3}, { '+columns' => [\'id+20 as calc']});
+ isa_ok $row, 'Mock::Basic::Row::MockBasic';
+ is_deeply $row->get_columns, +{
+ id => 3,
+ name => 'python',
+ calc => 23,
+ delete_fg => 0,
+ };
+};
done_testing;
View
@@ -32,4 +32,16 @@ subtest 'single / specific column' => sub {
};
};
+subtest 'single / specific +column' => sub {
+ my $row = $db->single('mock_basic',{id => 1},+{'+columns' => [\'id+20 as calc']});
+ isa_ok $row, 'Teng::Row';
+ is $row->id, 1;
+ is_deeply $row->get_columns, +{
+ id => 1,
+ name => 'perl',
+ delete_fg => 0,
+ calc => 21,
+ };
+};
+
done_testing;
View
@@ -48,6 +48,22 @@ subtest 'search with columns opts' => sub {
};
};
+subtest 'search with +columns opts' => sub {
+ my $itr = $db->search('mock_basic',{id => 1}, +{'+columns' => [\'id+20 as calc']});
+ isa_ok $itr, 'Teng::Iterator';
+
+ my $row = $itr->next;
+ isa_ok $row, 'Teng::Row';
+
+ is $row->id, 1;
+ is_deeply $row->get_columns, +{
+ id => 1,
+ name => 'perl',
+ delete_fg => 0,
+ calc => 21,
+ };
+};
+
subtest 'search without where' => sub {
my $itr = $db->search('mock_basic');
@@ -44,6 +44,16 @@ subtest 'simple_with_columns' => sub {
is $pager->next_page, 2, 'next_page';
is $pager->previous_page, undef;
};
+subtest 'simple_with_+columns' => sub {
+ my ($rows, $pager) = $db->search_with_pager(mock_basic => {}, {'+columns' => [\'id+20 as calc'], rows => 3, page => 1});
+ is join(',', map { $_->id } @$rows), '1,2,3';
+ is join(',', map { $_->calc } @$rows), '21,22,23';
+ is $pager->total_entries(), 32;
+ is $pager->entries_per_page(), 3;
+ is $pager->current_page(), 1;
+ is $pager->next_page, 2, 'next_page';
+ is $pager->previous_page, undef;
+};
done_testing;

0 comments on commit a955612

Please sign in to comment.