浏览 4827 次
锁定老帖子 主题:AST构建Hibernat动态查询
精华帖 (0) :: 良好帖 (8) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-05-25
java 代码
二、基本接口 java 代码
java 代码
java 代码
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-05-25
package com.ayufox.framework.core.dao.hqlx.ast; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.hql.antlr.SqlTokenTypes; import antlr.collections.AST; import com.ayufox.framework.core.Context; import com.ayufox.framework.core.common.CommonUtils; import com.ayufox.framework.core.dao.hql.Hql; import com.ayufox.framework.core.dao.hql.Parameter; /** * @author ray * */ public class HqlCompileExecutor implements SqlTokenTypes { private final static Log LOG = LogFactory.getLog(HqlCompileExecutor.class); private final static Map NODE_TYPE_MAP = new HashMap(); static { Field[] fields = SqlTokenTypes.class.getFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; int modifiers = field.getModifiers(); if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) { String name = field.getName(); try { Object value = field.get(null); NODE_TYPE_MAP.put(value, name); } catch (IllegalAccessException ex) { LOG.error("Imposible", ex); } } } } protected Hql hql; protected List<Parameter> parameters; protected Context context; private AST root; public HqlCompileExecutor(AST root, Context context) { init(root, context); } protected HqlCompileExecutor() { } protected void init(AST root, Context context) { this.context = context; this.root = root; this.parameters = new ArrayList<Parameter>(); } public Hql build() { if (this.hql == null) { String hql = buildHql(); if (LOG.isDebugEnabled()) { LOG.debug("hql:" + hql); for (int i = 0; i < this.parameters.size(); i++) { LOG.debug(" parameter[" + i + "]:name=[" + this.parameters.get(i).getName() + "],value=[" + this.parameters.get(i).getValue() + "]"); } } this.hql = new Hql(hql, this.parameters .toArray(new Parameter[this.parameters.size()])); } return this.hql; } protected String buildHql() { StringBuffer buffer = new StringBuffer(); print(buffer, this.root); return buffer.toString(); } protected void print(StringBuffer buffer, AST ast) { switch (ast.getType()) { case QUERY: //查询根节点 printChildren(buffer, ast, " "); break; case RANGE: //FROM根节点 printRANGE(buffer, ast); break; case IDENT: case ALIAS: case ROW_STAR: //* case DISTINCT: case ALL: case NUM_INT: case NUM_DOUBLE: case NUM_FLOAT: case NUM_LONG: printText(buffer, ast); break; case INNER: case OUTER: case FULL: case LEFT: case RIGHT: printText(buffer, ast); buffer.append(" join"); break; case JOIN: printJOIN(buffer, ast); break; case FROM: printSelfAndChildren(buffer, ast, " "); break; case SELECT: printSelfAndChildren(buffer, ast, ","); break; case SELECT_FROM: printSELECT_FROM(buffer, ast); break; case COUNT: case AGGREGATE: //聚合函数 printFunction(buffer, ast); break; case DOT: //. printDOT(buffer, ast); break; case AND: case OR: printLinkWord(buffer, ast); break; case WHERE: printWhere(buffer, ast); break; case CONSTRUCTOR: printCONSTRUCTOR(buffer, ast); break; case LIKE: case NOT_LIKE: case EQ: case GT: case GE: case LT: case LE: case IN: case NOT_IN: printCondition(buffer, ast); break; case BETWEEN: case NOT_BETWEEN: printBETWEEN(buffer, ast); break; case IN_LIST: //in printInList(buffer, ast); break; case COLON: //: printCOLON(buffer, ast); break; case ORDER: printORDER(buffer, ast); break; case GROUP: buffer.append("group by "); printChildren(buffer, ast, ","); break; case ASCENDING: case DESCENDING: printText(buffer, ast); break; default: LOG.warn("Incognizance node, type[" + NODE_TYPE_MAP.get(ast.getType()) + "(" + ast.getType() + ")] text[" + ast.getText() + "]"); } } protected void printCONSTRUCTOR(StringBuffer buffer, AST ast) { buffer.append("new "); AST constructor = ast.getFirstChild(); print(buffer, constructor); buffer.append('('); AST next = constructor.getNextSibling(); while (next != null) { if (buffer.charAt(buffer.length()-1) != '(') { buffer.append(','); } print(buffer, next); next = next.getNextSibling(); } buffer.append(')'); } protected void printORDER(StringBuffer buffer, AST ast) { buffer.append("order by "); AST child = ast.getFirstChild(); AST next = null; while (child != null) { print(buffer, child); next = child.getNextSibling(); if (next != null) { if (next.getType() == DESCENDING || next.getType() == ASCENDING) { buffer.append(" "); } else { buffer.append(","); } } child = next; } } protected void printRANGE(StringBuffer buffer, AST ast) { printChildren(buffer, ast, " "); AST next = ast.getNextSibling(); if (next != null && next.getType() == RANGE) { buffer.append(","); } } protected void printInList(StringBuffer buffer, AST ast) { buffer.append("("); printChildren(buffer, ast, ""); buffer.append(")"); } protected void printWhere(StringBuffer buffer, AST ast) { //where节点下只有一个子节点 int length = buffer.length(); AST child = ast.getFirstChild(); print(buffer, child); if (buffer.length() > length) { buffer.insert(length, ast.getText() + " "); } } /* * 连接词,即and和or */ protected void printLinkWord(StringBuffer buffer, AST ast) { int sourceLength = buffer.length(); AST left = ast.getFirstChild(); AST right = left.getNextSibling(); print(buffer, left); int middleLength = buffer.length(); print(buffer, right); int allLength = buffer.length(); //or/and的左表达式和右表达式都存在,需要加上括号和连接词 if ((allLength > middleLength) && (middleLength > sourceLength)) { buffer.insert(sourceLength, "("); buffer.insert(middleLength + 1, " " + ast.getText() + " "); buffer.append(")"); } } /* * : */ protected void printCOLON(StringBuffer buffer, AST ast) { int length = buffer.length(); printChildren(buffer, ast, ""); String name = buffer.substring(length, buffer.length()).replace('_', '.'); Object value = context.get(name); if (CommonUtils.isEmpty(value)) { throw new ValueNotExistException(name); } buffer.insert(length, ast.getText()); this.parameters.add(new Parameter(name, value)); } protected void printText(StringBuffer buffer, AST ast) { buffer.append(ast.getText()); } /* * 函数,如avg(..)、count(...)等 */ protected void printFunction(StringBuffer buffer, AST ast) { buffer.append(ast.getText()); buffer.append("("); printChildren(buffer, ast, " "); buffer.append(")"); } /* * . */ protected void printDOT(StringBuffer buffer, AST ast) { AST left = ast.getFirstChild(); print(buffer, left); buffer.append(ast.getText()); AST right = left.getNextSibling(); print(buffer, right); } /* * 打印所有子节点 * @param buffer 结果输出为止 * @param ast AST树 * @param join 子节点间连接符 */ protected void printChildren(StringBuffer buffer, AST ast, String join) { AST child = ast.getFirstChild(); while (child != null) { print(buffer, child); child = child.getNextSibling(); if (child != null) { buffer.append(join); } } } /* * 打印自身,然后再打印子节点 * @param buffer * @param ast * @param join * @param selfJoin */ protected void printSelfAndChildren(StringBuffer buffer, AST ast, String join) { buffer.append(ast.getText()); buffer.append(" "); printChildren(buffer, ast, join); } /* * join */ protected void printJOIN(StringBuffer buffer, AST ast) { int length = buffer.length(); printChildren(buffer, ast, " "); if (!buffer.substring(length, buffer.length()).contains(ast.getText())) { buffer.insert(length, "join "); } } protected void printSELECT_FROM(StringBuffer buffer, AST ast) { AST from = ast.getFirstChild(); AST select = from.getNextSibling(); if (select != null) { print(buffer, select); buffer.append(" "); } print(buffer, from); } protected void printBETWEEN(StringBuffer buffer, AST ast) { int length = buffer.length(); AST property = ast.getFirstChild(); AST first = property.getNextSibling(); AST second = first.getNextSibling(); try { print(buffer, property); buffer.append(" "); printText(buffer, ast); buffer.append(" "); print(buffer, first); buffer.append(" and "); print(buffer, second); } catch (ValueNotExistException e) { if (buffer.lastIndexOf(":") > length) { this.parameters.remove(this.parameters.size() - 1); } //将已输出的部分截断 buffer.setLength(length); } } /* * 双操作数条件式,譬如like、=、in等等 */ protected void printCondition(StringBuffer buffer, AST ast) { int length = buffer.length(); AST left = ast.getFirstChild(); print(buffer, left); buffer.append(" "); buffer.append(ast.getText()); buffer.append(" "); AST right = left.getNextSibling(); try { print(buffer, right); } catch (ValueNotExistException e) { //将已输出的部分截断 buffer.setLength(length); } } } |
|
返回顶楼 | |
发表时间:2007-05-27
这样还是要自己写hql语句,可不可以根据结果集合自动拼凑hql呢
|
|
返回顶楼 | |