use of io.requery.query.Tuple in project requery by requery.
the class FunctionalTest method testQueryRaw.
@Test
public void testQueryRaw() {
final int count = 5;
List<Person> people = new ArrayList<>();
for (int i = 0; i < count; i++) {
Person person = randomPerson();
data.insert(person);
people.add(person);
}
List<Long> resultIds = new ArrayList<>();
try (Result<Tuple> result = data.raw("select * from Person")) {
List<Tuple> list = result.toList();
assertEquals(count, list.size());
for (int i = 0; i < people.size(); i++) {
Tuple tuple = list.get(i);
String name = tuple.get("name");
assertEquals(people.get(i).getName(), name);
Number id = tuple.get("id");
assertEquals(people.get(i).getId(), id.intValue());
resultIds.add(id.longValue());
}
}
try (Result<Tuple> result = data.raw("select * from Person WHERE id IN ?", resultIds)) {
List<Tuple> list = result.toList();
List<Long> ids = new ArrayList<>(list.size());
for (Tuple tuple : list) {
ids.add(tuple.<Number>get("id").longValue());
}
assertEquals(resultIds, ids);
}
try (Result<Tuple> result = data.raw("select count(*) from Person")) {
// can be long or int depending on db
Number number = result.first().get(0);
assertEquals(count, number.intValue());
}
try (Result<Tuple> result = data.raw("select * from Person WHERE id = ?", people.get(0))) {
assertEquals(result.first().<Number>get("id").intValue(), people.get(0).getId());
}
}
use of io.requery.query.Tuple in project requery by requery.
the class FunctionalTest method testQuerySelectTrim.
@Test
public void testQuerySelectTrim() {
// TODO fix use ltrim/rtrim for SQLServer
Person person = randomPerson();
person.setName(" Name ");
data.insert(person);
Tuple result = data.select(Person.NAME.trim().as("name")).get().first();
String name = result.get(0);
assertEquals(name, "Name");
}
use of io.requery.query.Tuple in project requery by requery.
the class FunctionalTest method testQueryGroupBy.
@Test
public void testQueryGroupBy() {
for (int i = 0; i < 5; i++) {
Person person = randomPerson();
person.setAge(i);
data.insert(person);
}
try (Result<Tuple> result = data.select(Person.AGE).groupBy(Person.AGE).having(Person.AGE.greaterThan(3)).get()) {
assertTrue(result.toList().size() == 1);
}
assertTrue(data.select(Person.AGE).groupBy(Person.AGE).having(Person.AGE.lessThan(0)).get().toList().isEmpty());
}
use of io.requery.query.Tuple in project requery by requery.
the class EntityReader method batchRefresh.
@SafeVarargs
final Iterable<E> batchRefresh(Iterable<E> entities, Attribute<E, ?>... attributes) {
// if the type is immutable return a new collection with the rebuilt objects
final Collection<E> collection = type.isImmutable() ? new ArrayList<E>() : null;
if (keyAttribute == null) {
// non optimal case objects with multiple keys or no keys
for (E entity : entities) {
entity = refresh(entity, type.getProxyProvider().apply(entity), attributes);
if (collection != null) {
collection.add(entity);
}
}
} else {
Set<Expression<?>> selection = new LinkedHashSet<>();
Attribute[] selectAttributes;
if (attributes == null || attributes.length == 0) {
selection = defaultSelection;
selectAttributes = defaultSelectionAttributes;
} else {
LinkedHashSet<Attribute> selectedAttributes = new LinkedHashSet<>();
selection.add(keyAttribute);
selectedAttributes.add(keyAttribute);
for (Attribute<E, ?> attribute : attributes) {
if (attribute.isVersion()) {
selection.add(aliasVersion(attribute));
} else if (!attribute.isAssociation()) {
QueryAttribute<E, ?> queryAttribute = Attributes.query(attribute);
selection.add(queryAttribute);
}
selectedAttributes.add(attribute);
}
selectAttributes = selectedAttributes.toArray(new Attribute[selection.size()]);
}
Map<Object, EntityProxy<E>> map = new HashMap<>();
for (E entity : entities) {
EntityProxy<E> proxy = type.getProxyProvider().apply(entity);
Object key = proxy.key();
if (key == null) {
throw new MissingKeyException();
}
map.put(key, proxy);
}
Condition<?, ?> condition = Attributes.query(keyAttribute).in(map.keySet());
if (type.isCacheable()) {
final Consumer<E> collector = new Consumer<E>() {
@Override
public void accept(E e) {
if (collection != null) {
collection.add(e);
}
}
};
// readResult will merge the results into the target object in cache mode
ResultReader<E> resultReader = newResultReader(selectAttributes);
SelectOperation<E> select = new SelectOperation<>(context, resultReader);
QueryElement<? extends Result<E>> query = new QueryElement<>(QueryType.SELECT, context.getModel(), select);
try (Result<E> result = query.select(selection).where(condition).get()) {
result.each(collector);
}
} else {
try (Result<Tuple> result = queryable.select(selection).where(condition).get()) {
for (Tuple tuple : result) {
Object key = tuple.get((Expression) keyAttribute);
EntityProxy<E> proxy = map.get(key);
synchronized (proxy.syncObject()) {
for (Expression expression : selection) {
Object value = tuple.get(expression);
if (expression instanceof AliasedExpression) {
AliasedExpression aliased = (AliasedExpression) expression;
expression = aliased.getInnerExpression();
}
Attribute<E, Object> attribute = Attributes.query((Attribute) expression);
proxy.set(attribute, value, PropertyState.LOADED);
}
}
}
}
}
// associations TODO can be optimized
if (attributes != null) {
for (Attribute<E, ?> attribute : attributes) {
if (attribute.isAssociation()) {
for (EntityProxy<E> proxy : map.values()) {
refreshAssociation(proxy, attribute);
}
}
}
}
}
return collection == null ? entities : collection;
}
use of io.requery.query.Tuple in project requery by requery.
the class FunctionalTest method testQueryCoalesce.
@Test
public void testQueryCoalesce() {
Person person = randomPerson();
person.setName("Carol");
person.setEmail(null);
data.insert(person);
person = randomPerson();
person.setName("Bob");
person.setEmail("test@test.com");
person.setHomepage(null);
data.insert(person);
Result<Tuple> result = data.select(Coalesce.coalesce(Person.EMAIL, Person.NAME)).get();
List<Tuple> list = result.toList();
List<String> values = new ArrayList<>();
for (Tuple tuple : list) {
values.add(tuple.get(0).toString());
}
assertEquals(values.size(), 2);
assertTrue(values.contains("Carol"));
assertTrue(values.contains("test@test.com"));
}
Aggregations