use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QueryBuilder method resolveRelationField.
private static final <T extends Model> SelectStatement resolveRelationField(Object field, Class<T> clazz, List<String> fields, Rule rule, int depth) {
Relation<?> r = (Relation<?>) field;
SelectStatement select = new SelectStatement();
Class<? extends Model> target = r.getTarget();
Map<String, String> joinParams = unwrapRelation(r, fields.get(0), clazz);
String leftTable = joinParams.get("leftTable");
String selectField = joinParams.get("selectField");
String selectAs = joinParams.get("selectAs");
String onLeft = joinParams.get("onLeft");
String onRight = DatabaseBuilder.getTableName(target);
/*
* After the steps above the left side of the join is always known.
* What is currently unknown is, if there are any further sub-joins
* needed in order to accomplish the query. Therefore the right side
* of the join is provided with the result of this function.
*/
JoinStatement join = new JoinStatement();
join.left(leftTable, "table" + depth).right(buildJoin(target, fields.subList(1, fields.size()), rule, depth + 2), "table" + (depth + 1)).on(onLeft, onRight);
/*
* The select will fetch the correct field from the previous join
* that will be needed in the next step.
*/
select.from(join).distinct().select("table" + depth + "." + selectField + " AS " + selectAs);
return select;
}
use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QuerySet method filter.
public QuerySet<T> filter(Filter filter) throws NoSuchFieldException {
SelectStatement query = QueryBuilder.buildQuery(mClass, filter.getRules());
if (mQuery == null) {
mQuery = query;
} else {
JoinStatement join = new JoinStatement();
join.left(mQuery, "left").right(query, "right").on(Model.PK, Model.PK);
SelectStatement select = new SelectStatement();
select.from(join);
mQuery = select;
}
return this;
}
use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QuerySet method contains.
/**
* Checks if the result of this query contains the given
* object. Note, that this operation will execute the query
* on the database. Use only, if you have to.
*
* @param value
* @return
*/
public boolean contains(T value) {
if (mQuery != null) {
Where where = new Where();
where.setStatement(new Statement(Model.PK, value.getId()));
SelectStatement query = new SelectStatement();
query.from(mQuery).where(where);
return getCount(query) != 0;
}
return false;
}
use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QuerySet method all.
public QuerySet<T> all() {
if (mQuery == null) {
mQuery = new SelectStatement();
mQuery.from(DatabaseBuilder.getTableName(mClass));
}
return this;
}
use of com.orm.androrm.statement.SelectStatement in project androrm by androrm.
the class QuerySet method get.
public T get(int id) {
Where where = new Where();
where.setStatement(new Statement(Model.PK, id));
if (mQuery == null) {
mQuery = new SelectStatement();
mQuery.from(DatabaseBuilder.getTableName(mClass));
}
mQuery.where(where);
Cursor c = getCursor(mQuery);
T object = createObject(c);
closeConnection(c);
return object;
}
Aggregations