9 changed files with 0 additions and 447 deletions
@ -1,40 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
import com.mongodb.DBObject; |
|
||||||
import com.mongodb.util.JSON; |
|
||||||
|
|
||||||
public class BasicQuery implements Query { |
|
||||||
|
|
||||||
private DBObject dbo = null; |
|
||||||
|
|
||||||
|
|
||||||
public BasicQuery(String query) { |
|
||||||
super(); |
|
||||||
this.dbo = (DBObject) JSON.parse(query); |
|
||||||
} |
|
||||||
|
|
||||||
public BasicQuery(DBObject dbo) { |
|
||||||
super(); |
|
||||||
this.dbo = dbo; |
|
||||||
} |
|
||||||
|
|
||||||
public DBObject getQueryObject() { |
|
||||||
return dbo; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,161 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.LinkedHashMap; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
import org.springframework.data.document.InvalidDocumentStoreApiUageException; |
|
||||||
|
|
||||||
import com.mongodb.BasicDBObject; |
|
||||||
import com.mongodb.DBObject; |
|
||||||
|
|
||||||
public class Criteria implements CriteriaSpec { |
|
||||||
|
|
||||||
private QueryBuilder qb = null; |
|
||||||
|
|
||||||
private LinkedHashMap<String, Object> criteria = new LinkedHashMap<String, Object>(); |
|
||||||
|
|
||||||
private Object isValue = null; |
|
||||||
|
|
||||||
|
|
||||||
public Criteria(QueryBuilder qb) { |
|
||||||
super(); |
|
||||||
this.qb = qb; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
public Criteria and(String key) { |
|
||||||
return qb.find(key); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
public Criteria is(Object o) { |
|
||||||
if (isValue != null) { |
|
||||||
throw new InvalidDocumentStoreApiUageException("Multiple 'is' values declared."); |
|
||||||
} |
|
||||||
this.isValue = o; |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria lt(Object o) { |
|
||||||
criteria.put("$lt", o); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria lte(Object o) { |
|
||||||
criteria.put("$lte", o); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria gt(Object o) { |
|
||||||
criteria.put("$gt", o); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria gte(Object o) { |
|
||||||
criteria.put("$gte", o); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria in(Object... o) { |
|
||||||
criteria.put("$in", o); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria nin(Object... o) { |
|
||||||
criteria.put("$min", o); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria mod(Number value, Number remainder) { |
|
||||||
List<Object> l = new ArrayList<Object>(); |
|
||||||
l.add(value); |
|
||||||
l.add(remainder); |
|
||||||
criteria.put("$mod", l); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria all(Object o) { |
|
||||||
criteria.put("$is", o); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria size(Object o) { |
|
||||||
criteria.put("$is", o); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria exists(boolean b) { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria type(int t) { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria not() { |
|
||||||
criteria.put("$not", null); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Criteria regExp(String re) { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public void or(List<Query> queries) { |
|
||||||
criteria.put("$or", queries); |
|
||||||
} |
|
||||||
|
|
||||||
public Query build() { |
|
||||||
return qb.build(); |
|
||||||
} |
|
||||||
|
|
||||||
/* (non-Javadoc) |
|
||||||
* @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String) |
|
||||||
*/ |
|
||||||
public DBObject getCriteriaObject(String key) { |
|
||||||
DBObject dbo = new BasicDBObject(); |
|
||||||
boolean not = false; |
|
||||||
for (String k : criteria.keySet()) { |
|
||||||
if (not) { |
|
||||||
DBObject notDbo = new BasicDBObject(); |
|
||||||
notDbo.put(k, criteria.get(k)); |
|
||||||
dbo.put("$not", notDbo); |
|
||||||
not = false; |
|
||||||
} |
|
||||||
else { |
|
||||||
if ("$not".equals(k)) { |
|
||||||
not = true; |
|
||||||
} |
|
||||||
else { |
|
||||||
dbo.put(k, criteria.get(k)); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
DBObject queryCriteria = new BasicDBObject(); |
|
||||||
if (isValue != null) { |
|
||||||
queryCriteria.put(key, isValue); |
|
||||||
queryCriteria.putAll(dbo); |
|
||||||
} |
|
||||||
else { |
|
||||||
queryCriteria.put(key, dbo); |
|
||||||
} |
|
||||||
return queryCriteria; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,24 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
import com.mongodb.DBObject; |
|
||||||
|
|
||||||
public interface CriteriaSpec { |
|
||||||
|
|
||||||
DBObject getCriteriaObject(String key); |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,29 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
|
|
||||||
public class FieldSpecification { |
|
||||||
|
|
||||||
public FieldSpecification include(String key) { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public FieldSpecification exclude(String key) { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,46 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
import org.bson.types.BasicBSONList; |
|
||||||
|
|
||||||
import com.mongodb.BasicDBObject; |
|
||||||
import com.mongodb.DBObject; |
|
||||||
|
|
||||||
public class OrCriteria implements CriteriaSpec { |
|
||||||
|
|
||||||
Query[] queries = null; |
|
||||||
|
|
||||||
public OrCriteria(Query[] queries) { |
|
||||||
super(); |
|
||||||
this.queries = queries; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
/* (non-Javadoc) |
|
||||||
* @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String) |
|
||||||
*/ |
|
||||||
public DBObject getCriteriaObject(String key) { |
|
||||||
DBObject dbo = new BasicDBObject(); |
|
||||||
BasicBSONList l = new BasicBSONList(); |
|
||||||
for (Query q : queries) { |
|
||||||
l.add(q.getQueryObject()); |
|
||||||
} |
|
||||||
dbo.put(key, l); |
|
||||||
return dbo; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,25 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
import com.mongodb.DBObject; |
|
||||||
|
|
||||||
|
|
||||||
public interface Query { |
|
||||||
|
|
||||||
DBObject getQueryObject(); |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,68 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
import java.util.LinkedHashMap; |
|
||||||
|
|
||||||
import com.mongodb.BasicDBObject; |
|
||||||
import com.mongodb.DBObject; |
|
||||||
|
|
||||||
public class QueryBuilder implements Query { |
|
||||||
|
|
||||||
private LinkedHashMap<String, CriteriaSpec> criteria = new LinkedHashMap<String, CriteriaSpec>(); |
|
||||||
|
|
||||||
public Criteria find(String key) { |
|
||||||
Criteria c = new Criteria(this); |
|
||||||
this.criteria.put(key, c); |
|
||||||
return c; |
|
||||||
} |
|
||||||
|
|
||||||
public QueryBuilder or(Query... queries) { |
|
||||||
this.criteria.put("$or", new OrCriteria(queries)); |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public FieldSpecification fields() { |
|
||||||
return new FieldSpecification(); |
|
||||||
} |
|
||||||
|
|
||||||
public SliceSpecification slice() { |
|
||||||
return new SliceSpecification(); |
|
||||||
} |
|
||||||
|
|
||||||
public SortSpecification sort() { |
|
||||||
return new SortSpecification(); |
|
||||||
} |
|
||||||
|
|
||||||
public QueryBuilder limit(int limit) { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public Query build() { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
public DBObject getQueryObject() { |
|
||||||
DBObject dbo = new BasicDBObject(); |
|
||||||
for (String k : criteria.keySet()) { |
|
||||||
CriteriaSpec c = criteria.get(k); |
|
||||||
DBObject cl = c.getCriteriaObject(k); |
|
||||||
dbo.putAll(cl); |
|
||||||
} |
|
||||||
return dbo; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,25 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
|
|
||||||
public class SliceSpecification { |
|
||||||
|
|
||||||
public SliceSpecification on(String key, int size) { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,29 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2010 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.document.mongodb.query; |
|
||||||
|
|
||||||
|
|
||||||
public class SortSpecification { |
|
||||||
|
|
||||||
public enum SortOrder { |
|
||||||
ASCENDING, DESCENDING |
|
||||||
} |
|
||||||
|
|
||||||
public SortSpecification on(String key, SortOrder order) { |
|
||||||
return this; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue