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

com.github.antelopeframework.mybatis.criterion.LikeExpression 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 java.util.Locale;

import lombok.Getter;

/**
 * A criterion representing a "like" expression
 *
 * @author yangzhi.yzh
 */
@Getter
public class LikeExpression implements Criterion {
	private static final long serialVersionUID = 1L;

	private final String column;
	private final Object value;
	private final Character escapeChar;
	private final boolean ignoreCase;

	protected LikeExpression(String column, String value, Character escapeChar, boolean ignoreCase) {
		this.column = column;
		this.value = value;
		this.escapeChar = escapeChar;
		this.ignoreCase = ignoreCase;
	}

	protected LikeExpression(String column, String value) {
		this(column, value, null, false);
	}

	protected LikeExpression(String column, String value, MatchMode matchMode) {
		this(column, matchMode.toMatchString(value));
	}

	protected LikeExpression(String column, String value, MatchMode matchMode, Character escapeChar, boolean ignoreCase) {
		this(column, matchMode.toMatchString(value), escapeChar, ignoreCase);
	}

	@Override
	public String toSqlString(CriteriaQuery criteriaQuery) {
		final String escape = escapeChar == null ? "" : " escape \'" + escapeChar + "\'";

		if (ignoreCase) {
			return "lower(" + column + ')' + " like ?" + escape;
		} else {
			return column + " like ?" + escape;
		}
	}

	@Override
	public TypedValue[] getTypedValues(CriteriaQuery criteriaQuery) {
		final String matchValue = ignoreCase ? value.toString().toLowerCase(Locale.ROOT) : value.toString();
		return new TypedValue[] { new TypedValue(Types.VARCHAR, matchValue) };
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy