Search in sources :

Example 1 with NonSynchronizedVector

use of org.eclipse.persistence.internal.helper.NonSynchronizedVector in project eclipselink by eclipse-ee4j.

the class DeleteOperation method invoke.

/**
 * Execute <code>DELETE</code> operation on the database
 * @param   xrService parent <code>XRService</code> that owns this <code>Operation</code>
 * @param   invocation contains runtime argument values to be bound to the list of
 *          {@link Parameter}'s.
 * @return  result - can be <code>null</code> if the underlying <code>DELETE</code> operation on the
 *          database does not return a value
 *
 * @see  Operation
 */
@SuppressWarnings("rawtypes")
@Override
public Object invoke(XRServiceAdapter xrService, Invocation invocation) {
    DatabaseQuery query = classDescriptor.getQueryManager().getQuery(getFindByPKQuery());
    // a named query created via ORM metadata processing needs initialization
    if (query instanceof JPAQuery) {
        query = ((JPAQuery) query).processSQLQuery(xrService.getORSession().getActiveSession());
    }
    UnitOfWork uow = xrService.getORSession().acquireUnitOfWork();
    Object toBeDeleted;
    // a query created via ORM metadata processing does not have parameters set, however, the operation should
    if (query.getArguments().size() == 0) {
        int idx = 0;
        for (Parameter param : getParameters()) {
            // for custom SQL query (as configured via ORM metadata processing) we add args by position
            query.addArgument(Integer.toString(++idx), Util.SCHEMA_2_CLASS.get(param.getType()));
            query.addArgumentValue(invocation.getParameter(param.getName()));
        }
        toBeDeleted = uow.executeQuery(query);
    } else {
        // set query args or execute args for the non-JPAQuery case,
        // i.e. stored proc/funcs get populated from ORM metadata
        // whereas named queries (SQL strings) do not...
        List<String> queryArguments = query.getArguments();
        int queryArgumentsSize = queryArguments.size();
        Vector<Object> executeArguments = new NonSynchronizedVector<>();
        for (int i = 0; i < queryArgumentsSize; i++) {
            String argName = queryArguments.get(i);
            executeArguments.add(invocation.getParameter(argName));
        }
        toBeDeleted = uow.executeQuery(query, executeArguments);
    }
    // JPAQuery will return a single result in a Vector
    if (!isCollection() && toBeDeleted instanceof Vector) {
        if (((Vector) toBeDeleted).isEmpty()) {
            toBeDeleted = null;
        } else {
            toBeDeleted = ((Vector) toBeDeleted).firstElement();
        }
    }
    if (toBeDeleted != null) {
        uow.deleteObject(toBeDeleted);
        uow.commit();
    }
    return null;
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) JPAQuery(org.eclipse.persistence.internal.jpa.JPAQuery) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) Vector(java.util.Vector)

Example 2 with NonSynchronizedVector

use of org.eclipse.persistence.internal.helper.NonSynchronizedVector in project eclipselink by eclipse-ee4j.

the class DescriptorQueryManager method putCachedUpdateCalls.

/**
 * INTERNAL:
 * Cache a clone of the update SQL calls based on the updated fields.
 * If the max size is reached, do not cache the call.
 * The call's query must be dereferenced in order to allow the GC of a related session.
 * PERF: Allow caching of the update SQL call to avoid regeneration.
 */
public void putCachedUpdateCalls(Vector updateFields, Vector updateCalls) {
    Vector vectorToCache = updateCalls;
    if (!updateCalls.isEmpty()) {
        int updateCallsSize = updateCalls.size();
        vectorToCache = new NonSynchronizedVector(updateCallsSize);
        for (int i = 0; i < updateCallsSize; i++) {
            DatasourceCall updateCall = (DatasourceCall) updateCalls.get(i);
            // clone call and dereference query for DatasourceCall and EJBQLCall
            DatasourceCall clonedUpdateCall = (DatasourceCall) updateCall.clone();
            clonedUpdateCall.setQuery(null);
            vectorToCache.add(clonedUpdateCall);
        }
    }
    getCachedUpdateCalls().put(updateFields, vectorToCache);
}
Also used : Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) DatasourceCall(org.eclipse.persistence.internal.databaseaccess.DatasourceCall)

Example 3 with NonSynchronizedVector

use of org.eclipse.persistence.internal.helper.NonSynchronizedVector in project eclipselink by eclipse-ee4j.

the class ExpressionQueryMechanism method buildInsertStatement.

/**
 * Return the appropriate insert statement
 */
protected SQLInsertStatement buildInsertStatement(DatabaseTable table) {
    SQLInsertStatement insertStatement = new SQLInsertStatement();
    insertStatement.setTable(table);
    insertStatement.setModifyRow(getModifyRow());
    if (getDescriptor().hasReturningPolicies() && getDescriptor().getReturnFieldsToGenerateInsert() != null) {
        // In case of RelationalDescriptor only return fields for current table must be used.
        Vector<DatabaseField> returnFieldsForTable = new NonSynchronizedVector<>();
        for (DatabaseField item : getDescriptor().getReturnFieldsToGenerateInsert()) {
            if (table.equals(item.getTable())) {
                returnFieldsForTable.add(item);
            }
        }
        if (!returnFieldsForTable.isEmpty()) {
            insertStatement.setReturnFields(getDescriptor().getReturnFieldsToGenerateInsert());
        }
    }
    insertStatement.setHintString(getQuery().getHintString());
    return insertStatement;
}
Also used : DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) SQLInsertStatement(org.eclipse.persistence.internal.expressions.SQLInsertStatement) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector)

Example 4 with NonSynchronizedVector

use of org.eclipse.persistence.internal.helper.NonSynchronizedVector in project eclipselink by eclipse-ee4j.

the class NojiTestSet method runQuery.

@SuppressWarnings("unchecked")
@Test
public void runQuery() {
    DatabaseSession s = project.createDatabaseSession();
    s.dontLogMessages();
    s.login();
    Vector queryArgs = new NonSynchronizedVector();
    queryArgs.add("testsdfsdfasdfsdfsdfsdfsdfsdfdfsdfsdffds");
    boolean worked = false;
    String msg = null;
    Object o = null;
    try {
        o = s.executeQuery("Noji", Empty.class, queryArgs);
        worked = true;
    } catch (Exception e) {
        msg = e.getMessage();
    }
    assertTrue("invocation noji failed: " + msg, worked);
    assertNotNull("result is supposed to be not-null", o);
    Vector results = (Vector) o;
    DatabaseRecord record = (DatabaseRecord) results.get(0);
    BigDecimal x = (BigDecimal) record.get("X");
    assertTrue("wrong x value", x.intValue() == 33);
    s.logout();
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 5 with NonSynchronizedVector

use of org.eclipse.persistence.internal.helper.NonSynchronizedVector in project eclipselink by eclipse-ee4j.

the class Ni10TestSet method runQuery.

@SuppressWarnings("unchecked")
@Test
public void runQuery() {
    DatabaseSession s = project.createDatabaseSession();
    s.dontLogMessages();
    s.login();
    Vector queryArgs = new NonSynchronizedVector();
    queryArgs.add(-1);
    boolean worked = false;
    String msg = null;
    try {
        s.executeQuery("Ni10", Empty.class, queryArgs);
        worked = true;
    } catch (Exception e) {
        msg = e.getMessage();
    }
    assertTrue("invocation signtype_in_test failed: " + msg, worked);
    // test data range: 2 should NOT work
    queryArgs = new NonSynchronizedVector();
    queryArgs.add(2);
    worked = false;
    msg = null;
    try {
        s.executeQuery("Ni10", Empty.class, queryArgs);
        worked = true;
    } catch (Exception e) {
        msg = e.getMessage();
    }
    assertFalse("invocation signtype_in_test with 2 worked: " + msg, worked);
    // test data range: -2 should NOT work
    queryArgs = new NonSynchronizedVector();
    queryArgs.add(-2);
    worked = false;
    msg = null;
    try {
        s.executeQuery("Ni10", Empty.class, queryArgs);
        worked = true;
    } catch (Exception e) {
        msg = e.getMessage();
    }
    assertFalse("invocation signtype_in_test with -2 worked: " + msg, worked);
    s.logout();
}
Also used : DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) Test(org.junit.Test)

Aggregations

NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)40 Test (org.junit.Test)36 Vector (java.util.Vector)34 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)34 BigDecimal (java.math.BigDecimal)13 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)13 Date (java.sql.Date)4 StringReader (java.io.StringReader)3 SQLException (java.sql.SQLException)2 Struct (java.sql.Struct)2 Invocation (org.eclipse.persistence.internal.xr.Invocation)2 Operation (org.eclipse.persistence.internal.xr.Operation)2 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)2 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)2 Document (org.w3c.dom.Document)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 DatasourceCall (org.eclipse.persistence.internal.databaseaccess.DatasourceCall)1 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)1 SQLInsertStatement (org.eclipse.persistence.internal.expressions.SQLInsertStatement)1 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)1