use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableTest_addFk method test_alter_first.
public void test_alter_first() throws Exception {
String sql = //
"alter table Test2 add index FK4CF5DC0F5DD7C31 (test1_name), " + "add constraint FK4CF5DC0F5DD7C31 foreign key (test1_name) references Test1 (name)";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.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(//
"ALTER TABLE Test2" + //
"\n\tADD INDEX FK4CF5DC0F5DD7C31 (test1_name)," + "\n\tADD CONSTRAINT FK4CF5DC0F5DD7C31 FOREIGN KEY (test1_name) REFERENCES Test1 (name)", SQLUtils.toMySqlString(stmt));
Assert.assertEquals(//
"alter table Test2" + //
"\n\tadd index FK4CF5DC0F5DD7C31 (test1_name)," + "\n\tadd constraint FK4CF5DC0F5DD7C31 foreign key (test1_name) references Test1 (name)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(2, visitor.getTables().size());
Assert.assertEquals(2, visitor.getColumns().size());
TableStat tableStat = visitor.getTableStat("Test2");
assertNotNull(tableStat);
assertEquals(1, tableStat.getAlterCount());
assertEquals(1, tableStat.getCreateIndexCount());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlSchemaStatVisitorTest_Alter method test_0.
public void test_0() throws Exception {
String sql = "alter table sql_perf add index `idx_instance_8` (`host`,`port`,`hashcode`,`item`,`time`,`value`);";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.accept(visitor);
// System.out.println(sql);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
assertEquals(1, visitor.getTables().size());
assertTrue(visitor.containsTable("sql_perf"));
assertEquals(6, visitor.getColumns().size());
assertTrue(visitor.getColumns().contains(new Column("sql_perf", "host")));
assertTrue(visitor.getColumns().contains(new Column("sql_perf", "port")));
assertTrue(visitor.getColumns().contains(new Column("sql_perf", "hashcode")));
assertTrue(visitor.getColumns().contains(new Column("sql_perf", "item")));
assertTrue(visitor.getColumns().contains(new Column("sql_perf", "time")));
assertTrue(visitor.getColumns().contains(new Column("sql_perf", "value")));
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class OdpsInsertTest method exec_test.
public void exec_test(String resource) throws Exception {
// System.out.println(resource);
InputStream is = null;
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
Reader reader = new InputStreamReader(is, "UTF-8");
String input = Utils.read(reader);
JdbcUtils.close(reader);
String[] items = input.split("---------------------------");
String sql = items[0].trim();
String expect = items[1].trim();
OdpsStatementParser parser = new OdpsStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = new OdpsSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println(sql);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
//
// System.out.println();
// System.out.println("---------------------------");
// System.out.println(SQLUtils.toOdpsString(stmt));
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class OdpsSelectTest_over_rows_4 method test_select.
public void test_select() throws Exception {
//
String sql = "SELECT sum(c) over(order by ds rows UNBOUNDED PRECEDING) s, ds FROM t1";
Assert.assertEquals("SELECT SUM(c) OVER (ORDER BY ds ROWS UNBOUNDED PRECEDING) AS s\n" + "\t, ds\n" + "FROM t1", SQLUtils.formatOdps(sql));
Assert.assertEquals("select sum(c) over (order by ds rows unbounded preceding) as s\n" + "\t, ds\n" + "from t1", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
stmt.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(1, visitor.getTables().size());
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
// Assert.assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class OdpsSelect_distribute_by_Test method exec_test.
public void exec_test(String resource) throws Exception {
// System.out.println(resource);
InputStream is = null;
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
Reader reader = new InputStreamReader(is, "UTF-8");
String input = Utils.read(reader);
JdbcUtils.close(reader);
String[] items = input.split("---------------------------");
String sql = items[0].trim();
String expect = items[1].trim();
OdpsStatementParser parser = new OdpsStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = new OdpsSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println(sql);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
//
// System.out.println();
// System.out.println("---------------------------");
// System.out.println(SQLUtils.toOdpsString(stmt));
}
Aggregations