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

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

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

import java.util.ArrayList;

import com.github.antelopeframework.mybatis.util.StringHelper;

import lombok.Getter;

/**
 * Constrains the property to a specified list of values
 *
 * @author yangzhi.yzh
 */
@Getter
public class InExpression implements Criterion {
	private static final long serialVersionUID = 1L;
	
	private final String column;
	private final Object[] values;

	/**
	 * Constructs an InExpression
	 *
	 * @param column The property name to check
	 * @param values The values to check against
	 *
	 * @see Restrictions#in(String, java.util.Collection)
	 * @see Restrictions#in(String, Object[])
	 */
	protected InExpression(String column, Object[] values) {
		this.column = column;
		this.values = values;
	}

	@Override
	public String toSqlString(CriteriaQuery criteriaQuery) {
		final String singleValueParam = "?";
		final String params = values.length > 0 ? StringHelper.repeat(singleValueParam + ", ", values.length - 1 ) + singleValueParam : "";
		
		return column + " in (" + params + ')';
	}

	@Override
	public TypedValue[] getTypedValues(CriteriaQuery criteriaQuery) {
		final ArrayList list = new ArrayList();
		for (Object value : values) {
			list.add(new TypedValue(null, value));
		}
		
		return list.toArray(new TypedValue[list.size()]);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy