Search in sources :

Example 16 with DatabaseException

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();
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) DatabaseException(com.bluenimble.platform.db.DatabaseException) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 17 with DatabaseException

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();
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) DatabaseException(com.bluenimble.platform.db.DatabaseException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 18 with DatabaseException

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();
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) DatabaseException(com.bluenimble.platform.db.DatabaseException)

Example 19 with DatabaseException

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);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) Database(com.bluenimble.platform.db.Database) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException)

Aggregations

DatabaseException (com.bluenimble.platform.db.DatabaseException)19 JsonObject (com.bluenimble.platform.json.JsonObject)11 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)8 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)8 ApiSpace (com.bluenimble.platform.api.ApiSpace)8 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)8 Database (com.bluenimble.platform.db.Database)8 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)8 JsonArray (com.bluenimble.platform.json.JsonArray)3 IOException (java.io.IOException)3 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)2 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 Document (org.bson.Document)2 Field (com.bluenimble.platform.db.Database.Field)1 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)1 DeleteResult (com.mongodb.client.result.DeleteResult)1 ODatabaseExport (com.orientechnologies.orient.core.db.tool.ODatabaseExport)1 ODatabaseImport (com.orientechnologies.orient.core.db.tool.ODatabaseImport)1