Search in sources :

Example 6 with SelectExpressionItem

use of net.sf.jsqlparser.statement.select.SelectExpressionItem in project JSqlParser by JSQLParser.

the class SelectUtilsTest method testBuildSelectFromTableAndParsedExpression.

@Test
public void testBuildSelectFromTableAndParsedExpression() throws JSQLParserException {
    Select select = SelectUtils.buildSelectFromTableAndExpressions(new Table("mytable"), "a+b", "test");
    assertEquals("SELECT a + b, test FROM mytable", select.toString());
    assertTrue(((SelectExpressionItem) ((PlainSelect) select.getSelectBody()).getSelectItems().get(0)).getExpression() instanceof Addition);
}
Also used : Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) Table(net.sf.jsqlparser.schema.Table) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Test(org.junit.Test)

Example 7 with SelectExpressionItem

use of net.sf.jsqlparser.statement.select.SelectExpressionItem in project JSqlParser by JSQLParser.

the class SelectASTTest method testSelectASTColumn.

@Test
public void testSelectASTColumn() throws JSQLParserException {
    String sql = "SELECT  a,  b FROM  mytable  order by   b,  c";
    StringBuilder b = new StringBuilder(sql);
    Statement stmt = CCJSqlParserUtil.parse(sql);
    Select select = (Select) stmt;
    PlainSelect ps = (PlainSelect) select.getSelectBody();
    for (SelectItem item : ps.getSelectItems()) {
        SelectExpressionItem sei = (SelectExpressionItem) item;
        Column c = (Column) sei.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().beginColumn - 1, '*');
    }
    for (OrderByElement item : ps.getOrderByElements()) {
        Column c = (Column) item.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().beginColumn - 1, '#');
    }
    assertEquals("SELECT  *,  * FROM  mytable  order by   #,  #", b.toString());
}
Also used : Column(net.sf.jsqlparser.schema.Column) Statement(net.sf.jsqlparser.statement.Statement) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SimpleNode(net.sf.jsqlparser.parser.SimpleNode) Test(org.junit.Test)

Example 8 with SelectExpressionItem

use of net.sf.jsqlparser.statement.select.SelectExpressionItem in project spanner-jdbc by olavloite.

the class CloudSpannerResultSetMetaData method initColumns.

private void initColumns(Select select) {
    columns = new ArrayList<>();
    aliases = new ArrayList<>();
    select.getSelectBody().accept(new SelectVisitorAdapter() {

        @Override
        public void visit(PlainSelect plainSelect) {
            for (SelectItem selectItem : plainSelect.getSelectItems()) {
                selectItem.accept(new SelectItemVisitor() {

                    private boolean foundColumn = false;

                    @Override
                    public void visit(SelectExpressionItem selectExpressionItem) {
                        selectExpressionItem.getExpression().accept(new ExpressionVisitorAdapter() {

                            @Override
                            public void visit(Column column) {
                                registerColumn(column, selectExpressionItem.getAlias());
                                foundColumn = true;
                            }
                        });
                        if (!foundColumn) {
                            registerColumn(null, selectExpressionItem.getAlias());
                        }
                    }

                    @Override
                    public void visit(AllTableColumns allTableColumns) {
                        registerAllTableColumns(allTableColumns.getTable());
                    }

                    @Override
                    public void visit(AllColumns allColumns) {
                        for (Table table : tables) {
                            registerAllTableColumns(table);
                        }
                    }
                });
            }
        }
    });
}
Also used : SelectVisitorAdapter(net.sf.jsqlparser.statement.select.SelectVisitorAdapter) Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) AllTableColumns(net.sf.jsqlparser.statement.select.AllTableColumns) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) ExpressionVisitorAdapter(net.sf.jsqlparser.expression.ExpressionVisitorAdapter) SelectItemVisitor(net.sf.jsqlparser.statement.select.SelectItemVisitor) AllColumns(net.sf.jsqlparser.statement.select.AllColumns)

Example 9 with SelectExpressionItem

use of net.sf.jsqlparser.statement.select.SelectExpressionItem in project JSqlParser by JSQLParser.

the class SelectASTTest method testSelectASTColumnLF.

@Test
public void testSelectASTColumnLF() throws JSQLParserException {
    String sql = "SELECT  a,  b FROM  mytable \n order by   b,  c";
    StringBuilder b = new StringBuilder(sql);
    Statement stmt = CCJSqlParserUtil.parse(sql);
    Select select = (Select) stmt;
    PlainSelect ps = (PlainSelect) select.getSelectBody();
    for (SelectItem item : ps.getSelectItems()) {
        SelectExpressionItem sei = (SelectExpressionItem) item;
        Column c = (Column) sei.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '*');
    }
    for (OrderByElement item : ps.getOrderByElements()) {
        Column c = (Column) item.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '#');
    }
    assertEquals("SELECT  *,  * FROM  mytable \n order by   #,  #", b.toString());
}
Also used : Column(net.sf.jsqlparser.schema.Column) Statement(net.sf.jsqlparser.statement.Statement) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SimpleNode(net.sf.jsqlparser.parser.SimpleNode) Test(org.junit.Test)

Example 10 with SelectExpressionItem

use of net.sf.jsqlparser.statement.select.SelectExpressionItem in project JSqlParser by JSQLParser.

the class SelectASTTest method testSelectASTCommentCRLF.

@Test
public void testSelectASTCommentCRLF() throws JSQLParserException {
    String sql = "SELECT  /* testcomment */ \r\n a,  b FROM  -- testcomment2 \r\n mytable \r\n order by   b,  c";
    StringBuilder b = new StringBuilder(sql);
    Statement stmt = CCJSqlParserUtil.parse(sql);
    Select select = (Select) stmt;
    PlainSelect ps = (PlainSelect) select.getSelectBody();
    for (SelectItem item : ps.getSelectItems()) {
        SelectExpressionItem sei = (SelectExpressionItem) item;
        Column c = (Column) sei.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '*');
    }
    for (OrderByElement item : ps.getOrderByElements()) {
        Column c = (Column) item.getExpression();
        SimpleNode astNode = c.getASTNode();
        assertNotNull(astNode);
        b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '#');
    }
    assertEquals("SELECT  /* testcomment */ \r\n *,  * FROM  -- testcomment2 \r\n mytable \r\n order by   #,  #", b.toString());
}
Also used : Column(net.sf.jsqlparser.schema.Column) Statement(net.sf.jsqlparser.statement.Statement) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SimpleNode(net.sf.jsqlparser.parser.SimpleNode) Test(org.junit.Test)

Aggregations

SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)15 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)12 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)12 Column (net.sf.jsqlparser.schema.Column)8 Select (net.sf.jsqlparser.statement.select.Select)7 ArrayList (java.util.ArrayList)6 Expression (net.sf.jsqlparser.expression.Expression)6 Test (org.junit.Test)6 Column (herddb.model.Column)5 Statement (net.sf.jsqlparser.statement.Statement)5 StatementExecutionException (herddb.model.StatementExecutionException)4 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)4 AccessCurrentRowExpression (herddb.sql.expressions.AccessCurrentRowExpression)3 ColumnRef (herddb.sql.expressions.ColumnRef)3 Function (net.sf.jsqlparser.expression.Function)3 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)3 SimpleNode (net.sf.jsqlparser.parser.SimpleNode)3 OrderByElement (net.sf.jsqlparser.statement.select.OrderByElement)3 AbstractTableManager (herddb.core.AbstractTableManager)2 TableSpaceManager (herddb.core.TableSpaceManager)2