![JAR search and dependency download from the Maven repository](/logo.png)
com.github.antelopeframework.mybatis.criterion.Order Maven / Gradle / Ivy
package com.github.antelopeframework.mybatis.criterion;
import java.io.Serializable;
import java.sql.Types;
import lombok.Getter;
@Getter
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
private boolean ascending;
private boolean ignoreCase;
private String column;
private int sqlType;
/**
* Ascending order
*
* @param column The property to order on
* @param sqlType {@link java.sql.Types}
*
* @return The build Order instance
*/
public static Order asc(String column, int sqlType) {
return new Order(column, sqlType, true);
}
/**
* Descending order.
*
* @param column The property to order on
* @param sqlType {@link java.sql.Types}
*
* @return The build Order instance
*/
public static Order desc(String column, int sqlType) {
return new Order(column, sqlType, false);
}
/**
* Constructor for Order. Order instances are generally created by factory
* methods.
*
* @see #asc
* @see #desc
*/
protected Order(String column, int sqlType, boolean ascending) {
this.column = column;
this.sqlType = sqlType;
this.ascending = ascending;
}
/**
* Should this ordering ignore case? Has no effect on non-character
* properties.
*
* @return {@code this}, for method chaining
*/
public Order ignoreCase() {
ignoreCase = true;
return this;
}
/**
* Render the SQL fragment
*
* @param criteria The criteria
* @param criteriaQuery The overall query
*
* @return The ORDER BY fragment for this ordering
*/
public String toSqlString(CriteriaQuery criteriaQuery) {
final StringBuilder fragment = new StringBuilder();
final StringBuilder expression = new StringBuilder();
boolean lower = false;
if (ignoreCase) {
lower = sqlType == Types.VARCHAR || sqlType == Types.CHAR || sqlType == Types.LONGVARCHAR;
}
if (lower) {
expression.append("lower").append('(');
}
expression.append(column);
if (lower) {
expression.append(')');
}
fragment.append(renderOrderByElement(expression.toString(), null, ascending ? "asc" : "desc"));
return fragment.toString();
}
public String renderOrderByElement(String expression, String collation, String order) {
final StringBuilder orderByElement = new StringBuilder( expression );
if ( collation != null ) {
orderByElement.append( " " ).append( collation );
}
if ( order != null ) {
orderByElement.append( " " ).append( order );
}
return orderByElement.toString();
}
@Override
public String toString() {
return column + ' ' + ( ascending ? "asc" : "desc" );
}
// public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) {
// final StringBuilder orderByElement = new StringBuilder( expression );
// if ( collation != null ) {
// orderByElement.append( " " ).append( collation );
// }
// if ( order != null ) {
// orderByElement.append( " " ).append( order );
// }
// if ( nulls != NullPrecedence.NONE ) {
// orderByElement.append( " nulls " ).append( nulls.name().toLowerCase(Locale.ROOT) );
// }
//
// return orderByElement.toString();
// }
//
// @Override
// public String toString() {
// return column + ' ' + ( ascending ? "asc" : "desc" ) + ( nullPrecedence != null ? ' ' + nullPrecedence.name().toLowerCase(Locale.ROOT) : "" );
// }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy