⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleexpression.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
字号:
//$Id: SimpleExpression.java,v 1.4.2.5 2003/10/27 11:46:58 oneovthafew Exp $
package net.sf.hibernate.expression;

import java.util.Map;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.engine.SessionFactoryImplementor;
import net.sf.hibernate.engine.TypedValue;
import net.sf.hibernate.util.StringHelper;

/**
 * superclass for "simple" comparisons (with SQL binary operators)
 * @author Gavin King
 */
public abstract class SimpleExpression extends AbstractCriterion {

	private final String propertyName;
	private final Object value;
	private boolean ignoreCase;
	
	SimpleExpression(String propertyName, Object value) {
		this.propertyName = propertyName;
		this.value = value;
	}
	
	SimpleExpression(String propertyName, Object value, boolean ignoreCase) {
		this.propertyName = propertyName;
		this.value = value;
		this.ignoreCase = ignoreCase;
	}
	
	public SimpleExpression ignoreCase() {
		ignoreCase = true;
		return this;
	}

	public String toSqlString(SessionFactoryImplementor sessionFactory, Class persistentClass, String alias, Map aliasClasses) 
	throws HibernateException {
		
		String[] columns = getColumns(sessionFactory, persistentClass, propertyName, alias, aliasClasses);
		if (ignoreCase) {
			if ( columns.length!=1 ) throw new HibernateException(
				"case insensitive expression may only be applied to single-column properties: " +
				propertyName
			);
			return new StringBuffer()
				.append( sessionFactory.getDialect().getLowercaseFunction() ) 
				.append(StringHelper.OPEN_PAREN)
				.append( columns[0] )
				.append(StringHelper.CLOSE_PAREN)
				.append( getOp() )
				.append("?")
				.toString();
		}
		else {
			String result = StringHelper.join(
				" and ", 
				StringHelper.suffix( columns, getOp() + "?" )
			);
			if (columns.length>1) result = StringHelper.OPEN_PAREN + result + StringHelper.CLOSE_PAREN;
			return result;
		}
		
		//TODO: get SQL rendering out of this package!
	}
	
	public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory, Class persistentClass, Map aliasClasses) 
	throws HibernateException {
		Object icvalue = ignoreCase ? value.toString().toLowerCase() : value;
		return new TypedValue[] { getTypedValue(sessionFactory, persistentClass, propertyName, icvalue, aliasClasses) };
	}

	public String toString() {
		return propertyName + getOp() + value;
	}
	
	abstract String getOp();

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -