use of com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser in project Mycat-Server by MyCATApache.
the class DruidMysqlCreateTableTest method isInsertHasSlot.
private boolean isInsertHasSlot(String sql) {
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlInsertStatement insertStatement = (MySqlInsertStatement) parser.parseStatement();
List<SQLExpr> cc = insertStatement.getColumns();
for (SQLExpr sqlExpr : cc) {
SQLIdentifierExpr c = (SQLIdentifierExpr) sqlExpr;
if ("_slot".equalsIgnoreCase(c.getName()) && cc.size() == insertStatement.getValues().getValues().size())
return true;
}
return false;
}
use of com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser in project Mycat-Server by MyCATApache.
the class GlobalTableUtilTest method parseSql.
public String parseSql(String sql) {
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement statement = parser.parseStatement();
return GlobalTableUtil.addColumnIfCreate(sql, statement);
}
use of com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser in project Mycat-Server by MyCATApache.
the class DruidMysqlRouteStrategyTest method testModifySQLLimit.
public void testModifySQLLimit() throws Exception {
final SchemaConfig schema = schemaMap.get("TESTDB");
String sql = null;
RouteResultset rrs = null;
//SQL span multi datanode
sql = "select * from orders limit 2,3";
rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
Assert.assertEquals(true, rrs.isCacheAble());
Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
NodeNameAsserter nameAsserter = new NodeNameAsserter("dn2", "dn1");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
Assert.assertEquals(3, rrs.getLimitSize());
MySqlStatementParser parser = new MySqlStatementParser("SELECT * FROM orders LIMIT 0,5");
SQLStatement statement = parser.parseStatement();
Assert.assertEquals(statement.toString(), rrs.getNodes()[0].getStatement());
//SQL not span multi datanode
sql = "select * from customer where id=10000 limit 2,3";
rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
Assert.assertEquals(true, rrs.isCacheAble());
nodeMap = getNodeMap(rrs, 1);
nameAsserter = new NodeNameAsserter("dn1");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
Assert.assertEquals(3, rrs.getLimitSize());
Assert.assertEquals("select * from customer where id=10000 limit 2,3", rrs.getNodes()[0].getStatement());
}
use of com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser in project druid by alibaba.
the class MySqlMockExecuteHandlerImpl method executeQuery.
@Override
public ResultSet executeQuery(MockStatementBase statement, String sql) throws SQLException {
SQLStatementParser parser = new MySqlStatementParser(sql);
//
List<SQLStatement> stmtList = parser.parseStatementList();
if (stmtList.size() > 1) {
throw new SQLException("not support multi-statment. " + sql);
}
if (stmtList.size() == 0) {
throw new SQLException("executeQueryError : " + sql);
}
SQLStatement stmt = stmtList.get(0);
if (stmt instanceof CobarShowStatus) {
return showStatus(statement);
}
if (!(stmt instanceof SQLSelectStatement)) {
throw new SQLException("executeQueryError : " + sql);
}
SQLSelect select = ((SQLSelectStatement) stmt).getSelect();
SQLSelectQuery query = select.getQuery();
if (query instanceof SQLSelectQueryBlock) {
return executeQuery(statement, (SQLSelectQueryBlock) query);
}
throw new SQLException("TODO");
}
use of com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser in project druid by alibaba.
the class WallVisitorUtils method check.
public static void check(WallVisitor visitor, SQLCommentHint x) {
if (!visitor.getConfig().isHintAllow()) {
addViolation(visitor, ErrorCode.EVIL_HINTS, "hint not allow", x);
return;
}
String text = x.getText();
text = text.trim();
if (text.startsWith("!")) {
text = text.substring(1);
}
if (text.length() == 0) {
return;
}
int pos = 0;
for (; pos < text.length(); pos++) {
char ch = text.charAt(pos);
if (ch >= '0' && ch <= '9') {
continue;
} else {
break;
}
}
if (pos == 5) {
text = text.substring(5);
text = text.trim();
}
text = text.toUpperCase();
boolean isWhite = false;
for (String hint : whiteHints) {
if (text.equals(hint)) {
isWhite = true;
break;
}
}
if (!isWhite) {
if (text.startsWith("FORCE INDEX") || text.startsWith("IGNORE INDEX")) {
isWhite = true;
}
}
if (!isWhite) {
if (text.startsWith("SET")) {
SQLStatementParser parser = new MySqlStatementParser(text);
List<SQLStatement> statementList = parser.parseStatementList();
if (statementList != null && statementList.size() > 0) {
SQLStatement statement = statementList.get(0);
if (statement instanceof SQLSetStatement || statement instanceof MySqlSetCharSetStatement || statement instanceof MySqlSetNamesStatement) {
isWhite = true;
}
}
}
}
if (!isWhite) {
addViolation(visitor, ErrorCode.EVIL_HINTS, "hint not allow", x);
}
}
Aggregations