Search in sources :

Example 1 with DbmsTransformer

use of com.developmentontheedge.sql.format.DbmsTransformer in project be5 by DevelopmentOnTheEdge.

the class Project method translateSQL.

/**
 * Try to translate the SQL query to the current DBMS using com.developmentontheedge.sql
 * @param sql
 * @return
 */
public String translateSQL(String sql) {
    if (beSQL > 0) {
        if (--beSQL == 0)
            reconfigureFreemarker();
    }
    if (sqlParser == null) {
        throw new IllegalStateException("translateSQL was called without enterSQL");
    }
    sqlParser.parse(sql);
    List<String> messages = sqlParser.getMessages();
    if (!messages.isEmpty()) {
        throw new IllegalArgumentException(("SQL cannot be parsed:\nQuery:" + sql + "\nErrors: " + String.join("\n", messages)).replace("\r", "").replace("\n", System.lineSeparator()));
    }
    AstStart ast = sqlParser.getStartNode();
    if (databaseSystem != Rdbms.BESQL) {
        new MacroExpander().expandMacros(ast);
        Dbms dbms = databaseSystem == null ? Dbms.POSTGRESQL : Dbms.valueOf(databaseSystem.name());
        DbmsTransformer dbmsTransformer = new Context(dbms).getDbmsTransformer();
        dbmsTransformer.setParserContext(sqlParser.getContext());
        dbmsTransformer.transformAst(ast);
    }
    return ast.format();
}
Also used : LoadContext(com.developmentontheedge.be5.metadata.serialization.LoadContext) Context(com.developmentontheedge.sql.format.Context) AstStart(com.developmentontheedge.sql.model.AstStart) MacroExpander(com.developmentontheedge.sql.format.MacroExpander) Dbms(com.developmentontheedge.sql.format.Dbms) DbmsTransformer(com.developmentontheedge.sql.format.DbmsTransformer)

Aggregations

LoadContext (com.developmentontheedge.be5.metadata.serialization.LoadContext)1 Context (com.developmentontheedge.sql.format.Context)1 Dbms (com.developmentontheedge.sql.format.Dbms)1 DbmsTransformer (com.developmentontheedge.sql.format.DbmsTransformer)1 MacroExpander (com.developmentontheedge.sql.format.MacroExpander)1 AstStart (com.developmentontheedge.sql.model.AstStart)1