Added two new Order constants: ASC_NULLS_FIRST and DESC_NULLS_LAST.

Only supported on postgres. If needed we could allow for fallbacks
for other databases.
This commit is contained in:
Ray Greenwell
2011-12-03 22:06:08 +00:00
parent fb0a5f2884
commit acbdfc4044
3 changed files with 42 additions and 2 deletions
@@ -18,7 +18,26 @@ import com.samskivert.depot.impl.expression.LiteralExp;
public class OrderBy implements QueryClause
{
/** Indicates the order of the clause. */
public enum Order { ASC, DESC }
public enum Order
{
/** Ascending (nulls last). */
ASC,
/** Descending (nulls first). */
DESC,
/** Ascending; nulls first (postgres only). */
ASC_NULLS_FIRST,
/** Descending; nulls last (postgres only). */
DESC_NULLS_LAST;
@Override
public String toString ()
{
return name().replace('_', ' ');
}
}
/**
* Creates and returns a random order by clause.
@@ -272,7 +272,7 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
_builder.append(", ");
}
values[ii].accept(this);
_builder.append(" ").append(orders[ii]);
_builder.append(" ").append(validateOrder(orders[ii]));
}
return null;
}
@@ -974,6 +974,22 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
_builder.append(")");
}
/**
* Validate/massage the Order value as supported by the underlying database.
*/
protected OrderBy.Order validateOrder (OrderBy.Order order)
{
if ((order == OrderBy.Order.ASC_NULLS_FIRST) || (order == OrderBy.Order.DESC_NULLS_LAST)) {
if (false /*Boolean.getBoolean("com.samskivert.depot.massageOrderByNulls")*/) {
order = (order == OrderBy.Order.ASC_NULLS_FIRST)
? OrderBy.Order.ASC : OrderBy.Order.DESC;
} else {
throw new RuntimeException(order + " is not supported on your database.");
}
}
return order;
}
protected BuildVisitor (DepotTypes types, boolean allowComplexIndices)
{
_types = types;
@@ -21,6 +21,7 @@ import com.samskivert.depot.Exps;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.annotation.FullTextIndex.Configuration;
import com.samskivert.depot.annotation.FullTextIndex;
import com.samskivert.depot.clause.OrderBy;
import com.samskivert.depot.impl.expression.IntervalExp;
import com.samskivert.depot.impl.expression.DateFun.DatePart;
import com.samskivert.depot.impl.expression.DateFun.DateTruncate;
@@ -118,6 +119,10 @@ public class PostgreSQLBuilder
_builder.append("\"").append(field).append("\"");
}
@Override protected OrderBy.Order validateOrder (OrderBy.Order order) {
return order;
}
protected PGBuildVisitor (DepotTypes types)
{
super(types, true);