HHH-4943 ilike support is incomplete

This commit is contained in:
Strong Liu 2011-06-06 16:42:13 +08:00
parent c6db5cfdcb
commit 1320208baf
6 changed files with 61 additions and 21 deletions

View File

@ -23,6 +23,7 @@
*
*/
package org.hibernate.criterion;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
@ -31,8 +32,10 @@ import org.hibernate.engine.spi.TypedValue;
/**
* A case-insensitive "like"
*
* @author Gavin King
*/
@Deprecated
public class IlikeExpression implements Criterion {
private final String propertyName;
@ -44,14 +47,16 @@ public class IlikeExpression implements Criterion {
}
protected IlikeExpression(String propertyName, String value, MatchMode matchMode) {
this( propertyName, matchMode.toMatchString(value) );
this( propertyName, matchMode.toMatchString( value ) );
}
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
throws HibernateException {
Dialect dialect = criteriaQuery.getFactory().getDialect();
String[] columns = criteriaQuery.findColumns(propertyName, criteria);
if (columns.length!=1) throw new HibernateException("ilike may only be used with single-column properties");
String[] columns = criteriaQuery.findColumns( propertyName, criteria );
if ( columns.length != 1 ) {
throw new HibernateException( "ilike may only be used with single-column properties" );
}
if ( dialect instanceof PostgreSQLDialect ) {
return columns[0] + " ilike ?";
}
@ -63,8 +68,14 @@ public class IlikeExpression implements Criterion {
}
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return new TypedValue[] { criteriaQuery.getTypedValue( criteria, propertyName, value.toString().toLowerCase() ) };
throws HibernateException {
return new TypedValue[] {
criteriaQuery.getTypedValue(
criteria,
propertyName,
value.toString().toLowerCase()
)
};
}
public String toString() {

View File

@ -81,11 +81,19 @@ public class LikeExpression implements Criterion {
if ( columns.length != 1 ) {
throw new HibernateException( "Like may only be used with single-column properties" );
}
String lhs = ignoreCase
? dialect.getLowercaseFunction() + '(' + columns[0] + ')'
: columns[0];
return lhs + " like ?" + ( escapeChar == null ? "" : " escape \'" + escapeChar + "\'" );
String escape = escapeChar == null ? "" : " escape \'" + escapeChar + "\'";
String column = columns[0];
if ( ignoreCase ) {
if ( dialect.supportsCaseInsensitiveLike() ) {
return column +" " + dialect.getCaseInsensitiveLike() + " ?" + escape;
}
else {
return dialect.getLowercaseFunction() + '(' + column + ')' + " like ?" + escape;
}
}
else {
return column + " like ?" + escape;
}
}
public TypedValue[] getTypedValues(

View File

@ -47,7 +47,6 @@ public class Restrictions {
/**
* Apply an "equal" constraint to the identifier property
* @param propertyName
* @param value
* @return Criterion
*/
@ -99,7 +98,7 @@ public class Restrictions {
* @return Criterion
*/
public static Criterion ilike(String propertyName, String value, MatchMode matchMode) {
return new IlikeExpression(propertyName, value, matchMode);
return new LikeExpression(propertyName, value, matchMode, null, true);
}
/**
* A case-insensitive "like", similar to Postgres <tt>ilike</tt>
@ -110,7 +109,7 @@ public class Restrictions {
* @return Criterion
*/
public static Criterion ilike(String propertyName, Object value) {
return new IlikeExpression(propertyName, value);
return new LikeExpression(propertyName, value.toString());
}
/**
* Apply a "greater than" constraint to the named property

View File

@ -1606,6 +1606,21 @@ public abstract class Dialect {
return "lower";
}
/**
* The name of the SQL function that can do case insensitive <b>like</b> comparison.
* @return The dialect-specific "case insensitive" like function.
*/
public String getCaseInsensitiveLike(){
return "like";
}
/**
* Does the underlying Database supports case insensitive like comparison.
*/
public boolean supportsCaseInsensitiveLike(){
return false;
}
/**
* Meant as a means for end users to affect the select strings being sent
* to the database and perhaps manipulate them in some fashion.

View File

@ -214,7 +214,7 @@ public class PostgreSQLDialect extends Dialect {
}
public String getLimitString(String sql, boolean hasOffset) {
return new StringBuffer( sql.length()+20 )
return new StringBuilder( sql.length()+20 )
.append( sql )
.append( hasOffset ? " limit ? offset ?" : " limit ?" )
.toString();
@ -233,7 +233,7 @@ public class PostgreSQLDialect extends Dialect {
}
public String getIdentitySelectString(String table, String column, int type) {
return new StringBuffer().append("select currval('")
return new StringBuilder().append("select currval('")
.append(table)
.append('_')
.append(column)
@ -255,6 +255,15 @@ public class PostgreSQLDialect extends Dialect {
return "default values";
}
public String getCaseInsensitiveLike(){
return "ilike";
}
@Override
public boolean supportsCaseInsensitiveLike() {
return true;
}
public Class getNativeIdentifierGeneratorClass() {
return SequenceGenerator.class;
}
@ -360,15 +369,13 @@ public class PostgreSQLDialect extends Dialect {
public int registerResultSetOutParameter(CallableStatement statement, int col) throws SQLException {
// Register the type of the out param - PostgreSQL uses Types.OTHER
statement.registerOutParameter(col, Types.OTHER);
col++;
statement.registerOutParameter(col++, Types.OTHER);
return col;
}
public ResultSet getResultSet(CallableStatement ps) throws SQLException {
ps.execute();
ResultSet rs = (ResultSet) ps.getObject(1);
return rs;
return (ResultSet) ps.getObject(1);
}
public boolean supportsPooledSequences() {

View File

@ -108,7 +108,7 @@ public class StandardSQLFunction implements SQLFunction {
* {@inheritDoc}
*/
public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append( name ).append( '(' );
for ( int i = 0; i < arguments.size(); i++ ) {
buf.append( arguments.get( i ) );