From: Ash Berlin <ash_github@firemirror.com>
Date: Mon, 2 Mar 2009 19:12:46 +0000 (+0000)
Subject: A few simple order by tests. Not sure i'm happy with the waht of doing asc/desc
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=73382d73662f8af9f6774a4f3a86ed9d453e2a2d;p=dbsrgits%2FSQL-Abstract-2.0-ish.git

A few simple order by tests. Not sure i'm happy with the waht of doing asc/desc
---

diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm
index 7b18c04..e835c6e 100644
--- a/lib/SQL/Abstract.pm
+++ b/lib/SQL/Abstract.pm
@@ -67,7 +67,7 @@ class SQL::Abstract {
     $self = $self->new unless blessed($self);
 
     local $_ = $ast->[0];
-    s/^-/_/ or croak "Unknown type tag '$_'";
+    s/^-/_/g or croak "Unknown type tag '$_'";
     my $meth = $self->can($_) || \&_generic_func;
     return $meth->($self, $ast);
   }
@@ -76,6 +76,29 @@ class SQL::Abstract {
     
   }
 
+  method _where(ArrayRef $ast) {
+    my (undef, @clauses) = @$ast;
+  
+    return 'WHERE ' . $self->_recurse_where(\@clauses);
+  }
+
+  method _order_by(ArrayRef $ast) {
+    my (undef, @clauses) = @$ast;
+
+    my @output;
+   
+    for (@clauses) {
+      if ($_->[0] =~ /^-(asc|desc)$/) {
+        my $o = $1;
+        push @output, $self->generate($_->[1]) . " " . uc($o);
+        next;
+      }
+      push @output, $self->generate($_);
+    }
+
+    return "ORDER BY " . join(", ", @output);
+  }
+
   method _name(ArrayRef $ast) {
     my (undef, @names) = @$ast;
 
@@ -111,12 +134,6 @@ class SQL::Abstract {
     return "?";
   }
 
-  method _where(ArrayRef $ast) {
-    my (undef, @clauses) = @$ast;
-  
-    return 'WHERE ' . $self->_recurse_where(\@clauses);
-  }
-
   method _recurse_where($clauses) {
 
     my $OP = 'AND';
diff --git a/t/001_basic.t b/t/001_basic.t
index 693b2fc..d3aecc3 100644
--- a/t/001_basic.t
+++ b/t/001_basic.t
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 11;
+use Test::More tests => 14;
 
 use_ok('SQL::Abstract') or BAIL_OUT( "$@" );
 
@@ -23,6 +23,22 @@ is SQL::Abstract->generate(
   "Alias generator";
 
 is SQL::Abstract->generate(
+  [ -order_by => [ -name => qw/me date/ ] ]
+), "ORDER BY me.date";
+
+is SQL::Abstract->generate(
+  [ -order_by => 
+    [ -name => qw/me date/ ],
+    [ -name => qw/me foobar/ ],
+  ]
+), "ORDER BY me.date, me.foobar";
+
+is SQL::Abstract->generate(
+  [ -order_by => [ -desc => [ -name => qw/me date/ ] ] ]
+), "ORDER BY me.date DESC";
+
+
+is SQL::Abstract->generate(
   [ -where =>
       [ '>', [-name => qw/me id/], [-value => 500 ] ]
   ]