#161 error response

This commit is contained in:
yanhuqing666
2017-06-27 13:38:33 +08:00
parent d647a1c708
commit 265faafce6
35 changed files with 209 additions and 180 deletions

View File

@@ -1,12 +1,8 @@
package io.mycat.backend.mysql.nio.handler.builder;
import java.sql.SQLNonTransientException;
import java.sql.SQLSyntaxErrorException;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import io.mycat.MycatServer;
import io.mycat.backend.mysql.nio.handler.builder.BaseHandlerBuilder.MySQLNodeType;
import io.mycat.backend.mysql.nio.handler.builder.sqlvisitor.PushDownVisitor;
@@ -22,6 +18,10 @@ import io.mycat.route.util.RouterUtil;
import io.mycat.server.NonBlockingSession;
import io.mycat.server.parser.ServerParse;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.sql.SQLSyntaxErrorException;
public class MergeBuilder {
private boolean needCommonFlag;
private boolean needSendMakerFlag;
@@ -52,7 +52,7 @@ public class MergeBuilder {
* @throws SQLNonTransientException
* @throws SQLSyntaxErrorException
*/
public RouteResultset construct() throws SQLNonTransientException {
public RouteResultset construct() throws SQLException {
pdVisitor.visit();
String sql = pdVisitor.getSql().toString();
SQLStatementParser parser = new MySqlStatementParser(sql);

View File

@@ -23,18 +23,10 @@
*/
package io.mycat.route;
import java.sql.SQLNonTransientException;
import java.sql.SQLSyntaxErrorException;
import java.util.HashMap;
import java.util.Map;
import io.mycat.config.Versions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.mycat.cache.CachePool;
import io.mycat.cache.CacheService;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.Versions;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.SystemConfig;
import io.mycat.route.factory.RouteStrategyFactory;
@@ -43,6 +35,13 @@ import io.mycat.route.handler.HintHandlerFactory;
import io.mycat.route.handler.HintSQLHandler;
import io.mycat.server.ServerConnection;
import io.mycat.server.parser.ServerParse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.util.HashMap;
import java.util.Map;
public class RouteService {
private static final Logger LOGGER = LoggerFactory
@@ -68,7 +67,7 @@ public class RouteService {
public RouteResultset route(SystemConfig sysconf, SchemaConfig schema,
int sqlType, String stmt, String charset, ServerConnection sc)
throws SQLNonTransientException {
throws SQLException {
RouteResultset rrs = null;
String cacheKey = null;

View File

@@ -1,12 +1,12 @@
package io.mycat.route;
import java.sql.SQLNonTransientException;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.SystemConfig;
import io.mycat.server.ServerConnection;
import java.sql.SQLException;
/**
* 路由策略接口
* @author wang.dw
@@ -15,5 +15,5 @@ import io.mycat.server.ServerConnection;
public interface RouteStrategy {
public RouteResultset route(SystemConfig sysConfig,
SchemaConfig schema,int sqlType, String origSQL, String charset, ServerConnection sc, LayerCachePool cachePool)
throws SQLNonTransientException;
throws SQLException;
}

View File

@@ -1,14 +1,14 @@
package io.mycat.route.handler;
import java.sql.SQLNonTransientException;
import java.util.Map;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.SystemConfig;
import io.mycat.route.RouteResultset;
import io.mycat.server.ServerConnection;
import java.sql.SQLException;
import java.util.Map;
/**
* 按照注释中包含指定类型的内容做路由解析
*
@@ -18,5 +18,5 @@ public interface HintHandler {
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema,
int sqlType, String realSQL, String charset, ServerConnection sc,
LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap)
throws SQLNonTransientException;
throws SQLException;
}

View File

@@ -1,10 +1,6 @@
package io.mycat.route.handler;
import java.sql.SQLNonTransientException;
import java.util.Map;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.SystemConfig;
@@ -12,6 +8,11 @@ import io.mycat.route.RouteResultset;
import io.mycat.route.factory.RouteStrategyFactory;
import io.mycat.server.ServerConnection;
import io.mycat.server.parser.ServerParse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Map;
/**
* 处理情况 sql hint: mycat:db_type=master/slave<br/>
@@ -30,7 +31,7 @@ public class HintMasterDBHandler implements HintHandler {
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema, int sqlType,
String realSQL, String charset,
ServerConnection sc, LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap)
throws SQLNonTransientException {
throws SQLException {
// LOGGER.debug("realSQL: " + realSQL); // select * from travelrecord limit 1
// LOGGER.debug("sqlType: " + sqlType); // 7

View File

@@ -1,41 +1,32 @@
package io.mycat.route.handler;
import java.sql.SQLNonTransientException;
import java.sql.Types;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
import com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr;
import com.alibaba.druid.sql.ast.expr.SQLValuableExpr;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.SQLCallStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.SystemConfig;
import io.mycat.route.Procedure;
import io.mycat.route.ProcedureParameter;
import io.mycat.route.RouteResultset;
import io.mycat.route.RouteResultsetNode;
import io.mycat.route.RouteStrategy;
import io.mycat.route.*;
import io.mycat.route.factory.RouteStrategyFactory;
import io.mycat.server.ServerConnection;
import io.mycat.server.parser.ServerParse;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 处理注释中 类型为sql的情况 (按照 注释中的sql做路由解析而不是实际的sql
*/
@@ -51,7 +42,7 @@ public class HintSQLHandler implements HintHandler {
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema,
int sqlType, String realSQL, String charset, ServerConnection sc,
LayerCachePool cachePool, String hintSQLValue,int hintSqlType, Map hintMap)
throws SQLNonTransientException {
throws SQLException {
RouteResultset rrs = routeStrategy.route(sysConfig, schema, hintSqlType,
hintSQLValue, charset, sc, cachePool);

View File

@@ -1,5 +1,6 @@
package io.mycat.route.handler;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.Map;
@@ -45,7 +46,7 @@ public class HintSchemaHandler implements HintHandler {
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema,
int sqlType, String realSQL, String charset, ServerConnection sc,
LayerCachePool cachePool, String hintSQLValue,int hintSqlType, Map hintMap)
throws SQLNonTransientException {
throws SQLException {
SchemaConfig tempSchema = MycatServer.getInstance().getConfig().getSchemas().get(hintSQLValue);
if (tempSchema != null) {
return routeStrategy.route(sysConfig, tempSchema, sqlType, realSQL, charset, sc, cachePool);

View File

@@ -1,5 +1,6 @@
package io.mycat.route.impl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.sql.SQLSyntaxErrorException;
@@ -20,7 +21,7 @@ public abstract class AbstractRouteStrategy implements RouteStrategy {
@Override
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String origSQL,
String charset, ServerConnection sc, LayerCachePool cachePool) throws SQLNonTransientException {
String charset, ServerConnection sc, LayerCachePool cachePool) throws SQLException {
RouteResultset rrs = new RouteResultset(origSQL, sqlType, sc.getSession2());
@@ -56,18 +57,18 @@ public abstract class AbstractRouteStrategy implements RouteStrategy {
* 通过解析AST语法树类来寻找路由
*/
public abstract RouteResultset routeNormalSqlWithAST(SchemaConfig schema, String stmt, RouteResultset rrs,
String charset, LayerCachePool cachePool) throws SQLNonTransientException;
String charset, LayerCachePool cachePool) throws SQLException;
/**
* 路由信息指令, 如 SHOW、SELECT@@、DESCRIBE
*/
public abstract RouteResultset routeSystemInfo(SchemaConfig schema, int sqlType, String stmt, RouteResultset rrs)
throws SQLSyntaxErrorException;
throws SQLException;
/**
* 解析 Show 之类的语句
*/
public abstract RouteResultset analyseShowSQL(SchemaConfig schema, RouteResultset rrs, String stmt)
throws SQLNonTransientException;
throws SQLException;
}

View File

@@ -1,18 +1,10 @@
package io.mycat.route.impl;
import java.sql.SQLNonTransientException;
import java.sql.SQLSyntaxErrorException;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.google.common.base.Strings;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.model.SchemaConfig;
import io.mycat.route.RouteResultset;
@@ -21,6 +13,12 @@ import io.mycat.route.parser.druid.DruidParserFactory;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
import io.mycat.route.util.RouterUtil;
import io.mycat.server.parser.ServerParse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.util.List;
public class DruidMycatRouteStrategy extends AbstractRouteStrategy {
@@ -29,7 +27,7 @@ public class DruidMycatRouteStrategy extends AbstractRouteStrategy {
@Override
public RouteResultset routeNormalSqlWithAST(SchemaConfig schema,
String originSql, RouteResultset rrs, String charset,
LayerCachePool cachePool) throws SQLNonTransientException {
LayerCachePool cachePool) throws SQLException {
SQLStatementParser parser = new MySqlStatementParser(originSql);
MycatSchemaStatVisitor visitor = null;
SQLStatement statement;
@@ -80,7 +78,7 @@ public class DruidMycatRouteStrategy extends AbstractRouteStrategy {
*/
@Override
public RouteResultset analyseShowSQL(SchemaConfig schema,
RouteResultset rrs, String stmt) throws SQLSyntaxErrorException {
RouteResultset rrs, String stmt) throws SQLException {
String upStmt = stmt.toUpperCase();
int tabInd = upStmt.indexOf(" TABLES");
if (tabInd > 0) {// show tables
@@ -227,7 +225,7 @@ public class DruidMycatRouteStrategy extends AbstractRouteStrategy {
// }
public RouteResultset routeSystemInfo(SchemaConfig schema, int sqlType,
String stmt, RouteResultset rrs) throws SQLSyntaxErrorException {
String stmt, RouteResultset rrs) throws SQLException {
switch(sqlType){
case ServerParse.SHOW:// if origSQL is like show tables
return analyseShowSQL(schema, rrs, stmt);
@@ -250,7 +248,7 @@ public class DruidMycatRouteStrategy extends AbstractRouteStrategy {
* @author mycat
*/
private static RouteResultset analyseDescrSQL(SchemaConfig schema,
RouteResultset rrs, String stmt, int ind) {
RouteResultset rrs, String stmt, int ind) throws SQLException {
final String MATCHED_FEATURE = "DESCRIBE ";
final String MATCHED2_FEATURE = "DESC ";

View File

@@ -5,6 +5,7 @@ import io.mycat.cache.LayerCachePool;
import io.mycat.config.model.SchemaConfig;
import io.mycat.route.RouteResultset;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
/**
@@ -21,14 +22,14 @@ public interface DruidParser {
* @param schema
* @param stmt
*/
SchemaConfig parser(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, String originSql,LayerCachePool cachePool,MycatSchemaStatVisitor schemaStatVisitor) throws SQLNonTransientException;
SchemaConfig parser(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, String originSql,LayerCachePool cachePool,MycatSchemaStatVisitor schemaStatVisitor) throws SQLException;
/**
* 子类可覆盖如果该方法解析得不到表名、字段等信息的就覆盖该方法覆盖成空方法然后通过statementPparse去解析
* 通过visitor解析有些类型的Statement通过visitor解析得不到表名、
* @param stmt
*/
SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt,MycatSchemaStatVisitor visitor) throws SQLNonTransientException;
SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt,MycatSchemaStatVisitor visitor) throws SQLException;
/**
* 改写sql加limit加group by、加order by如有些没有加limit的可以通过该方法增加
@@ -37,7 +38,7 @@ public interface DruidParser {
* @param stmt
* @throws SQLNonTransientException
*/
void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt,LayerCachePool cachePool) throws SQLNonTransientException;
void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt,LayerCachePool cachePool) throws SQLException;
/**
* 获取解析到的信息
* @return

View File

@@ -1,5 +1,6 @@
package io.mycat.route.parser.druid.impl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -43,7 +44,7 @@ public class DefaultDruidParser implements DruidParser {
* @param schema
* @param stmt
*/
public SchemaConfig parser(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, String originSql,LayerCachePool cachePool,MycatSchemaStatVisitor schemaStatVisitor) throws SQLNonTransientException {
public SchemaConfig parser(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, String originSql,LayerCachePool cachePool,MycatSchemaStatVisitor schemaStatVisitor) throws SQLException {
ctx = new DruidShardingParseInfo();
//通过visitor解析
schema = visitorParse(schema, rrs,stmt,schemaStatVisitor);
@@ -60,7 +61,7 @@ public class DefaultDruidParser implements DruidParser {
*/
@Override
public void changeSql(SchemaConfig schema, RouteResultset rrs,
SQLStatement stmt,LayerCachePool cachePool) throws SQLNonTransientException {
SQLStatement stmt,LayerCachePool cachePool) throws SQLException {
}
@@ -71,7 +72,7 @@ public class DefaultDruidParser implements DruidParser {
*/
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
stmt.accept(visitor);
if(visitor.getNotSupportMsg()!= null){
throw new SQLNonTransientException(visitor.getNotSupportMsg());

View File

@@ -1,5 +1,6 @@
package io.mycat.route.parser.druid.impl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -8,6 +9,7 @@ import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
import io.mycat.config.ErrorCode;
import io.mycat.config.MycatPrivileges;
import io.mycat.config.MycatPrivileges.Checktype;
import io.mycat.config.model.SchemaConfig;
@@ -27,7 +29,7 @@ import io.mycat.server.util.SchemaUtil.SchemaInfo;
public class DruidDeleteParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
String schemaName = schema == null ? null : schema.getName();
MySqlDeleteStatement delete = (MySqlDeleteStatement) stmt;
SQLTableSource tableSource = delete.getTableSource();
@@ -49,8 +51,8 @@ public class DruidDeleteParser extends DefaultDruidParser {
SQLExprTableSource deleteTableSource = (SQLExprTableSource) tableSource;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, deleteTableSource);
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
if(!MycatPrivileges.checkPrivilege(rrs.getSession().getSource(), schemaInfo.schema, schemaInfo.table, Checktype.DELETE)){
String msg = "The statement DML privilege check is not passed, sql:" + stmt;

View File

@@ -1,5 +1,6 @@
package io.mycat.route.parser.druid.impl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.ArrayList;
import java.util.Date;
@@ -22,6 +23,7 @@ import com.alibaba.druid.sql.parser.SQLStatementParser;
import io.mycat.MycatServer;
import io.mycat.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler;
import io.mycat.config.ErrorCode;
import io.mycat.config.MycatPrivileges;
import io.mycat.config.MycatPrivileges.Checktype;
import io.mycat.config.model.SchemaConfig;
@@ -42,14 +44,14 @@ import io.mycat.util.StringUtil;
public class DruidInsertParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
MySqlInsertStatement insert = (MySqlInsertStatement) stmt;
String schemaName = schema == null ? null : schema.getName();
SQLExprTableSource tableSource = insert.getTableSource();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, tableSource);
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined";
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
if(!MycatPrivileges.checkPrivilege(rrs.getSession().getSource(), schemaInfo.schema, schemaInfo.table, Checktype.INSERT)){
String msg = "The statement DML privilege check is not passed, sql:" + stmt;
@@ -64,12 +66,12 @@ public class DruidInsertParser extends DefaultDruidParser {
// 整个schema都不分库或者该表不拆分
TableConfig tc = schema.getTables().get(tableName);
if (tc == null) {
String msg = "can't find table [" + tableName + "] define in schema:" + schema.getName();
throw new SQLNonTransientException(msg);
String msg = "Table '"+schema.getName()+"."+tableName+"' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
if (insert.getQuery() != null) {
// insert into .... select ....
String msg = "TODO:insert into .... select .... not supported!";
String msg = "insert into .... select .... not supported!";
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
@@ -375,7 +377,7 @@ public class DruidInsertParser extends DefaultDruidParser {
if (insertStmt.getColumns() == null || insertStmt.getColumns().size() == 0) {
TableMeta tbMeta = MycatServer.getInstance().getTmManager().getSyncTableMeta(schemaInfo.schema, schemaInfo.table);
if (tbMeta == null) {
String msg = "can't find table [" + schemaInfo.table + "] define in schema:" + schemaInfo.schema;
String msg = "Meta data of table '"+schemaInfo.schema+"."+schemaInfo.table+"' doesn't exist";
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}

View File

@@ -1,6 +1,7 @@
package io.mycat.route.parser.druid.impl;
import java.sql.SQLNonTransientException;
import java.sql.SQLException;
import java.util.*;
import com.alibaba.druid.sql.ast.*;
@@ -48,7 +49,7 @@ public class DruidSelectParser extends DefaultDruidParser {
}
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt,
MycatSchemaStatVisitor visitor) throws SQLNonTransientException {
MycatSchemaStatVisitor visitor) throws SQLException {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
String schemaName = schema == null ? null : schema.getName();
@@ -76,8 +77,8 @@ public class DruidSelectParser extends DefaultDruidParser {
SQLExprTableSource fromSource = (SQLExprTableSource) mysqlFrom;
schemaInfo = SchemaUtil.getSchemaInfo(schemaName, fromSource);
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000",ErrorCode.ER_NO_DB_ERROR);
}
// 兼容PhpAdmin's, 支持对MySQL元数据的模拟返回
//TODO:refactor INFORMATION_SCHEMA,MYSQL 等系統表的去向???
@@ -106,7 +107,7 @@ public class DruidSelectParser extends DefaultDruidParser {
return schema;
}
if (schemaInfo.schemaConfig == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
String msg = "No Supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
if (!MycatPrivileges.checkPrivilege(rrs.getSession().getSource(), schemaInfo.schema, schemaInfo.table, Checktype.SELECT)) {
@@ -121,8 +122,8 @@ public class DruidSelectParser extends DefaultDruidParser {
}
TableConfig tc= schema.getTables().get(schemaInfo.table);
if (tc == null) {
String msg = "can't find table [" + schemaInfo.table + "] define in schema:" + schema.getName();
throw new SQLNonTransientException(msg);
String msg = "Table '"+schema.getName()+"."+schemaInfo.table+"' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
super.visitorParse(schema, rrs, stmt, visitor);
if(visitor.isHasSubQuery()){
@@ -154,7 +155,7 @@ public class DruidSelectParser extends DefaultDruidParser {
}
private void parseOrderAggGroupMysql(SchemaConfig schema, SQLStatement stmt, RouteResultset rrs,
MySqlSelectQueryBlock mysqlSelectQuery, TableConfig tc) throws SQLNonTransientException {
MySqlSelectQueryBlock mysqlSelectQuery, TableConfig tc) throws SQLException {
if (mysqlSelectQuery.getOrderBy() != null) {
rrs.setSqlStatement(stmt);
rrs.setNeedOptimizer(true);
@@ -171,7 +172,7 @@ public class DruidSelectParser extends DefaultDruidParser {
// }
}
private void parseAggExprCommon(SchemaConfig schema, RouteResultset rrs, SQLSelectQueryBlock mysqlSelectQuery, Map<String, String> aliaColumns, TableConfig tc) throws SQLNonTransientException {
private void parseAggExprCommon(SchemaConfig schema, RouteResultset rrs, SQLSelectQueryBlock mysqlSelectQuery, Map<String, String> aliaColumns, TableConfig tc) throws SQLException {
List<SQLSelectItem> selectList = mysqlSelectQuery.getSelectList();
for (SQLSelectItem item : selectList) {
SQLExpr itemExpr = item.getExpr();
@@ -199,7 +200,7 @@ public class DruidSelectParser extends DefaultDruidParser {
} else if (itemExpr instanceof SQLAllColumnExpr) {
TableMeta tbMeta = MycatServer.getInstance().getTmManager().getSyncTableMeta(schema.getName(),tc.getName());
if (tbMeta == null) {
String msg = "can't find table [" + tc.getName() + "] define in schema:" + schema.getName();
String msg = "Meta data of table '"+schema.getName()+"."+tc.getName()+"' doesn't exist";
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
@@ -289,7 +290,7 @@ public class DruidSelectParser extends DefaultDruidParser {
}
}
private Map<String, String> parseAggGroupCommon(SchemaConfig schema, SQLStatement stmt, RouteResultset rrs,
SQLSelectQueryBlock mysqlSelectQuery, TableConfig tc) throws SQLNonTransientException {
SQLSelectQueryBlock mysqlSelectQuery, TableConfig tc) throws SQLException {
Map<String, String> aliaColumns = new HashMap<String, String>();
Map<String, Integer> aggrColumns = new HashMap<String, Integer>();
List<String> havingColsName = new ArrayList<String>();
@@ -445,7 +446,7 @@ public class DruidSelectParser extends DefaultDruidParser {
// return map;
// }
private SchemaConfig executeComplexSQL(String schemaName, SchemaConfig schema, RouteResultset rrs, SQLSelectStatement selectStmt)
throws SQLNonTransientException {
throws SQLException {
SchemaInfo schemaInfo = SchemaUtil.isNoSharding(rrs.getSession().getSource(), schemaName, selectStmt.getSelect().getQuery(), selectStmt);
if (schemaInfo == null) {
rrs.setSqlStatement(selectStmt);
@@ -462,7 +463,7 @@ public class DruidSelectParser extends DefaultDruidParser {
*/
@Override
public void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, LayerCachePool cachePool)
throws SQLNonTransientException {
throws SQLException {
if (rrs.isFinishedExecute() || rrs.isNeedOptimizer()) {
return;
}
@@ -528,7 +529,7 @@ public class DruidSelectParser extends DefaultDruidParser {
}
private void tryRouteSingleTable(SchemaConfig schema, RouteResultset rrs, LayerCachePool cachePool)
throws SQLNonTransientException {
throws SQLException {
if (rrs.isFinishedRoute()) {
return;// 避免重复路由
}

View File

@@ -1,5 +1,6 @@
package io.mycat.route.parser.druid.impl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -14,6 +15,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery;
import io.mycat.MycatServer;
import io.mycat.config.ErrorCode;
import io.mycat.config.MycatPrivileges;
import io.mycat.config.MycatPrivileges.Checktype;
import io.mycat.config.model.SchemaConfig;
@@ -29,7 +31,7 @@ import io.mycat.server.util.SchemaUtil.SchemaInfo;
public class DruidSingleUnitSelectParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt,
MycatSchemaStatVisitor visitor) throws SQLNonTransientException {
MycatSchemaStatVisitor visitor) throws SQLException {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
String schemaName = schema == null ? null : schema.getName();
@@ -62,8 +64,8 @@ public class DruidSingleUnitSelectParser extends DefaultDruidParser {
SQLExprTableSource fromSource = (SQLExprTableSource) mysqlFrom;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, fromSource);
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
// 兼容PhpAdmin's, 支持对MySQL元数据的模拟返回
if (SchemaUtil.INFORMATION_SCHEMA.equals(schemaInfo.schema)) {
@@ -91,7 +93,7 @@ public class DruidSingleUnitSelectParser extends DefaultDruidParser {
return schema;
}
if (schemaInfo.schemaConfig == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
String msg = "No Supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
if (!MycatPrivileges.checkPrivilege(rrs.getSession().getSource(), schemaInfo.schema, schemaInfo.table, Checktype.SELECT)) {

View File

@@ -1,5 +1,6 @@
package io.mycat.route.parser.druid.impl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.Date;
import java.util.List;
@@ -26,6 +27,7 @@ import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;
import io.mycat.MycatServer;
import io.mycat.config.ErrorCode;
import io.mycat.config.MycatPrivileges;
import io.mycat.config.MycatPrivileges.Checktype;
import io.mycat.config.model.ERTable;
@@ -46,7 +48,7 @@ import io.mycat.util.StringUtil;
*/
public class DruidUpdateParser extends DefaultDruidParser {
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
MySqlUpdateStatement update = (MySqlUpdateStatement) stmt;
SQLTableSource tableSource = update.getTableSource();
String schemaName = schema == null ? null : schema.getName();
@@ -65,8 +67,8 @@ public class DruidUpdateParser extends DefaultDruidParser {
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, (SQLExprTableSource) tableSource);
//数据库校验
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
//权限控制
if(!MycatPrivileges.checkPrivilege(rrs.getSession().getSource(), schemaInfo.schema, schemaInfo.table, Checktype.UPDATE)){

View File

@@ -1,5 +1,6 @@
package io.mycat.route.parser.druid.impl.ddl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.ArrayList;
import java.util.List;
@@ -25,6 +26,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeCo
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
import io.mycat.MycatServer;
import io.mycat.config.ErrorCode;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.TableConfig;
import io.mycat.meta.protocol.MyCatMeta.ColumnMeta;
@@ -46,13 +48,13 @@ import io.mycat.util.StringUtil;
public class DruidAlterTableParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
SQLAlterTableStatement alterTable = (SQLAlterTableStatement) stmt;
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, alterTable.getTableSource());
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
boolean support = false;
for (SQLAlterTableItem alterItem : alterTable.getItems()) {

View File

@@ -1,5 +1,6 @@
package io.mycat.route.parser.druid.impl.ddl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -7,6 +8,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import io.mycat.config.ErrorCode;
import io.mycat.config.model.SchemaConfig;
import io.mycat.route.RouteResultset;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
@@ -18,15 +20,15 @@ import io.mycat.server.util.SchemaUtil.SchemaInfo;
public class DruidCreateIndexParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt,
MycatSchemaStatVisitor visitor) throws SQLNonTransientException {
MycatSchemaStatVisitor visitor) throws SQLException {
SQLCreateIndexStatement createStmt = (SQLCreateIndexStatement) stmt;
SQLTableSource tableSource = createStmt.getTable();
if (tableSource instanceof SQLExprTableSource) {
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, (SQLExprTableSource) tableSource);
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
RouterUtil.routeToDDLNode(schemaInfo, rrs);
return schemaInfo.schemaConfig;

View File

@@ -1,5 +1,6 @@
package io.mycat.route.parser.druid.impl.ddl;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import com.alibaba.druid.sql.ast.SQLName;
@@ -12,6 +13,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import io.mycat.config.ErrorCode;
import io.mycat.config.model.SchemaConfig;
import io.mycat.route.RouteResultset;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
@@ -26,7 +28,7 @@ import io.mycat.util.StringUtil;
public class DruidCreateTableParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement)stmt;
//创建新表select from禁止
if(createStmt.getSelect() != null) {
@@ -44,8 +46,8 @@ public class DruidCreateTableParser extends DefaultDruidParser {
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, createStmt.getTableSource());
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
//如果这个不是no_sharing表格那么就需要这么进行检查

View File

@@ -1,10 +1,8 @@
package io.mycat.route.parser.druid.impl.ddl;
import java.sql.SQLNonTransientException;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
import io.mycat.config.ErrorCode;
import io.mycat.config.model.SchemaConfig;
import io.mycat.route.RouteResultset;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
@@ -13,6 +11,8 @@ import io.mycat.route.util.RouterUtil;
import io.mycat.server.util.SchemaUtil;
import io.mycat.server.util.SchemaUtil.SchemaInfo;
import java.sql.SQLException;
/**
*
* @author huqing.yan
@@ -21,13 +21,13 @@ import io.mycat.server.util.SchemaUtil.SchemaInfo;
public class DruidDropIndexParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
String schemaName = schema == null ? null : schema.getName();
SQLDropIndexStatement dropStmt = (SQLDropIndexStatement) stmt;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, dropStmt.getTableName());
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
RouterUtil.routeToDDLNode(schemaInfo, rrs);
return schemaInfo.schemaConfig;

View File

@@ -1,10 +1,8 @@
package io.mycat.route.parser.druid.impl.ddl;
import java.sql.SQLNonTransientException;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
import io.mycat.config.ErrorCode;
import io.mycat.config.model.SchemaConfig;
import io.mycat.route.RouteResultset;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
@@ -13,10 +11,13 @@ import io.mycat.route.util.RouterUtil;
import io.mycat.server.util.SchemaUtil;
import io.mycat.server.util.SchemaUtil.SchemaInfo;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
public class DruidDropTableParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
SQLDropTableStatement dropTable = (SQLDropTableStatement) stmt;
if(dropTable.getTableSources().size()>1){
String msg = "dropping multi-tables is not supported, sql:" + stmt;
@@ -25,8 +26,8 @@ public class DruidDropTableParser extends DefaultDruidParser {
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, dropTable.getTableSources().get(0));
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
RouterUtil.routeToDDLNode(schemaInfo, rrs);
return schemaInfo.schemaConfig;

View File

@@ -1,10 +1,8 @@
package io.mycat.route.parser.druid.impl.ddl;
import java.sql.SQLNonTransientException;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
import io.mycat.config.ErrorCode;
import io.mycat.config.model.SchemaConfig;
import io.mycat.route.RouteResultset;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
@@ -13,16 +11,18 @@ import io.mycat.route.util.RouterUtil;
import io.mycat.server.util.SchemaUtil;
import io.mycat.server.util.SchemaUtil.SchemaInfo;
import java.sql.SQLException;
public class DruidTruncateTableParser extends DefaultDruidParser {
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor)
throws SQLNonTransientException {
throws SQLException {
String schemaName = schema == null ? null : schema.getName();
SQLTruncateStatement truncateTable = (SQLTruncateStatement) stmt;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, truncateTable.getTableSources().get(0));
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
RouterUtil.routeToDDLNode(schemaInfo, rrs);
return schemaInfo.schemaConfig;

View File

@@ -28,6 +28,7 @@ import io.mycat.sqlengine.EngineCtx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
/**
@@ -90,8 +91,8 @@ public class BatchInsertSequence implements Catlet {
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, insert.getTableSource());
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined";
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000",ErrorCode.ER_NO_DB_ERROR);
}
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.schema));
if(!MycatPrivileges.checkPrivilege(rrs.getSession().getSource(), schemaInfo.schema, schemaInfo.table, Checktype.INSERT)){

View File

@@ -6,6 +6,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.wall.spi.WallVisitorUtils;
import io.mycat.MycatServer;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.ErrorCode;
import io.mycat.config.loader.console.ZookeeperPath;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.TableConfig;
@@ -28,6 +29,7 @@ import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -119,7 +121,7 @@ public class RouterUtil {
return count;
}
public static RouteResultset routeFromParser(DruidParser druidParser, SchemaConfig schema, RouteResultset rrs, SQLStatement statement, String originSql,LayerCachePool cachePool,MycatSchemaStatVisitor visitor) throws SQLNonTransientException{
public static RouteResultset routeFromParser(DruidParser druidParser, SchemaConfig schema, RouteResultset rrs, SQLStatement statement, String originSql,LayerCachePool cachePool,MycatSchemaStatVisitor visitor) throws SQLException {
schema = druidParser.parser(schema, rrs, statement, originSql,cachePool,visitor);
if(rrs.isFinishedExecute()){
return null;
@@ -207,7 +209,7 @@ public class RouterUtil {
}
public static void routeToDDLNode(SchemaInfo schemaInfo, RouteResultset rrs) throws SQLNonTransientException {
public static void routeToDDLNode(SchemaInfo schemaInfo, RouteResultset rrs) throws SQLException {
String stmt = getFixedSql(removeSchema(rrs.getStatement(),schemaInfo.schema));
List<String> dataNodes;
Map<String, TableConfig> tables = schemaInfo.schemaConfig.getTables();
@@ -215,7 +217,8 @@ public class RouterUtil {
if (tables != null && (tc != null)) {
dataNodes = tc.getDataNodes();
} else {
throw new SQLNonTransientException("table '" + schemaInfo.table + "' doesn't exist");
String msg = "Table '"+schemaInfo.schema+"."+schemaInfo.table+"' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
Iterator<String> iterator1 = dataNodes.iterator();
int nodeSize = dataNodes.size();
@@ -551,7 +554,7 @@ public class RouterUtil {
}
public static void routeToRandomNode(RouteResultset rrs,
SchemaConfig schema, String tableName) {
SchemaConfig schema, String tableName) throws SQLException {
String dataNode = getRandomDataNode(schema, tableName);
routeToSingleNode(rrs,dataNode);
}
@@ -565,12 +568,15 @@ public class RouterUtil {
* @author mycat
*/
private static String getRandomDataNode(SchemaConfig schema,
String table) {
String table) throws SQLException {
String dataNode = null;
Map<String, TableConfig> tables = schema.getTables();
TableConfig tc;
if (tables != null && (tc = tables.get(table)) != null) {
dataNode = tc.getRandomDataNode();
} else {
String msg = "Table '"+schema.getName()+"."+table+"' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
return dataNode;
}
@@ -637,7 +643,7 @@ public class RouterUtil {
*/
public static RouteResultset tryRouteForTables(SchemaConfig schema, DruidShardingParseInfo ctx,
RouteCalculateUnit routeUnit, RouteResultset rrs, boolean isSelect, LayerCachePool cachePool)
throws SQLNonTransientException {
throws SQLException {
List<String> tables = ctx.getTables();
@@ -671,9 +677,8 @@ public class RouterUtil {
for(String tableName : tables) {
TableConfig tableConfig = schema.getTables().get(tableName);
if(tableConfig == null) {
String msg = "can't find table define in schema "+ tableName + " schema:" + schema.getName();
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
String msg = "Table '"+schema.getName()+"."+tableName+"' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
if (!tableConfig.isGlobalTable() && tablesRouteMap.get(tableName) == null) { // 余下的表都是单库表
@@ -714,12 +719,11 @@ public class RouterUtil {
*/
public static RouteResultset tryRouteForOneTable(SchemaConfig schema, DruidShardingParseInfo ctx,
RouteCalculateUnit routeUnit, String tableName, RouteResultset rrs, boolean isSelect,
LayerCachePool cachePool) throws SQLNonTransientException {
LayerCachePool cachePool) throws SQLException {
TableConfig tc = schema.getTables().get(tableName);
if(tc == null) {
String msg = "can't find table [" + tableName + "] define in schema:" + schema.getName();
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
String msg = "Table '"+schema.getName()+"."+tableName+"' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}

View File

@@ -25,6 +25,8 @@ package io.mycat.server;
import java.io.IOException;
import java.nio.channels.NetworkChannel;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
@@ -241,11 +243,20 @@ public class ServerConnection extends FrontendConnection {
schema, type, sql, this.charset, this);
} catch (Exception e) {
StringBuilder s = new StringBuilder();
LOGGER.warn(s.append(this).append(sql).toString() + " err:" + e.toString(),e);
String msg = e.getMessage();
writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
return;
if(e instanceof SQLException && !(e instanceof SQLNonTransientException)) {
SQLException sqle = (SQLException)e;
StringBuilder s = new StringBuilder();
LOGGER.warn(s.append(this).append(sql).toString() + " err:" + sqle.toString(), sqle);
String msg = sqle.getMessage();
writeErrMessage(sqle.getErrorCode(), msg == null ? sqle.getClass().getSimpleName() : msg);
return;
}else {
StringBuilder s = new StringBuilder();
LOGGER.warn(s.append(this).append(sql).toString() + " err:" + e.toString(), e);
String msg = e.getMessage();
writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
return;
}
}
if (rrs != null) {
// session执行

View File

@@ -59,6 +59,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.*;
@@ -309,15 +310,15 @@ public class ExplainHandler {
}
}
private static boolean isInsertSeq(String stmt, SchemaConfig schema) throws SQLNonTransientException {
private static boolean isInsertSeq(String stmt, SchemaConfig schema) throws SQLException {
SQLStatementParser parser = new MySqlStatementParser(stmt);
MySqlInsertStatement statement = (MySqlInsertStatement) parser.parseStatement();
String schemaName = schema == null ? null : schema.getName();
SQLExprTableSource tableSource = statement.getTableSource();
SchemaUtil.SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schemaName, tableSource);
if(schemaInfo == null){
String msg = "No Database is selected Or defined";
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
String tableName = schemaInfo.table;
schema = schemaInfo.schemaConfig;

View File

@@ -35,6 +35,7 @@ import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -386,7 +387,7 @@ public final class ServerLoadDataInfileHandler implements LoadDataInfileHandler
rrs.setNodes(nodes);
return rrs;
} catch (SQLNonTransientException e)
} catch (SQLException e)
{
throw new RuntimeException(e);
}

View File

@@ -1,26 +1,16 @@
package io.mycat.server.util;
import java.sql.SQLNonTransientException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;
import io.mycat.MycatServer;
import io.mycat.config.ErrorCode;
import io.mycat.config.MycatPrivileges;
import io.mycat.config.MycatPrivileges.Checktype;
import io.mycat.config.model.SchemaConfig;
@@ -28,7 +18,12 @@ import io.mycat.route.util.RouterUtil;
import io.mycat.server.ServerConnection;
import io.mycat.util.StringUtil;
import static io.mycat.server.parser.ServerParseShow.FULL_TABLE_CHECK;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static io.mycat.server.parser.ServerParseShow.TABLE_PAT;
/**
@@ -88,7 +83,7 @@ public class SchemaUtil
}
public static SchemaInfo isNoSharding(ServerConnection source,String schema, SQLSelectQuery sqlSelectQuery, SQLStatement selectStmt)
throws SQLNonTransientException {
throws SQLException {
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
return isNoSharding(source, schema, ((MySqlSelectQueryBlock) sqlSelectQuery).getFrom(), selectStmt);
} else if (sqlSelectQuery instanceof MySqlUnionQuery) {
@@ -98,7 +93,7 @@ public class SchemaUtil
}
}
private static SchemaInfo isNoSharding(ServerConnection source,String schema, MySqlUnionQuery sqlSelectQuery, SQLStatement stmt)
throws SQLNonTransientException {
throws SQLException {
SQLSelectQuery left = sqlSelectQuery.getLeft();
SQLSelectQuery right = sqlSelectQuery.getRight();
SchemaInfo leftInfo = isNoSharding(source, schema, left, stmt);
@@ -112,7 +107,7 @@ public class SchemaUtil
return StringUtil.equals(leftInfo.schema, rightInfo.schema)?leftInfo:null;
}
private static SchemaInfo isNoSharding(ServerConnection source,String schema, SQLTableSource tables, SQLStatement stmt)
throws SQLNonTransientException {
throws SQLException {
if (tables instanceof SQLExprTableSource) {
return isNoSharding(source, schema, (SQLExprTableSource) tables, stmt);
} else if (tables instanceof SQLJoinTableSource) {
@@ -126,11 +121,11 @@ public class SchemaUtil
}
private static SchemaInfo isNoSharding(ServerConnection source, String schema, SQLExprTableSource table, SQLStatement stmt)
throws SQLNonTransientException {
throws SQLException {
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(schema, table);
if (schemaInfo == null) {
String msg = "No MyCAT Database is selected Or defined, sql:" + stmt;
throw new SQLNonTransientException(msg);
String msg = "No database selected";
throw new SQLException(msg,"3D000", ErrorCode.ER_NO_DB_ERROR);
}
Checktype chekctype = Checktype.SELECT;
if (stmt instanceof MySqlUpdateStatement) {
@@ -153,7 +148,7 @@ public class SchemaUtil
}
public static SchemaInfo isNoSharding(ServerConnection source, String schema, SQLJoinTableSource tables, SQLStatement stmt)
throws SQLNonTransientException {
throws SQLException {
SQLTableSource left = tables.getLeft();
SQLTableSource right = tables.getRight();
SchemaInfo leftInfo = isNoSharding(source, schema, left, stmt);

View File

@@ -1,5 +1,6 @@
package io.mycat.route;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.List;
import java.util.Map;
@@ -49,7 +50,7 @@ public class DruidMysqlCreateTableTest
}
@Test
public void testCreate() throws SQLNonTransientException {
public void testCreate() throws SQLException {
SchemaConfig schema = schemaMap.get("mysqldb");
RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, originSql1, null,
@@ -66,7 +67,7 @@ public class DruidMysqlCreateTableTest
}
// @Test
public void testInsert() throws SQLNonTransientException {
public void testInsert() throws SQLException {
SchemaConfig schema = schemaMap.get("mysqldb");
RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, "insert into autoslot (id,sid) values(1,2) ", null,

View File

@@ -1,5 +1,6 @@
package io.mycat.route;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.Map;
@@ -29,7 +30,7 @@ public class DruidMysqlHavingTest
}
@Test
public void testHaving() throws SQLNonTransientException {
public void testHaving() throws SQLException {
String sql = "select avg(offer_id) avgofferid, member_id from offer_detail group by member_id having avgofferid > 100";
SchemaConfig schema = schemaMap.get("cndb");
RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null,

View File

@@ -1,5 +1,6 @@
package io.mycat.route;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.Map;
@@ -30,7 +31,7 @@ public class DruidMysqlSqlParserTest
}
@Test
public void testLimitPage() throws SQLNonTransientException {
public void testLimitPage() throws SQLException {
String sql = "select * from offer order by id desc limit 5,10";
SchemaConfig schema = schemaMap.get("mysqldb");
RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null,
@@ -75,7 +76,7 @@ public class DruidMysqlSqlParserTest
}
@Test
public void testLockTableSql() throws SQLNonTransientException{
public void testLockTableSql() throws SQLException {
String sql = "lock tables goods write";
SchemaConfig schema = schemaMap.get("TESTDB");
RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.LOCK, sql, null, null, cachePool);

View File

@@ -1,5 +1,6 @@
package io.mycat.route;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.Map;
@@ -32,7 +33,7 @@ public class TestSelectBetweenSqlParser {
}
@Test
public void testBetweenSqlRoute() throws SQLNonTransientException {
public void testBetweenSqlRoute() throws SQLException {
String sql = "select * from offer_detail where offer_id between 1 and 33";
SchemaConfig schema = schemaMap.get("cndb");
RouteResultset rrs = RouteStrategyFactory.getRouteStrategy().route(new SystemConfig(),schema, -1, sql, null,

View File

@@ -23,6 +23,7 @@
*/
package io.mycat.route.perf;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import io.mycat.SimpleCachePool;
@@ -47,7 +48,7 @@ public class NoShardingSpace {
schema = schemaLoader.getSchemas().get("dubbo");
}
public void testDefaultSpace() throws SQLNonTransientException {
public void testDefaultSpace() throws SQLException {
SchemaConfig schema = this.schema;
String stmt = "insert into offer (member_id, gmt_create) values ('1','2001-09-13 20:20:33')";
for (int i = 0; i < total; i++) {
@@ -55,7 +56,7 @@ public class NoShardingSpace {
}
}
public static void main(String[] args) throws SQLNonTransientException {
public static void main(String[] args) throws SQLException {
NoShardingSpace test = new NoShardingSpace();
System.currentTimeMillis();

View File

@@ -23,6 +23,7 @@
*/
package io.mycat.route.perf;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import io.mycat.SimpleCachePool;
@@ -50,7 +51,7 @@ public class ShardingDefaultSpace {
/**
* 路由到defaultSpace的性能测试
*/
public void testDefaultSpace() throws SQLNonTransientException {
public void testDefaultSpace() throws SQLException {
SchemaConfig schema = this.getSchema();
String sql = "insert into offer (member_id, gmt_create) values ('1','2001-09-13 20:20:33')";
for (int i = 0; i < total; i++) {

View File

@@ -23,6 +23,7 @@
*/
package io.mycat.route.perf;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import io.mycat.SimpleCachePool;
@@ -52,7 +53,7 @@ public class ShardingMultiTableSpace {
*
* @throws SQLNonTransientException
*/
public void testTableSpace() throws SQLNonTransientException {
public void testTableSpace() throws SQLException {
SchemaConfig schema = getSchema();
String sql = "select id,member_id,gmt_create from offer where member_id in ('1','22','333','1124','4525')";
for (int i = 0; i < total; i++) {