Search in sources :

Example 16 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class AbstractLuceneQuery method oneResult.

@Nullable
private T oneResult(boolean unique) {
    try {
        int maxDoc = maxDoc();
        if (maxDoc == 0) {
            return null;
        }
        final ScoreDoc[] scoreDocs = searcher.search(createQuery(), maxDoc, Sort.INDEXORDER, false, false).scoreDocs;
        int index = 0;
        QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
        Long offset = modifiers.getOffset();
        if (offset != null) {
            index = offset.intValue();
        }
        Long limit = modifiers.getLimit();
        if (unique && (limit == null ? scoreDocs.length - index > 1 : limit > 1 && scoreDocs.length > 1)) {
            throw new NonUniqueResultException("Unique result requested, but " + scoreDocs.length + " found.");
        } else if (scoreDocs.length > index) {
            Document document;
            if (fieldsToLoad != null) {
                document = searcher.doc(scoreDocs[index].doc, fieldsToLoad);
            } else {
                document = searcher.doc(scoreDocs[index].doc);
            }
            return transformer.apply(document);
        } else {
            return null;
        }
    } catch (IOException | IllegalArgumentException e) {
        throw new QueryException(e);
    }
}
Also used : NonUniqueResultException(com.querydsl.core.NonUniqueResultException) QueryModifiers(com.querydsl.core.QueryModifiers) QueryException(com.querydsl.core.QueryException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class AbstractLuceneQuery method innerCount.

private long innerCount() {
    try {
        final int maxDoc = searcher.getIndexReader().maxDoc();
        if (maxDoc == 0) {
            return 0;
        }
        TotalHitCountCollector collector = new TotalHitCountCollector();
        searcher.search(createQuery(), getFilter(), collector);
        return collector.getTotalHits();
    } catch (IOException | IllegalArgumentException e) {
        throw new QueryException(e);
    }
}
Also used : QueryException(com.querydsl.core.QueryException) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) IOException(java.io.IOException)

Example 18 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class AbstractLuceneQuery method iterate.

@Override
public CloseableIterator<T> iterate() {
    final QueryMetadata metadata = queryMixin.getMetadata();
    final List<OrderSpecifier<?>> orderBys = metadata.getOrderBy();
    final Integer queryLimit = metadata.getModifiers().getLimitAsInteger();
    final Integer queryOffset = metadata.getModifiers().getOffsetAsInteger();
    Sort sort = querySort;
    int limit;
    final int offset = queryOffset != null ? queryOffset : 0;
    try {
        limit = maxDoc();
        if (limit == 0) {
            return new EmptyCloseableIterator<T>();
        }
    } catch (IOException | IllegalArgumentException e) {
        throw new QueryException(e);
    }
    if (queryLimit != null && queryLimit < limit) {
        limit = queryLimit;
    }
    if (sort == null && !orderBys.isEmpty()) {
        sort = serializer.toSort(orderBys);
    }
    try {
        ScoreDoc[] scoreDocs;
        int sumOfLimitAndOffset = limit + offset;
        if (sumOfLimitAndOffset < 1) {
            throw new QueryException("The given limit (" + limit + ") and offset (" + offset + ") cause an integer overflow.");
        }
        if (sort != null) {
            scoreDocs = searcher.search(createQuery(), // sumOfLimitAndOffset).scoreDocs;
            sumOfLimitAndOffset, sort, false, false).scoreDocs;
        } else {
            scoreDocs = searcher.search(createQuery(), sumOfLimitAndOffset, Sort.INDEXORDER, false, false).scoreDocs;
        }
        if (offset < scoreDocs.length) {
            return new ResultIterator<T>(scoreDocs, offset, searcher, fieldsToLoad, transformer);
        }
        return new EmptyCloseableIterator<T>();
    } catch (final IOException e) {
        throw new QueryException(e);
    }
}
Also used : DefaultQueryMetadata(com.querydsl.core.DefaultQueryMetadata) QueryMetadata(com.querydsl.core.QueryMetadata) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) QueryException(com.querydsl.core.QueryException) EmptyCloseableIterator(com.mysema.commons.lang.EmptyCloseableIterator) Sort(org.apache.lucene.search.Sort) OrderSpecifier(com.querydsl.core.types.OrderSpecifier)

Example 19 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class AnnotationMapper method createMap.

@Override
public Map<Path<?>, Object> createMap(RelationalPath<?> path, Object object) {
    try {
        Map<String, Path<?>> columnToPath = new HashMap<String, Path<?>>();
        for (Path<?> column : path.getColumns()) {
            columnToPath.put(ColumnMetadata.getName(column), column);
        }
        Map<Path<?>, Object> values = new HashMap<Path<?>, Object>();
        for (Field field : ReflectionUtils.getFields(object.getClass())) {
            Column ann = field.getAnnotation(Column.class);
            if (ann != null) {
                field.setAccessible(true);
                Object propertyValue = field.get(object);
                if (propertyValue != null) {
                    if (columnToPath.containsKey(ann.value())) {
                        values.put(columnToPath.get(ann.value()), propertyValue);
                    }
                } else if (withNullBindings) {
                    values.put(columnToPath.get(ann.value()), Null.DEFAULT);
                }
            }
        }
        return values;
    } catch (IllegalAccessException e) {
        throw new QueryException(e);
    }
}
Also used : RelationalPath(com.querydsl.sql.RelationalPath) Path(com.querydsl.core.types.Path) Field(java.lang.reflect.Field) QueryException(com.querydsl.core.QueryException) HashMap(java.util.HashMap) Column(com.querydsl.sql.Column)

Example 20 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class DefaultMapper method createMap.

@Override
public Map<Path<?>, Object> createMap(RelationalPath<?> entity, Object bean) {
    try {
        Map<Path<?>, Object> values = new LinkedHashMap<>();
        Class<?> beanClass = bean.getClass();
        Map<String, Path<?>> columns = getColumns(entity);
        // populate in column order
        for (Map.Entry<String, Path<?>> entry : columns.entrySet()) {
            Path<?> path = entry.getValue();
            Field beanField = ReflectionUtils.getFieldOrNull(beanClass, entry.getKey());
            if (beanField != null && !Modifier.isStatic(beanField.getModifiers())) {
                beanField.setAccessible(true);
                Object propertyValue = beanField.get(bean);
                if (propertyValue != null) {
                    values.put(path, propertyValue);
                } else if (withNullBindings && !isPrimaryKeyColumn(entity, path)) {
                    values.put(path, Null.DEFAULT);
                }
            }
        }
        return values;
    } catch (IllegalAccessException e) {
        throw new QueryException(e);
    }
}
Also used : RelationalPath(com.querydsl.sql.RelationalPath) Path(com.querydsl.core.types.Path) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field) QueryException(com.querydsl.core.QueryException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

QueryException (com.querydsl.core.QueryException)22 IOException (java.io.IOException)11 ScoreDoc (org.apache.lucene.search.ScoreDoc)6 SQLException (java.sql.SQLException)5 EmptyCloseableIterator (com.mysema.commons.lang.EmptyCloseableIterator)3 DefaultQueryMetadata (com.querydsl.core.DefaultQueryMetadata)3 NonUniqueResultException (com.querydsl.core.NonUniqueResultException)3 QueryMetadata (com.querydsl.core.QueryMetadata)3 QueryModifiers (com.querydsl.core.QueryModifiers)3 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)3 Field (java.lang.reflect.Field)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Document (org.apache.lucene.document.Document)3 Sort (org.apache.lucene.search.Sort)3 TotalHitCountCollector (org.apache.lucene.search.TotalHitCountCollector)3 Nullable (org.jetbrains.annotations.Nullable)3 Path (com.querydsl.core.types.Path)2 RelationalPath (com.querydsl.sql.RelationalPath)2 Statement (java.sql.Statement)2