Search in sources :

Example 1 with Delete

use of com.gitub.raymanrt.orientqb.delete.Delete in project gora by apache.

the class OrientDBStore method deleteByQuery.

/**
 * {@inheritDoc}
 */
@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
    Delete delete = new Delete();
    delete.from(orientDBMapping.getDocumentClass());
    Map<String, Object> params = new HashMap<String, Object>();
    if (query.getFields() == null || (query.getFields().length == getFields().length)) {
        if (query.getStartKey() != null) {
            delete.where(projection("_id").ge(Parameter.parameter("start")));
            params.put("start", query.getStartKey());
        }
        if (query.getEndKey() != null) {
            delete.where(projection("_id").le(Parameter.parameter("end")));
            params.put("end", query.getEndKey());
        }
        OCommandSQL dbQuery = new OCommandSQL(delete.toString().replace("DELETE", "DELETE FROM"));
        try (ODatabaseDocumentTx deleteTx = connectionPool.acquire()) {
            deleteTx.activateOnCurrentThread();
            int deleteCount;
            if (params.isEmpty()) {
                deleteCount = deleteTx.command(dbQuery).execute();
            } else {
                deleteCount = deleteTx.command(dbQuery).execute(params);
            }
            if (deleteCount > 0) {
                return deleteCount;
            } else {
                return 0;
            }
        } catch (Exception e) {
            throw new GoraException(e);
        }
    } else {
        OrientDBQuery<K, T> dataStoreQuery = new OrientDBQuery<>(this);
        dataStoreQuery.setStartKey(query.getStartKey());
        dataStoreQuery.setEndKey(query.getEndKey());
        dataStoreQuery.populateOrientDBQuery(orientDBMapping, getFieldsToQuery(null), getFields());
        try (ODatabaseDocumentTx selectTx = connectionPool.acquire()) {
            selectTx.activateOnCurrentThread();
            List<ODocument> result = selectTx.command(dataStoreQuery.getOrientDBQuery()).execute(dataStoreQuery.getParams());
            if (result != null && result.isEmpty()) {
                return 0;
            } else {
                for (ODocument doc : result) {
                    for (String docField : query.getFields()) {
                        if (doc.containsField(orientDBMapping.getDocumentField(docField))) {
                            doc.removeField(orientDBMapping.getDocumentField(docField));
                        }
                    }
                    doc.save();
                }
                return result.size();
            }
        } catch (Exception e) {
            throw new GoraException(e);
        }
    }
}
Also used : Delete(com.gitub.raymanrt.orientqb.delete.Delete) HashMap(java.util.HashMap) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OrientDBQuery(org.apache.gora.orientdb.query.OrientDBQuery) GoraException(org.apache.gora.util.GoraException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with Delete

use of com.gitub.raymanrt.orientqb.delete.Delete in project gora by apache.

the class OrientDBStore method delete.

/**
 * {@inheritDoc}
 */
@Override
public boolean delete(K key) throws GoraException {
    Delete delete = new Delete();
    delete.from(orientDBMapping.getDocumentClass()).where(projection("_id").eq(Parameter.parameter("key")));
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("key", key);
    OCommandSQL query = new OCommandSQL(delete.toString().replace("DELETE", "DELETE FROM"));
    try (ODatabaseDocumentTx deleteTx = connectionPool.acquire()) {
        deleteTx.activateOnCurrentThread();
        int deleteCount = deleteTx.command(query).execute(params);
        if (deleteCount == 1) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : Delete(com.gitub.raymanrt.orientqb.delete.Delete) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) GoraException(org.apache.gora.util.GoraException) HashMap(java.util.HashMap) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Aggregations

Delete (com.gitub.raymanrt.orientqb.delete.Delete)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 GoraException (org.apache.gora.util.GoraException)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OrientDBQuery (org.apache.gora.orientdb.query.OrientDBQuery)1