-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathMongoDBRemoveQuery.java
More file actions
59 lines (49 loc) · 1.8 KB
/
MongoDBRemoveQuery.java
File metadata and controls
59 lines (49 loc) · 1.8 KB
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
package sqlancer.mongodb.query;
import org.bson.Document;
import org.bson.types.ObjectId;
import com.mongodb.client.result.DeleteResult;
import sqlancer.GlobalState;
import sqlancer.Main;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.mongodb.MongoDBConnection;
import sqlancer.mongodb.MongoDBQueryAdapter;
import sqlancer.mongodb.MongoDBSchema;
public class MongoDBRemoveQuery extends MongoDBQueryAdapter {
private final String objectId;
private final MongoDBSchema.MongoDBTable table;
public MongoDBRemoveQuery(MongoDBSchema.MongoDBTable table, String objectId) {
this.objectId = objectId;
this.table = table;
}
@Override
public boolean couldAffectSchema() {
return true;
}
@Override
public <G extends GlobalState<?, ?, MongoDBConnection>> boolean execute(G globalState, String... fills)
throws Exception {
try {
DeleteResult result = globalState.getConnection().getDatabase().getCollection(table.getName())
.deleteOne(new Document("_id", new ObjectId(objectId)));
if (result.wasAcknowledged()) {
Main.nrSuccessfulActions.addAndGet(1);
} else {
Main.nrUnsuccessfulActions.addAndGet(1);
}
return result.wasAcknowledged();
} catch (Exception e) {
Main.nrUnsuccessfulActions.addAndGet(1);
return false;
}
}
@Override
public ExpectedErrors getExpectedErrors() {
return new ExpectedErrors();
}
@Override
public String getLogString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("db.").append(table.getName()).append(".remove({'_id': '").append(objectId).append("'})");
return stringBuilder.toString();
}
}