Java Code Examples for net.sf.jsqlparser.statement.select.Select#toString()

The following examples show how to use net.sf.jsqlparser.statement.select.Select#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: SqlServer2012Dialect.java    From hasor with Apache License 2.0 6 votes vote down vote up
@Override
public BoundSql getCountSql(FxQuery fxSql, Map<String, Object> paramMap) {
    String buildSqlString = fxSql.buildQueryString(paramMap);
    List<Object> paramArrays = fxSql.buildParameterSource(paramMap);
    //
    // .含有 order by 去掉它
    if (buildSqlString.toLowerCase().contains("order by")) {
        Select selectStatement = parseSelect(buildSqlString, fxSql);
        PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
        if (plainSelect.getOrderByElements() != null) {
            List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
            plainSelect.setOrderByElements(null);
            buildSqlString = selectStatement.toString();
            plainSelect.setOrderByElements(orderByElements);
        }
    }
    // .拼 count 语句
    StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append("SELECT COUNT(*) FROM (");
    sqlBuilder.append(buildSqlString);
    sqlBuilder.append(") as TEMP_T");
    return new BoundSql(sqlBuilder.toString(), paramArrays.toArray());
}
 
Example 2
Source File: DataPermissionInterceptor.java    From DataPermissionHelper with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] args = invocation.getArgs();
    MappedStatement mappedStatement = (MappedStatement) args[0];
    Object parameter = args[1];
    //从当前线程获取需要进行数据权限控制的业务
    DataPermission dataPermission = DPHelper.getLocalDataPermissions();
    //判断有没有进行数据权限控制,是不是最高权限的管理员(这里指的是数据权限的白名单用户)
    if (dataPermission != null && dataPermission.getAdmin() == false) {
        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
        String sql = boundSql.getSql();
        //获得方法类型
        Select select = (Select) CCJSqlParserUtil.parse(sql);
        select.getSelectBody().accept(new SelectVisitorImpl());
        //判断当前sql是否被修改
        if (DPHelper.getChangeTable()) {
            //访问各个visitor
            //TODO:解析动态sql会失败
            BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), select.toString(), boundSql
                    .getParameterMappings(), parameter);
            String newMsId = mappedStatement.getId() + DATA_PERMISSION;
            MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql), newMsId);
            args[0] = newMs;
            DPHelper.clearChangeTable();
        }
    }
    return invocation.proceed();
}
 
Example 3
Source File: ElasticSearchSqlServiceImpl.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
/**
 * 查询替换为别名进行
 *
 * @param sql
 * @return
 * @throws JSQLParserException
 */
private String replaceTableName(String sql) throws JSQLParserException {
    Statement statement = CCJSqlParserUtil.parse(sql);
    Select select = (Select) statement;

    StringBuilder buffer = new StringBuilder();
    ExpressionDeParser expressionDeParser = new ExpressionDeParser();

    TableNameParser tableNameParser = new TableNameParser(expressionDeParser, buffer);

    expressionDeParser.setSelectVisitor(tableNameParser);
    expressionDeParser.setBuffer(buffer);
    select.getSelectBody().accept(tableNameParser);
    return select.toString();
}
 
Example 4
Source File: CassandraCqlParser.java    From cassandra-jdbc-driver with Apache License 2.0 4 votes vote down vote up
private static CassandraCqlStatement parseSql(CassandraConfiguration config, String sql, Map<String, String> hints) {
    CassandraStatementType stmtType = CassandraStatementType.UNKNOWN;
    if (Strings.isNullOrEmpty(sql)) {
        return new CassandraCqlStatement(Strings.nullToEmpty(sql),
                new CassandraCqlStmtConfiguration(config, stmtType, hints));
    }

    CassandraCqlStatement sqlStmt = null;
    CassandraCqlStmtConfiguration stmtConfig = null;
    try {
        // workaround for limitation of JSqlParser - escaping keyword-like columns
        Matcher m = SQL_KEYWORDS_PATTERN.matcher(sql);
        sql = m.replaceAll(SQL_KEYWORD_ESCAPING);

        // go ahead to parse the SQL
        Statement s = CCJSqlParserUtil.parse(sql);

        // now translate the SQL query to CQL
        sql = s.toString();
        if (s instanceof Select) {
            stmtType = CassandraStatementType.SELECT;
        } else if (sql.startsWith(CassandraStatementType.INSERT.getType())) {
            stmtType = CassandraStatementType.INSERT;
        } else if (sql.startsWith(CassandraStatementType.UPDATE.getType())) {
            stmtType = CassandraStatementType.UPDATE;
        } else if (sql.startsWith(CassandraStatementType.DELETE.getType())) {
            stmtType = CassandraStatementType.DELETE;
        } else if (sql.startsWith(CassandraStatementType.TRUNCATE.getType())) {
            stmtType = CassandraStatementType.TRUNCATE;
        } else if (sql.startsWith(CassandraStatementType.CREATE.getType())) {
            stmtType = CassandraStatementType.CREATE;
        } else if (sql.startsWith(CassandraStatementType.ALTER.getType())) {
            stmtType = CassandraStatementType.ALTER;
        } else if (sql.startsWith(CassandraStatementType.DROP.getType())) {
            stmtType = CassandraStatementType.DROP;
        }

        stmtConfig = new CassandraCqlStmtConfiguration(config, stmtType, hints);

        if (stmtType.isQuery()) {
            Select select = (Select) s;
            SqlToCqlTranslator trans = new SqlToCqlTranslator(stmtConfig);
            select.getSelectBody().accept(trans);
            sql = select.toString();
        }
    } catch (Throwable t) {
        Logger.debug("Failed to parse the given SQL, fall back to CQL parser");
        sqlStmt = parseCql(config, sql, hints);
        sql = sqlStmt.getCql();
    }

    if (sqlStmt == null) {
        sqlStmt = new CassandraCqlStatement(sql, stmtConfig == null
                ? new CassandraCqlStmtConfiguration(config, stmtType, hints) : stmtConfig);
    }

    return sqlStmt;
}