Java Code Examples for net.sf.jsqlparser.statement.select.SelectExpressionItem

The following examples show how to use net.sf.jsqlparser.statement.select.SelectExpressionItem. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private void validate() throws ParseException {
    List<SelectItem> selectItems = sqlCommandInfoHolder.getSelectItems();
    List<SelectItem> filteredItems = Lists.newArrayList(Iterables.filter(selectItems, new Predicate<SelectItem>() {
        @Override
        public boolean apply(SelectItem selectItem) {
            try {
                if (SelectExpressionItem.class.isInstance(selectItem)
                        && Column.class.isInstance(((SelectExpressionItem) selectItem).getExpression())) {
                    return true;
                }
            } catch (NullPointerException e) {
                return false;
            }
            return false;
        }
    }));

    SqlUtils.isFalse((selectItems.size() >1
            || SqlUtils.isSelectAll(selectItems))
            && sqlCommandInfoHolder.isDistinct(),"cannot run distinct one more than one column");
    SqlUtils.isFalse(sqlCommandInfoHolder.getGoupBys().size() == 0 && selectItems.size()!=filteredItems.size() && !SqlUtils.isSelectAll(selectItems)
            && !SqlUtils.isCountAll(selectItems)&& !sqlCommandInfoHolder.isTotalGroup(),"illegal expression(s) found in select clause.  Only column names supported");
}
 
Example 2
Source Project: Mybatis-PageHelper   Source File: FunctionCountTest.java    License: MIT License 6 votes vote down vote up
@Test
public void test() {
    Select select = select("select max(name),code,min(aa),nvl(ab,0),heh from user where a > 100");
    List<SelectItem> selectItems = ((PlainSelect) select.getSelectBody()).getSelectItems();
    for (SelectItem item : selectItems) {
        if (item instanceof SelectExpressionItem) {
            Expression exp = ((SelectExpressionItem) item).getExpression();
            if (exp instanceof Function) {
                System.out.println("Function:" + item.toString());
            } else {
                System.out.println("Not a function:" + exp.toString());
            }
        } else {
            System.out.println("Not a function:" + item.toString());
        }
    }
}
 
Example 3
Source Project: spanner-jdbc   Source File: CloudSpannerResultSetMetaData.java    License: MIT License 5 votes vote down vote up
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);
            }
          }
        });
      }
    }
  });
}
 
Example 4
Source Project: dynamic-add-date   Source File: AddDateInterceptor.java    License: MIT License 5 votes vote down vote up
@Override
public void visit(PlainSelect plainSelect) {
    if (index != -1) {
        plainSelect.getSelectItems().set(index, new SelectExpressionItem(new QuotationTimestampValue(columnValue)));
    } else {
        plainSelect.getSelectItems().add(new SelectExpressionItem(new QuotationTimestampValue(columnValue)));
    }
}
 
Example 5
Source Project: dynamic-add-date   Source File: AddDateInterceptor.java    License: MIT License 5 votes vote down vote up
@Override
public void visit(WithItem withItem) {
    if (index != -1) {
        withItem.getWithItemList().set(index, new SelectExpressionItem(new QuotationTimestampValue(columnValue)));
    } else {
        withItem.getWithItemList().add(new SelectExpressionItem(new QuotationTimestampValue(columnValue)));
    }
}
 
Example 6
Source Project: quetzal   Source File: CTEToNestedQueryConverter.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private Expression getExpression(SelectItem item, Table table) {
	SelectExpressionItem rightSelectItem = (SelectExpressionItem) item;
	Expression rightExp = rightSelectItem.getExpression();
	if (rightExp instanceof Column) {
		Column c = (Column) rightExp;
		if (c.getTable() == null) {
			c.setTable(table);
		}
	}
	return rightExp;
}
 
Example 7
@Override
public void visit(SelectExpressionItem selectExpressionItem) {
    selectExpressionItem.getExpression().accept(new ExpressionVisitorImpl());
}
 
Example 8
Source Project: spanner-jdbc   Source File: DMLWhereClauseVisitorAdapter.java    License: MIT License 4 votes vote down vote up
@Override
public void visit(SelectExpressionItem selectExpressionItem) {
  invalid = true;
  super.visit(selectExpressionItem);
}
 
Example 9
Source Project: openprodoc   Source File: ObjPD.java    License: GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Search in ANY object using SQL Syntax subset, similar to CMIS SQL
 * @param SQL complete query
 * @return and opened @Cursor
 * @throws PDException In any Error
 */
public Cursor SearchSelect(String SQL) throws PDException
{
if (PDLog.isDebug())
    PDLog.Debug("ObjPD.SearchSelect>:"+SQL);
Query QBE=null;
try {
Select ParsedSQL = (Select) CCJSqlParserUtil.parse(SQL);
//-- Calculate Table Names ------------
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> TableListSQL = tablesNamesFinder.getTableList(ParsedSQL);
Vector <String> OPDTabs=CalculateTabs(TableListSQL);
//-- Calculate Fields -------------
List<SelectItem> selectItems = ((PlainSelect)ParsedSQL.getSelectBody()).getSelectItems();
Vector<String> Fields=new Vector<String>();
if (!( selectItems.get(0) instanceof AllColumns))
    for (int i = 0; i < selectItems.size(); i++)
        Fields.add(((SelectExpressionItem)selectItems.get(i)).getExpression().toString());      
Record Rec=CalculateRec(Fields, OPDTabs);
//-- Calculate Conds in Select ------------
Expression When = ((PlainSelect)ParsedSQL.getSelectBody()).getWhere();
Conditions CondSel=EvalExpr(When);

//-- Check Additional-Security Conditions ----
Conditions FinalConds;
Conditions AddedConds=NeededMoreConds(TableListSQL, OPDTabs);
if (AddedConds==null)
    FinalConds=CondSel;
else
    {
    FinalConds=new Conditions();
    FinalConds.addCondition(AddedConds);
    FinalConds.addCondition(CondSel);
    }
//-- Calculate Order ------------
Vector <String> Order=new Vector<String>();
Vector <Boolean> OrderAsc=new Vector<Boolean>();
List<OrderByElement> orderByElements = ((PlainSelect)ParsedSQL.getSelectBody()).getOrderByElements(); 
if (orderByElements!=null)
    for (int i = 0; i < orderByElements.size(); i++)
        {
        Order.add(orderByElements.get(i).getExpression().toString());
        OrderAsc.add(orderByElements.get(i).isAsc());
        }
//-- Create Query --------------
QBE=new Query(OPDTabs, Rec, FinalConds, Order, OrderAsc);
if (PDLog.isDebug())
    PDLog.Debug("ObjPD.SearchSelect <");
} catch (Exception Ex)
    {
    Ex.printStackTrace();
    PDException.GenPDException("Processing_SQL", Ex.getLocalizedMessage());
    }
return(getDrv().OpenCursor(QBE));
}