net.sf.jsqlparser.expression.Alias Java Examples

The following examples show how to use net.sf.jsqlparser.expression.Alias. 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
Source File: FromItemVisitorImpl.java    From DataPermissionHelper with Apache License 2.0 7 votes vote down vote up
@Override
public void visit(Table table) {
    String tableName = table.getName();
    //关键点:解析到需要进行数据权限控制的表时进行拼装,可以从当前线程获取表数据
    //需要进行的数据权限控制的表数据
    Map<String, IdsAndColumn> tables = DPHelper.getLocalDataPermissions().getTables();
    if (tables.containsKey(tableName)) {
        IdsAndColumn idsAndColumn = tables.get(tableName);
        List<String> ids = idsAndColumn.getIds();
        List<String> columns = idsAndColumn.getColumns();

        SubSelect subSelect = new SubSelect();
        String subSql = SqlSpliceUtils.spliceIdAndColumn(tableName, ids, columns);
        try {
            subSelect.setSelectBody(((Select) (CCJSqlParserUtil.parse(subSql))).getSelectBody());
        } catch (JSQLParserException e) {
            logger.error("数据权限sql解析异常");
        }
        //TODO:采用随机别名不能避免重名
        subSelect.setAlias(table.getAlias() != null ? table.getAlias() : new Alias("DP" + UUID.randomUUID()
                .toString().replace("-", "")));
        this.subSelect = subSelect;
    }
}
 
Example #2
Source File: SQLCommandInfoHolder.java    From sql-to-mongo-db-query-converter with Apache License 2.0 6 votes vote down vote up
private FromHolder generateFromHolder(FromHolder tholder, FromItem fromItem, List<Join> ljoin) throws ParseException, com.github.vincentrussell.query.mongodb.sql.converter.ParseException {
	Alias alias = fromItem.getAlias();
	tholder.addFrom(fromItem,(alias != null ? alias.getName() : null));
	
	if(ljoin != null) {
 	for (Join j : ljoin) {
 		SqlUtils.updateJoinType(j);
 		if(j.isInner() || j.isLeft()) {
 			tholder = generateFromHolder(tholder,j.getRightItem(),null);
 		}	
 		else{
 			throw new ParseException("Join type not suported");
 		}
 	}
	}
	return tholder;
}
 
Example #3
Source File: QueryConverter.java    From sql-to-mongo-db-query-converter with Apache License 2.0 6 votes vote down vote up
private void parseFunctionForAggregation(Function function, Document document, List<String> groupBys, Alias alias) throws ParseException {
    String op = function.getName().toLowerCase();
    String aggField = SqlUtils.generateAggField(function, alias);
    switch(op) {
    	case "count":
    		document.put(aggField,new Document("$sum",1));
    		break;
    	case "sum":
    	case "min":
    	case "max":
    	case "avg":
    		createFunction(op,aggField, document,"$"+ SqlUtils.getFieldFromFunction(function));
    		break;
    	default:
    		throw new ParseException("could not understand function:" + function.getName());
    }
}
 
Example #4
Source File: SqlServer.java    From genericdao with Artistic License 2.0 6 votes vote down vote up
/**
 * 包装SetOperationList
 *
 * @param setOperationList
 * @return
 */
private SelectBody wrapSetOperationList(SetOperationList setOperationList) {
    //获取最后一个plainSelect
    PlainSelect plainSelect = setOperationList.getPlainSelects().get(setOperationList.getPlainSelects().size() - 1);

    PlainSelect selectBody = new PlainSelect();
    List<SelectItem> selectItems = getSelectItems(plainSelect);
    selectBody.setSelectItems(selectItems);

    //设置fromIterm
    SubSelect fromItem = new SubSelect();
    fromItem.setSelectBody(setOperationList);
    fromItem.setAlias(new Alias(WRAP_TABLE));
    selectBody.setFromItem(fromItem);
    //order by
    if (isNotEmptyList(plainSelect.getOrderByElements())) {
        selectBody.setOrderByElements(plainSelect.getOrderByElements());
        plainSelect.setOrderByElements(null);
    }
    return selectBody;
}
 
Example #5
Source File: SqlServerParser.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
/**
 * 包装SetOperationList
 *
 * @param setOperationList
 * @return
 */
protected SelectBody wrapSetOperationList(SetOperationList setOperationList) {
    //获取最后一个plainSelect
    SelectBody setSelectBody = setOperationList.getSelects().get(setOperationList.getSelects().size() - 1);
    if (!(setSelectBody instanceof PlainSelect)) {
        throw new PageException("目前无法处理该SQL,您可以将该SQL发送给[email protected]协助作者解决!");
    }
    PlainSelect plainSelect = (PlainSelect) setSelectBody;
    PlainSelect selectBody = new PlainSelect();
    List<SelectItem> selectItems = getSelectItems(plainSelect);
    selectBody.setSelectItems(selectItems);

    //设置fromIterm
    SubSelect fromItem = new SubSelect();
    fromItem.setSelectBody(setOperationList);
    fromItem.setAlias(new Alias(WRAP_TABLE));
    selectBody.setFromItem(fromItem);
    //order by
    if (isNotEmptyList(plainSelect.getOrderByElements())) {
        selectBody.setOrderByElements(plainSelect.getOrderByElements());
        plainSelect.setOrderByElements(null);
    }
    return selectBody;
}
 
Example #6
Source File: CloudSpannerResultSetMetaData.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Override
public String getColumnLabel(int column) throws SQLException {
  Alias alias = getAlias(column);
  if (alias != null)
    return alias.getName();
  return resultSet.getType().getStructFields().get(column - 1).getName();
}
 
Example #7
Source File: CloudSpannerResultSetMetaData.java    From spanner-jdbc with MIT License 4 votes vote down vote up
private void registerColumn(Column column, Alias alias) {
  columns.add(column);
  aliases.add(alias);
}
 
Example #8
Source File: CloudSpannerResultSetMetaData.java    From spanner-jdbc with MIT License 4 votes vote down vote up
private Alias getAlias(int column) throws SQLException {
  initMetaData();
  if (aliases == null || column > aliases.size())
    return null;
  return aliases.get(column - 1);
}
 
Example #9
Source File: SqlUtils.java    From sql-to-mongo-db-query-converter with Apache License 2.0 4 votes vote down vote up
public static String generateAggField(Function f, Alias alias)  throws ParseException{
	String aliasStr = (alias == null? null : alias.getName());
	return generateAggField(f, aliasStr);
}