com.alibaba.fastsql.sql.ast.SQLDataType Java Examples

The following examples show how to use com.alibaba.fastsql.sql.ast.SQLDataType. 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: Scope.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean visit(SQLSelectItem x) {
    String alias = x.getAlias();
    if (alias == null) {
        alias = x.computeAlias();
    }
    if (alias == null) {
        alias = x.getExpr().toString();
    }
    SQLDataType sqlDataType = x.computeDataType();
    SQLObject pre = scope.put(alias, x.getExpr());
    if (pre != null) {
        throw new UnsupportedOperationException("歧义的别名:" + alias);
    }
    return super.visit(x);
}
 
Example #2
Source File: RexTranslator.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void endVisit(SQLCastExpr x) {
    boolean auto = x.isTry();
    SQLExpr expr = x.getExpr();
    SQLDataType dataType = x.computeDataType();
    Integer prec = getPrec(dataType);
    Integer scale = getScale(dataType);
    String type = dataType.getName();
    result = relBuilder.cast(convertExpr(expr), Type.of(type,true).getBase(), prec, scale, auto);
}
 
Example #3
Source File: RexTranslator.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private Integer getScale(SQLDataType dataType) {
    SQLDataTypeImpl dataType1 = (SQLDataTypeImpl) (dataType);
    return Optional.ofNullable(dataType1.getArguments())
            .filter(i -> i.size() > 1)
            .map(i -> i.get(1))
            .map(i -> (SQLNumericLiteralExpr) i)
            .map(i -> i.getNumber())
            .map(i -> i.intValue()).orElse(null);
}
 
Example #4
Source File: RexTranslator.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private Integer getPrec(SQLDataType dataType) {
    SQLDataTypeImpl dataType1 = (SQLDataTypeImpl) (dataType);
    return Optional.ofNullable(dataType1.getArguments())
            .filter(i -> i.size() > 0)
            .map(i -> i.get(0))
            .map(i -> (SQLNumericLiteralExpr) i)
            .map(i -> i.getNumber())
            .map(i -> i.intValue()).orElse(null);
}
 
Example #5
Source File: SelectSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
/**
 * impl example
 * select @@last_insert_id, max(1+1),1+2 as b ,'' as b, '3' as c , null as d from dual;
 *
 * @param request
 * @param receiver
 * @return
 */
protected ExecuteCode onSelectDual(SQLRequest<SQLSelectStatement> request, Response receiver) {
    SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) (request.getAst().getSelect().getQuery());
    List<SQLSelectItem> selectItems = queryBlock.getSelectList();

    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    List<Object> payloadList = new ArrayList<>();
    for (SQLSelectItem selectItem : selectItems) {
        SQLExpr expr = selectItem.getExpr();
        SQLDataType dataType = expr.computeDataType();
        if (expr instanceof SQLIdentifierExpr) {
            receiver.sendError(new MycatException("no support field query. field={} ", expr));
            return ExecuteCode.PROXY_ERROR;
        } else if (expr instanceof SQLVariantRefExpr) {
            receiver.sendError(new MycatException("no support variable. field={} ", expr));
            return ExecuteCode.PROXY_ERROR;
        }

        boolean isNull = dataType == null;
        int dataTypeInt;
        Object payload;
        String column = normalize(selectItem.getAlias());
        if (isNull) {
            dataTypeInt = JDBCType.NULL.getVendorTypeNumber();
            payload = null;
        } else if ((dataType.isInt() || dataType.isNumberic()) && !(expr instanceof SQLNumericLiteralExpr)) {//数学计算
            dataTypeInt = dataType.jdbcType();
            if (column == null) {
                column = expr.toString();
            }
            try {
                payload = TypeCalculation.calculateLiteralValue(expr.toString(), Collections.emptyMap());
            } catch (java.lang.UnsupportedOperationException e) {
                receiver.sendError(new MycatException("no support variable calculate. field={} ", expr));
                return ExecuteCode.PROXY_ERROR;
            }
        } else {
            dataTypeInt = dataType.jdbcType();
            payload = ((SQLValuableExpr) expr).getValue();
        }

        if (column == null) {
            column = payload == null ? NULL : payload.toString();
        }
        resultSetBuilder.addColumnInfo(column, dataTypeInt);
        payloadList.add(payload);
    }
    resultSetBuilder.addObjectRowPayload(payloadList);
    receiver.sendResultSet(() -> resultSetBuilder.build(), Collections::emptyList);
    return ExecuteCode.PERFORMED;
}