Search in sources :

Example 16 with AstStart

use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.

the class Be5QueryExecutor method getFinalSql.

@Override
public String getFinalSql() {
    DebugQueryLogger dql = new DebugQueryLogger();
    dql.log("Orig", query.getQuery());
    String queryText = meta.getQueryCode(query, UserInfoHolder.getCurrentRoles());
    dql.log("After FreeMarker", queryText);
    if (queryText.isEmpty())
        return "";
    AstStart ast;
    try {
        ast = SqlQuery.parse(queryText);
    } catch (RuntimeException e) {
        log.log(Level.SEVERE, "SqlQuery.parse error: ", e);
        throw Be5Exception.internalInQuery(e, query);
    // ast = SqlQuery.parse("select 'error'");
    }
    dql.log("Compiled", ast);
    resolveTypeOfRefColumn(ast);
    // CONTEXT
    // FILTERS
    filterHelper.applyFilters(ast, query.getEntity().getName(), new HashMap<>(parametersMap));
    // CATEGORY
    // applyCategory( dql, ast );
    contextApplier.applyContext(ast);
    subQueryKeys = contextApplier.subQueryKeys().toSet();
    dql.log("With context", ast);
    // ID COLUMN
    if (query.getType() == QueryType.D1 && query.getEntity().findTableDefinition() != null && !hasColumnWithLabel(ast, DatabaseConstants.ID_COLUMN_LABEL)) {
        new ColumnAdder().addColumn(ast, query.getEntity().getName(), query.getEntity().getPrimaryKey(), DatabaseConstants.ID_COLUMN_LABEL);
        dql.log("With ID column", ast);
    } else {
        dql.log("Without ID column", ast);
    }
    // SIMPLIFY
    Simplifier.simplify(ast);
    dql.log("Simplified", ast);
    if (extraQuery == ExtraQuery.COUNT) {
        countFromQuery(ast.getQuery());
        dql.log("Count(1) from query", ast);
    }
    if (extraQuery == ExtraQuery.DEFAULT) {
        // SORT ORDER
        applySort(dql, ast);
        // LIMITS
        new LimitsApplier(offset, limit).transform(ast);
        dql.log("With limits", ast);
    }
    return new Formatter().format(ast, context, parserContext);
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ColumnAdder(com.developmentontheedge.sql.format.ColumnAdder) Formatter(com.developmentontheedge.sql.format.Formatter) LimitsApplier(com.developmentontheedge.sql.format.LimitsApplier)

Example 17 with AstStart

use of com.developmentontheedge.sql.model.AstStart 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)

Example 18 with AstStart

use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.

the class ColumnAdderTest method check.

protected void check(String tableName, String columnName, String query, String expected) {
    AstStart ast = parse(query);
    new ColumnAdder().addColumn(ast, tableName, columnName, "___ID");
    assertEquals(expected, ast.format());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ColumnAdder(com.developmentontheedge.sql.format.ColumnAdder)

Example 19 with AstStart

use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.

the class ContextApplierApplyParametersTest method typeSupportNumber.

@Test
public void typeSupportNumber() {
    AstStart start = SqlQuery.parse("SELECT * FROM table WHERE totalSize = <parameter:totalSize type=\"java.lang.Integer\"/>");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().parameter("totalSize", "123").build());
    contextApplier.applyContext(start);
    assertEquals("SELECT * FROM table WHERE totalSize = 123", start.getQuery().toString());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) Test(org.junit.Test)

Example 20 with AstStart

use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.

the class ContextApplierApplyParametersTest method typeSupportNumberNoParameterInIfCondition.

@Test
public void typeSupportNumberNoParameterInIfCondition() {
    AstStart start = SqlQuery.parse("SELECT * FROM table WHERE (1=1)" + "<if parameter=\"totalSize\"> AND totalSize = <parameter:totalSize type=\"java.lang.Integer\"/></if>");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().build());
    contextApplier.applyContext(start);
    assertEquals("SELECT * FROM table WHERE (1 = 1)", start.getQuery().toString());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) Test(org.junit.Test)

Aggregations

AstStart (com.developmentontheedge.sql.model.AstStart)34 Test (org.junit.Test)26 ContextApplier (com.developmentontheedge.sql.format.ContextApplier)16 Context (com.developmentontheedge.sql.format.Context)13 Formatter (com.developmentontheedge.sql.format.Formatter)13 DefaultParserContext (com.developmentontheedge.sql.model.DefaultParserContext)11 BasicQueryContext (com.developmentontheedge.sql.format.BasicQueryContext)10 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)6 Collections (java.util.Collections)6 Assert (org.junit.Assert)5 QueryResolver (com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver)4 ColumnRef (com.developmentontheedge.sql.format.ColumnRef)4 FilterApplier (com.developmentontheedge.sql.format.FilterApplier)4 Ignore (org.junit.Ignore)4 ColumnAdder (com.developmentontheedge.sql.format.ColumnAdder)3 Dbms (com.developmentontheedge.sql.format.Dbms)3 LimitsApplier (com.developmentontheedge.sql.format.LimitsApplier)3