提交 1bb9cb16 authored 作者: Thomas Mueller's avatar Thomas Mueller

Remove JdbcParseSQLException

上级 fd9cd70d
/*
* 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 { ...@@ -499,7 +499,7 @@ public class Parser {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(e); buff.append(e);
} }
return DbException.getSyntaxError(sqlCommand, parseIndex, buff.toString(), expectedList); return DbException.getSyntaxError(sqlCommand, parseIndex, buff.toString());
} }
private Prepared parseBackup() { private Prepared parseBackup() {
......
...@@ -11,11 +11,10 @@ import java.io.IOException; ...@@ -11,11 +11,10 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Properties; import java.util.Properties;
import org.h2.api.JdbcParseSQLException;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.jdbc.JdbcSQLException; import org.h2.jdbc.JdbcSQLException;
import org.h2.util.SortedProperties; import org.h2.util.SortedProperties;
...@@ -190,24 +189,12 @@ public class DbException extends RuntimeException { ...@@ -190,24 +189,12 @@ public class DbException extends RuntimeException {
* *
* @param sql the SQL statement * @param sql the SQL statement
* @param index the position of the error in the SQL statement * @param index the position of the error in the SQL statement
* @param expected the expected keyword at the given position * @param message the message
* @return the exception * @return the exception
*/ */
public static DbException getSyntaxError(String sql, int index, String expected) { public static DbException getSyntaxError(String sql, int index, String message) {
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); sql = StringUtils.addAsterisk(sql, index);
return new DbException(getParseJdbcSQLException(ErrorCode.SYNTAX_ERROR_2, null, index, expected, sql, message)); return new DbException(getJdbcSQLException(ErrorCode.SYNTAX_ERROR_2, null, sql, message));
} }
/** /**
...@@ -340,29 +327,9 @@ public class DbException extends RuntimeException { ...@@ -340,29 +327,9 @@ public class DbException extends RuntimeException {
* @return the SQLException object * @return the SQLException object
*/ */
private static JdbcSQLException getJdbcSQLException(int errorCode, Throwable cause, String... params) { 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 sqlstate = ErrorCode.getState(errorCode);
String message = translate(sqlstate, params); String message = translate(sqlstate, params);
if (expectedTokens == null) {
return new JdbcSQLException(message, null, sqlstate, errorCode, cause, null); return new JdbcSQLException(message, null, sqlstate, errorCode, cause, null);
} else {
return new JdbcParseSQLException(message, null, sqlstate, errorCode, cause, null, expectedTokens,
syntaxErrorPosition);
}
} }
/** /**
......
...@@ -26,7 +26,6 @@ import java.util.GregorianCalendar; ...@@ -26,7 +26,6 @@ import java.util.GregorianCalendar;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.UUID; import java.util.UUID;
import org.h2.api.JdbcParseSQLException;
import org.h2.api.Trigger; import org.h2.api.Trigger;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.test.TestBase; import org.h2.test.TestBase;
...@@ -87,7 +86,6 @@ public class TestPreparedStatement extends TestBase { ...@@ -87,7 +86,6 @@ public class TestPreparedStatement extends TestBase {
testBlob(conn); testBlob(conn);
testClob(conn); testClob(conn);
testParameterMetaData(conn); testParameterMetaData(conn);
testParseException(conn);
conn.close(); conn.close();
deleteDb("preparedStatement"); deleteDb("preparedStatement");
} }
...@@ -1184,26 +1182,4 @@ public class TestPreparedStatement extends TestBase { ...@@ -1184,26 +1182,4 @@ public class TestPreparedStatement extends TestBase {
assertTrue(!rs.next()); 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论