Search in sources :

Example 1 with MongoSKDatabase

use of fr.romitou.mongosk.elements.MongoSKDatabase in project MongoSK by Romitou.

the class ExprAllMongoDatabases method get.

@Override
protected MongoSKDatabase[] get(@Nonnull final Event e) {
    MongoSKServer mongoSKServer = exprMongoSKServer.getSingle(e);
    if (mongoSKServer == null)
        return new MongoSKDatabase[0];
    SubscriberHelpers.ObservableSubscriber<Document> observableSubscriber = new SubscriberHelpers.OperationSubscriber<>();
    mongoSKServer.getMongoClient().listDatabases().subscribe(observableSubscriber);
    return observableSubscriber.get().stream().map(doc -> doc.getString("name")).map(name -> mongoSKServer.getMongoClient().getDatabase(name)).map(MongoSKDatabase::new).toArray(MongoSKDatabase[]::new);
}
Also used : SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) Document(org.bson.Document) Name(ch.njol.skript.doc.Name) MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) Event(org.bukkit.event.Event) SkriptParser(ch.njol.skript.lang.SkriptParser) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase) ExpressionType(ch.njol.skript.lang.ExpressionType) Since(ch.njol.skript.doc.Since) Expression(ch.njol.skript.lang.Expression) Examples(ch.njol.skript.doc.Examples) Skript(ch.njol.skript.Skript) Description(ch.njol.skript.doc.Description) Kleenean(ch.njol.util.Kleenean) SimpleExpression(ch.njol.skript.lang.util.SimpleExpression) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) Document(org.bson.Document)

Example 2 with MongoSKDatabase

use of fr.romitou.mongosk.elements.MongoSKDatabase in project MongoSK by Romitou.

the class ExprMongoCommandResult method get.

@Override
protected MongoSKDocument[] get(@Nonnull Event e) {
    String jsonString = exprJsonString.getSingle(e);
    MongoSKDatabase mongoSKDatabase = exprMongoSKDatabase.getSingle(e);
    if (mongoSKDatabase == null)
        return new MongoSKDocument[0];
    SubscriberHelpers.ObservableSubscriber<Document> documentObservableSubscriber = new SubscriberHelpers.OperationSubscriber<>();
    try {
        mongoSKDatabase.getMongoDatabase().runCommand(Document.parse(jsonString).toBsonDocument()).subscribe(documentObservableSubscriber);
    } catch (BSONException | JsonParseException ex) {
        LoggerHelper.severe("An error occurred when changing the document's JSON: " + ex.getMessage(), "Provided JSON: " + jsonString, "Please check its validity on https://jsonlint.com.");
    }
    // Nobody has subscribed. :(
    if (documentObservableSubscriber.getSubscription() == null)
        return new MongoSKDocument[0];
    return documentObservableSubscriber.get().stream().map(MongoSKDocument::new).toArray(MongoSKDocument[]::new);
}
Also used : SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) BSONException(org.bson.BSONException) Document(org.bson.Document) MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument) JsonParseException(org.bson.json.JsonParseException) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase)

Example 3 with MongoSKDatabase

use of fr.romitou.mongosk.elements.MongoSKDatabase in project MongoSK by Romitou.

the class ExprMongoCollection method get.

@Override
protected MongoSKCollection[] get(@Nonnull final Event e) {
    String collectionName = exprCollectionName.getSingle(e);
    MongoSKDatabase mongoSKDatabase = exprMongoSKDatabase.getSingle(e);
    if (collectionName == null || mongoSKDatabase == null)
        return new MongoSKCollection[0];
    MongoCollection<Document> mongoCollection = mongoSKDatabase.getMongoDatabase().getCollection(collectionName);
    if (mongoCollection == null)
        return new MongoSKCollection[0];
    return new MongoSKCollection[] { new MongoSKCollection(mongoCollection) };
}
Also used : MongoSKCollection(fr.romitou.mongosk.elements.MongoSKCollection) Document(org.bson.Document) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase)

Example 4 with MongoSKDatabase

use of fr.romitou.mongosk.elements.MongoSKDatabase in project MongoSK by Romitou.

the class ExprMongoDatabase method get.

@Override
protected MongoSKDatabase[] get(@Nonnull final Event e) {
    String databaseName = exprDatabaseName.getSingle(e);
    MongoSKServer mongoSKServer = exprMongoSKServer.getSingle(e);
    if (databaseName == null || mongoSKServer == null)
        return new MongoSKDatabase[0];
    MongoDatabase mongoDatabase = mongoSKServer.getMongoClient().getDatabase(databaseName);
    if (mongoDatabase == null)
        return new MongoSKDatabase[0];
    return new MongoSKDatabase[] { new MongoSKDatabase(mongoDatabase) };
}
Also used : MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase)

Example 5 with MongoSKDatabase

use of fr.romitou.mongosk.elements.MongoSKDatabase in project MongoSK by Romitou.

the class EffCreateMongoCollection method execute.

@Override
protected void execute(@Nonnull Event e) {
    String databaseName = exprCollectionName.getSingle(e);
    MongoSKDatabase mongoSKDatabase = exprMongoSKDatabase.getSingle(e);
    if (databaseName == null || mongoSKDatabase == null)
        return;
    SubscriberHelpers.ObservableSubscriber<Void> voidSubscriber = new SubscriberHelpers.OperationSubscriber<>();
    mongoSKDatabase.getMongoDatabase().createCollection(databaseName).subscribe(voidSubscriber);
    voidSubscriber.await();
}
Also used : SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase)

Aggregations

MongoSKDatabase (fr.romitou.mongosk.elements.MongoSKDatabase)7 SubscriberHelpers (fr.romitou.mongosk.SubscriberHelpers)5 Document (org.bson.Document)4 Skript (ch.njol.skript.Skript)2 Description (ch.njol.skript.doc.Description)2 Examples (ch.njol.skript.doc.Examples)2 Name (ch.njol.skript.doc.Name)2 Since (ch.njol.skript.doc.Since)2 Expression (ch.njol.skript.lang.Expression)2 ExpressionType (ch.njol.skript.lang.ExpressionType)2 SkriptParser (ch.njol.skript.lang.SkriptParser)2 SimpleExpression (ch.njol.skript.lang.util.SimpleExpression)2 Kleenean (ch.njol.util.Kleenean)2 MongoSKCollection (fr.romitou.mongosk.elements.MongoSKCollection)2 MongoSKServer (fr.romitou.mongosk.elements.MongoSKServer)2 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 Event (org.bukkit.event.Event)2 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)1 MongoSKDocument (fr.romitou.mongosk.elements.MongoSKDocument)1