Search in sources :

Example 6 with SQLGrantStatement

use of com.alibaba.druid.sql.ast.statement.SQLGrantStatement in project druid by alibaba.

the class MySqlGrantTest_ads method test_10.

public void test_10() throws Exception {
    String sql = "GRANT ALL  ON   sysdb.*   TO   '%'@'192.168.1/20', 'ALIYUN$ads_user1@aliyun.com'";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLGrantStatement stmt = (SQLGrantStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    SQLExpr user0 = stmt.getUsers().get(0);
    assertEquals(user0.toString().split("'@'")[0].replaceAll("'", ""), "%");
    assertEquals(user0.toString().split("'@'")[1].replaceAll("'", ""), "192.168.1/20");
    assertEquals("ALIYUN$ads_user1@aliyun.com", stmt.getUsers().get(1).toString().split("'@'")[0].replaceAll("'", ""));
    assertEquals(1, stmt.getUsers().get(1).toString().split("'@'").length);
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    String output = SQLUtils.toMySqlString(stmt);
    assertEquals(// 
    "GRANT ALL ON sysdb.* TO '%'@'192.168.1/20','ALIYUN$ads_user1@aliyun.com'", output);
    assertEquals(1, visitor.getTables().size());
    assertEquals(0, visitor.getColumns().size());
    assertEquals(0, visitor.getConditions().size());
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLGrantStatement(com.alibaba.druid.sql.ast.statement.SQLGrantStatement)

Example 7 with SQLGrantStatement

use of com.alibaba.druid.sql.ast.statement.SQLGrantStatement in project druid by alibaba.

the class MySqlGrantTest_ads method test_11.

public void test_11() throws Exception {
    String sql = "GRANT Select, Show  ON   ads.*   TO    'ALIYUN$ads_user1@aliyun.com'";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLGrantStatement stmt = (SQLGrantStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    assertEquals("ALIYUN$ads_user1@aliyun.com", stmt.getUsers().get(0).toString().split("'@'")[0].replaceAll("'", ""));
    assertEquals(1, stmt.getUsers().get(0).toString().split("'@'").length);
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    String output = SQLUtils.toMySqlString(stmt);
    assertEquals(// 
    "GRANT SELECT, SHOW ON ads.* TO 'ALIYUN$ads_user1@aliyun.com'", output);
    assertEquals(1, visitor.getTables().size());
    assertEquals(0, visitor.getColumns().size());
    assertEquals(0, visitor.getConditions().size());
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLGrantStatement(com.alibaba.druid.sql.ast.statement.SQLGrantStatement)

Example 8 with SQLGrantStatement

use of com.alibaba.druid.sql.ast.statement.SQLGrantStatement in project druid by alibaba.

the class MySqlGrantTest_34 method test_0.

public void test_0() throws Exception {
    String sql = "GRANT USAGE ON *.* TO 'bob'@'%.example.org' IDENTIFIED BY 'cleartext password';";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLGrantStatement stmt = (SQLGrantStatement) statementList.get(0);
    // print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    SQLExprTableSource resource = (SQLExprTableSource) stmt.getResource();
    Assert.assertEquals("*", resource.getSchema());
    String output = SQLUtils.toMySqlString(stmt);
    // 
    Assert.assertEquals(// 
    "GRANT USAGE ON *.* TO 'bob'@'%.example.org' IDENTIFIED BY 'cleartext password';", output);
    // 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(1, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("City")));
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("t2", "id")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLGrantStatement(com.alibaba.druid.sql.ast.statement.SQLGrantStatement)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)8 SQLGrantStatement (com.alibaba.druid.sql.ast.statement.SQLGrantStatement)8 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)8 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1