private LazyList<T> findAll() { return new LazyList<>(null, metaModel); }
private LazyList<T> find(String query, Object... params) { if (query.equals("*")) { if (params.length == 0) { return findAll(); } else { throw new IllegalArgumentException("cannot provide parameters with query: '*'"); } } return fullQuery ? new LazyList<T>(true, metaModel, this.query, params) : new LazyList<T>(query, metaModel, params); }
public static <T extends Model> LazyList<T> findBySql(Class<T> clazz, String fullQuery, Object... params) { return new LazyList<>(false, metaModelOf(clazz), fullQuery, params); }
public static <T extends Model> LazyList<T> findAll(Class<T> clazz) { return new LazyList(null, metaModelOf(clazz)); }
public static <T extends Model> LazyList<T> where(Class<T> clazz, String subquery, Object... params) { if (subquery.trim().equals("*")) { if (empty(params)) { return findAll(clazz); } else { throw new IllegalArgumentException( "cannot provide parameters with query: '*', use findAll() method instead"); } } return new LazyList<>(subquery, metaModelOf(clazz), params); }
public static <T extends Model> T findByCompositeKeys(Class<T> clazz, Object...values) { if (values == null || values.length == 0) { return null; } MetaModel metaModel = metaModelOf(clazz); String[] compositeKeys = metaModel.getCompositeKeys(); if (compositeKeys == null || compositeKeys.length != values.length){ return null; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < compositeKeys.length; i++) { sb.append(i == 0 ? "" : " AND ").append(compositeKeys[i]) .append(" = ?"); } LazyList<T> list = new LazyList<T>(sb.toString(), metaModel, values).limit(1); return list.isEmpty() ? null : list.get(0); }
private void processParent(BelongsToAssociation association) { if (delegate.isEmpty()) { // no need to process parents if no models selected. return; } //need to remove duplicates because more than one child can belong to the same parent. Set<Object> distinctParentIds = collectDistinct(association.getFkName()); distinctParentIds.remove(null); // remove null parent id if (distinctParentIds.isEmpty()) { return; } final MetaModel parentMetaModel = metaModelOf(association.getTargetClass()); final Map<Object, Model> parentById = new HashMap<>(); StringBuilder query = new StringBuilder().append(parentMetaModel.getIdName()).append(" IN ("); appendQuestions(query, distinctParentIds.size()); query.append(')'); for (Model parent : new LazyList<>(query.toString(), parentMetaModel, distinctParentIds.toArray())) { parentById.put(parent.getId(), parent); } //now that we have the parents in the has, we need to distribute them into list of children that are //stored in the delegate. for (Model child : delegate) { // parent could be null, which is fine child.setCachedParent(parentById.get(child.get(association.getFkName()))); } }
/** * @author Evan Leonard */ private void processPolymorphicParent(BelongsToPolymorphicAssociation association) { if (delegate.isEmpty()) { // no need to process children if no models selected. return; } //need to remove duplicates because more than one child can belong to the same parent. Set<Object> distinctParentIds = collectDistinct("parent_id", "parent_type", association.getParentClassName()); distinctParentIds.remove(null); // remove null parent id if (distinctParentIds.isEmpty()) { return; } final MetaModel parentMetaModel = metaModelOf(association.getTargetClass()); final Map<Object, Model> parentById = new HashMap<>(); StringBuilder query = new StringBuilder().append(parentMetaModel.getIdName()).append(" IN ("); appendQuestions(query, distinctParentIds.size()); query.append(')'); for (Model parent : new LazyList<>(query.toString(), parentMetaModel, distinctParentIds.toArray())) { parentById.put(association.getParentClassName() + ":" + parent.getId(), parent); } //now that we have the parents in the has, we need to distribute them into list of children that are //stored in the delegate. for (Model child : delegate) { // parent could be null, which is fine child.setCachedParent(parentById.get(association.getParentClassName() + ":" + child.get("parent_id"))); } }
public static <T extends Model> T findFirst(Class<T> clazz, String subQuery, Object... params) { LazyList<T> list = new LazyList<T>(subQuery, metaModelOf(clazz), params).limit(1); return list.isEmpty() ? null : list.get(0); }
private void processPolymorphicChildren(OneToManyPolymorphicAssociation association) { if (delegate.isEmpty()) {//no need to process children if no models selected. return; } MetaModel childMetaModel = metaModelOf(association.getTargetClass()); Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); StringBuilder query = new StringBuilder().append("parent_id IN ("); appendQuestions(query, ids.size()); query.append(") AND parent_type = '").append(association.getTypeLabel()).append('\''); for (Model child : new LazyList<>(query.toString(), childMetaModel, ids.toArray()).orderBy(childMetaModel.getIdName())) { if (childrenByParentId.get(child.get("parent_id")) == null) { childrenByParentId.put(child.get("parent_id"), new SuperLazyList<>()); } childrenByParentId.get(child.get("parent_id")).add(child); } for (T parent : delegate) { List<Model> children = childrenByParentId.get(parent.getId()); if (children != null) { parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
private void processChildren(OneToManyAssociation association) { if(delegate.isEmpty()){//no need to process children if no models selected. return; } final MetaModel childMetaModel = metaModelOf(association.getTargetClass()); final String fkName = association.getFkName(); final Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); StringBuilder query = new StringBuilder().append(fkName).append(" IN ("); appendQuestions(query, ids.size()); query.append(')'); for (Model child : new LazyList<>(query.toString(), childMetaModel, ids.toArray()).orderBy(childMetaModel.getIdName())) { if(childrenByParentId.get(child.get(fkName)) == null){ childrenByParentId.put(child.get(fkName), new SuperLazyList<>()); } childrenByParentId.get(child.get(fkName)).add(child); } for(T parent : delegate){ List<Model> children = childrenByParentId.get(parent.getId()); if(children != null){ parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
public static <T extends Model> T findById(Class<T> clazz, Object id) { if (id == null) { return null; } MetaModel metaModel = metaModelOf(clazz); LazyList<T> list = new LazyList<T>(metaModel.getIdName() + " = ?", metaModel, id).limit(1); return list.isEmpty() ? null : list.get(0); }
private LazyList<T> findAll() { return new LazyList<T>(null, metaModel); }
allParams[0] = getId(); System.arraycopy(params, 0, allParams, 1, params.length); return new LazyList<>(true, metaModelOf(manyToManyAssociation.getTargetClass()), query, allParams); } else if (oneToManyPolymorphicAssociation != null) { subQuery = "parent_id = ? AND " + " parent_type = '" + oneToManyPolymorphicAssociation.getTypeLabel() + "'" + additionalCriteria; allParams[0] = getId(); System.arraycopy(params, 0, allParams, 1, params.length); return new LazyList<>(subQuery, targetMM, allParams);
private LazyList<T> find(String query, Object... params) { if (query.equals("*")) { if (params.length == 0) { return findAll(); } else { throw new IllegalArgumentException("cannot provide parameters with query: '*'"); } } return fullQuery ? new LazyList<T>(true, metaModel, this.query, params) : new LazyList<T>(query, metaModel, params); }
private LazyList<T> find(String query, Object... params) { if (query.equals("*")) { if (params.length == 0) { return findAll(); } else{ throw new IllegalArgumentException("cannot provide parameters with query: '*'"); } } return fullQuery ? new LazyList<T>(true, metaModel, this.query, params) : new LazyList<T>(query, metaModel, params); }
public static <T extends Model> LazyList<T> findAll(Class<T> clazz) { return new LazyList(null, metaModelOf(clazz)); }
public static <T extends Model> LazyList<T> where(Class<T> clazz, String subquery, Object... params) { if (subquery.trim().equals("*")) { if (empty(params)) { return findAll(clazz); } else { throw new IllegalArgumentException( "cannot provide parameters with query: '*', use findAll() method instead"); } } return new LazyList<T>(subquery, metaModelOf(clazz), params); }
public static <T extends Model> LazyList<T> where(Class<T> clazz, String subquery, Object... params) { if (subquery.trim().equals("*")) { if (empty(params)) { return findAll(clazz); } else { throw new IllegalArgumentException( "cannot provide parameters with query: '*', use findAll() method instead"); } } return new LazyList<>(subquery, metaModelOf(clazz), params); }
public static <T extends Model> T findById(Class<T> clazz, Object id) { if (id == null) { return null; } MetaModel metaModel = metaModelOf(clazz); LazyList<T> list = new LazyList<T>(metaModel.getIdName() + " = ?", metaModel, id).limit(1); return list.isEmpty() ? null : list.get(0); }