Search in sources :

Example 66 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGSelectTest42 method test_0.

public void test_0() throws Exception {
    String sql = "UPDATE sys_account AS a SET online = 2 FROM auto_handler_online o WHERE a.id = o.account_id AND a.online != 2";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals("UPDATE sys_account\n" + "SET online = 2\n" + "FROM auto_handler_online o\n" + "WHERE a.id = o.account_id\n" + "\tAND a.online != 2", SQLUtils.toPGString(stmt));
    Assert.assertEquals("update sys_account\n" + "set online = 2\n" + "from auto_handler_online o\n" + "where a.id = o.account_id\n" + "\tand a.online != 2", 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(4, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getTables().size());
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 67 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGSelectTest43 method test_0.

public void test_0() throws Exception {
    String sql = "select task_id from mod_period_time where task_id in (SELECT task_id as b FROM mod_period_time order by task_id desc limit 1)";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals("SELECT task_id\n" + "FROM mod_period_time\n" + "WHERE task_id IN (SELECT task_id AS b\n" + "\tFROM mod_period_time\n" + "\tORDER BY task_id DESC\n" + "\tLIMIT 1)", SQLUtils.toPGString(stmt));
    Assert.assertEquals("select task_id\n" + "from mod_period_time\n" + "where task_id in (select task_id as b\n" + "\tfrom mod_period_time\n" + "\torder by task_id desc\n" + "\tlimit 1)", 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(1, visitor.getTables().size());
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 68 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGSelectTest8 method test_0.

public void test_0() throws Exception {
    String sql = //
    "select id, name, beanId, \"algLable.id\", \"algLable.name\"" + //
    ", \"algLable.schemaName\", \"algLable.tableName\", \"algLable.fieldName\"" + //
    " from cnres.function_select_algmodule_rule() " + //
    " as a( id text,name text,beanId text, \"algLable.id\" text,\"algLable.name\" text" + ", \"algLable.schemaName\" text,\"algLable.tableName\" text, \"algLable.fieldName\" text)";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
    statemen.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    Assert.assertEquals(8, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getTables().size());
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 69 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGAlterTableTest2 method test_0.

public void test_0() throws Exception {
    String sql = "ALTER TABLE \"reviews\" DROP \"review_type\"";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
    statemen.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("reviews")));
    Assert.assertTrue(visitor.getTables().get(new TableStat.Name("reviews")).getAlterCount() == 1);
    Assert.assertTrue(visitor.getColumns().size() == 1);
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 70 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGCommentTest method test_1.

public void test_1() {
    String sql = "select a.id,--single line comment test a.name from a;";
    PGSQLStatementParser parser;
    try {
        parser = new PGSQLStatementParser(sql);
        parser.parseStatementList();
    } catch (Exception e) {
        assertTrue(e instanceof ParserException);
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) ParserException(com.alibaba.druid.sql.parser.ParserException) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Aggregations

PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)113 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)109 PGSchemaStatVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor)102 PGSelectStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement)3 PGExportParameterVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGExportParameterVisitor)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)1 SQLUnionQuery (com.alibaba.druid.sql.ast.statement.SQLUnionQuery)1 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)1 PGDeleteStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement)1 PGInsertStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement)1 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)1 PGOutputVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor)1 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)1 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)1 Column (com.alibaba.druid.stat.TableStat.Column)1