|
I need this function too (masking permission and quering for permitted objects in one shot). it can be standardized through the dialect just like Gavin wrote. I vote for one standard naming convention for bitwise functions in Hibernate distribution standard dialects. import java.util.List; /**
public static class SQLFunctionTemplates implements SQLFunction { private SQLFunctionTemplate templateNarg; public SQLFunctionTemplates(Type type, String template1arg, String templateNarg) { this.template1arg = new SQLFunctionTemplate(type, template1arg); this.templateNarg = new SQLFunctionTemplate(type, templateNarg); } private SQLFunctionTemplate getSQLFunctionTemplate(List args) { if (args.size() == 1) return template1arg; return templateNarg; } public Type getReturnType(Type columnType, Mapping mapping) throws QueryException { return template1arg.getReturnType(columnType, mapping); } public boolean hasArguments() {
return true;
} public String render(List args, SessionFactoryImplementor factory) throws QueryException {
return getSQLFunctionTemplate(args).render(args, factory);
} Sorry, error in bit_or function. registerFunction("bit_or", new SQLFunctionTemplates(Hibernate.INTEGER, "bit_or(?1)", "?1 | ?2 | ?3 | ?4")); To me it makes sense to include the bitwise operators as part of the syntax, rather than as functions, because then the correct operator precidence is available. BTW: the registerFunction line for bit_and and bit_or can take advantage of org.hibernate.dialect.function.VarArgsSQLFunction. Using this you can get rid of the inner class, and just do the following in the constructor: registerFunction("bit_not", new SQLFunctionTemplate(Hibernate.INTEGER, "~?1")); Note the additional surrounding brackets, this ensures the function-like behaviour rather than using the DB's operator precidence. |
|||||||||||||||||||||||||||||||||||||||||
A workaround is to define a SQLFunctionTemplate on a Dialect subclass