Search in sources :

Example 6 with Tuple

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());
    }
}
Also used : ArrayList(java.util.ArrayList) Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Tuple(io.requery.query.Tuple) Test(org.junit.Test)

Example 7 with Tuple

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");
}
Also used : Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Tuple(io.requery.query.Tuple) Test(org.junit.Test)

Example 8 with Tuple

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());
}
Also used : Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Tuple(io.requery.query.Tuple) Test(org.junit.Test)

Example 9 with Tuple

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WHERE(io.requery.sql.Keyword.WHERE) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QueryElement(io.requery.query.element.QueryElement) AliasedExpression(io.requery.query.AliasedExpression) EntityProxy(io.requery.proxy.EntityProxy) Consumer(io.requery.util.function.Consumer) AliasedExpression(io.requery.query.AliasedExpression) Expression(io.requery.query.Expression) QueryAttribute(io.requery.meta.QueryAttribute) Tuple(io.requery.query.Tuple)

Example 10 with Tuple

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"));
}
Also used : ArrayList(java.util.ArrayList) Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Tuple(io.requery.query.Tuple) Test(org.junit.Test)

Aggregations

Tuple (io.requery.query.Tuple)14 Test (org.junit.Test)13 Group_Person (io.requery.test.model.Group_Person)12 Person (io.requery.test.model.Person)12 Group (io.requery.test.model.Group)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Attribute (io.requery.meta.Attribute)1 QueryAttribute (io.requery.meta.QueryAttribute)1 EntityProxy (io.requery.proxy.EntityProxy)1 AliasedExpression (io.requery.query.AliasedExpression)1 Expression (io.requery.query.Expression)1 QueryElement (io.requery.query.element.QueryElement)1 WHERE (io.requery.sql.Keyword.WHERE)1 Person (io.requery.test.autovalue.Person)1 Consumer (io.requery.util.function.Consumer)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1