[Bps-public-commit] dbix-searchbuilder branch, master, updated. 1.58-2-g6c1d32a
Ruslan Zakirov
ruz at bestpractical.com
Wed Nov 17 17:48:49 EST 2010
The branch, master has been updated
via 6c1d32a1cccd98fc1b72a41b877b417e13ac2c5f (commit)
via 3e50f6cc1a88165cf092354e75bc024f40f29141 (commit)
from e041232d14451135c66824c9d97e6ac5661b6f5c (commit)
Summary of changes:
SearchBuilder.pm | 30 ++++++++
t/02distinct_values.t | 176 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 206 insertions(+), 0 deletions(-)
create mode 100644 t/02distinct_values.t
- Log -----------------------------------------------------------------
commit 3e50f6cc1a88165cf092354e75bc024f40f29141
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date: Thu Nov 18 01:48:09 2010 +0300
Collection->DistinctFieldValues method
diff --git a/SearchBuilder.pm b/SearchBuilder.pm
index 519abbd..0a2e5db 100755
--- a/SearchBuilder.pm
+++ b/SearchBuilder.pm
@@ -574,6 +574,36 @@ sub Last {
return ( $self->Next );
}
+=head2 DistinctColumnValues
+
+=cut
+
+sub DistinctFieldValues {
+ my $self = shift;
+ my %args = (
+ Field => undef,
+ Sort => undef,
+ Max => undef,
+ @_%2 ? (Field => @_) : (@_)
+ );
+
+ my $query_string = $self->_BuildJoins;
+ $query_string .= ' '. $self->_WhereClause
+ if $self->_isLimited > 0;
+
+ my $column = 'main.'. $args{'Field'};
+ $query_string = 'SELECT DISTINCT '. $column .' FROM '. $query_string;
+
+ if ( $args{'Sort'} ) {
+ $query_string .= ' ORDER BY '. $column
+ .' '. ($args{'Sort'} =~ /^des/i ? 'DESC' : 'ASC');
+ }
+
+ my $dbh = $self->_Handle->dbh;
+ my $list = $dbh->selectcol_arrayref( $query_string, { MaxRows => $args{'Max'} } );
+ return $list? @$list : ();
+}
+
=head2 ItemsArrayRef
commit 6c1d32a1cccd98fc1b72a41b877b417e13ac2c5f
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date: Thu Nov 18 01:48:45 2010 +0300
tests for DistinctFieldValues method
diff --git a/t/02distinct_values.t b/t/02distinct_values.t
new file mode 100644
index 0000000..f5a2ef0
--- /dev/null
+++ b/t/02distinct_values.t
@@ -0,0 +1,176 @@
+#!/usr/bin/perl -w
+
+
+use strict;
+use warnings;
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@AvailableDrivers);
+
+use constant TESTS_PER_DRIVER => 9;
+
+my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+foreach my $d ( @AvailableDrivers ) {
+SKIP: {
+ unless( has_schema( 'TestApp', $d ) ) {
+ skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+ }
+ unless( should_test( $d ) ) {
+ skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+ }
+
+ my $handle = get_handle( $d );
+ connect_handle( $handle );
+ isa_ok($handle->dbh, 'DBI::db');
+
+ my $ret = init_schema( 'TestApp', $handle );
+ isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back");
+
+ my $count_all = init_data( 'TestApp::User', $handle );
+ ok( $count_all, "init users data" );
+
+ my $users_obj = TestApp::Users->new( $handle );
+ isa_ok( $users_obj, 'DBIx::SearchBuilder' );
+ is( $users_obj->_Handle, $handle, "same handle as we used in constructor");
+
+# unlimit new object and check
+ $users_obj->UnLimit;
+ is_deeply(
+ [$users_obj->DistinctFieldValues('GroupName', Sort => 'ASC')],
+ [undef, qw(boss dev sales)],
+ 'Correct list'
+ );
+ is_deeply(
+ [$users_obj->DistinctFieldValues('GroupName', Sort => 'DESC')],
+ [reverse undef, qw(boss dev sales)],
+ 'Correct list'
+ );
+ $users_obj->CleanSlate;
+
+ $users_obj->Limit( FIELD => 'Login', OPERATOR => 'LIKE', VALUE => 'k' );
+ is_deeply(
+ [$users_obj->DistinctFieldValues('GroupName', Sort => 'ASC')],
+ [qw(dev sales)],
+ 'Correct list'
+ );
+ is_deeply(
+ [$users_obj->DistinctFieldValues('GroupName', Sort => 'DESC')],
+ [reverse qw(dev sales)],
+ 'Correct list'
+ );
+
+ cleanup_schema( 'TestApp', $handle );
+}} # SKIP, foreach blocks
+
+1;
+
+package TestApp;
+
+sub schema_mysql {
+<<EOF;
+CREATE TEMPORARY TABLE Users (
+ id integer AUTO_INCREMENT,
+ Login varchar(18) NOT NULL,
+ GroupName varchar(36),
+ PRIMARY KEY (id))
+EOF
+
+}
+
+sub schema_pg {
+<<EOF;
+CREATE TEMPORARY TABLE Users (
+ id serial PRIMARY KEY,
+ Login varchar(18) NOT NULL,
+ GroupName varchar(36)
+)
+EOF
+
+}
+
+sub schema_sqlite {
+
+<<EOF;
+CREATE TABLE Users (
+ id integer primary key,
+ Login varchar(18) NOT NULL,
+ GroupName varchar(36)
+)
+EOF
+
+}
+
+sub schema_oracle { [
+ "CREATE SEQUENCE Users_seq",
+ "CREATE TABLE Users (
+ id integer CONSTRAINT Users_Key PRIMARY KEY,
+ Login varchar(18) NOT NULL,
+ GroupName varchar(36)
+ )",
+] }
+
+sub cleanup_schema_oracle { [
+ "DROP SEQUENCE Users_seq",
+ "DROP TABLE Users",
+] }
+
+
+1;
+
+package TestApp::User;
+
+use base $ENV{SB_TEST_CACHABLE}?
+ qw/DBIx::SearchBuilder::Record::Cachable/:
+ qw/DBIx::SearchBuilder::Record/;
+
+sub _Init {
+ my $self = shift;
+ my $handle = shift;
+ $self->Table('Users');
+ $self->_Handle($handle);
+}
+
+sub _ClassAccessible {
+ {
+ id =>
+ {read => 1, type => 'int(11)' },
+ Login =>
+ {read => 1, write => 1, type => 'varchar(18)' },
+ GroupName =>
+ {read => 1, write => 1, type => 'varchar(36)' },
+ }
+}
+
+sub init_data {
+ return (
+ [ 'Login', 'GroupName' ],
+ [ 'cubic', 'dev' ],
+ [ 'obra', 'boss' ],
+ [ 'kevin', 'dev' ],
+ [ 'keri', 'sales' ],
+ [ 'some', undef ],
+ );
+}
+
+1;
+
+package TestApp::Users;
+
+# use TestApp::User;
+use base qw/DBIx::SearchBuilder/;
+
+sub _Init {
+ my $self = shift;
+ $self->SUPER::_Init( Handle => shift );
+ $self->Table('Users');
+}
+
+sub NewItem
+{
+ my $self = shift;
+ return TestApp::User->new( $self->_Handle );
+}
+
+1;
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list