use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class SQLite_SelectTest_0 method test_0.
public void test_0() throws Exception {
String sql = "select first_name, last_name from mytable limit 3, 4";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.SQLITE);
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor 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(1, visitor.getTables().size());
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("mytable")));
Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
}
use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class SQLServerCreateTableTest_8 method test_0.
public void test_0() throws Exception {
String sql = "create table ACT_RU_VARIABLE (" + " ID_ nvarchar(64) not null," + " DOUBLE_ double precision," + " LONG_ numeric(19,0)," + " TEXT_ nvarchar(4000)," + " primary key (ID_)" + ")";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
Assert.assertEquals(1, statementList.size());
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
Assert.assertEquals("CREATE TABLE ACT_RU_VARIABLE (" + "\n\tID_ nvarchar(64) NOT NULL," + "\n\tDOUBLE_ DOUBLE PRECISION," + "\n\tLONG_ numeric(19, 0)," + "\n\tTEXT_ nvarchar(4000)," + "\n\tPRIMARY KEY (ID_)" + "\n)", output);
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
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(4, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ACT_RU_VARIABLE")));
Assert.assertTrue(visitor.getColumns().contains(new Column("ACT_RU_VARIABLE", "ID_")));
Assert.assertTrue(visitor.getColumns().contains(new Column("ACT_RU_VARIABLE", "DOUBLE_")));
}
use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class ExportConditions method evaluate.
public String evaluate(String sql, String dbType, Boolean compactValues) {
try {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : statementList) {
stmt.accept(visitor);
}
List<List<Object>> rows = new ArrayList<List<Object>>();
List<Condition> conditions = visitor.getConditions();
for (int i = 0; i < conditions.size(); ++i) {
TableStat.Condition condition = conditions.get(i);
Column column = condition.getColumn();
String operator = condition.getOperator();
List<Object> values = condition.getValues();
List<Object> row = new ArrayList<Object>();
row.add(column.getTable());
row.add(column.getName());
row.add(operator);
if (values.size() == 0) {
row.add(null);
} else if (values.size() == 1) {
if (compactValues != null && compactValues.booleanValue()) {
row.add(values);
} else {
row.add(values.get(0));
}
} else {
row.add(values);
}
rows.add(row);
}
return JSONUtils.toJSONString(rows);
} catch (Throwable ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class DB2SelectTest_12 method test_0.
public void test_0() throws Exception {
String sql = "SELECT * FROM TASK_POLICE_QUERY_BATCH WHERE STATUS = '0' ORDER BY PRIORITY FETCH FIRST 100 ROWS ONLY";
DB2StatementParser parser = new DB2StatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
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(1, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("TASK_POLICE_QUERY_BATCH")));
Assert.assertTrue(visitor.getColumns().contains(new Column("TASK_POLICE_QUERY_BATCH", "STATUS")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("SELECT *" + "\nFROM TASK_POLICE_QUERY_BATCH" + "\nWHERE STATUS = '0'" + "\nORDER BY PRIORITY" + //
"\nFETCH FIRST 100 ROWS ONLY", SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("select *" + "\nfrom TASK_POLICE_QUERY_BATCH" + "\nwhere STATUS = '0'" + "\norder by PRIORITY" + //
"\nfetch first 100 rows only", SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.
the class DB2SelectTest_13 method test_0.
public void test_0() throws Exception {
String sql = //
"SELECT WORKDEPT, EMPNO, SALARY, BONUS, COMM " + //
" FROM DSN8B10.EMP" + //
" WHERE WORKDEPT IN ('D11','D21')" + " FOR UPDATE;";
DB2StatementParser parser = new DB2StatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
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(5, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("DSN8B10.EMP")));
Assert.assertTrue(visitor.getColumns().contains(new Column("DSN8B10.EMP", "WORKDEPT")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("SELECT WORKDEPT, EMPNO, SALARY, BONUS, COMM" + "\nFROM DSN8B10.EMP" + "\nWHERE WORKDEPT IN ('D11', 'D21')" + //
"\nFOR UPDATE", SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("select WORKDEPT, EMPNO, SALARY, BONUS, COMM" + "\nfrom DSN8B10.EMP" + "\nwhere WORKDEPT in ('D11', 'D21')" + //
"\nfor update", SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Aggregations