| 32 | import static org.jongo.query.BsonSpecialChar.specialChar; |
| 33 | |
| 34 | public class BsonQueryFactory implements QueryFactory { |
| 35 | |
| 36 | private static final String DEFAULT_TOKEN = "#"; |
| 37 | |
| 38 | /** |
| 39 | * The marshall operator will be replacing the token during query parsing as following: |
| 40 | * {"firstname":#} -> {"firstname":{MARSHALL_OPERATOR: 0}} |
| 41 | * 0 being the index of the parameter to be inserted in place of that placeholder. |
| 42 | * Previously $marshall but upgrading to mongo driver 4 the new parser does not allow $ prefixed strings |
| 43 | * if they're not mongo operators. |
| 44 | * With a UUID prefixed string there should be no risk of collision. |
| 45 | */ |
| 46 | private static final String MARSHALL_OPERATOR = "8a6e4178-8fba-4d22-af43-840512e3a999-marshall"; |
| 47 | |
| 48 | private final String token; |
| 49 | private final boolean singleCharToken; |
| 50 | private final Marshaller marshaller; |
| 51 | |
| 52 | private static class BsonQuery implements Query { |
| 53 | private final DBObject dbo; |
| 54 | |
| 55 | public BsonQuery(DBObject dbo) { |
| 56 | this.dbo = dbo; |
| 57 | } |
| 58 | |
| 59 | public DBObject toDBObject() { |
| 60 | return dbo; |
| 61 | } |
| 62 | |
| 63 | public org.bson.BsonDocument toBsonDocument() { |
| 64 | return BsonDocumentWrapper.asBsonDocument(dbo, MongoClient.getDefaultCodecRegistry()); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public BsonQueryFactory(Marshaller marshaller) { |
| 69 | this(marshaller, DEFAULT_TOKEN); |
| 70 | } |
| 71 | |
| 72 | public BsonQueryFactory(Marshaller marshaller, String token) { |
| 73 | this.singleCharToken = token.length() == 1; |
| 74 | this.token = token; |
| 75 | this.marshaller = marshaller; |
| 76 | } |
| 77 | |
| 78 | public Query createQuery(final String query, Object... parameters) { |
| 79 | |
| 80 | if (query == null) { |
| 81 | return new BsonQuery(null); |
| 82 | } |
| 83 | if (parameters == null) { |
| 84 | parameters = new Object[]{null}; |
| 85 | } |
| 86 | |
| 87 | String quotedQuery = addRequiredQuotesAndParameters(query, parameters); |
| 88 | |
| 89 | final Object[] params = parameters; |
| 90 | |
| 91 | DBObject dbo; |
nothing calls this directly
no outgoing calls
no test coverage detected