Java Code Examples for com.alibaba.fastsql.sql.ast.SQLExpr#toString()

The following examples show how to use com.alibaba.fastsql.sql.ast.SQLExpr#toString() . 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: MemoryTableMeta.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
private String getSqlName(SQLExpr sqlName) {
    if (sqlName == null) {
        return null;
    }

    if (sqlName instanceof SQLPropertyExpr) {
        SQLIdentifierExpr owner = (SQLIdentifierExpr) ((SQLPropertyExpr) sqlName).getOwner();
        return DruidDdlParser.unescapeName(owner.getName()) + "."
               + DruidDdlParser.unescapeName(((SQLPropertyExpr) sqlName).getName());
    } else if (sqlName instanceof SQLIdentifierExpr) {
        return DruidDdlParser.unescapeName(((SQLIdentifierExpr) sqlName).getName());
    } else if (sqlName instanceof SQLCharExpr) {
        return ((SQLCharExpr) sqlName).getText();
    } else if (sqlName instanceof SQLMethodInvokeExpr) {
        return DruidDdlParser.unescapeName(((SQLMethodInvokeExpr) sqlName).getMethodName());
    } else if (sqlName instanceof MySqlOrderingExpr) {
        return getSqlName(((MySqlOrderingExpr) sqlName).getExpr());
    } else {
        return sqlName.toString();
    }
}
 
Example 2
Source File: ShowStatementRewriter.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public static String rewriteShowTables(String defaultSchema, SQLShowTablesStatement ast) {
    SQLExpr where = ast.getWhere();
    SQLName from = ast.getFrom();
    SQLExpr like = ast.getLike();
    boolean full = ast.isFull();
    String schema = SQLUtils.normalize(from == null ? defaultSchema : from.getSimpleName());
    if (schema == null) {
        throw new MycatException(1046, "No database selected");
    }
    String schemaCondition = " TABLE_SCHEMA = '" + schema + "' ";
    String whereCondition = " " + (where == null ? "true" : where.toString()) + " ";
    String likeCondition = like == null ? " true " : " TABLE_NAME like " + " " + like.toString() + " ";
    String fullCondition = !full ? " true " : " TABLE_TYPE  = 'BASE TABLE' ";

    String sql = MessageFormat.format("select TABLE_NAME as {0} from information_schema.`TABLES` where {1} ",
            "`" + "Tables_in_" + schema + "`", String.join(" and ", schemaCondition, whereCondition, likeCondition, fullCondition)
    );
    LOGGER.info(ast + "->" + sql);
    return sql;
}
 
Example 3
Source File: MemoryTableMeta.java    From canal with Apache License 2.0 6 votes vote down vote up
private String getSqlName(SQLExpr sqlName) {
    if (sqlName == null) {
        return null;
    }

    if (sqlName instanceof SQLPropertyExpr) {
        SQLIdentifierExpr owner = (SQLIdentifierExpr) ((SQLPropertyExpr) sqlName).getOwner();
        return DruidDdlParser.unescapeName(owner.getName()) + "."
               + DruidDdlParser.unescapeName(((SQLPropertyExpr) sqlName).getName());
    } else if (sqlName instanceof SQLIdentifierExpr) {
        return DruidDdlParser.unescapeName(((SQLIdentifierExpr) sqlName).getName());
    } else if (sqlName instanceof SQLCharExpr) {
        return ((SQLCharExpr) sqlName).getText();
    } else if (sqlName instanceof SQLMethodInvokeExpr) {
        return DruidDdlParser.unescapeName(((SQLMethodInvokeExpr) sqlName).getMethodName());
    } else if (sqlName instanceof MySqlOrderingExpr) {
        return getSqlName(((MySqlOrderingExpr) sqlName).getExpr());
    } else {
        return sqlName.toString();
    }
}
 
Example 4
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;
}