Search in sources :

Example 1 with SObject

use of com.sforce.async.SObject in project teiid by teiid.

the class InsertExecutionImpl method execute.

@Override
public void execute() throws TranslatorException {
    try {
        Insert insert = (Insert) command;
        if (insert.getParameterValues() == null) {
            DataPayload data = new DataPayload();
            data.setType(this.objectName);
            buildSingleRowInsertPayload(insert, data);
            if (insert.isUpsert()) {
                result = getConnection().upsert(data);
            } else {
                result = getConnection().create(data);
            }
        } else {
            if (this.activeJob == null) {
                this.activeJob = getConnection().createBulkJob(this.objectName, insert.isUpsert() ? OperationEnum.upsert : OperationEnum.insert, false);
                counts = new ArrayList<Integer>();
            }
            if (this.activeJob.getState() == JobStateEnum.Open) {
                while (this.rowIter.hasNext()) {
                    List<SObject> rows = buildBulkRowPayload(insert, this.rowIter, this.executionFactory.getMaxBulkInsertBatchSize());
                    batches.add(getConnection().addBatch(rows, activeJob));
                }
                this.activeJob = getConnection().closeJob(this.activeJob.getId());
            }
            BatchResult[] batchResult = getConnection().getBulkResults(this.activeJob, batches);
            for (BatchResult br : batchResult) {
                for (Result r : br.getResult()) {
                    if (r.isSuccess() && r.isCreated()) {
                        counts.add(1);
                    } else if (r.getErrors().length > 0) {
                        counts.add(Statement.EXECUTE_FAILED);
                        this.context.addWarning(new SQLWarning(r.getErrors()[0].getMessage(), r.getErrors()[0].getStatusCode().name()));
                    } else {
                        counts.add(Statement.SUCCESS_NO_INFO);
                    }
                }
            }
            // now process the next set of batch rows
            this.activeJob = null;
        }
    } catch (ResourceException e) {
        throw new TranslatorException(e);
    }
}
Also used : SQLWarning(java.sql.SQLWarning) SObject(com.sforce.async.SObject) ResourceException(javax.resource.ResourceException) TranslatorException(org.teiid.translator.TranslatorException) Insert(org.teiid.language.Insert) BatchResult(com.sforce.async.BatchResult) Result(com.sforce.async.Result) BatchResult(com.sforce.async.BatchResult)

Aggregations

BatchResult (com.sforce.async.BatchResult)1 Result (com.sforce.async.Result)1 SObject (com.sforce.async.SObject)1 SQLWarning (java.sql.SQLWarning)1 ResourceException (javax.resource.ResourceException)1 Insert (org.teiid.language.Insert)1 TranslatorException (org.teiid.translator.TranslatorException)1