package com.hcks.cmfds.commons.lang; import java.io.Serializable; /** * * 查询时的顺序 * * @author:liusha * @date:Feb 3, 2011 11:12:44 AM */ public class QueryOrder implements Serializable { private static final long serialVersionUID = 5291198501266064857L; /** * 是否升序 */ private boolean ascending; /** * 忽略排序 */ private boolean ignoreCase; public boolean isIgnoreCase() { return ignoreCase; } public void setIgnoreCase(boolean ignoreCase) { this.ignoreCase = ignoreCase; } /** * 属性名称 */ private String propertyName; /** * 返回字符串 */ public String toString(){ return propertyName + " " +(ascending ? "asc" : "desc"); } /** * 带参数的构造方法 * @param propertyName * @param ascending */ public QueryOrder(String propertyName,boolean ascending){ this.propertyName = propertyName; this.ascending = ascending; } /** * 带参数的构造方法 * @param propertyName * @param ascending */ public QueryOrder(String propertyName,String ascending){ this.propertyName = propertyName; if(null !=ascending) { if(ascending.equals("asc")) { this.ascending = true; }else if(ascending.equals("desc")) { this.ascending = false; } } } /** * 静态方法:返回升序的QueryOrder实例 * @param propertyName * @return */ public static QueryOrder asc(String propertyName){ return new QueryOrder(propertyName,true); } /** * 静态方法:返回降序的QueryOrder实例 * @param propertyName * @return */ public static QueryOrder desc(String propertyName){ return new QueryOrder(propertyName,false); } /** * 忽略排序方法 * @return */ public QueryOrder ignoreCase(){ ignoreCase = true; return this; } public void appendSqlOrder(StringBuilder sb){ if(sb == null) sb = new StringBuilder(); if(sb.toString().indexOf("order by") == -1){ sb.append(" order by "); } sb.append(" ").append(toString()); } public void appendSqlOrder(StringBuilder sb,boolean isSplit){ if(sb == null) sb = new StringBuilder(); if(sb.toString().indexOf("order by") == -1){ sb.append(" order by "); } if(isSplit) { sb.append(" ").append(toString()).append(","); }else { sb.append(" ").append(toString()); } } }