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);
}
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);
}
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);
}
});
}
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;
}
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());
}
Aggregations