Search in sources :

Example 6 with RQueryImpl

use of com.evolveum.midpoint.repo.sql.query2.RQueryImpl in project midpoint by Evolveum.

the class QueryInterpreter2Test method test580QueryObjectypeByTypeAndExtensionAttribute.

@Test
public void test580QueryObjectypeByTypeAndExtensionAttribute() throws Exception {
    Session session = open();
    try {
        ObjectQuery query = QueryBuilder.queryFor(ObjectType.class, prismContext).type(UserType.class).item(UserType.F_EXTENSION, new QName("http://example.com/p", "weapon")).eq("some weapon name").build();
        //            EqualFilter eq = EqualFilter.createEqual(
        //                    new ItemPath(ObjectType.F_EXTENSION, new QName("http://example.com/p", "weapon")),
        //                    UserType.class, prismContext, "some weapon name");
        //            TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, eq);
        RQueryImpl realQuery = (RQueryImpl) getInterpretedQuery2Whole(session, ObjectType.class, query, false, null);
        String expected = "select\n" + "  o.oid, o.fullObject,\n" + "  o.stringsCount,\n" + "  o.longsCount,\n" + "  o.datesCount,\n" + "  o.referencesCount,\n" + "  o.polysCount,\n" + "  o.booleansCount\n" + "from\n" + "  RObject o\n" + "    left join o.strings s with ( s.ownerType = :ownerType and s.name = :name )\n" + "where\n" + "  (\n" + "    o.objectTypeClass = :objectTypeClass and\n" + "    s.value = :value\n" + "  )\n";
        assertEqualsIgnoreWhitespace(expected, realQuery.getQuery().getQueryString());
        assertEquals("Wrong property URI for 'weapon'", "http://example.com/p#weapon", realQuery.getQuerySource().getParameters().get("name").getValue());
    } finally {
        close(session);
    }
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) QName(javax.xml.namespace.QName) RQueryImpl(com.evolveum.midpoint.repo.sql.query2.RQueryImpl) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 7 with RQueryImpl

use of com.evolveum.midpoint.repo.sql.query2.RQueryImpl in project midpoint by Evolveum.

the class ObjectRetriever method executeQueryDiagnosticsRequest.

public RepositoryQueryDiagResponse executeQueryDiagnosticsRequest(RepositoryQueryDiagRequest request, OperationResult result) {
    LOGGER_PERFORMANCE.debug("> execute query diagnostics {}", request);
    Session session = null;
    try {
        // beware, not all databases support read-only transactions!
        session = baseHelper.beginReadOnlyTransaction();
        final String implementationLevelQuery;
        final Map<String, RepositoryQueryDiagResponse.ParameterValue> implementationLevelQueryParameters;
        final Query query;
        final boolean isMidpointQuery = request.getImplementationLevelQuery() == null;
        if (isMidpointQuery) {
            QueryEngine2 engine = new QueryEngine2(getConfiguration(), prismContext);
            RQueryImpl rQuery = (RQueryImpl) engine.interpret(request.getQuery(), request.getType(), null, false, session);
            query = rQuery.getQuery();
            implementationLevelQuery = query.getQueryString();
            implementationLevelQueryParameters = new HashMap<>();
            for (Map.Entry<String, QueryParameterValue> entry : rQuery.getQuerySource().getParameters().entrySet()) {
                implementationLevelQueryParameters.put(entry.getKey(), new RepositoryQueryDiagResponse.ParameterValue(entry.getValue().getValue(), entry.getValue().toString()));
            }
        } else {
            implementationLevelQuery = (String) request.getImplementationLevelQuery();
            implementationLevelQueryParameters = new HashMap<>();
            query = session.createQuery(implementationLevelQuery);
        }
        List<?> objects = request.isTranslateOnly() ? null : query.list();
        if (isMidpointQuery && objects != null) {
            // raw GetObjectResult instances are useless outside repo-sql-impl module, so we'll convert them to objects
            @SuppressWarnings("unchecked") List<GetObjectResult> listOfGetObjectResults = (List<GetObjectResult>) objects;
            objects = queryResultToPrismObjects(listOfGetObjectResults, request.getType(), null, session, result);
        }
        RepositoryQueryDiagResponse response = new RepositoryQueryDiagResponse(objects, implementationLevelQuery, implementationLevelQueryParameters);
        session.getTransaction().rollback();
        return response;
    } catch (SchemaException | QueryException | RuntimeException ex) {
        baseHelper.handleGeneralException(ex, session, result);
        throw new IllegalStateException("shouldn't get here");
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
    }
}
Also used : QueryParameterValue(com.evolveum.midpoint.repo.sql.query2.hqm.QueryParameterValue) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QueryParameterValue(com.evolveum.midpoint.repo.sql.query2.hqm.QueryParameterValue) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) QueryEngine2(com.evolveum.midpoint.repo.sql.query2.QueryEngine2) RQueryImpl(com.evolveum.midpoint.repo.sql.query2.RQueryImpl)

Example 8 with RQueryImpl

use of com.evolveum.midpoint.repo.sql.query2.RQueryImpl in project midpoint by Evolveum.

the class QueryEngine2 method interpret.

public RQuery interpret(ObjectQuery query, Class<? extends Containerable> type, Collection<SelectorOptions<GetOperationOptions>> options, boolean countingObjects, Session session) throws QueryException {
    QueryInterpreter2 interpreter = new QueryInterpreter2(repoConfiguration);
    RootHibernateQuery hibernateQuery = interpreter.interpret(query, type, options, prismContext, countingObjects, session);
    return new RQueryImpl(hibernateQuery.getAsHqlQuery(session), hibernateQuery);
}
Also used : RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)

Example 9 with RQueryImpl

use of com.evolveum.midpoint.repo.sql.query2.RQueryImpl in project midpoint by Evolveum.

the class QueryInterpreter2Test method test630QueryGenericBoolean.

//    @Test(enabled = false)
//    public void atest100() throws Exception {
//        Session session = open();
//
//        try {
//            String expected = null;//HibernateToSqlTranslator.toSql(main);
//
//            List<EqualFilter> secondaryEquals = new ArrayList<>();
//            EqualFilter eq = EqualFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, SchemaConstantsGenerated.ICF_S_UID),
//                    new PrismPropertyDefinitionImpl(SchemaConstantsGenerated.ICF_S_UID, DOMUtil.XSD_STRING, prismContext),
//                    "8daaeeae-f0c7-41c9-b258-2a3351aa8876");
//            secondaryEquals.add(eq);
//            eq = EqualFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, SchemaConstantsGenerated.ICF_S_NAME),
//                    new PrismPropertyDefinitionImpl(SchemaConstantsGenerated.ICF_S_NAME, DOMUtil.XSD_STRING, prismContext),
//                    "some-name");
//            secondaryEquals.add(eq);
//
//            OrFilter secondaryIdentifierFilter = OrFilter.createOr((List) secondaryEquals);
//            RefFilter ref = RefFilter.createReferenceEqual(ShadowType.F_RESOURCE_REF, ShadowType.class,
//                    prismContext, "ef2bc95b-76e0-48e2-86d6-3d4f02d3e1a2");
//
//            AndFilter filter = AndFilter.createAnd(ref, secondaryIdentifierFilter);
//            ObjectQuery query = ObjectQuery.createObjectQuery(filter);
//            LOGGER.debug("Query\n{}", query);
//
//            QueryEngine engine = new QueryEngine(repositoryService.getConfiguration(), prismContext);
//            RQuery rQuery = engine.interpret(query, ShadowType.class, null, false, session);
//            RQueryCriteriaImpl rci = (RQueryCriteriaImpl) rQuery;
//            System.out.println(rci);
//            System.out.println(rci.getCriteria());
//            //just test if DB will handle it or throws some exception
//            List l = rQuery.list();
//            LOGGER.info(">>>>>>>>asdfasdfasdfasdf{}",l.size());
//
//            String real = getInterpretedQuery2(session, ShadowType.class, query);
//
//            LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[]{expected, real});
//            AssertJUnit.assertEquals(expected, real);
//        } finally {
//            close(session);
//        }
//    }
@Test
public void test630QueryGenericBoolean() throws Exception {
    Session session = open();
    try {
        ObjectQuery objectQuery = QueryBuilder.queryFor(GenericObjectType.class, prismContext).item(ObjectType.F_EXTENSION, SKIP_AUTOGENERATION).eq(true).build();
        objectQuery.setUseNewQueryInterpreter(true);
        RQueryImpl realQuery = (RQueryImpl) getInterpretedQuery2Whole(session, GenericObjectType.class, objectQuery, false, null);
        String expected = "select\n" + "  g.oid, g.fullObject,\n" + "  g.stringsCount,\n" + "  g.longsCount,\n" + "  g.datesCount,\n" + "  g.referencesCount,\n" + "  g.polysCount,\n" + "  g.booleansCount\n" + "from\n" + "  RGenericObject g\n" + "    left join g.booleans b with ( b.ownerType = :ownerType and b.name = :name )\n" + "where\n" + "  b.value = :value\n";
        assertEqualsIgnoreWhitespace(expected, realQuery.getQuery().getQueryString());
        assertEquals("Wrong property URI for 'skipAutogeneration'", "http://example.com/p#skipAutogeneration", realQuery.getQuerySource().getParameters().get("name").getValue());
        OperationResult result = new OperationResult("search");
        List<PrismObject<GenericObjectType>> objects = repositoryService.searchObjects(GenericObjectType.class, objectQuery, null, result);
        result.computeStatus();
        AssertJUnit.assertTrue(result.isSuccess());
        AssertJUnit.assertNotNull(objects);
        assertEquals(1, objects.size());
        PrismObject<GenericObjectType> obj = objects.get(0);
        AssertJUnit.assertTrue(obj.getCompileTimeClass().equals(GenericObjectType.class));
        result = new OperationResult("count");
        long count = repositoryService.countObjects(GenericObjectType.class, objectQuery, result);
        result.computeStatus();
        AssertJUnit.assertTrue(result.isSuccess());
        assertEquals(1, count);
    } finally {
        close(session);
    }
}
Also used : RQueryImpl(com.evolveum.midpoint.repo.sql.query2.RQueryImpl) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Aggregations

RQueryImpl (com.evolveum.midpoint.repo.sql.query2.RQueryImpl)8 Session (org.hibernate.Session)7 Test (org.testng.annotations.Test)7 File (java.io.File)3 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)2 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)2 QName (javax.xml.namespace.QName)2 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)1 QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)1 QueryEngine2 (com.evolveum.midpoint.repo.sql.query2.QueryEngine2)1 QueryParameterValue (com.evolveum.midpoint.repo.sql.query2.hqm.QueryParameterValue)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)1