Search in sources :

Example 6 with Formatter

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

the class OrderByFilterTest method testOrderByFilterUnion.

@Test
public void testOrderByFilterUnion() {
    AstStart start = SqlQuery.parse("SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%'");
    Map<String, String> columns = Collections.singletonMap("name", "DESC");
    new OrderByFilter().apply(start, columns);
    assertEquals("SELECT * FROM (SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%') " + "tmp ORDER BY 1 DESC", new Formatter().format(start, new Context(Dbms.MYSQL), new DefaultParserContext()));
}
Also used : Context(com.developmentontheedge.sql.format.Context) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) Formatter(com.developmentontheedge.sql.format.Formatter) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) OrderByFilter(com.developmentontheedge.sql.format.OrderByFilter) Test(org.junit.Test)

Example 7 with Formatter

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

the class ParserTarget method format.

protected void format(String dbmsName, Element element) {
    Dbms dbms;
    if (Dbms.DB2.getName().equals(dbmsName))
        dbms = Dbms.DB2;
    else if (Dbms.MYSQL.getName().equals(dbmsName))
        dbms = Dbms.MYSQL;
    else if (Dbms.ORACLE.getName().equals(dbmsName))
        dbms = Dbms.ORACLE;
    else if (Dbms.POSTGRESQL.getName().equals(dbmsName))
        dbms = Dbms.POSTGRESQL;
    else if (Dbms.SQLSERVER.getName().equals(dbmsName))
        dbms = Dbms.SQLSERVER;
    else {
        test.getResult().addError(test, new Exception("Unknown DBMS \"" + dbmsName + "\". DBMS should be: db2, mysql, oracle, postgresql or sqlserver."));
        return;
    }
    Formatter formatter = new Formatter();
    String formatResult = trimAllLine(formatter.format(astStart, new Context(dbms), parser.getContext()));
    String result = trimAllLine(XmlTest.getCData(element));
    assertEquals("Incorrect result, dbms=" + dbms.getName(), result, formatResult);
    if (!formatResult.equals(result))
        test.getResult().addFailure(test, new ComparisonFailure("Incorrect result, dbms=" + dbms.getName(), result, formatResult));
}
Also used : Context(com.developmentontheedge.sql.format.Context) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) Dbms(com.developmentontheedge.sql.format.Dbms) Formatter(com.developmentontheedge.sql.format.Formatter) ComparisonFailure(junit.framework.ComparisonFailure)

Example 8 with Formatter

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

the class Be5QueryExecutor method applySort.

private void applySort(DebugQueryLogger dql, AstStart ast) {
    if (sortColumn >= 0) {
        try {
            DynamicProperty[] schema = getSchema(new Formatter().format(ast, context, parserContext));
            int sortCol = getQuerySortingColumn(schema);
            if (sortCol > 0) {
                AstSelect sel = (AstSelect) ast.getQuery().jjtGetChild(ast.getQuery().jjtGetNumChildren() - 1);
                AstOrderBy orderBy = sel.getOrderBy();
                if (orderBy == null) {
                    orderBy = new AstOrderBy();
                    sel.addChild(orderBy);
                    AstLimit astLimit = sel.children().select(AstLimit.class).findFirst().orElse(null);
                    if (astLimit != null) {
                        sel.removeChild(astLimit);
                        sel.addChild(astLimit);
                    }
                }
                AstOrderingElement oe = new AstOrderingElement(AstNumericConstant.of(sortCol));
                if (sortDesc) {
                    oe.setDirectionToken(new Token(0, "DESC"));
                }
                orderBy.addChild(oe);
                orderBy.moveToFront(oe);
            }
            dql.log("With sort", ast);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) AstLimit(com.developmentontheedge.sql.model.AstLimit) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Token(com.developmentontheedge.sql.model.Token)

Example 9 with Formatter

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

the class Be5QueryExecutor method executeSubQuery.

@Override
public List<DynamicPropertySet> executeSubQuery(String subqueryName, CellFormatter.VarResolver varResolver) {
    AstBeSqlSubQuery subQuery = contextApplier.applyVars(subqueryName, varResolver::resolve);
    if (subQuery.getQuery() == null) {
        return Collections.emptyList();
    }
    String finalSql = new Formatter().format(subQuery.getQuery(), context, parserContext);
    List<DynamicPropertySet> dynamicPropertySets;
    try {
        dynamicPropertySets = listDps(finalSql);
    } catch (Throwable e) {
        // TODO only for Document presentation, for operations must be error throw
        Be5Exception be5Exception = Be5Exception.internalInQuery(e, query);
        log.log(Level.SEVERE, be5Exception.toString() + " Final SQL: " + finalSql, be5Exception);
        DynamicPropertySetSupport dynamicProperties = new DynamicPropertySetSupport();
        dynamicProperties.add(new DynamicProperty("___ID", String.class, "-1"));
        dynamicProperties.add(new DynamicProperty("error", String.class, UserInfoHolder.isSystemDeveloper() ? Be5Exception.getMessage(e) : "error"));
        dynamicPropertySets = Collections.singletonList(dynamicProperties);
    }
    // return Collections.singletonList(dynamicProperties);
    return dynamicPropertySets;
}
Also used : DynamicPropertySet(com.developmentontheedge.beans.DynamicPropertySet) Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) Formatter(com.developmentontheedge.sql.format.Formatter) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) DynamicPropertySetSupport(com.developmentontheedge.beans.DynamicPropertySetSupport)

Example 10 with Formatter

use of com.developmentontheedge.sql.format.Formatter 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)

Aggregations

Formatter (com.developmentontheedge.sql.format.Formatter)16 Context (com.developmentontheedge.sql.format.Context)13 AstStart (com.developmentontheedge.sql.model.AstStart)12 DefaultParserContext (com.developmentontheedge.sql.model.DefaultParserContext)11 Test (org.junit.Test)10 ColumnRef (com.developmentontheedge.sql.format.ColumnRef)4 FilterApplier (com.developmentontheedge.sql.format.FilterApplier)4 Dbms (com.developmentontheedge.sql.format.Dbms)3 SqlParser (com.developmentontheedge.sql.model.SqlParser)3 ProjectElementException (com.beanexplorer.enterprise.metadata.exception.ProjectElementException)2 Entity (com.beanexplorer.enterprise.metadata.model.Entity)2 Project (com.beanexplorer.enterprise.metadata.model.Project)2 Query (com.beanexplorer.enterprise.metadata.model.Query)2 LoadContext (com.beanexplorer.enterprise.metadata.serialization.LoadContext)2 Rdbms (com.beanexplorer.enterprise.metadata.sql.Rdbms)2 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)2 LimitsApplier (com.developmentontheedge.sql.format.LimitsApplier)2 OrderByFilter (com.developmentontheedge.sql.format.OrderByFilter)2 SQLException (java.sql.SQLException)2 SimpleDateFormat (java.text.SimpleDateFormat)2