All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.antelopeframework.mybatis.criterion.SimpleExpression Maven / Gradle / Ivy

There is a newer version: 1.1.5
Show newest version
package com.github.antelopeframework.mybatis.criterion;


import java.sql.Types;

import lombok.Getter;

/**
 * superclass for "simple" comparisons (with SQL binary operators)
 * 
 * @author yangzhi.yzh
 */
@Getter
public class SimpleExpression implements Criterion {
	private static final long serialVersionUID = 1L;
	
	private final String column;
	private final int sqlType;
	private final Object value;
	private boolean ignoreCase;
	private final String op;
	
	protected SimpleExpression(String column, Object value, String op) {
		this.column = column;
		this.value = value;
		this.op = op;
		this.sqlType = sqlType(value);
	}

	protected SimpleExpression(String propertyName, Object value, String op, boolean ignoreCase) {
		this.column = propertyName;
		this.value = value;
		this.ignoreCase = ignoreCase;
		this.op = op;
		
		this.sqlType = sqlType(value);
	}
	
	private static int sqlType(Object value) {
		int sqlType = Types.OTHER;
		
		if (value instanceof Character) {
			sqlType = Types.CHAR;
		} else if (value instanceof String) {
			sqlType = Types.VARCHAR;
		}
		
		return sqlType;
	}
	
	/**
	 * Make case insensitive.  No effect for non-String values
	 *
	 * @return {@code this}, for method chaining
	 */
	public SimpleExpression ignoreCase() {
		ignoreCase = true;
		return this;
	}

	@Override
	public String toSqlString(CriteriaQuery criteriaQuery) {
		final StringBuilder fragment = new StringBuilder();
		final boolean lower = ignoreCase && sqlType == Types.VARCHAR || sqlType == Types.CHAR;

		if (lower) {
			fragment.append("lower").append('(');
		}

		fragment.append(column);
		if (lower) {
			fragment.append(')');
		}

		fragment.append(getOp()).append("?");
		

		return fragment.toString();
	}

	@Override
	public TypedValue[] getTypedValues(CriteriaQuery criteriaQuery) {
		return new TypedValue[]{new TypedValue(null, value)};
	}
	
	@Override
	public String toString() {
		return column + getOp() + value;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy