use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.
the class PGSelectTest21 method test_1.
public void test_1() throws Exception {
String sql = "with a(a1,b1) as (select * from b) select * from a";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
// print(statementList);
assertTrue(statemen instanceof PGSelectStatement);
assertEquals(1, ((PGSelectStatement) statemen).getSelect().getWithSubQuery().getEntries().size());
StringBuffer sb = new StringBuffer();
String alias = ((PGSelectStatement) statemen).getSelect().getWithSubQuery().getEntries().get(0).getAlias();
assertEquals("a", alias);
assertEquals(2, ((PGSelectStatement) statemen).getSelect().getWithSubQuery().getEntries().get(0).getColumns().size());
}
use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.
the class PGSelectTest23 method test_0.
public void test_0() throws Exception {
String sql = "select id, login_name, name, password, salt, roles, register_date from user WHERE ( name like ? ) limit ? offset ?";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
// print(statementList);
Assert.assertEquals("SELECT id, login_name, name, password, salt" + "\n\t, roles, register_date" + "\nFROM user" + "\nWHERE name LIKE ?" + "\nLIMIT ? OFFSET ?", SQLUtils.toPGString(stmt));
Assert.assertEquals("select id, login_name, name, password, salt" + "\n\t, roles, register_date" + "\nfrom user" + "\nwhere name like ?" + "\nlimit ? offset ?", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
Assert.assertEquals(7, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getTables().size());
}
use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.
the class PGSelectTest25 method test_0.
public void test_0() throws Exception {
String sql = "select COALESCE((SELECT project_deduct_mandays from mytable_01 limit 1)) from t";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
Assert.assertEquals("SELECT COALESCE((" + "\n\t\tSELECT project_deduct_mandays" + "\n\t\tFROM mytable_01" + "\n\t\tLIMIT 1" + "\n\t))" + "\nFROM t", SQLUtils.toPGString(stmt));
Assert.assertEquals("select COALESCE((" + "\n\t\tselect project_deduct_mandays" + "\n\t\tfrom mytable_01" + "\n\t\tlimit 1" + "\n\t))" + "\nfrom t", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
Assert.assertEquals(1, visitor.getColumns().size());
Assert.assertEquals(2, visitor.getTables().size());
}
use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.
the class PGSelectTest27 method test_0.
public void test_0() throws Exception {
String sql = "select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json->>2 from dual";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
Assert.assertEquals("SELECT '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json ->> 2" + "\nFROM dual", SQLUtils.toPGString(stmt));
Assert.assertEquals("select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json ->> 2" + "\nfrom dual", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getTables().size());
}
use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.
the class PGSelectTest29 method test_0.
public void test_0() throws Exception {
String sql = "select '{\"a\": {\"b\":{\"c\": \"foo\"}}}'::json #>> '{a,b}' from dual";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
Assert.assertEquals("SELECT '{\"a\": {\"b\":{\"c\": \"foo\"}}}'::json #>> '{a,b}'" + "\nFROM dual", SQLUtils.toPGString(stmt));
Assert.assertEquals("select '{\"a\": {\"b\":{\"c\": \"foo\"}}}'::json #>> '{a,b}'" + "\nfrom dual", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getTables().size());
}
Aggregations