use of com.orm.androrm.statement.Statement in project androrm by androrm.
the class FilterTest method testIs.
public void testIs() {
Filter f = new Filter();
f.is("product__name", "foo");
List<Rule> filters = f.getRules();
Rule filter = filters.get(0);
Statement s = filter.getStatement();
Set<String> keys = s.getKeys();
assertEquals("product__name", filter.getKey());
assertTrue(keys.contains("name"));
assertEquals("name = 'foo'", s.toString());
Product p = new Product();
p.setName("test product");
p.save(getContext());
f = new Filter();
f.is("supplier__product", p);
filters = f.getRules();
filter = filters.get(0);
s = filter.getStatement();
keys = s.getKeys();
assertEquals("supplier__product", filter.getKey());
assertTrue(keys.contains("product"));
assertEquals("product = '" + p.getId() + "'", s.toString());
}
use of com.orm.androrm.statement.Statement in project androrm by androrm.
the class FilterTest method testContains.
public void testContains() {
Filter set = new Filter();
set.contains("supplier__name", "foo");
List<Rule> filters = set.getRules();
Rule filter = filters.get(0);
Statement s = filter.getStatement();
Set<String> keys = s.getKeys();
assertEquals("supplier__name", filter.getKey());
assertTrue(keys.contains("name"));
assertTrue(s instanceof LikeStatement);
assertEquals("name LIKE '%foo%'", s.toString());
}
use of com.orm.androrm.statement.Statement in project androrm by androrm.
the class FilterTest method testIn.
public void testIn() {
Filter set = new Filter();
List<Integer> values = new ArrayList<Integer>();
values.add(3);
values.add(5);
set.in("product__id", values);
List<Rule> filters = set.getRules();
Rule filter = filters.get(0);
Statement s = filter.getStatement();
Set<String> keys = s.getKeys();
assertEquals("product__id", filter.getKey());
assertTrue(keys.contains("id"));
assertTrue(s instanceof InStatement);
assertEquals("id IN ('3','5')", s.toString());
List<Product> products = new ArrayList<Product>();
Product p1 = new Product();
p1.setName("test 1");
p1.save(getContext());
Product p2 = new Product();
p2.setName("test 2");
p2.save(getContext());
products.add(p1);
products.add(p2);
set = new Filter();
set.in("supplier__product", products);
filters = set.getRules();
filter = filters.get(0);
s = filter.getStatement();
keys = s.getKeys();
assertEquals("supplier__product", filter.getKey());
assertTrue(keys.contains("product"));
assertTrue(s instanceof InStatement);
assertEquals("product IN ('" + p1.getId() + "','" + p2.getId() + "')", s.toString());
}
use of com.orm.androrm.statement.Statement in project androrm by androrm.
the class RuleTest method testFilter.
public void testFilter() {
Statement s = new Statement("bar", "baz");
Rule f = new Rule("foo", s);
assertEquals("foo", f.getKey());
assertEquals(s, f.getStatement());
}
use of com.orm.androrm.statement.Statement 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;
}
Aggregations