[Catalyst-commits] r8314 - in
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk:
lib/CatalystX/CRUD/Model t t/lib/MyApp/Controller
karpet at dev.catalyst.perl.org
karpet at dev.catalyst.perl.org
Thu Aug 28 19:36:02 BST 2008
Author: karpet
Date: 2008-08-28 19:36:02 +0100 (Thu, 28 Aug 2008)
New Revision: 8314
Modified:
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Model/RDBO.pm
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/01-rdbo.t
CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/Foo.pm
Log:
add tests and fix for sort table prefixing
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Model/RDBO.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Model/RDBO.pm 2008-08-28 18:35:29 UTC (rev 8313)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/lib/CatalystX/CRUD/Model/RDBO.pm 2008-08-28 18:36:02 UTC (rev 8314)
@@ -352,7 +352,7 @@
my $fpk = $meta->{map_to}->[1];
$obj->$addmethod( { $fpk => $fk_val } );
$obj->save;
-
+
# so next access reflects change.
$obj->forget_related($rel_name);
}
@@ -374,7 +374,7 @@
object_class => $meta->{map_class},
where => $query,
);
-
+
# so next access reflects change
$obj->forget_related($rel_name);
return $obj;
@@ -423,6 +423,13 @@
return $self->{treat_like_int};
}
+sub _join_with_table_prefix {
+ my ( $self, $q, $prefix ) = @_;
+ return join( ' ',
+ map { $prefix . '.' . $_->[0], $_->[1] }
+ map { [%$_] } @{ $q->{sort_order} } );
+}
+
sub make_query {
my $self = shift;
my $c = $self->context;
@@ -432,16 +439,18 @@
# many2many relationships always have two tables,
# and we are sorting my the 2nd one. The 1st one is the mapper.
if ( $c->req->params->{'cxc-m2m'} ) {
- unless ( $q->{sort_by} =~ m/t\d\./ ) {
- $q->{sort_by} = join( '.', 't2', $q->{sort_by} );
+ if ( length( $q->{sort_by} ) and !( $q->{sort_by} =~ m/t\d\./ ) ) {
+ $q->{sort_by} = $self->_join_with_table_prefix( $q, 't2' );
}
}
else {
- unless ( $q->{sort_by} =~ m/t\d\./ ) {
- $q->{sort_by} = join( '.', 't1', $q->{sort_by} );
+ if ( length( $q->{sort_by} ) and !( $q->{sort_by} =~ m/t\d\./ ) ) {
+ $q->{sort_by} = $self->_join_with_table_prefix( $q, 't1' );
}
}
+ #carp dump $q;
+
return $q;
}
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/01-rdbo.t
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/01-rdbo.t 2008-08-28 18:35:29 UTC (rev 8313)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/01-rdbo.t 2008-08-28 18:36:02 UTC (rev 8314)
@@ -1,4 +1,4 @@
-use Test::More tests => 14;
+use Test::More tests => 20;
BEGIN {
$ENV{CATALYST_DEBUG} = $ENV{PERL_DEBUG} || 0;
@@ -24,8 +24,7 @@
is( $res->headers->{status}, 200, "get 200" );
-ok( $res = request('/foo/1/bars/2/add'),
- "GET /foo/1/bars/2/add" );
+ok( $res = request('/foo/1/bars/2/add'), "GET /foo/1/bars/2/add" );
is( $res->headers->{status}, 400, "cannot GET add related" );
@@ -41,3 +40,48 @@
is( $res->headers->{status}, 204, "POST remove related OK" );
+ok( $res = request('/foo/search?id=1&cxc-order=id'),
+ "search id=1 with order" );
+
+is( $res->content, '{
+ limit => 50,
+ offset => 0,
+ plain_query => { id => [1] },
+ plain_query_str => "id = 1",
+ query => ["id", [1]],
+ sort_by => "t1.id ASC",
+ sort_order => [{ id => "ASC" }],
+}', "search query with order dir assumed"
+);
+
+#dump $res;
+
+ok( $res = request('/foo/search?id=1&cxc-sort=id&cxc-dir=desc'),
+ "search id=1 with sort/dir" );
+
+#dump $res;
+
+is( $res->content, '{
+ limit => 50,
+ offset => 0,
+ plain_query => { id => [1] },
+ plain_query_str => "id = 1",
+ query => ["id", [1]],
+ sort_by => "t1.id DESC",
+ sort_order => [{ id => "DESC" }],
+}', "search query with explicit order/dir"
+);
+
+ok( $res = request('/foo/search?id=1'), "search id=1 with no sort" );
+is( $res->content, '{
+ limit => 50,
+ offset => 0,
+ plain_query => { id => [1] },
+ plain_query_str => "id = 1",
+ query => ["id", [1]],
+ sort_by => "t1.id DESC",
+ sort_order => [{ id => "DESC" }],
+}', "search query with default PK order"
+);
+
+#dump $res;
Modified: CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/Foo.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/Foo.pm 2008-08-28 18:35:29 UTC (rev 8313)
+++ CatalystX-CRUD/CatalystX-CRUD-Model-RDBO/trunk/t/lib/MyApp/Controller/Foo.pm 2008-08-28 18:36:02 UTC (rev 8314)
@@ -42,6 +42,9 @@
sub end : Private {
my ( $self, $c ) = @_;
$c->log->debug( "resp status = " . $c->res->status ) if $c->debug;
+ if ( $c->stash->{results} ) {
+ $c->res->body( Data::Dump::dump( $c->stash->{results}->query ) );
+ }
1;
}
More information about the Catalyst-commits
mailing list