HHH-8159 - Apply fixups indicated by analysis tools

This commit is contained in:
Steve Ebersole 2013-04-25 09:46:12 -05:00
parent 932d85ba26
commit 955c99cc43
17 changed files with 170 additions and 202 deletions

View File

@ -23,15 +23,11 @@
*/
package org.hibernate;
import org.jboss.logging.Logger;
/**
* A problem occurred translating a Hibernate query to SQL due to invalid query syntax, etc.
*/
public class QueryException extends HibernateException {
private static final Logger log = Logger.getLogger( QueryException.class );
private String queryString;
private final String queryString;
/**
* Constructs a QueryException using the specified exception message.
@ -39,7 +35,7 @@ public class QueryException extends HibernateException {
* @param message A message explaining the exception condition
*/
public QueryException(String message) {
super( message );
this( message, null, null );
}
/**
@ -48,8 +44,8 @@ public QueryException(String message) {
* @param message A message explaining the exception condition
* @param cause The underlying cause
*/
public QueryException(String message, Throwable cause) {
super( message, cause );
public QueryException(String message, Exception cause) {
this( message, null, cause );
}
/**
@ -59,7 +55,18 @@ public QueryException(String message, Throwable cause) {
* @param queryString The query being evaluated when the exception occurred
*/
public QueryException(String message, String queryString) {
super( message );
this( message, queryString, null );
}
/**
* Constructs a QueryException using the specified exception message and query-string.
*
* @param message A message explaining the exception condition
* @param queryString The query being evaluated when the exception occurred
* @param cause The underlying cause
*/
public QueryException(String message, String queryString, Exception cause) {
super( message, cause );
this.queryString = queryString;
}
@ -69,7 +76,7 @@ public QueryException(String message, String queryString) {
* @param cause The underlying cause
*/
public QueryException(Exception cause) {
super( cause );
this( "A query exception occurred", null, cause );
}
/**
@ -81,27 +88,10 @@ public String getQueryString() {
return queryString;
}
/**
* Set the query string. Even an option since often the part of the code generating the exception does not
* have access to the query overall.
*
* @param queryString The query string.
*/
public void setQueryString(String queryString) {
if ( this.queryString != null ) {
log.debugf(
"queryString overriding non-null previous value [%s] : %s",
this.queryString,
queryString
);
}
this.queryString = queryString;
}
@Override
public String getMessage() {
String msg = super.getMessage();
if ( queryString!=null ) {
if ( queryString != null ) {
msg += " [" + queryString + ']';
}
return msg;

View File

@ -210,7 +210,7 @@ else if ( hasWithClause( normalizedSelect ) ) {
sb.append( limit ).append( " rows only" );
if ( hasForUpdateClause( forUpdateIndex ) ) {
sb.append(' ');
sb.append( ' ' );
sb.append( query.substring( forUpdateIndex ) );
}
else if ( hasWithClause( normalizedSelect ) ) {

View File

@ -693,7 +693,7 @@ public final Map<String, SQLFunction> getFunctions() {
// keyword support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
protected void registerKeyword(String word) {
sqlKeywords.add(word);
sqlKeywords.add( word );
}
public Set<String> getKeywords() {

View File

@ -65,6 +65,7 @@
* @author Phillip Baird
* @author Fred Toussi
*/
@SuppressWarnings("deprecation")
public class HSQLDialect extends Dialect {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
@ -158,7 +159,8 @@ public HSQLDialect() {
// datetime functions
if ( hsqldbVersion < 20 ) {
registerFunction( "sysdate", new NoArgSQLFunction( "sysdate", StandardBasicTypes.DATE, false ) );
} else {
}
else {
registerFunction( "sysdate", new NoArgSQLFunction( "sysdate", StandardBasicTypes.TIMESTAMP, false ) );
}
registerFunction( "current_date", new NoArgSQLFunction( "current_date", StandardBasicTypes.DATE, false ) );

View File

@ -298,7 +298,7 @@ public boolean supportsEmptyInList() {
}
@Override
public boolean supportsExpectedLobUsagePattern () {
public boolean supportsExpectedLobUsagePattern() {
return false;
}

View File

@ -22,6 +22,7 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.dialect;
import java.sql.Types;
import org.hibernate.cfg.Environment;
@ -31,8 +32,10 @@
/**
* An Hibernate 3 SQL dialect for Mimer SQL. This dialect requires Mimer SQL 9.2.1 or later
* because of the mappings to NCLOB, BINARY, and BINARY VARYING.
*
* @author Fredrik lund <fredrik.alund@mimer.se>
*/
@SuppressWarnings("deprecation")
public class MimerSQLDialect extends Dialect {
private static final int NATIONAL_CHAR_LENGTH = 2000;
@ -43,7 +46,6 @@ public class MimerSQLDialect extends Dialect {
* this is also the maximum width of the table (exluding LOBs). To avoid breaking the limit all the
* time we limit the length of the character columns to CHAR_MAX_LENTH, NATIONAL_CHAR_LENGTH for national
* characters, and BINARY_MAX_LENGTH for binary types.
*
*/
public MimerSQLDialect() {
super();
@ -55,7 +57,7 @@ public MimerSQLDialect() {
registerColumnType( Types.CHAR, "NCHAR(1)" );
registerColumnType( Types.VARCHAR, NATIONAL_CHAR_LENGTH, "NATIONAL CHARACTER VARYING($l)" );
registerColumnType( Types.VARCHAR, "NCLOB($l)" );
registerColumnType( Types.LONGVARCHAR, "CLOB($1)");
registerColumnType( Types.LONGVARCHAR, "CLOB($1)" );
registerColumnType( Types.FLOAT, "FLOAT" );
registerColumnType( Types.DOUBLE, "DOUBLE PRECISION" );
registerColumnType( Types.DATE, "DATE" );
@ -63,180 +65,137 @@ public MimerSQLDialect() {
registerColumnType( Types.TIMESTAMP, "TIMESTAMP" );
registerColumnType( Types.VARBINARY, BINARY_MAX_LENGTH, "BINARY VARYING($l)" );
registerColumnType( Types.VARBINARY, "BLOB($1)" );
registerColumnType( Types.LONGVARBINARY, "BLOB($1)");
registerColumnType( Types.LONGVARBINARY, "BLOB($1)" );
registerColumnType( Types.BINARY, BINARY_MAX_LENGTH, "BINARY" );
registerColumnType( Types.BINARY, "BLOB($1)" );
registerColumnType( Types.NUMERIC, "NUMERIC(19, $l)" );
registerColumnType( Types.BLOB, "BLOB($l)" );
registerColumnType( Types.CLOB, "NCLOB($l)" );
registerFunction("abs", new StandardSQLFunction("abs") );
registerFunction("sign", new StandardSQLFunction("sign", StandardBasicTypes.INTEGER) );
registerFunction("ceiling", new StandardSQLFunction("ceiling") );
registerFunction("floor", new StandardSQLFunction("floor") );
registerFunction("round", new StandardSQLFunction("round") );
registerFunction( "abs", new StandardSQLFunction( "abs" ) );
registerFunction( "sign", new StandardSQLFunction( "sign", StandardBasicTypes.INTEGER ) );
registerFunction( "ceiling", new StandardSQLFunction( "ceiling" ) );
registerFunction( "floor", new StandardSQLFunction( "floor" ) );
registerFunction( "round", new StandardSQLFunction( "round" ) );
registerFunction("dacos", new StandardSQLFunction("dacos", StandardBasicTypes.DOUBLE) );
registerFunction("acos", new StandardSQLFunction("dacos", StandardBasicTypes.DOUBLE) );
registerFunction("dasin", new StandardSQLFunction("dasin", StandardBasicTypes.DOUBLE) );
registerFunction("asin", new StandardSQLFunction("dasin", StandardBasicTypes.DOUBLE) );
registerFunction("datan", new StandardSQLFunction("datan", StandardBasicTypes.DOUBLE) );
registerFunction("atan", new StandardSQLFunction("datan", StandardBasicTypes.DOUBLE) );
registerFunction("datan2", new StandardSQLFunction("datan2", StandardBasicTypes.DOUBLE) );
registerFunction("atan2", new StandardSQLFunction("datan2", StandardBasicTypes.DOUBLE) );
registerFunction("dcos", new StandardSQLFunction("dcos", StandardBasicTypes.DOUBLE) );
registerFunction("cos", new StandardSQLFunction("dcos", StandardBasicTypes.DOUBLE) );
registerFunction("dcot", new StandardSQLFunction("dcot", StandardBasicTypes.DOUBLE) );
registerFunction("cot", new StandardSQLFunction("dcot", StandardBasicTypes.DOUBLE) );
registerFunction("ddegrees", new StandardSQLFunction("ddegrees", StandardBasicTypes.DOUBLE) );
registerFunction("degrees", new StandardSQLFunction("ddegrees", StandardBasicTypes.DOUBLE) );
registerFunction("dexp", new StandardSQLFunction("dexp", StandardBasicTypes.DOUBLE) );
registerFunction("exp", new StandardSQLFunction("dexp", StandardBasicTypes.DOUBLE) );
registerFunction("dlog", new StandardSQLFunction("dlog", StandardBasicTypes.DOUBLE) );
registerFunction("log", new StandardSQLFunction("dlog", StandardBasicTypes.DOUBLE) );
registerFunction("dlog10", new StandardSQLFunction("dlog10", StandardBasicTypes.DOUBLE) );
registerFunction("log10", new StandardSQLFunction("dlog10", StandardBasicTypes.DOUBLE) );
registerFunction("dradian", new StandardSQLFunction("dradian", StandardBasicTypes.DOUBLE) );
registerFunction("radian", new StandardSQLFunction("dradian", StandardBasicTypes.DOUBLE) );
registerFunction("dsin", new StandardSQLFunction("dsin", StandardBasicTypes.DOUBLE) );
registerFunction("sin", new StandardSQLFunction("dsin", StandardBasicTypes.DOUBLE) );
registerFunction("soundex", new StandardSQLFunction("soundex", StandardBasicTypes.STRING) );
registerFunction("dsqrt", new StandardSQLFunction("dsqrt", StandardBasicTypes.DOUBLE) );
registerFunction("sqrt", new StandardSQLFunction("dsqrt", StandardBasicTypes.DOUBLE) );
registerFunction("dtan", new StandardSQLFunction("dtan", StandardBasicTypes.DOUBLE) );
registerFunction("tan", new StandardSQLFunction("dtan", StandardBasicTypes.DOUBLE) );
registerFunction("dpower", new StandardSQLFunction("dpower") );
registerFunction("power", new StandardSQLFunction("dpower") );
registerFunction( "dacos", new StandardSQLFunction( "dacos", StandardBasicTypes.DOUBLE ) );
registerFunction( "acos", new StandardSQLFunction( "dacos", StandardBasicTypes.DOUBLE ) );
registerFunction( "dasin", new StandardSQLFunction( "dasin", StandardBasicTypes.DOUBLE ) );
registerFunction( "asin", new StandardSQLFunction( "dasin", StandardBasicTypes.DOUBLE ) );
registerFunction( "datan", new StandardSQLFunction( "datan", StandardBasicTypes.DOUBLE ) );
registerFunction( "atan", new StandardSQLFunction( "datan", StandardBasicTypes.DOUBLE ) );
registerFunction( "datan2", new StandardSQLFunction( "datan2", StandardBasicTypes.DOUBLE ) );
registerFunction( "atan2", new StandardSQLFunction( "datan2", StandardBasicTypes.DOUBLE ) );
registerFunction( "dcos", new StandardSQLFunction( "dcos", StandardBasicTypes.DOUBLE ) );
registerFunction( "cos", new StandardSQLFunction( "dcos", StandardBasicTypes.DOUBLE ) );
registerFunction( "dcot", new StandardSQLFunction( "dcot", StandardBasicTypes.DOUBLE ) );
registerFunction( "cot", new StandardSQLFunction( "dcot", StandardBasicTypes.DOUBLE ) );
registerFunction( "ddegrees", new StandardSQLFunction( "ddegrees", StandardBasicTypes.DOUBLE ) );
registerFunction( "degrees", new StandardSQLFunction( "ddegrees", StandardBasicTypes.DOUBLE ) );
registerFunction( "dexp", new StandardSQLFunction( "dexp", StandardBasicTypes.DOUBLE ) );
registerFunction( "exp", new StandardSQLFunction( "dexp", StandardBasicTypes.DOUBLE ) );
registerFunction( "dlog", new StandardSQLFunction( "dlog", StandardBasicTypes.DOUBLE ) );
registerFunction( "log", new StandardSQLFunction( "dlog", StandardBasicTypes.DOUBLE ) );
registerFunction( "dlog10", new StandardSQLFunction( "dlog10", StandardBasicTypes.DOUBLE ) );
registerFunction( "log10", new StandardSQLFunction( "dlog10", StandardBasicTypes.DOUBLE ) );
registerFunction( "dradian", new StandardSQLFunction( "dradian", StandardBasicTypes.DOUBLE ) );
registerFunction( "radian", new StandardSQLFunction( "dradian", StandardBasicTypes.DOUBLE ) );
registerFunction( "dsin", new StandardSQLFunction( "dsin", StandardBasicTypes.DOUBLE ) );
registerFunction( "sin", new StandardSQLFunction( "dsin", StandardBasicTypes.DOUBLE ) );
registerFunction( "soundex", new StandardSQLFunction( "soundex", StandardBasicTypes.STRING ) );
registerFunction( "dsqrt", new StandardSQLFunction( "dsqrt", StandardBasicTypes.DOUBLE ) );
registerFunction( "sqrt", new StandardSQLFunction( "dsqrt", StandardBasicTypes.DOUBLE ) );
registerFunction( "dtan", new StandardSQLFunction( "dtan", StandardBasicTypes.DOUBLE ) );
registerFunction( "tan", new StandardSQLFunction( "dtan", StandardBasicTypes.DOUBLE ) );
registerFunction( "dpower", new StandardSQLFunction( "dpower" ) );
registerFunction( "power", new StandardSQLFunction( "dpower" ) );
registerFunction("date", new StandardSQLFunction("date", StandardBasicTypes.DATE) );
registerFunction("dayofweek", new StandardSQLFunction("dayofweek", StandardBasicTypes.INTEGER) );
registerFunction("dayofyear", new StandardSQLFunction("dayofyear", StandardBasicTypes.INTEGER) );
registerFunction("time", new StandardSQLFunction("time", StandardBasicTypes.TIME) );
registerFunction("timestamp", new StandardSQLFunction("timestamp", StandardBasicTypes.TIMESTAMP) );
registerFunction("week", new StandardSQLFunction("week", StandardBasicTypes.INTEGER) );
registerFunction( "date", new StandardSQLFunction( "date", StandardBasicTypes.DATE ) );
registerFunction( "dayofweek", new StandardSQLFunction( "dayofweek", StandardBasicTypes.INTEGER ) );
registerFunction( "dayofyear", new StandardSQLFunction( "dayofyear", StandardBasicTypes.INTEGER ) );
registerFunction( "time", new StandardSQLFunction( "time", StandardBasicTypes.TIME ) );
registerFunction( "timestamp", new StandardSQLFunction( "timestamp", StandardBasicTypes.TIMESTAMP ) );
registerFunction( "week", new StandardSQLFunction( "week", StandardBasicTypes.INTEGER ) );
registerFunction("varchar", new StandardSQLFunction("varchar", StandardBasicTypes.STRING) );
registerFunction("real", new StandardSQLFunction("real", StandardBasicTypes.FLOAT) );
registerFunction("bigint", new StandardSQLFunction("bigint", StandardBasicTypes.LONG) );
registerFunction("char", new StandardSQLFunction("char", StandardBasicTypes.CHARACTER) );
registerFunction("integer", new StandardSQLFunction("integer", StandardBasicTypes.INTEGER) );
registerFunction("smallint", new StandardSQLFunction("smallint", StandardBasicTypes.SHORT) );
registerFunction( "varchar", new StandardSQLFunction( "varchar", StandardBasicTypes.STRING ) );
registerFunction( "real", new StandardSQLFunction( "real", StandardBasicTypes.FLOAT ) );
registerFunction( "bigint", new StandardSQLFunction( "bigint", StandardBasicTypes.LONG ) );
registerFunction( "char", new StandardSQLFunction( "char", StandardBasicTypes.CHARACTER ) );
registerFunction( "integer", new StandardSQLFunction( "integer", StandardBasicTypes.INTEGER ) );
registerFunction( "smallint", new StandardSQLFunction( "smallint", StandardBasicTypes.SHORT ) );
registerFunction("ascii_char", new StandardSQLFunction("ascii_char", StandardBasicTypes.CHARACTER) );
registerFunction("ascii_code", new StandardSQLFunction("ascii_code", StandardBasicTypes.STRING));
registerFunction("unicode_char", new StandardSQLFunction("unicode_char", StandardBasicTypes.LONG));
registerFunction("unicode_code", new StandardSQLFunction("unicode_code", StandardBasicTypes.STRING));
registerFunction("upper", new StandardSQLFunction("upper") );
registerFunction("lower", new StandardSQLFunction("lower") );
registerFunction("char_length", new StandardSQLFunction("char_length", StandardBasicTypes.LONG) );
registerFunction("bit_length", new StandardSQLFunction("bit_length", StandardBasicTypes.STRING));
registerFunction( "ascii_char", new StandardSQLFunction( "ascii_char", StandardBasicTypes.CHARACTER ) );
registerFunction( "ascii_code", new StandardSQLFunction( "ascii_code", StandardBasicTypes.STRING ) );
registerFunction( "unicode_char", new StandardSQLFunction( "unicode_char", StandardBasicTypes.LONG ) );
registerFunction( "unicode_code", new StandardSQLFunction( "unicode_code", StandardBasicTypes.STRING ) );
registerFunction( "upper", new StandardSQLFunction( "upper" ) );
registerFunction( "lower", new StandardSQLFunction( "lower" ) );
registerFunction( "char_length", new StandardSQLFunction( "char_length", StandardBasicTypes.LONG ) );
registerFunction( "bit_length", new StandardSQLFunction( "bit_length", StandardBasicTypes.STRING ) );
getDefaultProperties().setProperty(Environment.USE_STREAMS_FOR_BINARY, "true");
getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, "50");
getDefaultProperties().setProperty( Environment.USE_STREAMS_FOR_BINARY, "true" );
getDefaultProperties().setProperty( Environment.STATEMENT_BATCH_SIZE, "50" );
}
/**
* The syntax used to add a column to a table
*/
@Override
public String getAddColumnString() {
return "add column";
}
/**
* We do not have to drop constraints before we drop the table
*/
@Override
public boolean dropConstraints() {
return false;
}
/**
* TODO: Check if Mimer SQL cannot handle the way DB2 does
*/
@Override
public boolean supportsIdentityColumns() {
return false;
}
/**
* Mimer SQL supports sequences
* @return boolean
*/
@Override
public boolean supportsSequences() {
return true;
}
/**
* The syntax used to get the next value of a sequence in Mimer SQL
*/
@Override
public String getSequenceNextValString(String sequenceName) {
return "select next_value of " + sequenceName + " from system.onerow";
}
/**
* The syntax used to create a sequence. Since we presume the sequences will be used as keys,
* we make them unique.
*/
@Override
public String getCreateSequenceString(String sequenceName) {
return "create unique sequence " + sequenceName;
}
/**
* The syntax used to drop sequences
*/
@Override
public String getDropSequenceString(String sequenceName) {
return "drop sequence " + sequenceName + " restrict";
}
/**
* Mimer SQL does not support limit
*/
@Override
public boolean supportsLimit() {
return false;
}
/**
* The syntax for using cascade on constraints
*/
@Override
public String getCascadeConstraintsString() {
return " cascade";
}
/**
* The syntax for fetching all sequnces avialable in the current schema.
*/
@Override
public String getQuerySequencesString() {
return "select sequence_schema || '.' || sequence_name from information_schema.ext_sequences";
}
/**
* Does the <tt>FOR UPDATE OF</tt> syntax specify particular
* columns?
*/
@Override
public boolean forUpdateOfColumns() {
return false;
}
/**
* Support the FOR UPDATE syntax? For now, returns false since
* the current version of the Mimer SQL JDBC Driver does not support
* updatable resultsets. Otherwise, Mimer SQL actually supports the for update syntax.
* @return boolean
*/
public boolean supportsForUpdate() {
return false;
}
/**
* For now, simply return false since we don't updatable result sets.
*/
@Override
public boolean supportsOuterJoinForUpdate() {
return false;
}
}

View File

@ -54,7 +54,7 @@ public String getWriteLockString(int timeout) {
return getForUpdateSkipLockedString();
}
else {
return super.getWriteLockString(timeout);
return super.getWriteLockString( timeout );
}
}

View File

@ -28,7 +28,6 @@
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.QueryTimeoutException;
import org.hibernate.cfg.Environment;
@ -44,7 +43,6 @@
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.sql.CaseFragment;
import org.hibernate.sql.DecodeCaseFragment;
import org.hibernate.sql.JoinFragment;
@ -255,7 +253,7 @@ public String getLimitString(String sql, boolean hasOffset) {
pagingSelect.append( "select * from ( select row_.*, rownum rownum_ from ( " );
}
else {
pagingSelect.append("select * from ( ");
pagingSelect.append( "select * from ( " );
}
pagingSelect.append( sql );
if (hasOffset) {

View File

@ -122,7 +122,7 @@ public String getWriteLockString(int timeout) {
else if ( timeout > 0 ) {
// convert from milliseconds to seconds
final float seconds = timeout / 1000.0f;
timeout = Math.round(seconds);
timeout = Math.round( seconds );
return " for update wait " + timeout;
}
else {

View File

@ -42,6 +42,7 @@
* @author Scott Marlow
* @since 3.5
*/
@SuppressWarnings("deprecation")
public class OptimisticLockingStrategy implements LockingStrategy {
private final Lockable lockable;
private final LockMode lockMode;
@ -65,7 +66,7 @@ public void lock(Serializable id, Object version, Object object, int timeout, Se
if ( !lockable.isVersioned() ) {
throw new OptimisticLockException( object, "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
}
final EntityEntry entry = session.getPersistenceContext().getEntry(object);
final EntityEntry entry = session.getPersistenceContext().getEntry( object );
// Register the EntityVerifyVersionProcess action to run just prior to transaction commit.
( (EventSource) session ).getActionQueue().registerProcess( new EntityVerifyVersionProcess( object, entry ) );
}

View File

@ -89,10 +89,14 @@ public int convertToFirstRowValue(int zeroBasedFirstResult) {
@Override
public String getProcessedSql() {
boolean useLimitOffset = supportsLimit() && supportsLimitOffset()
&& LimitHelper.hasFirstRow( selection ) && LimitHelper.hasMaxRows( selection );
final boolean useLimitOffset = supportsLimit()
&& supportsLimitOffset()
&& LimitHelper.hasFirstRow( selection )
&& LimitHelper.hasMaxRows( selection );
return dialect.getLimitString(
sql, useLimitOffset ? LimitHelper.getFirstRow( selection ) : 0, getMaxOrLimit()
sql,
useLimitOffset ? LimitHelper.getFirstRow( selection ) : 0,
getMaxOrLimit()
);
}
}

View File

@ -310,7 +310,7 @@ private static int shallowIndexOf(StringBuilder sb, String search, int fromIndex
pos = lowercase.indexOf( search, cur );
if ( pos != -1 ) {
for ( int iter = cur; iter < pos; iter++ ) {
char c = sb.charAt( iter );
final char c = sb.charAt( iter );
if ( c == '(' ) {
depth = depth + 1;
}

View File

@ -1059,9 +1059,7 @@ protected void handleResultVariableRef(AST resultVariableRef) throws SemanticExc
public int[] getNamedParameterLocations(String name) throws QueryException {
Object o = namedParameters.get( name );
if ( o == null ) {
QueryException qe = new QueryException( QueryTranslator.ERROR_NAMED_PARAMETER_DOES_NOT_APPEAR + name );
qe.setQueryString( queryTranslatorImpl.getQueryString() );
throw qe;
throw new QueryException( QueryTranslator.ERROR_NAMED_PARAMETER_DOES_NOT_APPEAR + name, queryTranslatorImpl.getQueryString() );
}
if ( o instanceof Integer ) {
return new int[]{ (Integer) o };

View File

@ -39,8 +39,7 @@ public QuerySyntaxException(String message) {
}
public QuerySyntaxException(String message, String hql) {
this( message );
setQueryString( hql );
super( message, hql );
}
public static QuerySyntaxException convert(RecognitionException e) {

View File

@ -77,8 +77,10 @@
* @author Joshua Davis (pgmjsd@sourceforge.net)
*/
public class QueryTranslatorImpl implements FilterTranslator {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, QueryTranslatorImpl.class.getName());
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
QueryTranslatorImpl.class.getName()
);
private SessionFactoryImplementor factory;
@ -87,7 +89,8 @@ public class QueryTranslatorImpl implements FilterTranslator {
private boolean shallowQuery;
private Map tokenReplacements;
private Map enabledFilters; //TODO:this is only needed during compilation .. can we eliminate the instvar?
//TODO:this is only needed during compilation .. can we eliminate the instvar?
private Map enabledFilters;
private boolean compiled;
private QueryLoader queryLoader;
@ -110,9 +113,9 @@ public class QueryTranslatorImpl implements FilterTranslator {
*/
public QueryTranslatorImpl(
String queryIdentifier,
String query,
Map enabledFilters,
SessionFactoryImplementor factory) {
String query,
Map enabledFilters,
SessionFactoryImplementor factory) {
this.queryIdentifier = queryIdentifier;
this.hql = query;
this.compiled = false;
@ -131,8 +134,8 @@ public QueryTranslatorImpl(
* @throws MappingException There was a problem querying defined mappings.
*/
public void compile(
Map replacements,
boolean shallow) throws QueryException, MappingException {
Map replacements,
boolean shallow) throws QueryException, MappingException {
doCompile( replacements, shallow, null );
}
@ -147,9 +150,9 @@ public void compile(
* @throws MappingException There was a problem querying defined mappings.
*/
public void compile(
String collectionRole,
Map replacements,
boolean shallow) throws QueryException, MappingException {
String collectionRole,
Map replacements,
boolean shallow) throws QueryException, MappingException {
doCompile( replacements, shallow, collectionRole );
}
@ -177,12 +180,12 @@ private synchronized void doCompile(Map replacements, boolean shallow, String co
try {
// PHASE 1 : Parse the HQL into an AST.
HqlParser parser = parse( true );
final HqlParser parser = parse( true );
// PHASE 2 : Analyze the HQL AST, and produce an SQL AST.
HqlSqlWalker w = analyze( parser, collectionRole );
final HqlSqlWalker w = analyze( parser, collectionRole );
sqlAst = ( Statement ) w.getAST();
sqlAst = (Statement) w.getAST();
// at some point the generate phase needs to be moved out of here,
// because a single object-level DML might spawn multiple SQL DML
@ -200,15 +203,19 @@ private synchronized void doCompile(Map replacements, boolean shallow, String co
}
else {
// PHASE 3 : Generate the SQL.
generate( ( QueryNode ) sqlAst );
generate( (QueryNode) sqlAst );
queryLoader = new QueryLoader( this, factory, w.getSelectClause() );
}
compiled = true;
}
catch ( QueryException qe ) {
qe.setQueryString( hql );
throw qe;
if ( qe.getQueryString() == null ) {
throw new QueryException( qe.getMessage(), hql, qe );
}
else {
throw qe;
}
}
catch ( RecognitionException e ) {
// we do not actually propagate ANTLRExceptions as a cause, so
@ -223,12 +230,13 @@ private synchronized void doCompile(Map replacements, boolean shallow, String co
throw new QueryException( e.getMessage(), hql );
}
this.enabledFilters = null; //only needed during compilation phase...
//only needed during compilation phase...
this.enabledFilters = null;
}
private void generate(AST sqlAst) throws QueryException, RecognitionException {
if ( sql == null ) {
SqlGenerator gen = new SqlGenerator(factory);
final SqlGenerator gen = new SqlGenerator( factory );
gen.statement( sqlAst );
sql = gen.getSQL();
if ( LOG.isDebugEnabled() ) {
@ -240,16 +248,17 @@ private void generate(AST sqlAst) throws QueryException, RecognitionException {
}
}
private static final ASTPrinter SQL_TOKEN_PRINTER = new ASTPrinter( SqlTokenTypes.class );
private HqlSqlWalker analyze(HqlParser parser, String collectionRole) throws QueryException, RecognitionException {
HqlSqlWalker w = new HqlSqlWalker( this, factory, parser, tokenReplacements, collectionRole );
AST hqlAst = parser.getAST();
final HqlSqlWalker w = new HqlSqlWalker( this, factory, parser, tokenReplacements, collectionRole );
final AST hqlAst = parser.getAST();
// Transform the tree.
w.statement( hqlAst );
if ( LOG.isDebugEnabled() ) {
ASTPrinter printer = new ASTPrinter( SqlTokenTypes.class );
LOG.debug( printer.showAsString( w.getAST(), "--- SQL AST ---" ) );
LOG.debug( SQL_TOKEN_PRINTER.showAsString( w.getAST(), "--- SQL AST ---" ) );
}
w.getParseErrorHandler().throwQueryException();
@ -259,16 +268,15 @@ private HqlSqlWalker analyze(HqlParser parser, String collectionRole) throws Que
private HqlParser parse(boolean filter) throws TokenStreamException, RecognitionException {
// Parse the query string into an HQL AST.
HqlParser parser = HqlParser.getInstance( hql );
final HqlParser parser = HqlParser.getInstance( hql );
parser.setFilter( filter );
LOG.debugf( "parse() - HQL: %s", hql );
parser.statement();
AST hqlAst = parser.getAST();
final AST hqlAst = parser.getAST();
JavaConstantConverter converter = new JavaConstantConverter();
NodeTraverser walker = new NodeTraverser( converter );
final NodeTraverser walker = new NodeTraverser( new JavaConstantConverter() );
walker.traverseDepthFirst( hqlAst );
showHqlAst( hqlAst );
@ -277,10 +285,11 @@ private HqlParser parse(boolean filter) throws TokenStreamException, Recognition
return parser;
}
private static final ASTPrinter HQL_TOKEN_PRINTER = new ASTPrinter( HqlTokenTypes.class );
void showHqlAst(AST hqlAst) {
if ( LOG.isDebugEnabled() ) {
ASTPrinter printer = new ASTPrinter( HqlTokenTypes.class );
LOG.debug( printer.showAsString( hqlAst, "--- HQL AST ---" ) );
LOG.debug( HQL_TOKEN_PRINTER.showAsString( hqlAst, "--- HQL AST ---" ) );
}
}

View File

@ -61,7 +61,7 @@ public String getRenderText(SessionFactoryImplementor sessionFactory) {
try {
return typeAsLiteralType().objectToSQLString( getValue(), sessionFactory.getDialect() );
}
catch( Throwable t ) {
catch( Exception t ) {
throw new QueryException( "Unable to render boolean literal value", t );
}
}

View File

@ -244,8 +244,12 @@ private void compile() throws QueryException, MappingException {
renderSQL();
}
catch ( QueryException qe ) {
qe.setQueryString( queryString );
throw qe;
if ( qe.getQueryString() == null ) {
throw generateQueryException( qe.getMessage(), qe );
}
else {
throw qe;
}
}
catch ( MappingException me ) {
throw me;
@ -253,9 +257,7 @@ private void compile() throws QueryException, MappingException {
catch ( Exception e ) {
LOG.debug( "Unexpected query compilation problem", e );
e.printStackTrace();
QueryException qe = new QueryException( "Incorrect query syntax", e );
qe.setQueryString( queryString );
throw qe;
throw generateQueryException( "Incorrect query syntax", e );
}
postInstantiate();
@ -264,6 +266,14 @@ private void compile() throws QueryException, MappingException {
}
public QueryException generateQueryException(String message) {
return new QueryException( message, queryString );
}
public QueryException generateQueryException(String message, Exception cause) {
return new QueryException( message, queryString, cause );
}
@Override
public String getSQLString() {
return sqlString;
@ -547,9 +557,7 @@ else if ( o instanceof Integer ) {
public int[] getNamedParameterLocs(String name) throws QueryException {
Object o = namedParameters.get( name );
if ( o == null ) {
QueryException qe = new QueryException( ERROR_NAMED_PARAMETER_DOES_NOT_APPEAR + name );
qe.setQueryString( queryString );
throw qe;
throw generateQueryException( ERROR_NAMED_PARAMETER_DOES_NOT_APPEAR + name );
}
if ( o instanceof Integer ) return new int[] { (Integer) o };
else {