Search in sources :

Example 51 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project jaybird by FirebirdSQL.

the class V10Statement method prepare.

@Override
public void prepare(final String statementText) throws SQLException {
    try {
        synchronized (getSynchronizationObject()) {
            checkTransactionActive(getTransaction());
            final StatementState currentState = getState();
            if (!isPrepareAllowed(currentState)) {
                throw new SQLNonTransientException(String.format("Current statement state (%s) does not allow call to prepare", currentState));
            }
            resetAll();
            final FbWireDatabase db = getDatabase();
            if (currentState == StatementState.NEW) {
                try {
                    sendAllocate();
                    getXdrOut().flush();
                } catch (IOException ex) {
                    switchState(StatementState.ERROR);
                    throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
                }
                try {
                    processAllocateResponse(db.readGenericResponse(getStatementWarningCallback()));
                } catch (IOException ex) {
                    switchState(StatementState.ERROR);
                    throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
                }
            } else {
                checkStatementValid();
            }
            try {
                sendPrepare(statementText);
                getXdrOut().flush();
            } catch (IOException ex) {
                switchState(StatementState.ERROR);
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            try {
                processPrepareResponse(db.readGenericResponse(getStatementWarningCallback()));
            } catch (IOException ex) {
                switchState(StatementState.ERROR);
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
            }
        }
    } catch (SQLException e) {
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    }
}
Also used : StatementState(org.firebirdsql.gds.ng.StatementState) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) IOException(java.io.IOException)

Example 52 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project cobar by alibaba.

the class ServerConnection method execute.

public void execute(String sql, int type) {
    // 状态检查
    if (txInterrupted) {
        writeErrMessage(ErrorCode.ER_YES, "Transaction error, need to rollback.");
        return;
    }
    // 检查当前使用的DB
    String db = this.schema;
    if (db == null) {
        writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
        return;
    }
    SchemaConfig schema = CobarServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
        return;
    }
    // 路由计算
    RouteResultset rrs = null;
    try {
        rrs = ServerRouter.route(schema, sql, this.charset, this);
    } catch (SQLNonTransientException e) {
        StringBuilder s = new StringBuilder();
        LOGGER.warn(s.append(this).append(sql).toString(), e);
        String msg = e.getMessage();
        writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return;
    }
    // session执行
    session.execute(rrs, type);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) RouteResultset(com.alibaba.cobar.route.RouteResultset)

Example 53 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project cobar by alibaba.

the class ExplainHandler method getRouteResultset.

private static RouteResultset getRouteResultset(ServerConnection c, String stmt) {
    String db = c.getSchema();
    if (db == null) {
        c.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
        return null;
    }
    SchemaConfig schema = CobarServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        c.writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
        return null;
    }
    try {
        return ServerRouter.route(schema, stmt, c.getCharset(), c);
    } catch (SQLNonTransientException e) {
        StringBuilder s = new StringBuilder();
        logger.warn(s.append(c).append(stmt).toString(), e);
        String msg = e.getMessage();
        c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return null;
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Example 54 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.

the class DruidInsertParser method parserChildTable.

private RouteResultset parserChildTable(SchemaConfig schema, RouteResultset rrs, String tableName, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    TableConfig tc = schema.getTables().get(tableName);
    String joinKey = tc.getJoinKey();
    int joinKeyIndex = getJoinKeyIndex(insertStmt.getColumns(), joinKey);
    if (joinKeyIndex == -1) {
        String inf = "joinKey not provided :" + tc.getJoinKey() + "," + insertStmt;
        LOGGER.warn(inf);
        throw new SQLNonTransientException(inf);
    }
    if (isMultiInsert(insertStmt)) {
        String msg = "ChildTable multi insert not provided";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String joinKeyVal = insertStmt.getValues().getValues().get(joinKeyIndex).toString();
    String sql = insertStmt.toString();
    // try to route by ER parent partion key
    RouteResultset theRrs = RouterUtil.routeByERParentKey(null, schema, ServerParse.INSERT, sql, rrs, tc, joinKeyVal);
    if (theRrs != null) {
        rrs.setFinishedRoute(true);
        return theRrs;
    }
    // route by sql query root parent's datanode
    String findRootTBSql = tc.getLocateRTableKeySql().toLowerCase() + joinKeyVal;
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("find root parent's node sql " + findRootTBSql);
    }
    FetchStoreNodeOfChildTableHandler fetchHandler = new FetchStoreNodeOfChildTableHandler();
    String dn = fetchHandler.execute(schema.getName(), findRootTBSql, tc.getRootParent().getDataNodes());
    if (dn == null) {
        throw new SQLNonTransientException("can't find (root) parent sharding node for sql:" + sql);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("found partion node for child table to insert " + dn + " sql :" + sql);
    }
    return RouterUtil.routeToSingleNode(rrs, dn, sql);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) FetchStoreNodeOfChildTableHandler(io.mycat.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler) TableConfig(io.mycat.config.model.TableConfig) RouteResultset(io.mycat.route.RouteResultset)

Example 55 with SQLNonTransientException

use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.

the class DruidInsertParser method statementParse.

/**
 * 考虑因素:isChildTable、批量、是否分片
 */
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlInsertStatement insert = (MySqlInsertStatement) stmt;
    String tableName = StringUtil.removeBackquote(insert.getTableName().getSimpleName()).toUpperCase();
    ctx.addTable(tableName);
    if (RouterUtil.isNoSharding(schema, tableName)) {
        // 整个schema都不分库或者该表不拆分
        RouterUtil.routeForTableMeta(rrs, schema, tableName, rrs.getStatement());
        rrs.setFinishedRoute(true);
        return;
    }
    TableConfig tc = schema.getTables().get(tableName);
    if (tc == null) {
        String msg = "can't find table define in schema " + tableName + " schema:" + schema.getName();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    } else {
        // childTable的insert直接在解析过程中完成路由
        if (tc.isChildTable()) {
            parserChildTable(schema, rrs, tableName, insert);
            return;
        }
        String partitionColumn = tc.getPartitionColumn();
        if (partitionColumn != null) {
            // 拆分表必须给出column list,否则无法寻找分片字段的值
            if (insert.getColumns() == null || insert.getColumns().size() == 0) {
                throw new SQLSyntaxErrorException("partition table, insert must provide ColumnList");
            }
            // 批量insert
            if (isMultiInsert(insert)) {
                // String msg = "multi insert not provided" ;
                // LOGGER.warn(msg);
                // throw new SQLNonTransientException(msg);
                parserBatchInsert(schema, rrs, partitionColumn, tableName, insert);
            } else {
                parserSingleInsert(schema, rrs, partitionColumn, tableName, insert);
            }
        }
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) TableConfig(io.mycat.config.model.TableConfig) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)

Aggregations

SQLNonTransientException (java.sql.SQLNonTransientException)125 TableConfig (io.mycat.config.model.TableConfig)26 SQLException (java.sql.SQLException)23 Test (org.testng.annotations.Test)19 BaseTest (util.BaseTest)19 RouteResultset (io.mycat.route.RouteResultset)17 TableConfig (com.actiontech.dble.config.model.TableConfig)15 HashMap (java.util.HashMap)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)13 RouteResultsetNode (io.mycat.route.RouteResultsetNode)13 HashSet (java.util.HashSet)13 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)12 ColumnRoutePair (io.mycat.sqlengine.mpp.ColumnRoutePair)12 LinkedHashSet (java.util.LinkedHashSet)12 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)11 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)11 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)10 Map (java.util.Map)10 SlotFunction (io.mycat.route.function.SlotFunction)9 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)8