55
66
77/**
8- * Utility for constructing Query operation command with query, orderby, hint, explain, snapshot
8+ * Utility for constructing Query operation command with query, orderby, hint, explain, snapshot.
99 */
10- public class QueryOpBuilder {
11- private DBObject queryop ;
10+ class QueryOpBuilder {
11+
12+ private DBObject query ;
13+ private DBObject orderBy ;
14+ private DBObject hintObj ;
15+ private String hintStr ;
16+ private boolean explain ;
17+ private boolean snapshot ;
1218
13- /**
14- * Creates a builder with an empty query op
15- */
19+ private DBObject specialFields ;
20+
1621 public QueryOpBuilder (){
17- queryop = new BasicDBObject ();
1822 }
1923
20- /**
21- * Creates a new builder
22- * @param queryop
23- */
24- public QueryOpBuilder (DBObject queryop ){
25- this .queryop = queryop ;
24+ public QueryOpBuilder (DBObject query , DBObject orderBy , DBObject hintObj , String hintStr , boolean explain , boolean snapshot ,
25+ DBObject specialFields ) {
26+ this .query = query ;
27+ this .orderBy = orderBy ;
28+ this .hintObj = hintObj ;
29+ this .hintStr = hintStr ;
30+ this .explain = explain ;
31+ this .snapshot = snapshot ;
32+ this .specialFields = specialFields ;
2633 }
27-
34+
35+
2836 /**
2937 * Adds the query clause to the operation
3038 * @param query
3139 * @return
3240 */
3341 public QueryOpBuilder addQuery (DBObject query ){
34- return addToQueryObject ("query" , query , true );
42+ this .query = query ;
43+ return this ;
3544 }
3645
3746 /**
38- * Adds the groupby clause to the operation
47+ * Adds the orderby clause to the operation
3948 * @param orderBy
4049 * @return
4150 */
4251 public QueryOpBuilder addOrderBy (DBObject orderBy ){
43- return addToQueryObject ("orderby" , orderBy , false );
52+ this .orderBy = orderBy ;
53+ return this ;
4454 }
4555
4656 /**
@@ -49,75 +59,124 @@ public QueryOpBuilder addOrderBy(DBObject orderBy){
4959 * @return
5060 */
5161 public QueryOpBuilder addHint (String hint ){
52- if (hint != null )
53- addToQueryObject ("$hint" , hint );
54-
62+ this .hintStr = hint ;
5563 return this ;
5664 }
5765
66+ /**
67+ * Adds hint clause to the operation
68+ * @param hint
69+ * @return
70+ */
5871 public QueryOpBuilder addHint (DBObject hint ){
59- if (hint != null )
60- addToQueryObject ("$hint" , hint , false );
61-
72+ this .hintObj = hint ;
73+ return this ;
74+ }
75+
76+
77+ /**
78+ * Adds special fields to the operation
79+ * @param specialFields
80+ * @return
81+ */
82+ public QueryOpBuilder addSpecialFields (DBObject specialFields ){
83+ this .specialFields = specialFields ;
6284 return this ;
6385 }
6486
6587 /**
6688 * Adds the explain flag to the operation
89+ * @param explain
6790 * @return
6891 */
69- public QueryOpBuilder addExplain (){
70- return addToQueryObject ("$explain" , true );
92+ public QueryOpBuilder addExplain (boolean explain ){
93+ this .explain = explain ;
94+ return this ;
7195 }
7296
7397 /**
7498 * Adds the snapshot flag to the operation
99+ * @param snapshot
75100 * @return
76101 */
77- public QueryOpBuilder addSnapshot (){
78- return addToQueryObject ("$snapshot" , true );
102+ public QueryOpBuilder addSnapshot (boolean snapshot ){
103+ this .snapshot = snapshot ;
104+ return this ;
105+ }
106+
107+
108+ /**
109+ * Constructs the query operation DBObject
110+ * @return DBObject representing the query command to be sent to server
111+ */
112+ public DBObject get (){
113+ if (hasSpecialQueryFields ()){
114+ DBObject queryop = (specialFields == null ? new BasicDBObject () : specialFields );
115+
116+ addToQueryObject (queryop , "query" , query , true );
117+ addToQueryObject (queryop , "orderby" , orderBy , false );
118+ if (hintStr != null )
119+ addToQueryObject (queryop , "$hint" , hintStr );
120+ if (hintObj != null )
121+ addToQueryObject (queryop , "$hint" , hintObj );
122+
123+ if (explain )
124+ queryop .put ("$explain" , true );
125+ if (snapshot )
126+ queryop .put ("$snapshot" , true );
127+
128+ return queryop ;
129+ }
79130
131+ return query ;
80132 }
81133
134+ private boolean hasSpecialQueryFields (){
135+ if ( specialFields != null )
136+ return true ;
137+
138+ if ( orderBy != null && orderBy .keySet ().size () > 0 )
139+ return true ;
140+
141+ if ( hintStr != null || hintObj != null || snapshot || explain )
142+ return true ;
143+
144+ return false ;
145+ }
146+
82147 /**
83148 * Adds DBObject to the operation
149+ * @param dbobj DBObject to add field to
84150 * @param field name of the field
85151 * @param obj object to add to the operation. Ignore if <code>null</code>.
86152 * @param sendEmpty if <code>true</code> adds obj even if it's empty. Ignore if <code>false</code> and obj is empty.
87153 * @return
88154 */
89- public QueryOpBuilder addToQueryObject (String field , DBObject obj , boolean sendEmpty ) {
155+ private void addToQueryObject (DBObject dbobj , String field , DBObject obj , boolean sendEmpty ) {
90156 if (obj == null )
91- return this ;
157+ return ;
92158
93159 if (!sendEmpty && obj .keySet ().size () == 0 )
94- return this ;
160+ return ;
95161
96- return addToQueryObject (field , obj );
162+ addToQueryObject (dbobj , field , obj );
97163 }
98164
99165 /**
100166 * Adds an Object to the operation
167+ * @param dbobj DBObject to add field to
101168 * @param field name of the field
102169 * @param obj Object to be added. Ignore if <code>null</code>
103170 * @return
104171 */
105- public QueryOpBuilder addToQueryObject (String field , Object obj ) {
172+ private void addToQueryObject (DBObject dbobj , String field , Object obj ) {
106173
107174 if (obj == null )
108- return this ;
175+ return ;
109176
110- queryop .put (field , obj );
111-
112- return this ;
177+ dbobj .put (field , obj );
113178 }
114179
115- /**
116- * gets the constructed query operation object
117- * @return
118- */
119- public DBObject get (){
120- return queryop ;
121- }
180+
122181
123182}
0 commit comments