use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class ObjectPersistenceRuntimeXMLProject method buildDatabaseQueryDescriptor.
protected ClassDescriptor buildDatabaseQueryDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(DatabaseQuery.class);
descriptor.setDefaultRootElement("query");
descriptor.getInheritancePolicy().setClassIndicatorField(new XMLField("@xsi:type"));
descriptor.getInheritancePolicy().addClassIndicator(ReadAllQuery.class, getPrimaryNamespaceXPath() + "read-all-query");
descriptor.getInheritancePolicy().addClassIndicator(ReadObjectQuery.class, getPrimaryNamespaceXPath() + "read-object-query");
descriptor.getInheritancePolicy().addClassIndicator(DataReadQuery.class, getPrimaryNamespaceXPath() + "data-read-query");
descriptor.getInheritancePolicy().addClassIndicator(DataModifyQuery.class, getPrimaryNamespaceXPath() + "data-modify-query");
descriptor.getInheritancePolicy().addClassIndicator(DirectReadQuery.class, getPrimaryNamespaceXPath() + "direct-read-query");
descriptor.getInheritancePolicy().addClassIndicator(ValueReadQuery.class, getPrimaryNamespaceXPath() + "value-read-query");
descriptor.getInheritancePolicy().addClassIndicator(DeleteObjectQuery.class, getPrimaryNamespaceXPath() + "delete-object-query");
descriptor.getInheritancePolicy().addClassIndicator(DeleteAllQuery.class, getPrimaryNamespaceXPath() + "delete-all-query");
descriptor.getInheritancePolicy().addClassIndicator(InsertObjectQuery.class, getPrimaryNamespaceXPath() + "insert-object-query");
descriptor.getInheritancePolicy().addClassIndicator(UpdateObjectQuery.class, getPrimaryNamespaceXPath() + "update-object-query");
descriptor.getInheritancePolicy().addClassIndicator(DoesExistQuery.class, getPrimaryNamespaceXPath() + "does-exist-query");
descriptor.getInheritancePolicy().addClassIndicator(ReportQuery.class, getPrimaryNamespaceXPath() + "report-query");
XMLDirectMapping nameMapping = new XMLDirectMapping();
nameMapping.setAttributeName("name");
nameMapping.setGetMethodName("getName");
nameMapping.setSetMethodName("setName");
nameMapping.setXPath("@name");
descriptor.addMapping(nameMapping);
XMLCompositeObjectMapping expressionMapping = new XMLCompositeObjectMapping();
expressionMapping.setAttributeName("selectionCriteria");
expressionMapping.setGetMethodName("getSelectionCriteria");
expressionMapping.setSetMethodName("setSelectionCriteria");
expressionMapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object) {
return ((DatabaseQuery) object).getSelectionCriteria();
}
@Override
public void setAttributeValueInObject(Object object, Object value) {
if (!(object instanceof ObjectLevelReadQuery)) {
return;
}
ObjectLevelReadQuery query = (ObjectLevelReadQuery) object;
Expression expression = (Expression) value;
if (expression != null) {
expression = expression.rebuildOn(query.getExpressionBuilder());
}
query.setSelectionCriteria(expression);
}
});
expressionMapping.setReferenceClass(Expression.class);
expressionMapping.setXPath(getSecondaryNamespaceXPath() + "criteria");
descriptor.addMapping(expressionMapping);
XMLCompositeCollectionMapping argumentsMapping = new XMLCompositeCollectionMapping();
// Handle translation of argument lists to query-arguments.
argumentsMapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object) {
DatabaseQuery query = (DatabaseQuery) object;
List<String> arguments = query.getArguments();
List<String> types = query.getArgumentTypeNames();
List<Object> values = query.getArgumentValues();
Vector<QueryArgument> queryArguments = new Vector<>(arguments.size());
for (int index = 0; index < arguments.size(); index++) {
QueryArgument queryArgument = new QueryArgument();
queryArgument.setKey(arguments.get(index));
if (!types.isEmpty()) {
queryArgument.setTypeName(types.get(index));
}
if (!values.isEmpty()) {
queryArgument.setValue(values.get(index));
}
if (query.hasNullableArguments() && query.getNullableArguments().contains(new DatabaseField((String) queryArgument.getKey()))) {
queryArgument.setNullable(true);
}
queryArguments.add(queryArgument);
}
return queryArguments;
}
@Override
public void setAttributeValueInObject(Object object, Object value) {
DatabaseQuery query = (DatabaseQuery) object;
@SuppressWarnings({ "unchecked" }) List<QueryArgument> queryArguments = (List<QueryArgument>) value;
List<String> arguments = new ArrayList<>(queryArguments.size());
List<Class<?>> types = new ArrayList<>(queryArguments.size());
List<Object> values = new ArrayList<>(queryArguments.size());
for (int index = 0; index < queryArguments.size(); index++) {
QueryArgument queryArgument = queryArguments.get(index);
arguments.add((String) queryArgument.getKey());
if (queryArgument.getValue() != null) {
values.add(queryArgument.getValue());
}
if (queryArgument.getType() != null) {
types.add(queryArgument.getType());
}
if (queryArgument.isNullable()) {
query.getNullableArguments().add(new DatabaseField((String) queryArgument.getKey()));
}
}
query.setArguments(arguments);
if (!types.isEmpty()) {
query.setArgumentTypes(types);
}
if (!values.isEmpty()) {
query.setArgumentValues(values);
}
}
});
argumentsMapping.setAttributeName("argumentsMapping");
argumentsMapping.setXPath(getSecondaryNamespaceXPath() + "arguments/" + getSecondaryNamespaceXPath() + "argument");
argumentsMapping.setReferenceClass(QueryArgument.class);
descriptor.addMapping(argumentsMapping);
XMLDirectMapping shouldMaintainCacheMapping = new XMLDirectMapping();
shouldMaintainCacheMapping.setAttributeName("shouldMaintainCache");
shouldMaintainCacheMapping.setGetMethodName("shouldMaintainCache");
shouldMaintainCacheMapping.setSetMethodName("setShouldMaintainCache");
shouldMaintainCacheMapping.setXPath(getPrimaryNamespaceXPath() + "maintain-cache/text()");
shouldMaintainCacheMapping.setNullValue(Boolean.TRUE);
descriptor.addMapping(shouldMaintainCacheMapping);
XMLDirectMapping shouldBindAllParametersMapping = new XMLDirectMapping();
shouldBindAllParametersMapping.setAttributeName("shouldBindAllParameters");
shouldBindAllParametersMapping.setXPath(getPrimaryNamespaceXPath() + "bind-all-parameters/text()");
descriptor.addMapping(shouldBindAllParametersMapping);
XMLDirectMapping shouldCacheStatementMapping = new XMLDirectMapping();
shouldCacheStatementMapping.setAttributeName("shouldCacheStatement");
shouldCacheStatementMapping.setXPath(getPrimaryNamespaceXPath() + "cache-statement/text()");
descriptor.addMapping(shouldCacheStatementMapping);
XMLDirectMapping queryTimeoutMapping = new XMLDirectMapping();
queryTimeoutMapping.setAttributeName("queryTimeout");
queryTimeoutMapping.setGetMethodName("getQueryTimeout");
queryTimeoutMapping.setSetMethodName("setQueryTimeout");
queryTimeoutMapping.setXPath(getPrimaryNamespaceXPath() + "timeout/text()");
queryTimeoutMapping.setNullValue(DescriptorQueryManager.DefaultTimeout);
descriptor.addMapping(queryTimeoutMapping);
// feaure 2297
XMLDirectMapping shouldPrepareMapping = new XMLDirectMapping();
shouldPrepareMapping.setAttributeName("shouldPrepare");
shouldPrepareMapping.setGetMethodName("shouldPrepare");
shouldPrepareMapping.setSetMethodName("setShouldPrepare");
shouldPrepareMapping.setXPath(getPrimaryNamespaceXPath() + "prepare/text()");
shouldPrepareMapping.setNullValue(Boolean.TRUE);
descriptor.addMapping(shouldPrepareMapping);
XMLCompositeObjectMapping callMapping = new XMLCompositeObjectMapping();
callMapping.setAttributeName("call");
callMapping.setGetMethodName("getDatasourceCall");
callMapping.setSetMethodName("setDatasourceCall");
callMapping.setReferenceClass(Call.class);
callMapping.setXPath(getPrimaryNamespaceXPath() + "call");
descriptor.addMapping(callMapping);
XMLCompositeObjectMapping redirectorMapping = new XMLCompositeObjectMapping();
redirectorMapping.setAttributeName("redirector");
redirectorMapping.setGetMethodName("getRedirector");
redirectorMapping.setSetMethodName("setRedirector");
redirectorMapping.setReferenceClass(MethodBaseQueryRedirector.class);
redirectorMapping.setXPath(getPrimaryNamespaceXPath() + "query-redirector");
descriptor.addMapping(redirectorMapping);
return descriptor;
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class TestTimestampVersionLocking method testLocalTimestampPropertyCustomizer.
/**
* Check that setting the value SERVER on the OptimisticLockingPolicy in a
* DescriptorCustomizer will override the persistence property value.
*/
@Test
public void testLocalTimestampPropertyCustomizer() throws Exception {
// Have policy customized to use Server time
ClassDescriptorCustomizer.USETIME = TimestampLockingPolicy.SERVER_TIME;
EntityManager em = emfCustomized.createEntityManager();
try {
em.getTransaction().begin();
// Get the session for this transaction
Session session = ((EntityManagerImpl) em).getActiveSession();
// Get the TimestampQuery that would be used by the platform to get the current time
DatabaseQuery query = session.getDatasourcePlatform().getTimestampQuery();
// Add a Listener to the session
QueryListener listener = new QueryListener(query);
session.getEventManager().addListener(listener);
// Persist an Entity that will use Timestamp version locking and will trigger QueryListener
TimestampDog dog = new TimestampDog();
em.persist(dog);
em.getTransaction().commit();
// Make sure the query was executed
Assert.assertTrue("Query (" + listener.getQuery().getSQLString() + ") was not executed as expected.", listener.wasQueryExecuted());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class TestTimestampVersionLocking method testLocalTimestampPropertyFalseCustomizer.
/**
* Check that setting the value LOCAL on the OptimisticLockingPolicy in a
* DescriptorCustomizer will override the persistence property value.
*/
@Test
public void testLocalTimestampPropertyFalseCustomizer() throws Exception {
// Have policy customized to use local time
ClassDescriptorCustomizer.USETIME = TimestampLockingPolicy.LOCAL_TIME;
EntityManager em = emfFalseCustomized.createEntityManager();
try {
em.getTransaction().begin();
// Get the session for this transaction
Session session = ((EntityManagerImpl) em).getActiveSession();
// Get the TimestampQuery that would be used by the platform to get the current time
DatabaseQuery query = session.getDatasourcePlatform().getTimestampQuery();
// Add a Listener to the session
QueryListener listener = new QueryListener(query);
session.getEventManager().addListener(listener);
// Persist an Entity that will use Timestamp version locking and will trigger QueryListener
TimestampDog dog = new TimestampDog();
em.persist(dog);
em.getTransaction().commit();
// Make sure the query was not executed
Assert.assertTrue("Query (" + listener.getQuery().getSQLString() + ") was executed unexpectedly.", !listener.wasQueryExecuted());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class TestTimestampVersionLocking method testLocalTimestampPropertyFalse.
/**
* Check that setting the property "false" will get the server system time
* just like the default behavior.
*/
@Test
public void testLocalTimestampPropertyFalse() throws Exception {
EntityManager em = emfFalse.createEntityManager();
try {
em.getTransaction().begin();
// Get the session for this transaction
Session session = ((EntityManagerImpl) em).getActiveSession();
// Get the TimestampQuery that would be used by the platform to get the current time
DatabaseQuery query = session.getDatasourcePlatform().getTimestampQuery();
// Add a Listener to the session
QueryListener listener = new QueryListener(query);
session.getEventManager().addListener(listener);
// Persist an Entity that will use Timestamp version locking and will trigger QueryListener
TimestampDog dog = new TimestampDog();
em.persist(dog);
em.getTransaction().commit();
// Make sure the query was not executed
Assert.assertTrue("Query (" + listener.getQuery().getSQLString() + ") was executed unexpectedly.", listener.wasQueryExecuted());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class LOBValueWriter method buildAndExecuteCall.
protected void buildAndExecuteCall(DatabaseCall dbCall, AbstractSession session) {
DatabaseQuery query = dbCall.getQuery();
if (!query.isWriteObjectQuery()) {
// if not writequery, should not go through the locator writing..
return;
}
WriteObjectQuery writeQuery = (WriteObjectQuery) query;
writeQuery.setAccessor(accessor);
// build a select statement form the query
SQLSelectStatement selectStatement = buildSelectStatementForLocator(writeQuery, dbCall, session);
// then build a call from the statement
DatabaseCall call = buildCallFromSelectStatementForLocator(selectStatement, writeQuery, dbCall, session);
accessor.executeCall(call, call.getQuery().getTranslationRow(), session);
}
Aggregations