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);
}
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());
}
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);
}
}
});
}
}
});
}
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());
}
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());
}
Aggregations