本文整理汇总了Java中com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser类的典型用法代码示例。如果您正苦于以下问题:Java MySqlStatementParser类的具体用法?Java MySqlStatementParser怎么用?Java MySqlStatementParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MySqlStatementParser类属于com.alibaba.druid.sql.dialect.mysql.parser包,在下文中一共展示了MySqlStatementParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: changeCreateTable
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
private static String changeCreateTable(SchemaConfig schema,String tableName,String sql) {
if (schema.getTables().containsKey(tableName)) {
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement insertStatement = parser.parseStatement();
if (insertStatement instanceof MySqlCreateTableStatement) {
TableConfig tableConfig = schema.getTables().get(tableName);
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
if (algorithm instanceof SlotFunction) {
SQLColumnDefinition column = new SQLColumnDefinition();
column.setDataType(new SQLCharacterDataType("int"));
column.setName(new SQLIdentifierExpr("_slot"));
column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
((SQLCreateTableStatement) insertStatement).getTableElementList().add(column);
return insertStatement.toString();
}
}
}
return sql;
}
示例2: updateMetaData
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
public void updateMetaData(String schema, String sql, boolean isSuccess, boolean needNotifyOther) {
SQLStatementParser parser = new MySqlStatementParser(sql);
SQLStatement statement = parser.parseStatement();
if (statement instanceof MySqlCreateTableStatement) {
createTable(schema, sql, (MySqlCreateTableStatement) statement, isSuccess, needNotifyOther);
} else if (statement instanceof SQLDropTableStatement) {
dropTable(schema, sql, (SQLDropTableStatement) statement, isSuccess, needNotifyOther);
} else if (statement instanceof SQLAlterTableStatement) {
alterTable(schema, sql, (SQLAlterTableStatement) statement, isSuccess, needNotifyOther);
} else if (statement instanceof SQLTruncateStatement) {
truncateTable(schema, sql, (SQLTruncateStatement) statement, isSuccess, needNotifyOther);
} else if (statement instanceof SQLCreateIndexStatement) {
createIndex(schema, sql, (SQLCreateIndexStatement) statement, isSuccess, needNotifyOther);
} else if (statement instanceof SQLDropIndexStatement) {
dropIndex(schema, sql, (SQLDropIndexStatement) statement, isSuccess, needNotifyOther);
} else {
// TODO: further
}
}
示例3: isSupportSelect
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
private static boolean isSupportSelect(String stmt) {
SQLStatementParser parser = new MySqlStatementParser(stmt);
SQLStatement statement = parser.parseStatement();
if (!(statement instanceof SQLSelectStatement)) {
return false;
}
SQLSelectQuery sqlSelectQuery = ((SQLSelectStatement) statement).getSelect().getQuery();
if (!(sqlSelectQuery instanceof MySqlSelectQueryBlock)) {
return false;
}
MySqlSelectQueryBlock selectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
SQLTableSource mysqlFrom = selectQueryBlock.getFrom();
if (mysqlFrom != null) {
return false;
}
for (SQLSelectItem item : selectQueryBlock.getSelectList()) {
SQLExpr selectItem = item.getExpr();
if (!isVariantRef(selectItem)) {
return false;
}
}
return true;
}
示例4: isInsertSeq
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
private static boolean isInsertSeq(ServerConnection c, 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(c.getUser(), schemaName, tableSource);
String tableName = schemaInfo.getTable();
schema = schemaInfo.getSchemaConfig();
TableConfig tableConfig = schema.getTables().get(tableName);
if (tableConfig == null) {
return false;
} else if (tableConfig.isAutoIncrement()) {
return true;
}
return false;
}
示例5: parserSQL
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
@Override
public SQLStatement parserSQL(String originSql) throws SQLSyntaxErrorException {
SQLStatementParser parser = new MySqlStatementParser(originSql);
/**
* thrown SQL SyntaxError if parser error
*/
try {
List<SQLStatement> list = parser.parseStatementList();
if (list.size() > 1) {
throw new SQLSyntaxErrorException("MultiQueries is not supported,use single query instead ");
}
return list.get(0);
} catch (Exception t) {
LOGGER.info("routeNormalSqlWithAST", t);
if (t.getMessage() != null) {
throw new SQLSyntaxErrorException(t.getMessage());
} else {
throw new SQLSyntaxErrorException(t);
}
}
}
示例6: test
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
public void test(String sql) throws Exception {
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SqlVisitor visitor = new SqlVisitor(null);
statementList.get(0).accept(visitor);
System.out.println();
System.out.println(sql);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("alias : " + visitor.getAliasMap());
System.out.println("conditions : " + visitor.getConditions());
System.out.println("columns : " + visitor.getColumns());
}
示例7: test
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
private List<List<TableStat.Condition>> test(String sql){
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement statemen = parser.parseStatement();
List<Object> parameters = new ArrayList<Object>();
parameters.add(10);
OrVisitor visitor = new OrVisitor();
OrVisitor.OrEntity orEntity = new OrVisitor.OrEntity(visitor, statemen);
List<List<TableStat.Condition>> conditions = orEntity.getOrConditions();
System.out.println();
System.out.println(sql);
System.out.println(conditions);
System.out.println();
return conditions;
}
示例8: test
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
public void test(String sql, String logicName, String targetName){
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement statement = parser.parseStatement();
ReplaceTableNameVisitor replaceVisitor = new ReplaceTableNameVisitor(logicName, targetName);
StringBuilder out = new StringBuilder();
MySqlOutputVisitor outPutVisitor = new MySqlOutputVisitor(out);
// 替换表名
statement.accept(replaceVisitor);
// 输出sql
statement.accept(outPutVisitor);
System.out.println();
System.out.println(sql);
System.out.println(out.toString());
// 输出sql后要还原statement以便下次替换表名
replaceVisitor.reset();
}
示例9: getCountSql
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
@Override
public String getCountSql(String sql) {
SQLStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
// 将AST通过visitor输出
StringBuilder out = new StringBuilder();
MysqlCountOutputVisitor visitor = new MysqlCountOutputVisitor(out);
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLSelectStatement) {
stmt.accept(visitor);
out.append(";");
}
}
return out.toString();
}
示例10: route
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
public void route(SystemConfig sysConfig, SchemaConfig schema,int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool) {
int rs = ServerParse.parse(realSQL);
this.sqltype = rs & 0xff;
this.sysConfig=sysConfig;
this.schema=schema;
this.charset=charset;
this.sc=sc;
this.cachePool=cachePool;
try {
// RouteStrategy routes=RouteStrategyFactory.getRouteStrategy();
// rrs =RouteStrategyFactory.getRouteStrategy().route(sysConfig, schema, sqlType2, realSQL,charset, sc, cachePool);
MySqlStatementParser parser = new MySqlStatementParser(realSQL);
SQLStatement statement = parser.parseStatement();
if(statement instanceof SQLSelectStatement) {
SQLSelectStatement st=(SQLSelectStatement)statement;
SQLSelectQuery sqlSelectQuery =st.getSelect().getQuery();
if(sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock)st.getSelect().getQuery();
joinParser=new JoinParser(mysqlSelectQuery,realSQL);
joinParser.parser();
}
}
/*
if (routes instanceof DruidMysqlRouteStrategy) {
SQLSelectStatement st=((DruidMysqlRouteStrategy) routes).getSQLStatement();
SQLSelectQuery sqlSelectQuery =st.getSelect().getQuery();
if(sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock)st.getSelect().getQuery();
joinParser=new JoinParser(mysqlSelectQuery,realSQL);
joinParser.parser();
}
}
*/
} catch (Exception e) {
}
}
示例11: isMycatSeq
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
private static boolean isMycatSeq(String stmt, SchemaConfig schema)
{
if(pattern.matcher(stmt).find()) {
return true;
}
SQLStatementParser parser =new MySqlStatementParser(stmt);
MySqlInsertStatement statement = (MySqlInsertStatement) parser.parseStatement();
String tableName= statement.getTableName().getSimpleName();
TableConfig tableConfig= schema.getTables().get(tableName.toUpperCase());
if(tableConfig==null) {
return false;
}
if(tableConfig.isAutoIncrement())
{
boolean isHasIdInSql=false;
String primaryKey = tableConfig.getPrimaryKey();
List<SQLExpr> columns = statement.getColumns();
for (SQLExpr column : columns)
{
String columnName = column.toString();
if(primaryKey.equalsIgnoreCase(columnName))
{
isHasIdInSql = true;
break;
}
}
if(!isHasIdInSql) {
return true;
}
}
return false;
}
示例12: parseConditionValues
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
/**
* 解析 SQL 获取指定表及条件列的值
*
* @param sql
* @param tableName
* @param colnumName
* @return
*/
public List<Object> parseConditionValues(String sql, String tableName, String colnumName) {
List<Object> values = null;
if ( sql != null && tableName != null && columnName != null ) {
values = new ArrayList<Object>();
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatement();
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
String currentTable = visitor.getCurrentTable();
if ( tableName.equalsIgnoreCase( currentTable ) ) {
List<Condition> conditions = visitor.getConditions();
for(Condition condition: conditions) {
String ccN = condition.getColumn().getName();
ccN = fixName(ccN);
if ( colnumName.equalsIgnoreCase( ccN ) ) {
List<Object> ccVL = condition.getValues();
values.addAll( ccVL );
}
}
}
}
return values;
}
示例13: throwExceptionParse
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
public void throwExceptionParse(String sql, boolean throwException) throws NoSuchMethodException {
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement sqlStatement = statementList.get(0);
MySqlUpdateStatement update = (MySqlUpdateStatement) sqlStatement;
SchemaConfig schemaConfig = mock(SchemaConfig.class);
Map<String, TableConfig> tables = mock(Map.class);
TableConfig tableConfig = mock(TableConfig.class);
String tableName = "hotnews";
when((schemaConfig).getTables()).thenReturn(tables);
when(tables.get(tableName)).thenReturn(tableConfig);
when(tableConfig.getParentTC()).thenReturn(null);
RouteResultset routeResultset = new RouteResultset(sql, 11);
Class c = DruidUpdateParser.class;
Method method = c.getDeclaredMethod("confirmShardColumnNotUpdated", new Class[]{SQLUpdateStatement.class, SchemaConfig.class, String.class, String.class, String.class, RouteResultset.class});
method.setAccessible(true);
try {
method.invoke(c.newInstance(), update, schemaConfig, tableName, "ID", "", routeResultset);
if (throwException) {
System.out.println("未抛异常,解析通过则不对!");
Assert.assertTrue(false);
} else {
System.out.println("未抛异常,解析通过,此情况分片字段可能在update语句中但是实际不会被更新");
Assert.assertTrue(true);
}
} catch (Exception e) {
if (throwException) {
System.out.println(e.getCause().getClass());
Assert.assertTrue(e.getCause() instanceof SQLNonTransientException);
System.out.println("抛异常原因为SQLNonTransientException则正确");
} else {
System.out.println("抛异常,需要检查");
Assert.assertTrue(false);
}
}
}
示例14: isInsertHasSlot
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
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;
}
示例15: testModifySQLLimit
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; //导入依赖的package包/类
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());
}