提交 b90b9770 authored 作者: noelgrandin's avatar noelgrandin

When the H2 parser fails to parse a SQL statement, throw a more specific exception that

contains extra information to make it possible for smart editors to do autocomplete.
Patch from Nicolas Fortin.
上级 6eb510c1
......@@ -44,6 +44,8 @@ Change Log
</li><li>Add a CONTAINS_UNCOMMITTED column to the SESSIONS metadata table, to allow detecting when rogue
sessions are creating large transactions.
</li><li>Some small fixes to the GEOMETRY support, patches by Nicolas Fortin.
</li><li>When the H2 parser fails to parse a SQL statement, throw a more specific exception that
contains extra information to make it possible for smart editors to do autocomplete. Patch from Nicolas Fortin.
</li></ul>
<h2>Version 1.3.173 (2013-07-28)</h2>
......
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.api;
import java.util.List;
import org.h2.jdbc.JdbcSQLException;
/**
* A JDBC SQL Exception with additional parameters, it provides the syntax error
* position and expected token.
* <p>
* When the H2 parser encounters an error, it normally throws one of these
* exceptions. Clients may use this information to implement things like
* autocomplete in editors, or just better display of errors.
*
* @author Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888
*/
public class JdbcParseSQLException extends JdbcSQLException {
private final List<String> expectedTokens;
private final int syntaxErrorPosition;
/**
* Creates a JdbcParseSQLException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
* @param expectedTokens H2 parser expected tokens
* @param syntaxErrorPosition Syntax error character index
*/
public JdbcParseSQLException(String message, String sql, String state, int errorCode, Throwable cause,
String stackTrace, List<String> expectedTokens, int syntaxErrorPosition) {
super(message, sql, state, errorCode, cause, stackTrace);
this.expectedTokens = expectedTokens;
this.syntaxErrorPosition = syntaxErrorPosition;
}
/**
* H2 parser expected tokens
*/
public List<String> getExpectedTokens() {
return expectedTokens;
}
/**
* Syntax error character position
*/
public int getSyntaxErrorPosition() {
return syntaxErrorPosition;
}
}
......@@ -499,7 +499,7 @@ public class Parser {
buff.appendExceptFirst(", ");
buff.append(e);
}
return DbException.getSyntaxError(sqlCommand, parseIndex, buff.toString());
return DbException.getSyntaxError(sqlCommand, parseIndex, buff.toString(), expectedList);
}
private Prepared parseBackup() {
......
......@@ -11,9 +11,11 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.Properties;
import org.h2.api.JdbcParseSQLException;
import org.h2.constant.ErrorCode;
import org.h2.jdbc.JdbcSQLException;
import org.h2.util.SortedProperties;
......@@ -192,8 +194,20 @@ public class DbException extends RuntimeException {
* @return the exception
*/
public static DbException getSyntaxError(String sql, int index, String expected) {
return getSyntaxError(sql, index, expected, java.util.Arrays.asList(expected));
}
/**
* Create a syntax error exception.
*
* @param sql the SQL statement
* @param index the position of the error in the SQL statement
* @param expected the expected keyword(s) at the given position
* @return the exception
*/
public static DbException getSyntaxError(String sql, int index, String message, List<String> expected) {
sql = StringUtils.addAsterisk(sql, index);
return get(ErrorCode.SYNTAX_ERROR_2, sql, expected);
return new DbException(getParseJdbcSQLException(ErrorCode.SYNTAX_ERROR_2, null, index, expected, sql, message));
}
/**
......@@ -326,9 +340,29 @@ public class DbException extends RuntimeException {
* @return the SQLException object
*/
private static JdbcSQLException getJdbcSQLException(int errorCode, Throwable cause, String... params) {
return getParseJdbcSQLException(errorCode, cause, 0, null, params);
}
/**
* Gets the SQL exception object for a specific error code.
*
* @param errorCode the error code
* @param cause the cause of the exception
* @param syntaxErrorPosition Syntax error character index
* @param expectedTokens H2 parser expected tokens
* @param params the list of parameters of the message
* @return the SQLException object
*/
private static JdbcSQLException getParseJdbcSQLException(int errorCode, Throwable cause, int syntaxErrorPosition,
List<String> expectedTokens, String... params) {
String sqlstate = ErrorCode.getState(errorCode);
String message = translate(sqlstate, params);
if (expectedTokens == null) {
return new JdbcSQLException(message, null, sqlstate, errorCode, cause, null);
} else {
return new JdbcParseSQLException(message, null, sqlstate, errorCode, cause, null, expectedTokens,
syntaxErrorPosition);
}
}
/**
......
......@@ -26,6 +26,7 @@ import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import java.util.UUID;
import org.h2.api.JdbcParseSQLException;
import org.h2.api.Trigger;
import org.h2.constant.ErrorCode;
import org.h2.test.TestBase;
......@@ -86,6 +87,7 @@ public class TestPreparedStatement extends TestBase {
testBlob(conn);
testClob(conn);
testParameterMetaData(conn);
testParseException(conn);
conn.close();
deleteDb("preparedStatement");
}
......@@ -1182,4 +1184,26 @@ public class TestPreparedStatement extends TestBase {
assertTrue(!rs.next());
}
private void testParseException(Connection conn) {
try {
conn.prepareStatement("SELECT * FROM");
fail();
} catch (JdbcParseSQLException ex) {
assertEquals(14, ex.getSyntaxErrorPosition());
assertEquals(new Object[] { "identifier" }, ex.getExpectedTokens().toArray());
} catch (SQLException ex) {
fail();
}
try {
conn.prepareStatement("ALTER");
fail();
} catch (JdbcParseSQLException ex) {
assertEquals(6, ex.getSyntaxErrorPosition());
assertEquals(new Object[] { "TABLE", "USER", "INDEX", "SCHEMA", "SEQUENCE", "VIEW" }, ex
.getExpectedTokens().toArray());
} catch (SQLException ex) {
fail();
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论