use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class MongoDatabaseImpl method delete.
@Override
public int delete(String eType, Object id) throws DatabaseException {
checkNotNull(eType);
if (id == null) {
throw new DatabaseException("can't delete object (missing object id)");
}
MongoCollection<Document> collection = db.getCollection(eType);
if (collection == null) {
return 0;
}
DeleteResult result = collection.deleteOne(eq(DatabaseObjectImpl.ObjectIdKey, new ObjectId(String.valueOf(id))));
return (int) result.getDeletedCount();
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class OrientDatabase method addRemove.
private void addRemove(String queryTpl, DatabaseObject parent, String collection, DatabaseObject child) throws DatabaseException {
if (parent == null || child == null) {
return;
}
ODocument parentDoc = ((DatabaseObjectImpl) parent).document;
ODocument childDoc = ((DatabaseObjectImpl) child).document;
if (parentDoc.getIdentity() == null || !parentDoc.getIdentity().isPersistent()) {
throw new DatabaseException("Parent Object " + parent.entity() + " is not a persistent object");
}
if (childDoc.getIdentity() == null || !childDoc.getIdentity().isPersistent()) {
throw new DatabaseException("Child Object " + child.entity() + " is not a persistent object");
}
String query = format(queryTpl, parentDoc.getIdentity().toString(), collection, childDoc.getIdentity().toString());
db.command(new OCommandSQL(query)).execute();
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class OrientDatabase method createEntity.
@Override
public void createEntity(String eType, Field... fields) throws DatabaseException {
eType = checkNotNull(eType);
if (fields == null || fields.length == 0) {
throw new DatabaseException("entity " + eType + ". fields missing");
}
OClass oClass = db.getMetadata().getSchema().getClass(eType);
if (oClass != null) {
throw new DatabaseException("entity " + eType + " already exists");
}
oClass = db.getMetadata().getSchema().createClass(eType);
String[] props = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
props[i] = f.name();
if (f.type() != null) {
OProperty property = oClass.createProperty(f.name(), FieldTypes.get(f.type()));
property.setNotNull(f.required());
if (f.unique()) {
property.createIndex(INDEX_TYPE.UNIQUE);
}
}
}
db.getMetadata().getSchema().save();
}
use of com.bluenimble.platform.db.DatabaseException in project serverless by bluenimble.
the class Find method main.
public static void main(String[] args) throws DatabaseException {
Database db = new DatabaseServer().get();
final JsonArray records = new JsonArray();
JsonObject result = (JsonObject) new JsonObject().set("records", records);
db.find("Cities", new JsonQuery(new JsonObject()), new Database.Visitor() {
@Override
public boolean onRecord(DatabaseObject dbo) {
try {
dbo.set("org", "Labs");
dbo.save();
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
records.add(dbo.toJson(null));
return false;
}
@Override
public boolean optimize() {
return true;
}
});
System.out.println(result);
}
Aggregations