Thursday, August 12, 2010

Custom BIT functions in HQL


public class MySQLDialect extends MySQLInnoDBDialect {

public MySQLDialect() {
super();
registerFunction("bit_or", new StandardSQLFunction("BIT_OR", Hibernate.INTEGER));
registerFunction("bitwise_and", new VarArgsSQLFunction(Hibernate.INTEGER, "", "&", ""));
registerFunction("bitwise_or", new VarArgsSQLFunction(Hibernate.INTEGER, "", "|", ""));
}
}

String queryString = "select bit_or(cc.protocol) as protocolBitmask "
+ "from CtlCountry cc, ApHardwareTypeProfile atp, ApProfileRadio radio "
+ "where cc.platformName=atp.ctlPlatformName "
+ "and atp.profileType=:profileType "
+ "and atp.profileType=radio.profile "
+ "and cc.countryCode=:country "
+ "and radio.radioIndex=:radioIndex "
+ "and bitwise_and(cc.protocol, radio.radioCap) = cc.protocol";

MySQL's BIT_OR(), BIT_AND() operators are like COUNT(), AVG(), they apply bit operations on all matched rows. Bitwise operators &, | apply on expression.

A strange "java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode" error occurred when I used upper case function name "BIT_OR" in HQL. After I changed it to use lower case "bit_or", it works.

3 comments:

skb said...
This comment has been removed by the author.
skb said...
This comment has been removed by the author.
skb said...
This comment has been removed by the author.