-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathInserter.java
111 lines (98 loc) · 3.08 KB
/
Inserter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.db.mongo;
import com.mongodb.BasicDBList;
import org.bson.Document;
import sirius.db.mixing.Mapping;
import sirius.kernel.commons.Watch;
import sirius.kernel.health.Microtiming;
/**
* Fluent builder to build an insert statement.
*/
public class Inserter {
private final String database;
private final Mongo mongo;
private Document obj = new Document();
protected Inserter(Mongo mongo, String database) {
this.mongo = mongo;
this.database = database;
}
/**
* Sets a field to the given value.
*
* @param key the name of the field to set
* @param value the value to set the field to
* @return the builder itself for fluent method calls
*/
public Inserter set(Mapping key, Object value) {
return set(key.toString(), value);
}
/**
* Sets a field to the given value.
*
* @param key the name of the field to set
* @param value the value to set the field to
* @return the builder itself for fluent method calls
*/
public Inserter set(String key, Object value) {
obj.put(key, QueryBuilder.FILTERS.transform(value));
return this;
}
/**
* Sets a field to the given list of values.
*
* @param key the name of the field to set
* @param values the values to set the field to
* @return the builder itself for fluent method calls
*/
public Inserter setList(Mapping key, Object... values) {
return setList(key.toString(), values);
}
/**
* Sets a field to the given list of values.
*
* @param key the name of the field to set
* @param values the values to set the field to
* @return the builder itself for fluent method calls
*/
public Inserter setList(String key, Object... values) {
BasicDBList list = new BasicDBList();
for (Object value : values) {
list.add(QueryBuilder.FILTERS.transform(value));
}
obj.put(key, list);
return this;
}
/**
* Executes the insert statement into the given collection.
*
* @param type the type to insert into
* @return the inserted document
*/
public Doc into(Class<?> type) {
return into(QueryBuilder.getRelationName(type));
}
/**
* Executes the insert statement into the given collection.
*
* @param collection the collection to insert the document into
* @return the inserted document
*/
public Doc into(String collection) {
if (Mongo.LOG.isFINE()) {
Mongo.LOG.FINE("INSERT: %s\nObject: %s", collection, obj);
}
Watch w = Watch.start();
mongo.db(database).getCollection(collection).insertOne(obj);
mongo.callDuration.addValue(w.elapsedMillis());
if (Microtiming.isEnabled()) {
w.submitMicroTiming("mongo", "INSERT - " + collection + ": " + obj.keySet());
}
return new Doc(obj);
}
}