Search in sources :

Example 11 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlFilterConverterTest method checkEqFilters.

@Test
public void checkEqFilters() throws SearchException {
    // EQ -- String
    Filter filterEq1 = Filter.createEqualityFilter("uid", "test");
    ConvertedExpression expressionEq1 = simpleConverter.convertToSqlFilter(filterEq1, null, null);
    String queryEq1 = toSelectSQL(expressionEq1);
    assertEquals(queryEq1, "select doc.`*` from `table` as doc where doc.uid = 'test'");
    // EQ -- Integer
    Filter filterEq2 = Filter.createEqualityFilter("age", 23);
    ConvertedExpression expressionEq2 = simpleConverter.convertToSqlFilter(filterEq2, null, null);
    String queryEq2 = toSelectSQL(expressionEq2);
    assertEquals(queryEq2, "select doc.`*` from `table` as doc where doc.age = 23");
    // EQ -- Long
    Filter filterEq3 = Filter.createEqualityFilter("age", 23L);
    ConvertedExpression expressionEq3 = simpleConverter.convertToSqlFilter(filterEq3, null, null);
    String queryEq3 = toSelectSQL(expressionEq3);
    assertEquals(queryEq3, "select doc.`*` from `table` as doc where doc.age = 23");
    // EQ -- Date
    Filter filterEq4 = Filter.createEqualityFilter("added", getUtcDateFromMillis(1608130698398L));
    ConvertedExpression expressionEq4 = simpleConverter.convertToSqlFilter(filterEq4, null, null);
    String queryEq4 = toSelectSQL(expressionEq4);
    assertEquals(queryEq4, "select doc.`*` from `table` as doc where doc.added = '2020-12-16T14:58:18.398'");
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Test(org.testng.annotations.Test)

Example 12 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlFilterConverterTest method checkMultivaluedLowerFilters.

@Test
public void checkMultivaluedLowerFilters() throws SearchException {
    Filter userUidFilter = Filter.createEqualityFilter(Filter.createLowercaseFilter("uid"), "test").multiValued();
    ConvertedExpression expressionUserUid = simpleConverter.convertToSqlFilter(userUidFilter, null, null);
    String queryUserUid = toSelectSQL(expressionUserUid);
    assertEquals(queryUserUid, "select doc.`*` from `table` as doc where JSON_CONTAINS(lower(doc.uid)->'$.v', CAST('[\"test\"]' AS JSON))");
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Test(org.testng.annotations.Test)

Example 13 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlEntryManager method findEntriesImpl.

protected <T> PagedResult<EntryData> findEntriesImpl(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, BatchOperation<T> batchOperation, SearchReturnDataType returnDataType, int start, int count, int chunkSize) {
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    String[] currentLdapReturnAttributes = ldapReturnAttributes;
    if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
        currentLdapReturnAttributes = getAttributes(null, propertiesAnnotations, false);
    }
    Filter searchFilter;
    if (objectClasses.length > 0) {
        LOG.trace("Filter: {}", filter);
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    // Find entries
    LOG.trace("-------------------------------------------------------");
    LOG.trace("Filter: {}", filter);
    LOG.trace("objectClasses count: {} ", objectClasses.length);
    LOG.trace("objectClasses: {}", ArrayHelper.toString(objectClasses));
    LOG.trace("Search filter: {}", searchFilter);
    // Prepare default sort
    OrderSpecifier<?>[] defaultSort = getDefaultSort(entryClass);
    if (StringHelper.isNotEmpty(sortBy)) {
        OrderSpecifier<?> requestedSort = buildSort(sortBy, sortOrder);
        if (ArrayHelper.isEmpty(defaultSort)) {
            defaultSort = new OrderSpecifier[] { requestedSort };
        } else {
            defaultSort = ArrayHelper.arrayMerge(new OrderSpecifier[] { requestedSort }, defaultSort);
        }
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    ParsedKey keyWithInum = toSQLKey(baseDN);
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilter(searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
    }
    PagedResult<EntryData> searchResult = null;
    try {
        SqlBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new SqlBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        }
        searchResult = searchImpl(keyWithInum.getKey(), objectClasses[0], convertedExpression, scope, currentLdapReturnAttributes, defaultSort, batchOperationWraper, returnDataType, start, count, chunkSize);
        if (searchResult == null) {
            throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression));
        }
        return searchResult;
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s'", keyWithInum.getKey()), ex);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression), ex);
    }
}
Also used : EntryData(io.jans.orm.model.EntryData) SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) DateTimeParseException(java.time.format.DateTimeParseException) AuthenticationException(io.jans.orm.exception.AuthenticationException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) OrderSpecifier(com.querydsl.core.types.OrderSpecifier)

Example 14 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlFilterConverter method convertToSqlFilter.

@SuppressWarnings({ "unchecked", "rawtypes" })
public ConvertedExpression convertToSqlFilter(Filter genericFilter, Map<String, PropertyAnnotation> propertiesAnnotationsMap, Function<? super Filter, Boolean> processor, boolean skipAlias) throws SearchException {
    Map<String, Class<?>> jsonAttributes = new HashMap<>();
    ConvertedExpression convertedExpression = convertToSqlFilterImpl(genericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
    return convertedExpression;
}
Also used : HashMap(java.util.HashMap) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression)

Example 15 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlFilterConverterTest method checkNotFilters.

@Test
public void checkNotFilters() throws SearchException {
    Filter notFilter1 = Filter.createNOTFilter(Filter.createLessOrEqualFilter("age", 23));
    ConvertedExpression expressionNot1 = simpleConverter.convertToSqlFilter(notFilter1, null, null);
    String queryUserUid1 = toSelectSQL(expressionNot1);
    assertEquals(queryUserUid1, "select doc.`*` from `table` as doc where not doc.age <= 23");
    Filter notFilter2 = Filter.createNOTFilter(Filter.createANDFilter(Filter.createLessOrEqualFilter("age", 23), Filter.createGreaterOrEqualFilter("age", 25)));
    ConvertedExpression expressionNot2 = simpleConverter.convertToSqlFilter(notFilter2, null, null);
    String queryUserUid2 = toSelectSQL(expressionNot2);
    assertEquals(queryUserUid2, "select doc.`*` from `table` as doc where not (doc.age <= 23 and doc.age >= 25)");
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Test(org.testng.annotations.Test)

Aggregations

ConvertedExpression (io.jans.orm.sql.model.ConvertedExpression)25 Filter (io.jans.orm.search.filter.Filter)24 Test (org.testng.annotations.Test)19 SearchException (io.jans.orm.exception.operation.SearchException)5 AuthenticationException (io.jans.orm.exception.AuthenticationException)4 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)4 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)4 MappingException (io.jans.orm.exception.MappingException)4 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)4 DateTimeParseException (java.time.format.DateTimeParseException)4 ParsedKey (io.jans.orm.impl.model.ParsedKey)3 EntryData (io.jans.orm.model.EntryData)3 Expression (com.querydsl.core.types.Expression)1 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)1 Predicate (com.querydsl.core.types.Predicate)1 AttributeEnum (io.jans.orm.annotation.AttributeEnum)1 FilterType (io.jans.orm.search.filter.FilterType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1