KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > hql > ast > exec > BasicExecutor


1 // $Id: BasicExecutor.java,v 1.2 2005/07/16 22:25:42 oneovthafew Exp $
2
package org.hibernate.hql.ast.exec;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import org.hibernate.HibernateException;
9 import org.hibernate.engine.QueryParameters;
10 import org.hibernate.engine.RowSelection;
11 import org.hibernate.engine.SessionImplementor;
12 import org.hibernate.exception.JDBCExceptionHelper;
13 import org.hibernate.hql.ast.HqlSqlWalker;
14 import org.hibernate.hql.ast.QuerySyntaxException;
15 import org.hibernate.hql.ast.SqlGenerator;
16 import org.hibernate.param.ParameterSpecification;
17 import org.hibernate.persister.entity.Queryable;
18
19 import antlr.RecognitionException;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 /**
25  * Implementation of BasicExecutor.
26  *
27  * @author Steve Ebersole
28  */

29 public class BasicExecutor extends AbstractStatementExecutor {
30     private static final Log log = LogFactory.getLog( BasicExecutor.class );
31
32     private final Queryable persister;
33     private final String JavaDoc sql;
34
35     public BasicExecutor(HqlSqlWalker walker, Queryable persister) {
36         super( walker, log );
37         this.persister = persister;
38         try {
39             SqlGenerator gen = new SqlGenerator( getFactory() );
40             gen.statement( walker.getAST() );
41             sql = gen.getSQL();
42             gen.getParseErrorHandler().throwQueryException();
43         }
44         catch ( RecognitionException e ) {
45             throw new QuerySyntaxException( e );
46         }
47     }
48
49     public int execute(QueryParameters parameters, SessionImplementor session) throws HibernateException {
50
51         coordinateSharedCacheCleanup( session );
52
53         PreparedStatement JavaDoc st = null;
54         RowSelection selection = parameters.getRowSelection();
55
56         try {
57             try {
58                 st = session.getBatcher().prepareStatement( sql );
59                 Iterator JavaDoc paramSpecifications = getWalker().getParameters().iterator();
60                 int pos = 1;
61                 while ( paramSpecifications.hasNext() ) {
62                     final ParameterSpecification paramSpec = ( ParameterSpecification ) paramSpecifications.next();
63                     pos += paramSpec.bind( st, parameters, session, pos );
64                 }
65                 if ( selection != null ) {
66                     if ( selection.getTimeout() != null ) {
67                         st.setQueryTimeout( selection.getTimeout().intValue() );
68                     }
69                 }
70
71                 return st.executeUpdate();
72             }
73             finally {
74                 if ( st != null ) {
75                     session.getBatcher().closeStatement( st );
76                 }
77             }
78         }
79         catch( SQLException JavaDoc sqle ) {
80             throw JDBCExceptionHelper.convert(
81                     getFactory().getSQLExceptionConverter(),
82                     sqle,
83                     "could not execute update query",
84                     sql
85                 );
86         }
87     }
88
89     protected Queryable[] getAffectedQueryables() {
90         return new Queryable[] { persister };
91     }
92 }
93
Popular Tags