use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QueryBuilder method getRelationSelection.
@SuppressWarnings("unchecked")
private static final <T extends Model> SelectStatement getRelationSelection(Relation<?> r, Class<T> clazz, Rule rule) {
Class<? extends Model> target = r.getTarget();
Statement stmt = rule.getStatement();
Where where = new Where();
SelectStatement select = new SelectStatement();
if (r instanceof ManyToManyField) {
ManyToManyField<T, ?> m = (ManyToManyField<T, ?>) r;
stmt.setKey(DatabaseBuilder.getTableName(target));
where.setStatement(stmt);
select.from(m.getRelationTableName()).select(DatabaseBuilder.getTableName(clazz));
}
if (r instanceof OneToManyField) {
String backLinkFieldName = Model.getBackLinkFieldName(target, clazz);
stmt.setKey(backLinkFieldName);
where.setStatement(stmt);
select.from(DatabaseBuilder.getTableName(target)).select(backLinkFieldName + " AS " + DatabaseBuilder.getTableName(clazz));
}
select.where(where).distinct();
return select;
}
use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QueryBuilder method buildQuery.
private static final <T extends Model> SelectStatement buildQuery(Class<T> clazz, List<Rule> rules, int depth) {
String tableName = DatabaseBuilder.getTableName(clazz);
JoinStatement selfJoin = new JoinStatement();
selfJoin.left(tableName, "self" + depth);
SelectStatement subSelect = new SelectStatement();
Rule rule = rules.get(0);
List<String> fields = Arrays.asList(rule.getKey().split("__"));
if (fields.size() == 1) {
String fieldName = fields.get(0);
T instance = Model.getInstace(clazz);
if (instance != null) {
Object o = getFieldInstance(clazz, instance, fieldName);
if (DatabaseBuilder.isRelationalField(o)) {
// gather ids for fields
SelectStatement s = buildJoin(clazz, fields, rule, depth);
JoinStatement join = new JoinStatement();
join.left(tableName, "a").right(s, "b").on(Model.PK, tableName);
subSelect.from(join).select("a.*");
} else {
Where where = new Where();
where.setStatement(rule.getStatement());
subSelect.from(tableName).where(where);
}
}
} else {
int left = depth + 1;
int right = depth + 2;
JoinStatement join = new JoinStatement();
join.left(tableName, "outer" + left).right(buildJoin(clazz, fields, rule, depth), "outer" + right).on(Model.PK, tableName);
subSelect.from(join).select("outer" + left + ".*");
}
if (rules.size() == 1) {
return subSelect;
}
selfJoin.right(subSelect, "self" + (depth + 1)).on(Model.PK, Model.PK);
JoinStatement outerSelfJoin = new JoinStatement();
outerSelfJoin.left(subSelect, "outerSelf" + depth).right(buildQuery(clazz, rules.subList(1, rules.size()), (depth + 2)), "outerSelf" + (depth + 1)).on(Model.PK, Model.PK);
SelectStatement select = new SelectStatement();
select.from(outerSelfJoin).select("outerSelf" + depth + ".*");
return select;
}
use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QueryBuilder method resolveLastField.
private static final <T extends Model> SelectStatement resolveLastField(Object field, Class<T> clazz, Rule rule) {
SelectStatement select = new SelectStatement();
if (DatabaseBuilder.isRelationalField(field) && !(field instanceof ForeignKeyField)) {
Relation<?> r = (Relation<?>) field;
return getRelationSelection(r, clazz, rule);
}
String tableName = DatabaseBuilder.getTableName(clazz);
Where where = new Where();
where.setStatement(rule.getStatement());
select.from(tableName).distinct().select(Model.PK + " AS " + tableName).where(where);
return select;
}
use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QuerySet method containsAll.
/**
* See {@link QuerySet#contains}
* @param values
* @return
*/
public boolean containsAll(Collection<T> values) {
if (mQuery != null) {
List<Object> ids = new ArrayList<Object>();
for (T item : values) {
ids.add(item.getId());
}
Where where = new Where();
where.setStatement(new InStatement(Model.PK, ids));
SelectStatement query = new SelectStatement();
query.from(mQuery).where(where);
return getCount(query) == values.size();
}
return false;
}
use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QuerySet method orderBy.
public QuerySet<T> orderBy(String... columns) {
if (mQuery != null) {
SelectStatement query = new SelectStatement();
query.from(mQuery).orderBy(columns);
mQuery = query;
}
return this;
}
Aggregations