Search in sources :

Example 1 with QueryException

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

the class AbstractHibernateQuery method fetchCount.

@Override
public long fetchCount() {
    QueryModifiers modifiers = getMetadata().getModifiers();
    try {
        Query query = createQuery(modifiers, true);
        Long rv = (Long) query.uniqueResult();
        if (rv != null) {
            return rv;
        } else {
            throw new QueryException("Query returned null");
        }
    } finally {
        reset();
    }
}
Also used : QueryException(com.querydsl.core.QueryException) Query(org.hibernate.query.Query)

Example 2 with QueryException

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

the class GenericExporter method innerExport.

@SuppressWarnings("unchecked")
private void innerExport() {
    typeMappings = codegenModule.get(TypeMappings.class);
    queryTypeFactory = codegenModule.get(QueryTypeFactory.class);
    typeFactory = new TypeFactory(Arrays.asList(entityAnnotation, supertypeAnnotation, embeddableAnnotation), codegenModule.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS));
    // copy annotations helpers to typeFactory
    for (AnnotationHelper helper : annotationHelpers) {
        typeFactory.addAnnotationHelper(helper);
    }
    // process supertypes
    for (Class<?> cl : superTypes.keySet()) {
        createEntityType(cl, superTypes);
    }
    // process embeddables
    for (Class<?> cl : embeddableTypes.keySet()) {
        createEntityType(cl, embeddableTypes);
    }
    // process entities
    for (Class<?> cl : entityTypes.keySet()) {
        createEntityType(cl, entityTypes);
    }
    // process projections
    for (Class<?> cl : projectionTypes.keySet()) {
        createEntityType(cl, projectionTypes);
    }
    // add constructors and properties
    for (Map<Class<?>, EntityType> entries : Arrays.asList(superTypes, embeddableTypes, entityTypes, projectionTypes)) {
        for (Map.Entry<Class<?>, EntityType> entry : new HashSet<>(entries.entrySet())) {
            addConstructors(entry.getKey(), entry.getValue());
            addProperties(entry.getKey(), entry.getValue());
        }
    }
    // merge supertype fields into subtypes
    Set<EntityType> handled = new HashSet<EntityType>();
    for (EntityType type : superTypes.values()) {
        addSupertypeFields(type, allTypes, handled);
    }
    for (EntityType type : entityTypes.values()) {
        addSupertypeFields(type, allTypes, handled);
    }
    for (EntityType type : embeddableTypes.values()) {
        addSupertypeFields(type, allTypes, handled);
    }
    // extend types
    typeFactory.extendTypes();
    try {
        Serializer supertypeSerializer, entitySerializer, embeddableSerializer, projectionSerializer;
        if (serializerClass != null) {
            Serializer serializer = codegenModule.get(serializerClass);
            supertypeSerializer = serializer;
            entitySerializer = serializer;
            embeddableSerializer = serializer;
            projectionSerializer = serializer;
        } else {
            supertypeSerializer = codegenModule.get(SupertypeSerializer.class);
            entitySerializer = codegenModule.get(EntitySerializer.class);
            embeddableSerializer = codegenModule.get(EmbeddableSerializer.class);
            projectionSerializer = codegenModule.get(ProjectionSerializer.class);
        }
        // serialize super types
        serialize(supertypeSerializer, superTypes);
        // serialize entity types
        serialize(entitySerializer, entityTypes);
        // serialize embeddable types
        serialize(embeddableSerializer, embeddableTypes);
        // serialize projection types
        serialize(projectionSerializer, projectionTypes);
    } catch (IOException e) {
        throw new QueryException(e);
    }
}
Also used : IOException(java.io.IOException) QueryException(com.querydsl.core.QueryException) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 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.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 4 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(), getFilter(), maxDoc).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 (fieldSelector != null) {
                document = searcher.doc(scoreDocs[index].doc, fieldSelector);
            } 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 5 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(), getFilter(), sumOfLimitAndOffset, sort).scoreDocs;
        } else {
            scoreDocs = searcher.search(createQuery(), getFilter(), sumOfLimitAndOffset).scoreDocs;
        }
        if (offset < scoreDocs.length) {
            return new ResultIterator<T>(scoreDocs, offset, searcher, fieldSelector, 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)

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