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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations