Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » hql » ast » exec » [javadoc | source]
    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.hql.ast.exec;
   26   
   27   import java.sql.PreparedStatement;
   28   import java.sql.SQLException;
   29   import java.util.Iterator;
   30   
   31   import org.hibernate.HibernateException;
   32   import org.hibernate.engine.QueryParameters;
   33   import org.hibernate.engine.RowSelection;
   34   import org.hibernate.engine.SessionImplementor;
   35   import org.hibernate.exception.JDBCExceptionHelper;
   36   import org.hibernate.hql.ast.HqlSqlWalker;
   37   import org.hibernate.hql.ast.QuerySyntaxException;
   38   import org.hibernate.hql.ast.SqlGenerator;
   39   import org.hibernate.param.ParameterSpecification;
   40   import org.hibernate.persister.entity.Queryable;
   41   
   42   import antlr.RecognitionException;
   43   
   44   import org.slf4j.Logger;
   45   import org.slf4j.LoggerFactory;
   46   
   47   /**
   48    * Implementation of BasicExecutor.
   49    *
   50    * @author Steve Ebersole
   51    */
   52   public class BasicExecutor extends AbstractStatementExecutor {
   53   	private static final Logger log = LoggerFactory.getLogger( BasicExecutor.class );
   54   
   55   	private final Queryable persister;
   56   	private final String sql;
   57   
   58   	public BasicExecutor(HqlSqlWalker walker, Queryable persister) {
   59   		super( walker, log );
   60   		this.persister = persister;
   61   		try {
   62   			SqlGenerator gen = new SqlGenerator( getFactory() );
   63   			gen.statement( walker.getAST() );
   64   			sql = gen.getSQL();
   65   			gen.getParseErrorHandler().throwQueryException();
   66   		}
   67   		catch ( RecognitionException e ) {
   68   			throw QuerySyntaxException.convert( e );
   69   		}
   70   	}
   71   
   72   	public String[] getSqlStatements() {
   73   		return new String[] { sql };
   74   	}
   75   
   76   	public int execute(QueryParameters parameters, SessionImplementor session) throws HibernateException {
   77   
   78   		coordinateSharedCacheCleanup( session );
   79   
   80   		PreparedStatement st = null;
   81   		RowSelection selection = parameters.getRowSelection();
   82   
   83   		try {
   84   			try {
   85   				st = session.getBatcher().prepareStatement( sql );
   86   				Iterator paramSpecifications = getWalker().getParameters().iterator();
   87   				int pos = 1;
   88   				while ( paramSpecifications.hasNext() ) {
   89   					final ParameterSpecification paramSpec = ( ParameterSpecification ) paramSpecifications.next();
   90   					pos += paramSpec.bind( st, parameters, session, pos );
   91   				}
   92   				if ( selection != null ) {
   93   					if ( selection.getTimeout() != null ) {
   94   						st.setQueryTimeout( selection.getTimeout().intValue() );
   95   					}
   96   				}
   97   
   98   				return st.executeUpdate();
   99   			}
  100   			finally {
  101   				if ( st != null ) {
  102   					session.getBatcher().closeStatement( st );
  103   				}
  104   			}
  105   		}
  106   		catch( SQLException sqle ) {
  107   			throw JDBCExceptionHelper.convert(
  108   					getFactory().getSQLExceptionConverter(),
  109   			        sqle,
  110   			        "could not execute update query",
  111   			        sql
  112   				);
  113   		}
  114   	}
  115   
  116   	protected Queryable[] getAffectedQueryables() {
  117   		return new Queryable[] { persister };
  118   	}
  119   }

Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » hql » ast » exec » [javadoc | source]