Search in sources :

Example 16 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana by eclipse.

the class DefaultSelectQueryBuilderTest method shouldSelectWhereNameEq.

@Test
public void shouldSelectWhereNameEq() {
    String documentCollection = "documentCollection";
    String name = "Ada Lovelace";
    DocumentQuery query = select().from(documentCollection).where("name").eq(name).build();
    DocumentCondition condition = query.getCondition().get();
    Document document = condition.getDocument();
    assertTrue(query.getDocuments().isEmpty());
    assertEquals(documentCollection, query.getDocumentCollection());
    assertEquals(Condition.EQUALS, condition.getCondition());
    assertEquals("name", document.getName());
    assertEquals(name, document.get());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Document(org.jnosql.diana.api.document.Document) DocumentCondition(org.jnosql.diana.api.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Example 17 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class QueryAQLConverter method definesCondition.

private static void definesCondition(DocumentCondition condition, StringBuilder aql, Map<String, Object> params, char entity, int counter) {
    Document document = condition.getDocument();
    switch(condition.getCondition()) {
        case IN:
            appendCondition(aql, params, entity, document, IN);
            return;
        case EQUALS:
            appendCondition(aql, params, entity, document, EQUALS);
            return;
        case GREATER_EQUALS_THAN:
            appendCondition(aql, params, entity, document, GREATER_EQUALS_THAN);
            return;
        case GREATER_THAN:
            appendCondition(aql, params, entity, document, GREATER_THAN);
            return;
        case LESSER_THAN:
            appendCondition(aql, params, entity, document, LESSER_THAN);
            return;
        case LESSER_EQUALS_THAN:
            appendCondition(aql, params, entity, document, LESSER_EQUALS_THAN);
            return;
        case LIKE:
            appendCondition(aql, params, entity, document, LIKE);
            return;
        case AND:
            for (DocumentCondition dc : document.get(new TypeReference<List<DocumentCondition>>() {
            })) {
                if (isFirstCondition(aql, counter)) {
                    aql.append(AND);
                }
                definesCondition(dc, aql, params, entity, ++counter);
            }
            return;
        case OR:
            for (DocumentCondition dc : document.get(new TypeReference<List<DocumentCondition>>() {
            })) {
                if (isFirstCondition(aql, counter)) {
                    aql.append(OR);
                }
                definesCondition(dc, aql, params, entity, ++counter);
            }
            return;
        case NOT:
            DocumentCondition documentCondition = document.get(DocumentCondition.class);
            aql.append(NOT);
            definesCondition(documentCondition, aql, params, entity, ++counter);
            return;
        default:
            throw new IllegalArgumentException("The condition does not support in AQL: " + condition.getCondition());
    }
}
Also used : List(java.util.List) Document(org.jnosql.diana.api.document.Document) DocumentCondition(org.jnosql.diana.api.document.DocumentCondition)

Example 18 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class ArangoDBDocumentCollectionManagerAsyncTest method shouldSelect.

@Test
public void shouldSelect() {
    DocumentEntity entity = getEntity();
    AtomicReference<DocumentEntity> reference = new AtomicReference<>();
    AtomicBoolean condition = new AtomicBoolean(false);
    entityManagerAsync.insert(entity, d -> {
        condition.set(true);
        reference.set(d);
    });
    await().untilTrue(condition);
    DocumentEntity entity1 = reference.get();
    Document key = entity1.find("_key").get();
    condition.set(false);
    AtomicReference<List<DocumentEntity>> references = new AtomicReference<>();
    entityManagerAsync.select(select().from(entity1.getName()).where("_key").eq(key.get()).build(), l -> {
        condition.set(true);
        references.set(l);
    });
    await().untilTrue(condition);
    List<DocumentEntity> entities = references.get();
    assertFalse(entities.isEmpty());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 19 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class ArangoDBDocumentCollectionManagerAsyncTest method getEntity.

private DocumentEntity getEntity() {
    DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);
    Map<String, Object> map = new HashMap<>();
    map.put("name", "Poliana");
    map.put("city", "Salvador");
    entity.add(Document.of(KEY_NAME, random.nextLong()));
    List<Document> documents = Documents.of(map);
    documents.forEach(entity::add);
    return entity;
}
Also used : DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) HashMap(java.util.HashMap) Document(org.jnosql.diana.api.document.Document)

Example 20 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class ArangoDBDocumentCollectionManagerTest method shouldFindDocument.

@Test
public void shouldFindDocument() {
    DocumentEntity entity = entityManager.insert(getEntity());
    Document id = entity.find(KEY_NAME).get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    List<DocumentEntity> entities = entityManager.select(query);
    assertFalse(entities.isEmpty());
    DocumentEntity documentEntity = entities.get(0);
    assertEquals(entity.find(KEY_NAME).get().getValue().get(String.class), documentEntity.find(KEY_NAME).get().getValue().get(String.class));
    assertEquals(entity.find("name").get(), documentEntity.find("name").get());
    assertEquals(entity.find("city").get(), documentEntity.find("city").get());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.jnosql.diana.api.document.Document)126 Test (org.junit.jupiter.api.Test)102 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)94 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)71 List (java.util.List)41 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)37 ArrayList (java.util.ArrayList)25 DocumentCondition (org.jnosql.diana.api.document.DocumentCondition)25 Arrays.asList (java.util.Arrays.asList)23 HashMap (java.util.HashMap)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Map (java.util.Map)11 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)11 TypeReference (org.jnosql.diana.api.TypeReference)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Matchers.contains (org.hamcrest.Matchers.contains)10 DocumentQueryBuilder.select (org.jnosql.diana.api.document.query.DocumentQueryBuilder.select)10 Arrays (java.util.Arrays)8 Optional (java.util.Optional)8 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)8