Search in sources :

Example 1 with TokenMgrException

use of net.sf.jsqlparser.parser.TokenMgrException in project spanner-jdbc by olavloite.

the class CloudSpannerStatement method execute.

@Override
public boolean execute(String sql) throws SQLException {
    String[] sqlTokens = getTokens(sql);
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null)
        return custom.execute(sqlTokens);
    Statement statement = null;
    boolean ddl = isDDLStatement(sqlTokens);
    if (!ddl) {
        try {
            statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
        } catch (JSQLParserException | TokenMgrException e) {
            throw new CloudSpannerSQLException("Error while parsing sql statement " + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
        }
    }
    if (!ddl && statement instanceof Select) {
        determineForceSingleUseReadContext((Select) statement);
        if (!isForceSingleUseReadContext() && getConnection().isBatchReadOnly()) {
            List<Partition> partitions = partitionQuery(com.google.cloud.spanner.Statement.of(sql));
            currentResultSets = new ArrayList<>(partitions.size());
            for (Partition p : partitions) {
                currentResultSets.add(new CloudSpannerPartitionResultSet(this, getBatchReadOnlyTransaction(), p, sql));
            }
        } else {
            try (ReadContext context = getReadContext()) {
                com.google.cloud.spanner.ResultSet rs = context.executeQuery(com.google.cloud.spanner.Statement.of(sql));
                currentResultSets = Arrays.asList(new CloudSpannerResultSet(this, rs, sql));
                currentResultSetIndex = 0;
                lastUpdateCount = -1;
            }
        }
        return true;
    } else {
        lastUpdateCount = executeUpdate(sql);
        currentResultSetIndex = 0;
        currentResultSets = null;
        return false;
    }
}
Also used : Partition(com.google.cloud.spanner.Partition) PreparedStatement(java.sql.PreparedStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerPartitionResultSet(nl.topicus.jdbc.resultset.CloudSpannerPartitionResultSet) TokenMgrException(net.sf.jsqlparser.parser.TokenMgrException) ReadContext(com.google.cloud.spanner.ReadContext) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 2 with TokenMgrException

use of net.sf.jsqlparser.parser.TokenMgrException in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method execute.

@Override
public boolean execute() throws SQLException {
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null)
        return custom.execute(sqlTokens);
    Statement statement = null;
    boolean ddl = isDDLStatement();
    if (!ddl) {
        try {
            statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
        } catch (JSQLParserException | TokenMgrException e) {
            throw new CloudSpannerSQLException(PARSE_ERROR + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
        }
    }
    if (!ddl && statement instanceof Select) {
        determineForceSingleUseReadContext((Select) statement);
        com.google.cloud.spanner.Statement.Builder builder = createSelectBuilder(statement, sql);
        if (!isForceSingleUseReadContext() && getConnection().isBatchReadOnly()) {
            List<Partition> partitions = partitionQuery(builder.build());
            currentResultSets = new ArrayList<>(partitions.size());
            for (Partition p : partitions) {
                currentResultSets.add(new CloudSpannerPartitionResultSet(this, getBatchReadOnlyTransaction(), p, sql));
            }
        } else {
            try (ReadContext context = getReadContext()) {
                com.google.cloud.spanner.ResultSet rs = context.executeQuery(builder.build());
                currentResultSets = Arrays.asList(new CloudSpannerResultSet(this, rs, sql));
                currentResultSetIndex = 0;
                lastUpdateCount = -1;
            }
        }
        return true;
    } else {
        lastUpdateCount = executeUpdate();
        currentResultSets = null;
        currentResultSetIndex = 0;
        return false;
    }
}
Also used : Partition(com.google.cloud.spanner.Partition) PreparedStatement(java.sql.PreparedStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerPartitionResultSet(nl.topicus.jdbc.resultset.CloudSpannerPartitionResultSet) TokenMgrException(net.sf.jsqlparser.parser.TokenMgrException) ReadContext(com.google.cloud.spanner.ReadContext) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 3 with TokenMgrException

use of net.sf.jsqlparser.parser.TokenMgrException in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method executeQuery.

@Override
public ResultSet executeQuery() throws SQLException {
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null && custom.isQuery()) {
        return custom.executeQuery(sqlTokens);
    }
    Statement statement;
    try {
        statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
    } catch (JSQLParserException | TokenMgrException e) {
        throw new CloudSpannerSQLException(PARSE_ERROR + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
    }
    if (statement instanceof Select) {
        determineForceSingleUseReadContext((Select) statement);
        com.google.cloud.spanner.Statement.Builder builder = createSelectBuilder(statement, sql);
        try (ReadContext context = getReadContext()) {
            com.google.cloud.spanner.ResultSet rs = context.executeQuery(builder.build());
            return new CloudSpannerResultSet(this, rs, sql);
        }
    }
    throw new CloudSpannerSQLException("SQL statement not suitable for executeQuery. Expected SELECT-statement.", Code.INVALID_ARGUMENT);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) TokenMgrException(net.sf.jsqlparser.parser.TokenMgrException) ReadContext(com.google.cloud.spanner.ReadContext) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 4 with TokenMgrException

use of net.sf.jsqlparser.parser.TokenMgrException in project spanner-jdbc by olavloite.

the class CloudSpannerResultSetMetaData method initMetaData.

private void initMetaData() throws SQLException {
    if (initialized)
        return;
    initialized = true;
    Statement sqlStatement = null;
    if (sql == null)
        return;
    try {
        sqlStatement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
    } catch (JSQLParserException | TokenMgrException e) {
        // ignore
        return;
    }
    if (!(sqlStatement instanceof Select))
        return;
    Select select = (Select) sqlStatement;
    try {
        initTables(select);
        initColumns(select);
    } catch (ParseException e) {
        throw e.e;
    }
}
Also used : CloudSpannerStatement(nl.topicus.jdbc.statement.CloudSpannerStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) TokenMgrException(net.sf.jsqlparser.parser.TokenMgrException) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select)

Aggregations

JSQLParserException (net.sf.jsqlparser.JSQLParserException)4 TokenMgrException (net.sf.jsqlparser.parser.TokenMgrException)4 Statement (net.sf.jsqlparser.statement.Statement)4 Select (net.sf.jsqlparser.statement.select.Select)4 ReadContext (com.google.cloud.spanner.ReadContext)3 PreparedStatement (java.sql.PreparedStatement)3 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)3 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)3 CloudSpannerResultSet (nl.topicus.jdbc.resultset.CloudSpannerResultSet)3 Partition (com.google.cloud.spanner.Partition)2 SubSelect (net.sf.jsqlparser.statement.select.SubSelect)2 CloudSpannerPartitionResultSet (nl.topicus.jdbc.resultset.CloudSpannerPartitionResultSet)2 CloudSpannerStatement (nl.topicus.jdbc.statement.CloudSpannerStatement)1