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

The following examples show how to use net.sf.jsqlparser.statement.Statement#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: TableRenameUtil.java    From compass with Apache License 2.0 6 votes vote down vote up
public static String modifyTableNames(String sql,TableRenamer tableRenamer) 
{

	if(sql == null)
	{
		throw new IllegalArgumentException("sql is null");
	}
	
	Statement statement = null;
	try
	{
		statement = CCJSqlParserUtil.parse(sql);
	} 
	catch (JSQLParserException e) 
	{
		throw new IllegalArgumentException("Error when parsing sql:[" + sql+"]",e);
	}
	
	TableRenameVisitor tableRenameVisitor=new TableRenameVisitor(tableRenamer);
	statement.accept(tableRenameVisitor);
	return statement.toString();
}
 
Example 2
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;
}