druid sqlparser使用例子

Posted 友哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了druid sqlparser使用例子相关的知识,希望对你有一定的参考价值。

package com.alibaba.druid.bvt.sql.mysql;

import java.util.List;

import org.junit.Assert;

import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;

public class MySqlDeleteTest_0 extends MysqlTest {

public void test_0() throws Exception {
String sql = "DELETE a1, a2 FROM t1 AS a1 INNER JOIN t2 AS a2 WHERE a1.id=a2.id;";

MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
// print(statementList);

Assert.assertEquals(1, statementList.size());

MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.accept(visitor);

// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());

Assert.assertEquals(2, visitor.getTables().size());
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertEquals(2, visitor.getConditions().size());

Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t1")));
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));

Assert.assertTrue(visitor.getColumns().contains(new Column("t1", "id")));
Assert.assertTrue(visitor.getColumns().contains(new Column("t2", "id")));
}
}

  

以上是关于druid sqlparser使用例子的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot初始教程之数据库连接池(druid)

Druid连接池

druid数据源

Spring整合Druid

Druid.jar包

Spring Boot 使用 Druid 连接池详解