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);
}
}
}
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);
}
}
Aggregations