Search in sources :

Example 1 with AstBeSqlSubQuery

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

the class ContextApplier method getSubQueryPropertyMap.

public Map<String, String> getSubQueryPropertyMap(String key, Function<String, String> varResolver) {
    Map<String, String> propertyMap = new HashMap<>();
    AstBeSqlSubQuery subQuery = context.getSubQueries().get(key);
    if (subQuery == null)
        return propertyMap;
    String keyStr = subQuery.getFilterKeys();
    String valPropStr = subQuery.getFilterValProperties();
    if (keyStr != null && valPropStr != null) {
        String[] keys = keyStr.split(",");
        String[] valProps = valPropStr.split(",");
        for (int i = 0; i < keys.length; i++) {
            propertyMap.put(keys[i], varResolver.apply(valProps[i]));
        }
    }
    return propertyMap;
}
Also used : HashMap(java.util.HashMap) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery)

Example 2 with AstBeSqlSubQuery

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

the class ContextApplier method applyVars.

/**
 * todo rename getSubQuery
 */
public AstBeSqlSubQuery applyVars(String key, Function<String, String> varResolver) {
    AstBeSqlSubQuery subQuery = context.getSubQueries().get(key);
    if (subQuery == null)
        return null;
    AstBeSqlSubQuery result = (AstBeSqlSubQuery) subQuery.clone();
    if (result.getLimit() != null)
        new LimitsApplier(0, result.getLimit()).transformQuery(result.getQuery());
    applyFirstLevelVars(1, result, (AstBeSqlVar node) -> applyVar(key, node, varResolver));
    // result.tree().select( AstBeSqlVar.class ).forEach( varNode -> {
    // String value = varResolver.apply( subQuery.translateVar( varNode.getName() ) );
    // if( value == null )
    // value = varNode.getDefault();
    // SimpleNode constant;
    // 
    // if(varNode.jjtGetParent() instanceof AstBeSqlSubQuery && value != null && !"".equals(value.trim()))
    // {
    // constant = SqlQuery.parse(value).getQuery();
    // }
    // else
    // {
    // if(value == null)
    // {
    // value = "null";
    // }
    // constant = varNode.jjtGetParent() instanceof AstStringConstant ? new AstStringPart(value)
    // : new AstIdentifierConstant(value);
    // }
    // 
    // varNode.replaceWith( constant );
    // } );
    checkExpression(result, key, varResolver);
    return result;
}
Also used : AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) AstBeSqlVar(com.developmentontheedge.sql.model.AstBeSqlVar)

Example 3 with AstBeSqlSubQuery

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

the class SubQueryTest method testApplyWithVarsExecDelayedAddFilter.

@Test
@Ignore
public void testApplyWithVarsExecDelayedAddFilter() {
    // TODO us.ID vs ID in be3 (us from entity)
    AstStart start = SqlQuery.parse("SELECT '<sql exec=\"delayed\" filterKey=\"us.ID\" filterValProperty=\"___usID\" limit=\"1\" " + "entity=\"utilitySuppliers\" queryName=\"*** Selection view ***\" outColumns=\"Name\"></sql>' AS \"Услуга\",\n" + "ID AS \"___usID\" FROM table");
    QueryResolver resolver = (entity, query) -> {
        assertEquals("utilitySuppliers", entity);
        assertEquals("*** Selection view ***", query);
        return "SELECT us.ID AS \"CODE\",us.utilityType AS \"Name\" FROM utilitySuppliers us";
    };
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().queryResolver(resolver).build());
    contextApplier.applyContext(start);
    String key = contextApplier.subQueryKeys().findFirst().get();
    Map<String, String> vars = Collections.singletonMap("___usID", "5");
    AstBeSqlSubQuery subQuery = contextApplier.applyVars(key, vars::get);
    assertEquals("SELECT '" + key + "' AS \"Услуга\",\n" + "ID AS \"___usID\" FROM table", start.format());
    assertEquals("SELECT us.utilityType AS \"Name\" " + "FROM utilitySuppliers us WHERE us.ID = 5 LIMIT 1", subQuery.getQuery().format());
}
Also used : QueryResolver(com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver) Ignore(org.junit.Ignore) Map(java.util.Map) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) HashMap(java.util.HashMap) Test(org.junit.Test) AstStart(com.developmentontheedge.sql.model.AstStart) Assert(org.junit.Assert) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) Collections(java.util.Collections) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) QueryResolver(com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver) AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with AstBeSqlSubQuery

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

the class SubQueryTest method testSubQueryResolver.

@Test
public void testSubQueryResolver() {
    AstStart start = SqlQuery.parse("SELECT ID, '<sql limit=\"2\" queryName=\"test\"></sql>' FROM table");
    QueryResolver resolver = (entity, query) -> {
        assertNull(entity);
        assertEquals("test", query);
        return "SELECT * FROM subTable WHERE tableID=<var:ID/>";
    };
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().queryResolver(resolver).build());
    contextApplier.applyContext(start);
    String key = contextApplier.subQueryKeys().findFirst().get();
    Map<String, String> vars = Collections.singletonMap("ID", "5");
    AstBeSqlSubQuery subQuery = contextApplier.applyVars(key, vars::get);
    assertEquals("SELECT * FROM subTable WHERE tableID = 5 LIMIT 2", subQuery.getQuery().format());
}
Also used : QueryResolver(com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver) Ignore(org.junit.Ignore) Map(java.util.Map) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) HashMap(java.util.HashMap) Test(org.junit.Test) AstStart(com.developmentontheedge.sql.model.AstStart) Assert(org.junit.Assert) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) Collections(java.util.Collections) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) QueryResolver(com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver) AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Test(org.junit.Test)

Example 5 with AstBeSqlSubQuery

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

Aggregations

AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)11 ContextApplier (com.developmentontheedge.sql.format.ContextApplier)6 AstStart (com.developmentontheedge.sql.model.AstStart)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 BasicQueryContext (com.developmentontheedge.sql.format.BasicQueryContext)3 QueryResolver (com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver)3 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)3 Collections (java.util.Collections)3 Map (java.util.Map)3 Assert (org.junit.Assert)3 Ignore (org.junit.Ignore)3 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)2 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)1 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)1 DynamicPropertySet (com.developmentontheedge.beans.DynamicPropertySet)1 DynamicPropertySetSupport (com.developmentontheedge.beans.DynamicPropertySetSupport)1 Formatter (com.developmentontheedge.sql.format.Formatter)1 AstBeCondition (com.developmentontheedge.sql.model.AstBeCondition)1 AstBeConditionChain (com.developmentontheedge.sql.model.AstBeConditionChain)1