diff --git a/framework/db/src/com/cloud/utils/db/GenericQueryBuilder.java b/framework/db/src/com/cloud/utils/db/GenericQueryBuilder.java index 885bebb2ab3..28cfebc5f94 100755 --- a/framework/db/src/com/cloud/utils/db/GenericQueryBuilder.java +++ b/framework/db/src/com/cloud/utils/db/GenericQueryBuilder.java @@ -23,6 +23,39 @@ import java.util.UUID; import com.cloud.utils.db.SearchCriteria.Op; +/** + * GenericQueryBuilder builds a search query during runtime. It allows the + * search query to be built completely in Java rather than part SQL fragments + * and part entity field like HQL or JPQL. This class is different from + * GenericSearchBuilder in that it is used for building queries during runtime + * where GenericSearchBuilder expects the query to be built during load time + * and parameterized values to be set during runtime. + * + * GenericQueryBuilder allows results to be a native type, the entity bean, + * and a composite type. If you are just retrieving the entity bean, there + * is a simpler class called QueryBuilder that you can use. The usage + * is approximately the same. + * + * + * // Note that in the following search, it selects a func COUNT to be the + * // return result so for the second parameterized type is long. + * // Note the entity object itself must have came from search and + * // it uses the getters of the object to retrieve the field used in the search. + * + * GenericQueryBuilder sc = GenericQueryBuilder.create(HostVO.class, Long.class); + * HostVO entity = CountSearch.entity(); + * sc.select(null, FUNC.COUNT, null, null).where(entity.getType(), Op.EQ, Host.Type.Routing); + * sc.and(entity.getCreated(), Op.LT, new Date()); + * Long count = sc.find(); + * + * * + * + * @see GenericSearchBuilder + * @see QueryBuilder + * + * @param Entity object to perform the search on + * @param Result object + */ public class GenericQueryBuilder extends SearchBase, T, K> { final HashMap _params = new HashMap(); @@ -30,6 +63,12 @@ public class GenericQueryBuilder extends SearchBase GenericQueryBuilder create(Class entityType, Class resultType) { GenericDao dao = (GenericDao)GenericDaoBase.getDao(entityType); @@ -37,31 +76,60 @@ public class GenericQueryBuilder extends SearchBase(entityType, resultType); } - public GenericQueryBuilder and(Object useless, Op op, Object... values) { + /** + * Adds AND search condition + * + * @param field the field of the entity to perform the search on. + * @param op operator + * @param values parameterized values + * @return this + */ + public GenericQueryBuilder and(Object field, Op op, Object... values) { String uuid = UUID.randomUUID().toString(); constructCondition(uuid, " AND ", _specifiedAttrs.get(0), op); _params.put(uuid, values); return this; } - public GenericQueryBuilder or(Object useless, Op op, Object... values) { + /** + * Adds OR search condition + * + * @param field the field of the entity to perform the search on. + * @param op operator + * @param values parameterized values + * @return this + */ + public GenericQueryBuilder or(Object field, Op op, Object... values) { String uuid = UUID.randomUUID().toString(); constructCondition(uuid, " OR ", _specifiedAttrs.get(0), op); _params.put(uuid, values); return this; } - protected GenericQueryBuilder left(Object useless, Op op, Object... values) { + protected GenericQueryBuilder left(Object field, Op op, Object... values) { String uuid = UUID.randomUUID().toString(); constructCondition(uuid, " ( ", _specifiedAttrs.get(0), op); _params.put(uuid, values); return this; } - public GenericQueryBuilder op(Object useless, Op op, Object... values) { - return left(useless, op, values); + /** + * Adds search condition that starts with an open parenthesis. Call cp() + * to close the parenthesis. + * + * @param field the field of the entity to perform the search on. + * @param op operator + * @param values parameterized values + * @return this + */ + public GenericQueryBuilder op(Object field, Op op, Object... values) { + return left(field, op, values); } + /** + * If the query is supposed to return a list, use this. + * @return List of result objects + */ @SuppressWarnings("unchecked") public List list() { finalize(); @@ -75,6 +143,9 @@ public class GenericQueryBuilder extends SearchBase create() { SearchCriteria sc = super.create(); @@ -86,11 +157,20 @@ public class GenericQueryBuilder extends SearchBase lst = list(); + return lst.get(0); + } } } diff --git a/framework/db/src/com/cloud/utils/db/GenericSearchBuilder.java b/framework/db/src/com/cloud/utils/db/GenericSearchBuilder.java index b9bef61beeb..72ad278730b 100755 --- a/framework/db/src/com/cloud/utils/db/GenericSearchBuilder.java +++ b/framework/db/src/com/cloud/utils/db/GenericSearchBuilder.java @@ -36,6 +36,28 @@ import com.cloud.utils.db.SearchCriteria.Op; * runtime and, more importantly, the proper construction can be checked when * components are being loaded. However, if you prefer to just construct * the entire search at runtime, you can use GenericQueryBuilder. + * + * + * // To specify the GenericSearchBuilder, you should do this at load time. + * // Note that in the following search, it selects a func COUNT to be the + * // return result so for the second parameterized type is long. It also + * // presets the type in the search and declares created to be set during + * // runtime. Note the entity object itself must have came from search and + * // it uses the getters of the object to retrieve the field used in the search. + * + * GenericSearchBuilder CountSearch = _hostDao.createSearchBuilder(Long.class); + * HostVO entity = CountSearch.entity(); + * CountSearch.select(null, FUNC.COUNT, null, null).where(entity.getType(), Op.EQ).value(Host.Type.Routing); + * CountSearch.and(entity.getCreated(), Op.LT, "create_date").done(); + * + * // Later in the code during runtime + * SearchCriteria sc = CountSearch.create(); + * sc.setParameter("create_date", new Date()); + * Long count = _hostDao.customizedSearch(sc, null); + * + * + * @see GenericQueryBuilder for runtime construction of search query + * @see SearchBuilder for returning VO objects itself * * @param VO object this Search is build for. * @param Result object that should contain the results. @@ -118,11 +140,13 @@ public class GenericSearchBuilder extends SearchBase op(Object field, Op op, String name) { return left(field, op, name); @@ -132,6 +156,15 @@ public class GenericSearchBuilder extends SearchBase op(String name, Object field, Op op) { return left(field, op, name); } @@ -149,16 +182,39 @@ public class GenericSearchBuilder extends SearchBase or(Object field, Op op, String name) { constructCondition(name, " OR ", _specifiedAttrs.get(0), op); return this; } + /** + * Adds an OR condition but the values can be preset + * + * @param field field of the entity object + * @param op operator + * @return Preset + */ public Preset or(Object field, Op op) { Condition condition = constructCondition(UUID.randomUUID().toString(), " OR ", _specifiedAttrs.get(0), op); return new Preset(this, condition); } + /** + * Convenience method to create the search criteria and set a + * parameter in the search. + * + * @param name parameter name set during construction + * @param values values to be inserted for that parameter + * @return SearchCriteria + */ public SearchCriteria create(String name, Object... values) { SearchCriteria sc = create(); sc.setParameters(name, values);