Hi there,
Thanks for the tip, Gavin, I've got it working now after having some trouble realising the Hql parser wouldn't merely let me substitute a function name for the offending '&' character. I had to pass both the SQL column name and the comparison value as arguments to my new SQLFunction.
Code:
/**
* @author Michael Guyver
*/
public class MySQLDialect extends org.hibernate.dialect.MySQLDialect {
public MySQLDialect() {
super();
registerFunction("bitwise_and", new MySQLBitwiseAndSQLFunction("bitwise_and", Hibernate.INTEGER));
}
}
Code:
import java.util.List;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.Type;
/**
* @author Michael Guyver
*/
public class MySQLBitwiseAndSQLFunction extends StandardSQLFunction
implements SQLFunction {
public MySQLBitwiseAndSQLFunction(String name) {
super(name);
}
public MySQLBitwiseAndSQLFunction(String name, Type typeValue) {
super(name, typeValue);
}
public String render(List args) {
if (args.size() != 2){
throw new IllegalArgumentException("the function must be passed 2 arguments");
}
StringBuffer buffer = new StringBuffer(args.get(0).toString());
buffer.append(" & ").append(args.get(1));
return buffer.toString();
}
}
Once you've instructed Hibernate to use your new dialect you could write the following HQL, where 'listType' is a parameter:
Code:
from PlayerGroup as pg where bitwise_and('pg.groupType', :listType) > 0 order by pg.listOrder
This should generate some SQL which includes:
Code:
... where pg.groupType & 3 > 0 order by ...
where the parameter value was 3. Hope this helps anyone with a similar dilemma.
Mike
kenevel@hotmail.com