From 5526c9d823f46377deac0b50f03fd7d3a5e04f50 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 29 Nov 2006 02:40:45 +0000 Subject: [PATCH] The plot thickens. OrderBy wasn't handling multiple columns properly, which requires individual ordering instructions for each column. So we've modified it to do so which required some signature changes and renders the static methods only really appropriate for a single column (which is what you want 90% of the time anyway). --- .../samskivert/jdbc/depot/clause/OrderBy.java | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/clause/OrderBy.java b/src/java/com/samskivert/jdbc/depot/clause/OrderBy.java index 37a4740..7ddc391 100644 --- a/src/java/com/samskivert/jdbc/depot/clause/OrderBy.java +++ b/src/java/com/samskivert/jdbc/depot/clause/OrderBy.java @@ -25,6 +25,7 @@ import java.sql.SQLException; import java.util.Collection; import com.samskivert.jdbc.depot.Query; +import com.samskivert.jdbc.depot.expression.ColumnExp; import com.samskivert.jdbc.depot.expression.SQLExpression; /** @@ -33,29 +34,45 @@ import com.samskivert.jdbc.depot.expression.SQLExpression; public class OrderBy implements QueryClause { + /** Indicates the order of the clause. */ + public enum Order { ASC, DESC }; + /** - * Creates and returns an ascending order by clause on the supplied expressions. + * Creates and returns an ascending order by clause on the supplied column. */ - public static OrderBy ascending (SQLExpression... values) + public static OrderBy ascending (String column) { - OrderBy clause = new OrderBy(values); - clause._ascending = true; - return clause; + return ascending(new ColumnExp(null, column)); } /** - * Creates and returns a descending order by clause on the supplied expressions. + * Creates and returns a descending order by clause on the supplied column. */ - public static OrderBy descending (SQLExpression... values) + public static OrderBy descending (String column) { - OrderBy clause = new OrderBy(values); - clause._ascending = false; - return clause; + return descending(new ColumnExp(null, column)); } - public OrderBy (SQLExpression... values) + /** + * Creates and returns an ascending order by clause on the supplied expression. + */ + public static OrderBy ascending (SQLExpression value) + { + return new OrderBy(new SQLExpression[] { value } , new Order[] { Order.ASC }); + } + + /** + * Creates and returns a descending order by clause on the supplied expression. + */ + public static OrderBy descending (SQLExpression value) + { + return new OrderBy(new SQLExpression[] { value }, new Order[] { Order.DESC }); + } + + public OrderBy (SQLExpression[] values, Order[] orders) { _values = values; + _orders = orders; } // from QueryClause @@ -73,8 +90,8 @@ public class OrderBy builder.append(", "); } _values[ii].appendExpression(query, builder); + builder.append(" ").append(_orders[ii]); } - builder.append(_ascending ? " ASC" : " DESC"); } // from QueryClause @@ -90,6 +107,6 @@ public class OrderBy /** The expressions that are generated for the clause. */ protected SQLExpression[] _values; - /** Whether the ordering is to be ascending rather than descending. */ - protected boolean _ascending; + /** Whether the ordering is to be ascending or descending. */ + protected Order[] _orders; }