一、效果
二、基本接口
三、实现
java 代码
- public class HqlCompilerImplTest extends TestCase
- {
- private IHqlCompiler compiler = new HqlCompilerImpl();
- public void test1()
- {
- Hql hql = compiler.compile("from User u where u.username = :username and u.age = :age order by u.username,u.age",
- new MapContext());
- assertEquals("from User u order by u.username,u.age", hql.getHql());
- assertEquals(0, hql.getParameters().length);
- }
- public void test2)
- {
- MapContext context = new MapContext();
- context.add("username", "ray");
- Hql hql = compiler
- .compile(
- "from User u where u.username = :username and u.age = :age order by u.username,u.age",
- context);
- assertEquals(
- "from User u where u.username = :username order by u.username,u.age",
- hql.getHql());
- assertEquals(1, hql.getParameters().length);
- assertEquals("username", hql.getParameters()[0].getName());
- assertEquals("ray", hql.getParameters()[0].getValue());
- }
- public void test3()
- {
- MapContext context = new MapContext();
- context.add("username", "ray");
- context.add("age", new Integer(10));
- Hql hql = compiler
- .compile(
- "from User u where u.username = :username and u.age = :age order by u.username,u.age",
- context);
- assertEquals(
- "from User u where (u.username = :username and u.age = :age) order by u.username,u.age",
- hql.getHql());
- assertEquals(2, hql.getParameters().length);
- assertEquals("username", hql.getParameters()[0].getName());
- assertEquals("ray", hql.getParameters()[0].getValue());
- assertEquals("age", hql.getParameters()[1].getName());
- assertEquals(new Integer(10), hql.getParameters()[1].getValue());
- }
- }
二、基本接口
java 代码
- package com.ayufox.framework.core;
- /**
- * @author ray
- *
- */
- public interface Context
- {
- Object get(String name);
- }
java 代码
- package com.ayufox.framework.core.dao.hqlx;
- import com.ayufox.framework.core.Context;
- import com.ayufox.framework.core.dao.hql.Hql;
- /**
- * @author ray
- *
- */
- public interface IHqlCompiler
- {
- Hql compile(String hql, Object ... values);
- Hql compile(String hql, Context context);
- }
java 代码
- package com.ayufox.framework.core.dao.hqlx.ast;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- import java.util.Collections;
- import java.util.HashMap;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.hibernate.hql.ast.HqlParser;
- import antlr.RecognitionException;
- import antlr.TokenStreamException;
- import antlr.collections.AST;
- import com.ayufox.framework.core.Context;
- import com.ayufox.framework.core.cache.Cache;
- import com.ayufox.framework.core.cache.MapCache;
- import com.ayufox.framework.core.dao.hql.Hql;
- import com.ayufox.framework.core.dao.hqlx.DynamicMapContext;
- import com.ayufox.framework.core.dao.hqlx.IHqlCompiler;
- import com.ayufox.framework.core.dao.hqlx.IllegalSyntaxException;
- /**
- * @author ray
- *
- */
- public class HqlCompilerImpl implements IHqlCompiler
- {
- private final static Log LOG = LogFactory.getLog(HqlCompilerImpl.class);
- private Cache astCache = new MapCache(Collections
- .synchronizedMap(new HashMap()));
- public void setAstCache(Cache astCache)
- {
- this.astCache = astCache;
- }
- /* (non-Javadoc)
- * @see com.konceptusa.framework.core.dao.hqlx.IHqlCompiler#compile(java.lang.String, java.lang.Object[])
- */
- public Hql compile(String hql, Object... values)
- {
- return compile(hql, new DynamicMapContext(values));
- }
- /* (non-Javadoc)
- * @see com.konceptusa.framework.core.dao.hqlx.IHqlCompiler#compile(java.lang.String, com.konceptusa.framework.core.Context)
- */
- public Hql compile(String hql, Context context)
- {
- if (hql == null || context == null)
- {
- throw new IllegalArgumentException("hql or context can't be null");
- }
- AST ast = getRootAST(hql);
- HqlCompileExecutor compilerContext = new HqlCompileExecutor(ast,
- context);
- return compilerContext.build();
- }
- protected AST getRootAST(String hql)
- {
- AST ast = (AST) this.astCache.get(hql);
- if (ast == null)
- {
- ast = createAST(hql);
- this.astCache.put(hql, ast);
- if (LOG.isDebugEnabled())
- {
- LOG.debug("get ast[" + ast + "] from cache for hql[" + hql
- + "]");
- }
- }
- return ast;
- }
- private AST createAST(String hql)
- {
- HqlParser parser = HqlParser.getInstance(hql);
- try
- {
- parser.statement();
- }
- catch (RecognitionException e)
- {
- throw new IllegalSyntaxException(e);
- }
- catch (TokenStreamException e)
- {
- throw new IllegalSyntaxException(e);
- }
- AST ast = parser.getAST();
- parser.getParseErrorHandler().throwQueryException();
- if (LOG.isDebugEnabled())
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- parser.showAst(ast, new PrintStream(baos));
- LOG.debug("AST:" + new String(baos.toByteArray()));
- }
- return ast;
- }
- }