Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The sybase jdbc driver returns a integer as count result. #303

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,48 @@

import com.sap.olingo.jpa.metadata.core.edm.mapper.api.JPAServiceDocument;
import com.sap.olingo.jpa.processor.cb.impl.EntityManagerWrapper;
import com.sap.olingo.jpa.processor.cb.impl.SqlPagingFunctions;

public final class EntityManagerFactoryWrapper implements EntityManagerFactory {
private final EntityManagerFactory emf;
private final JPAServiceDocument sd;
private final SqlPagingFunctions sqlPagingFunctions;

public EntityManagerFactoryWrapper(final EntityManagerFactory emf, final JPAServiceDocument sd) {
public EntityManagerFactoryWrapper(final EntityManagerFactory emf, final JPAServiceDocument sd, final SqlPagingFunctions sqlPagingFunctions) {
super();
this.emf = emf;
this.sd = sd;
if ( sqlPagingFunctions == null ) {
this.sqlPagingFunctions = new SqlPagingFunctions();
} else {
this.sqlPagingFunctions = sqlPagingFunctions;
}
}

@Override
public EntityManager createEntityManager() {
return new EntityManagerWrapper(emf.createEntityManager(), sd);
return new EntityManagerWrapper(emf.createEntityManager(), sd,sqlPagingFunctions);
}

@Override
public EntityManager createEntityManager(@SuppressWarnings("rawtypes") final Map map) {
return new EntityManagerWrapper(emf.createEntityManager(map), sd);
return new EntityManagerWrapper(emf.createEntityManager(map), sd, sqlPagingFunctions);
}

@Override
public EntityManager createEntityManager(final SynchronizationType synchronizationType) {
return new EntityManagerWrapper(emf.createEntityManager(synchronizationType), sd);
return new EntityManagerWrapper(emf.createEntityManager(synchronizationType), sd, sqlPagingFunctions);
}

@Override
public EntityManager createEntityManager(final SynchronizationType synchronizationType,
@SuppressWarnings("rawtypes") final Map map) {
return new EntityManagerWrapper(emf.createEntityManager(synchronizationType, map), sd);
return new EntityManagerWrapper(emf.createEntityManager(synchronizationType, map), sd, sqlPagingFunctions);
}

@Override
public CriteriaBuilder getCriteriaBuilder() {
try (EntityManager em = new EntityManagerWrapper(emf.createEntityManager(), sd)) {
try (EntityManager em = new EntityManagerWrapper(emf.createEntityManager(), sd, sqlPagingFunctions)) {
return em.getCriteriaBuilder();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ class CriteriaBuilderImpl implements ProcessorCriteriaBuilder { // NOSONAR

private final JPAServiceDocument sd;
private final ParameterBuffer parameter;
private final SqlPagingFunctions sqlPagingFunctions;

CriteriaBuilderImpl(final JPAServiceDocument sd, final ParameterBuffer parameterBuffer) {
CriteriaBuilderImpl(final JPAServiceDocument sd, final ParameterBuffer parameterBuffer, final SqlPagingFunctions sqlPagingFunctions) {
this.sd = sd;
this.parameter = parameterBuffer;
this.sqlPagingFunctions = sqlPagingFunctions;
}

/**
Expand Down Expand Up @@ -287,17 +289,17 @@ public <T> CriteriaUpdate<T> createCriteriaUpdate(final Class<T> targetEntity) {

@Override
public ProcessorCriteriaQuery<Object> createQuery() {
return new CriteriaQueryImpl<>(Object.class, sd, this);
return new CriteriaQueryImpl<>(Object.class, sd, this,sqlPagingFunctions);
}

@Override
public <T> ProcessorCriteriaQuery<T> createQuery(final Class<T> resultClass) {
return new CriteriaQueryImpl<>(resultClass, sd, this);
return new CriteriaQueryImpl<>(resultClass, sd, this, sqlPagingFunctions);
}

@Override
public ProcessorCriteriaQuery<Tuple> createTupleQuery() {
return new CriteriaQueryImpl<>(Tuple.class, sd, this);
return new CriteriaQueryImpl<>(Tuple.class, sd, this, sqlPagingFunctions);
}

@Override
Expand Down Expand Up @@ -561,7 +563,7 @@ public In<List<Comparable<?>>> in(final List<Path<Comparable<?>>> paths,
*/
@Override
public <T> In<T> in(final Path<?> path) {
return new PredicateImpl.In<>(path, parameter);
return new PredicateImpl.In<>(path, parameter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ class CriteriaQueryImpl<T> implements ProcessorCriteriaQuery<T>, SqlConvertible
private Optional<Integer> maxResults;
private Optional<Integer> firstResult;
private final CriteriaBuilder cb;
private final SqlPagingFunctions sqlPagingFunctions;

CriteriaQueryImpl(final Class<T> clazz, final JPAServiceDocument sd, final AliasBuilder ab,
final CriteriaBuilder cb) {
final CriteriaBuilder cb, final SqlPagingFunctions sqlPagingFunctions ) {
super();
this.resultType = clazz;
this.sd = sd;
this.sqlPagingFunctions = sqlPagingFunctions;
this.where = Optional.empty();
this.orderList = Optional.empty();
this.groupBy = Optional.empty();
Expand All @@ -70,8 +72,8 @@ class CriteriaQueryImpl<T> implements ProcessorCriteriaQuery<T>, SqlConvertible
this.selectAliasBuilder = new AliasBuilder("S");
}

CriteriaQueryImpl(final Class<T> clazz, final JPAServiceDocument sd, final CriteriaBuilder cb) {
this(clazz, sd, new AliasBuilder(), cb);
CriteriaQueryImpl(final Class<T> clazz, final JPAServiceDocument sd, final CriteriaBuilder cb, final SqlPagingFunctions sqlPagingFunctions ) {
this(clazz, sd, new AliasBuilder(), cb, sqlPagingFunctions);
}

@Override
Expand Down Expand Up @@ -112,11 +114,11 @@ public StringBuilder asSQL(final StringBuilder statement) {
((SqlConvertible) e).asSQL(statement);
});
maxResults.ifPresent(limit -> statement.append(" ")
.append(SqlPagingFunctions.LIMIT)
.append(sqlPagingFunctions.LIMIT)
.append(" ")
.append(limit));
firstResult.ifPresent(offset -> statement.append(" ")
.append(SqlPagingFunctions.OFFSET)
.append(sqlPagingFunctions.OFFSET)
.append(" ")
.append(offset));
return statement;
Expand Down Expand Up @@ -405,7 +407,7 @@ public CriteriaQuery<T> select(final Selection<? extends T> selection) {

@Override
public <U> ProcessorSubquery<U> subquery(@Nonnull final Class<U> type) {
return new SubqueryImpl<>(type, this, aliasBuilder, cb);
return new SubqueryImpl<>(type, this, aliasBuilder, cb, sqlPagingFunctions);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sap.olingo.jpa.processor.cb.impl;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -34,11 +35,14 @@ public class EntityManagerWrapper implements EntityManager { // NOSONAR
private final EntityManager em;
private final JPAServiceDocument sd;
private final ParameterBuffer parameterBuffer;
private final SqlPagingFunctions sqlPagingFunctions;

public EntityManagerWrapper(final EntityManager em, final JPAServiceDocument sd) {

public EntityManagerWrapper(final EntityManager em, final JPAServiceDocument sd, final SqlPagingFunctions sqlPagingFunctions) {
super();
this.em = em;
this.sd = sd;
this.sqlPagingFunctions = sqlPagingFunctions;
this.cb = Optional.empty();
this.parameterBuffer = new ParameterBuffer();
}
Expand Down Expand Up @@ -676,7 +680,7 @@ public ProcessorCriteriaBuilder getCriteriaBuilder() {
if (!em.isOpen())
throw new IllegalStateException("Entity Manager had been closed");
return cb.orElseGet(() -> {
cb = Optional.of(new CriteriaBuilderImpl(sd, parameterBuffer));
cb = Optional.of(new CriteriaBuilderImpl(sd, parameterBuffer, sqlPagingFunctions));
return cb.get();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
package com.sap.olingo.jpa.processor.cb.impl;

enum SqlPagingFunctions {
LIMIT("LIMIT", Integer.MAX_VALUE),
OFFSET("OFFSET", 0);
import java.sql.Connection;
import java.sql.SQLException;

private final String keyWord;
private final int defaultValue;
public class SqlPagingFunctions {
public final PagingFunction LIMIT;
public final PagingFunction OFFSET;

private SqlPagingFunctions(final String keyWord, final int defaultValue) {
this.keyWord = keyWord;
this.defaultValue = defaultValue;
public SqlPagingFunctions() {
LIMIT = new PagingFunction("LIMIT", Integer.MAX_VALUE);
OFFSET = new PagingFunction("OFFSET", 0);
}

@Override
public String toString() {
return keyWord;
public SqlPagingFunctions(Connection conn) {
boolean issybase = false;
try {
String driver = conn.getMetaData().getURL();
issybase = driver.startsWith("jdbc:sybase");
} catch ( SQLException e ) {
e.printStackTrace();
}
if ( issybase ) {
LIMIT = new PagingFunction("ROWS LIMIT", Integer.MAX_VALUE);
} else {
LIMIT = new PagingFunction("LIMIT", Integer.MAX_VALUE);
}
OFFSET = new PagingFunction("OFFSET", 0);
}

int defaultValue() {
return defaultValue;
public class PagingFunction {
private final String keyWord;
private final int defaultValue;

public PagingFunction(final String keyWord, final int defaultValue) {
this.keyWord = keyWord;
this.defaultValue = defaultValue;
}

@Override
public String toString() {
return keyWord;
}

int defaultValue() {
return defaultValue;

}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ class SubqueryImpl<T> implements ProcessorSubquery<T>, SqlConvertible {
private Optional<Integer> firstResult;

SubqueryImpl(@Nonnull final Class<T> type, @Nonnull final CriteriaQuery<?> parent, final AliasBuilder ab,
final CriteriaBuilder cb) {
final CriteriaBuilder cb, final SqlPagingFunctions sqlPagingFunctions) {
super();
this.type = Objects.requireNonNull(type);
this.parent = Objects.requireNonNull(parent);
this.inner = new CriteriaQueryImpl<>(type, ((CriteriaQueryImpl<?>) parent).getServiceDocument(), ab, cb);
this.inner = new CriteriaQueryImpl<>(type, ((CriteriaQueryImpl<?>) parent).getServiceDocument(), ab, cb, sqlPagingFunctions);
maxResult = Optional.empty();
firstResult = Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public final class JPAODataServiceContext implements JPAODataSessionContextAcces
private final boolean useAbsoluteContextURL;
private final List<AnnotationProvider> annotationProvider;
private final JPAODataQueryDirectives queryDirectives;
private final Object sqlPagingFunctions;

public static JPAODataServiceContextBuilder with() {
return new Builder();
Expand All @@ -78,6 +79,7 @@ private JPAODataServiceContext(final Builder builder) {
useAbsoluteContextURL = builder.useAbsoluteContextURL;
annotationProvider = Arrays.asList(builder.annotationProvider);
queryDirectives = builder.queryDirectives;
sqlPagingFunctions = builder.sqlPagingFunctions;
}

@Override
Expand Down Expand Up @@ -153,6 +155,11 @@ public JPAODataQueryDirectives getQueryDirectives() {
return queryDirectives;
}

@Override
public Object setSqlPagingFunctions(Object sqlPagingFunctions) {
return this.sqlPagingFunctions;
}

static class Builder implements JPAODataServiceContextBuilder {

private String namespace;
Expand All @@ -172,6 +179,7 @@ static class Builder implements JPAODataServiceContextBuilder {
private boolean useAbsoluteContextURL = false;
private AnnotationProvider[] annotationProvider;
private JPAODataQueryDirectivesImpl queryDirectives;
private Object sqlPagingFunctions;

private Builder() {
super();
Expand Down Expand Up @@ -388,6 +396,12 @@ public JPAODataServiceContextBuilder setAnnotationProvider(final AnnotationProvi
return this;
}

@Override
public JPAODataServiceContextBuilder setSqlPagingFunctions(final Object sqlPagingFunctions) {
this.sqlPagingFunctions = sqlPagingFunctions;
return this;
}

@SuppressWarnings("unchecked")
private void createEmfWrapper() {
if (emf.isPresent()) {
Expand All @@ -398,7 +412,7 @@ private void createEmfWrapper() {
jpaEdm = new JPAEdmProvider(emf.get().getMetamodel(), postProcessor, packageName, nameBuilder, Arrays
.asList(annotationProvider));
emf = Optional.of(wrapperClass.getConstructor(EntityManagerFactory.class,
JPAServiceDocument.class).newInstance(emf.get(), jpaEdm.getServiceDocument()));
JPAServiceDocument.class).newInstance(emf.get(), jpaEdm.getServiceDocument(), sqlPagingFunctions));
LOGGER.trace("Criteria Builder Extension found. It will be used");
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
Expand All @@ -421,5 +435,7 @@ public JPAODataServiceContextBuilder setQueryDirectives(final JPAODataQueryDirec
this.queryDirectives = queryDirectives;
return this;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ <T extends JPAODataBatchProcessor> JPAODataServiceContextBuilder setBatchProcess

JPAODataServiceContextBuilder setAnnotationProvider(AnnotationProvider... annotationProvider);

JPAODataServiceContextBuilder setSqlPagingFunctions(Object sqlPagingFunctions);

/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ public default boolean useAbsoluteContextURL() {

public JPAODataQueryDirectives getQueryDirectives();

public Object setSqlPagingFunctions(final Object sqlPagingFunctions);

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public Long countResults() throws ODataApplicationException {
cq.where(whereClause);
cq.multiselect(cb.count(target));
final var result = em.createQuery(cq).getSingleResult();
return (Long) result.get(0);
final var cnt = result.get(0);
return ((Number)cnt).longValue();
} catch (final JPANoSelectionException e) {
return 0L;
}
Expand Down
Loading