Search in sources :

Example 11 with AstStart

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

the class SubQueryTest method testSubQueryResolverBeVar.

@Test
public void testSubQueryResolverBeVar() {
    AstStart start = SqlQuery.parse("SELECT 'select * from test' AS \"___code\", '<sql><var:___code safestr=\"no\"/></sql>' FROM table");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().build());
    contextApplier.applyContext(start);
    assertEquals("SELECT 'select * from test' AS \"___code\", '<sql> SubQuery# 1</sql>' FROM table", start.getQuery().toString());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) Test(org.junit.Test)

Example 12 with AstStart

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

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

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

the class DpsHelper method addParamsFromQuery.

public <T extends DynamicPropertySet> T addParamsFromQuery(T dps, BeModelElement modelElements, Query query) {
    AstStart ast;
    try {
        ast = SqlQuery.parse(meta.getQueryCode(query, UserInfoHolder.getCurrentRoles()));
    } catch (RuntimeException e) {
        log.log(Level.SEVERE, "SqlQuery.parse error: ", e);
        throw Be5Exception.internalInQuery(e, query);
    }
    List<String> usedParams = ast.tree().select(AstBeParameterTag.class).map(AstBeParameterTag::getName).toList();
    addDpForColumns(dps, modelElements, usedParams);
    return dps;
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart)

Example 15 with AstStart

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

the class Be5QueryExecutor method hasColumnWithLabel.

private boolean hasColumnWithLabel(AstStart ast, String idColumnLabel) {
    AstQuery query = ast.getQuery();
    Optional<AstSelect> selectOpt = query.children().select(AstSelect.class).collect(MoreCollectors.onlyOne());
    if (!selectOpt.isPresent())
        return false;
    AstSelect select = selectOpt.get();
    return select.getSelectList().children().select(AstDerivedColumn.class).map(AstDerivedColumn::getAlias).nonNull().map(alias -> alias.replaceFirst("^\"(.+)\"$", "$1")).has(idColumnLabel);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) Connection(java.sql.Connection) DynamicPropertySet(com.developmentontheedge.beans.DynamicPropertySet) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) Meta(com.developmentontheedge.be5.api.services.Meta) Query(com.developmentontheedge.be5.metadata.model.Query) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) ColumnAdder(com.developmentontheedge.sql.format.ColumnAdder) ResultSetParser(com.developmentontheedge.be5.api.sql.ResultSetParser) UserInfoHolder(com.developmentontheedge.be5.api.helpers.UserInfoHolder) Map(java.util.Map) AstLimit(com.developmentontheedge.sql.model.AstLimit) Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) Context(com.developmentontheedge.sql.format.Context) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) MoreCollectors(one.util.streamex.MoreCollectors) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) Logger(java.util.logging.Logger) EntityModel(com.developmentontheedge.be5.databasemodel.EntityModel) Objects(java.util.Objects) List(java.util.List) QueryType(com.developmentontheedge.be5.metadata.QueryType) LimitsApplier(com.developmentontheedge.sql.format.LimitsApplier) Simplifier(com.developmentontheedge.sql.format.Simplifier) AstBeParameterTag(com.developmentontheedge.sql.model.AstBeParameterTag) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) StreamEx(one.util.streamex.StreamEx) DatabaseConstants(com.developmentontheedge.be5.metadata.DatabaseConstants) Optional(java.util.Optional) FilterHelper(com.developmentontheedge.be5.api.helpers.FilterHelper) RecordModel(com.developmentontheedge.be5.databasemodel.RecordModel) ResultSetMetaData(java.sql.ResultSetMetaData) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstQuery(com.developmentontheedge.sql.model.AstQuery) DatabaseService(com.developmentontheedge.be5.api.services.DatabaseService) QueryContext(com.developmentontheedge.sql.format.QueryContext) HashMap(java.util.HashMap) AstStart(com.developmentontheedge.sql.model.AstStart) Token(com.developmentontheedge.sql.model.Token) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Entity(com.developmentontheedge.be5.metadata.model.Entity) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Injector(com.developmentontheedge.be5.env.Injector) DynamicPropertySetSupport(com.developmentontheedge.beans.DynamicPropertySetSupport) ParserContext(com.developmentontheedge.sql.model.ParserContext) AstSelect(com.developmentontheedge.sql.model.AstSelect) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) SqlService(com.developmentontheedge.be5.api.services.SqlService) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) DatabaseModel(com.developmentontheedge.be5.databasemodel.impl.DatabaseModel) DpsRecordAdapter(com.developmentontheedge.be5.api.sql.DpsRecordAdapter) Ast(com.developmentontheedge.sql.format.Ast) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstNumericConstant(com.developmentontheedge.sql.model.AstNumericConstant) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Collections(java.util.Collections) AstQuery(com.developmentontheedge.sql.model.AstQuery)

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