-
Notifications
You must be signed in to change notification settings - Fork 19
Database
Hakan Kargın edited this page May 26, 2022
·
2 revisions
It's a bit complicated but easy to use database management system.
Let's go through example (For mongoDB)
public class ExampleMongoProvider implements DatabaseProvider<Example> {
private final MongoClient client;
private final String databaseName;
private MongoCollection<BsonDocument> collection;
public ExampleMongoProvider(String ip, int port, String username, String password, String databaseName) {
MongoClientURI clientURI = !username.equals("") ? new MongoClientURI("mongodb://" + ip + ":" + port + "@" + username + ":" + password) : new MongoClientURI("mongodb://" + ip + ":" + port);
this.client = new MongoClient(clientURI);
this.databaseName = databaseName;
}
@Override
public void create() {
MongoDatabase mongoDatabase = this.client.getDatabase(this.databaseName);
if (!mongoDatabase.listCollectionNames().into(new ArrayList<>()).contains("example"))
mongoDatabase.createCollection("example");
this.collection = mongoDatabase.getCollection("example", BsonDocument.class);
}
@Nonnull
@Override
public List<Example> getValues() {
List<Example> examples = new ArrayList<>();
this.collection.find().into(new ArrayList<>())
.forEach(document -> examples.add(new example(document)));
return examples;
}
@Nonnull
@Override
public Example getValue(@Nonnull String key, @Nonnull Object value) {
String valueString = value.toString();
BsonDocument document = this.collection.find(new BsonDocument(key, new BsonString(valueString))).first();
if (document == null)
throw new IllegalArgumentException("no example with key " + key + " and value " + valueString + " found.");
return new Example(document);
}
@Override
public void insert(@Nonnull Example example) {
this.collection.insertOne(Example.toBsonDocument());
}
@Override
public void update(@Nonnull Example example) {
this.collection.updateOne(this.toSelectDocument(example), new BsonDocument("$set", example.toBsonDocument()));
}
@Override
public void delete(@Nonnull Example example) {
this.collection.deleteOne(this.toSelectDocument(example));
}
@Override
public void insert(@Nonnull Collection<Example> examples) {
if (examples.size() == 0)
return;
List<WriteModel<BsonDocument>> writes = new ArrayList<>();
examples.forEach(example -> writes.add(new InsertOneModel<>(example.toBsonDocument())));
this.collection.bulkWrite(writes);
}
@Override
public void update(@Nonnull Collection<Example> examples) {
if (examples.size() == 0)
return;
List<WriteModel<BsonDocument>> writes = new ArrayList<>();
examples.forEach(example -> writes.add(new UpdateOneModel<>(this.toSelectDocument(examples), new BsonDocument("$set", example.toBsonDocument()))));
this.collection.bulkWrite(writes);
}
@Override
public void delete(@Nonnull Collection<Example> examples) {
if (examples.size() == 0)
return;
List<WriteModel<BsonDocument>> writes = new ArrayList<>();
examples.forEach(example -> writes.add(new DeleteOneModel<>(this.toSelectDocument(example))));
this.collection.bulkWrite(writes);
}
/*
CONVERTERS
*/
private BsonDocument toSelectDocument(Example example) {
return new BsonDocument(ExampleMongoField.UID.getPath(), ExampleMongoField.UID.getValue(example));
}
}
And that's all, your database management class is ready !