Search in sources :

Example 11 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class ShowTableStatusSQLHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<MySqlShowTableStatusStatement> request, MycatDataContext dataContext, Response response) {
    MySqlShowTableStatusStatement ast = request.getAst();
    if (ast.getDatabase() == null && dataContext.getDefaultSchema() != null) {
        ast.setDatabase(new SQLIdentifierExpr(dataContext.getDefaultSchema()));
    }
    SQLName database = ast.getDatabase();
    if (database == null) {
        return response.sendError(new MycatException("NO DATABASES SELECTED"));
    }
    String sql = toNormalSQL(request.getAst());
    return DrdsRunnerHelper.runOnDrds(dataContext, DrdsRunnerHelper.preParse(sql, dataContext.getDefaultSchema()), response);
}
Also used : MycatException(io.mycat.MycatException) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlShowTableStatusStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement)

Example 12 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class ShowTablesSQLHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<SQLShowTablesStatement> request, MycatDataContext dataContext, Response response) {
    SQLShowTablesStatement ast = request.getAst();
    if (ast.getDatabase() == null && dataContext.getDefaultSchema() != null) {
        ast.setDatabase(new SQLIdentifierExpr(dataContext.getDefaultSchema()));
    }
    SQLName database = ast.getDatabase();
    if (database == null) {
        return response.sendError(new MycatException("NO DATABASES SELECTED"));
    }
    String sql = toNormalSQL(request.getAst());
    return DrdsRunnerHelper.runOnDrds(dataContext, DrdsRunnerHelper.preParse(sql, dataContext.getDefaultSchema()), response);
}
Also used : MycatException(io.mycat.MycatException) SQLShowTablesStatement(com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 13 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class MySQLSequenceHandlerImpl method updateSeqFromDb.

private void updateSeqFromDb(SeqInfoValue seqInfoValue, SequenceCallback callback) {
    String dataSourceName = seqInfoValue.getDataSourceName();
    mySQLAPIRuntime.create(dataSourceName, new MySQLAPISessionCallback() {

        @Override
        public void onSession(MySQLAPI mySQLAPI) {
            OneResultSetCollector collector = new OneResultSetCollector();
            String sql = seqInfoValue.getSql();
            mySQLAPI.query(sql, collector, new MySQLAPIExceptionCallback() {

                @Override
                public void onException(Exception exception, MySQLAPI mySQLAPI) {
                    seqInfoValue.getFetchLock().set(false);
                    callback.onException(exception);
                }

                @Override
                public void onFinished(boolean monopolize, MySQLAPI mySQLAPI) {
                    try {
                        mySQLAPI.close();
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                    try {
                        Iterator<Object[]> iterator = collector.iterator();
                        String seqText = (String) iterator.next()[0];
                        String[] values = seqText.split(",");
                        long currentValue = Long.parseLong(values[0]);
                        long increment = Long.parseLong(values[1]);
                        seqInfoValue.setValueBox(new AtomicLong(currentValue));
                        seqInfoValue.setMaxValue(currentValue + increment);
                    } catch (Exception e) {
                        callback.onException(e);
                        return;
                    } finally {
                        seqInfoValue.getFetchLock().set(false);
                    }
                    SeqInfoKey seqInfoKey = seqInfoValue.getSeqInfoKey();
                    nextId(seqInfoKey.getSchema(), seqInfoKey.getTable(), callback);
                }

                @Override
                public void onErrorPacket(ErrorPacket errorPacket, boolean monopolize, MySQLAPI mySQLAPI) {
                    seqInfoValue.getFetchLock().set(false);
                    try {
                        mySQLAPI.close();
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                    callback.onException(new MycatException(errorPacket.getErrorMessageString()));
                }
            });
        }

        @Override
        public void onException(Exception exception) {
            seqInfoValue.getFetchLock().set(false);
            callback.onException(exception);
        }
    });
}
Also used : MycatException(io.mycat.MycatException) MySQLAPIExceptionCallback(io.mycat.api.callback.MySQLAPIExceptionCallback) MySQLAPISessionCallback(io.mycat.api.callback.MySQLAPISessionCallback) OneResultSetCollector(io.mycat.api.collector.OneResultSetCollector) MycatException(io.mycat.MycatException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ErrorPacket(io.mycat.beans.mysql.packet.ErrorPacket) MySQLAPI(io.mycat.api.MySQLAPI)

Example 14 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class ShowStatementRewriter method showTableStatus.

public static String showTableStatus(MySqlShowTableStatusStatement ast, String databaseName, String tableName) {
    if (databaseName == null) {
        throw new MycatException(1046, "No database selected");
    }
    String like = ast.getLike() == null ? new SQLBooleanExpr(true).toString() : " TABLE_NAME like " + ast.getLike();
    SQLExpr where = ast.getWhere() == null ? new SQLBooleanExpr(true) : ast.getWhere();
    String schemaCondition = " TABLE_SCHEMA = '" + databaseName + "' ";
    String tableCondition = tableName != null ? " TABLE_NAME = '" + tableName + "' " : " true ";
    SQLSelectBuilderImpl sqlSelectBuilder = new SQLSelectBuilderImpl(DbType.mysql);
    String sql = sqlSelectBuilder.selectWithAlias("TABLE_NAME", "Name").selectWithAlias("ENGINE", "Engine").selectWithAlias("VERSION", "Version").selectWithAlias("ROW_FORMAT", "Row_format").selectWithAlias("AVG_ROW_LENGTH", "Avg_row_length").selectWithAlias("DATA_LENGTH", "Data_length").selectWithAlias("MAX_DATA_LENGTH", "Data_length").selectWithAlias("INDEX_LENGTH", "Max_data_length").selectWithAlias("DATA_FREE", "Data_free").selectWithAlias("AUTO_INCREMENT", "Auto_increment").selectWithAlias("CREATE_TIME", "UPDATE_TIME").selectWithAlias("UPDATE_TIME", "Update_time").selectWithAlias("CHECK_TIME", "Check_time").selectWithAlias("TABLE_COLLATION", "Collation").selectWithAlias("CHECKSUM", "Checksum").selectWithAlias("CREATE_OPTIONS", "Create_options").selectWithAlias("TABLE_COMMENT", "Comment").from("information_schema.`TABLES`").whereAnd(schemaCondition).whereAnd(tableCondition).whereAnd(like.toString()).whereAnd(where.toString()).toString();
    LOGGER.info(ast + "->" + sql);
    return sql;
}
Also used : SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) MycatException(io.mycat.MycatException) SQLSelectBuilderImpl(com.alibaba.druid.sql.builder.impl.SQLSelectBuilderImpl) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 15 with MycatException

use of io.mycat.MycatException in project Mycat2 by MyCATApache.

the class MigrateUtil method read.

@SneakyThrows
public static Flowable<Object[]> read(MigrateUtil.MigrateJdbcInput migrateJdbcInput, Partition backend) {
    MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
    ReplicaSelectorManager replicaSelectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
    String targetName = backend.getTargetName();
    String tableName = backend.getTable();
    String schemaName = backend.getSchema();
    String datasourceName = replicaSelectorRuntime.getDatasourceNameByReplicaName(targetName, true, null);
    List<DatasourceConfig> datasources = routerConfig.getDatasources();
    DatasourceConfig datasourceConfig = datasources.stream().filter(i -> i.getName().equals(datasourceName)).findFirst().orElseThrow((Supplier<Throwable>) () -> {
        MycatException mycatException = new MycatException("can not found datasource " + datasourceName);
        LOGGER.error("", mycatException);
        return mycatException;
    });
    return read(migrateJdbcInput, tableName, schemaName, datasourceConfig.getUrl(), datasourceConfig.getUser(), datasourceConfig.getPassword());
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DatasourceConfig(io.mycat.config.DatasourceConfig) MycatException(io.mycat.MycatException) MycatRouterConfig(io.mycat.config.MycatRouterConfig) ToString(groovy.transform.ToString) SneakyThrows(lombok.SneakyThrows)

Aggregations

MycatException (io.mycat.MycatException)31 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLName (com.alibaba.druid.sql.ast.SQLName)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 MySQLClientSession (io.mycat.proxy.session.MySQLClientSession)3 SQLException (java.sql.SQLException)3 CalciteSqlBasicCall (com.alibaba.druid.support.calcite.CalciteSqlBasicCall)2 ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)2 DatasourceConfig (io.mycat.config.DatasourceConfig)2 ProxyBufferImpl (io.mycat.proxy.buffer.ProxyBufferImpl)2 ReactorEnvThread (io.mycat.proxy.reactor.ReactorEnvThread)2 Future (io.vertx.core.Future)2 PromiseInternal (io.vertx.core.impl.future.PromiseInternal)2 Statement (java.sql.Statement)2 DateString (org.apache.calcite.util.DateString)2 TimeString (org.apache.calcite.util.TimeString)2 TimestampString (org.apache.calcite.util.TimestampString)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLShowTablesStatement (com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement)1