|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 ArangoDB GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.arangodb.example.document; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Iterator; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.junit.Assert; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import com.arangodb.ArangoDriver; |
| 29 | +import com.arangodb.ArangoException; |
| 30 | +import com.arangodb.CursorResult; |
| 31 | +import com.arangodb.util.AqlQueryOptions; |
| 32 | + |
| 33 | +public class AqlQueryWithSpecialReturnTypesExample extends BaseExample { |
| 34 | + |
| 35 | + private static final String DATABASE_NAME = "SimplePersonAqlQueryWithLimitExample"; |
| 36 | + |
| 37 | + private static final String COLLECTION_NAME = "SimplePersonAqlQueryWithLimitExample"; |
| 38 | + |
| 39 | + public ArangoDriver arangoDriver; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void _before() { |
| 43 | + removeTestDatabase(DATABASE_NAME); |
| 44 | + |
| 45 | + arangoDriver = getArangoDriver(getConfiguration()); |
| 46 | + createDatabase(arangoDriver, DATABASE_NAME); |
| 47 | + createCollection(arangoDriver, COLLECTION_NAME); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void simplePersonAqlWithLimitQuery() { |
| 52 | + // |
| 53 | + // You can find the ArangoDB Web interface here: |
| 54 | + // http://127.0.0.1:8529/ |
| 55 | + // |
| 56 | + // change the log level to "debug" in /src/test/resource/logback.xml to |
| 57 | + // see the HTTP communication |
| 58 | + |
| 59 | + try { |
| 60 | + // |
| 61 | + printHeadline("create example documents"); |
| 62 | + // |
| 63 | + createExamples(); |
| 64 | + |
| 65 | + // |
| 66 | + printHeadline("build query"); |
| 67 | + // |
| 68 | + |
| 69 | + // bind @gender to WOMAN |
| 70 | + HashMap<String, Object> bindVars = new HashMap<String, Object>(); |
| 71 | + bindVars.put("gender", FEMALE); |
| 72 | + |
| 73 | + // query (count = true, batchSize = 5) |
| 74 | + AqlQueryOptions aqlQueryOptions = new AqlQueryOptions().setCount(true).setBatchSize(5); |
| 75 | + |
| 76 | + // |
| 77 | + printHeadline("get query results in a map"); |
| 78 | + // |
| 79 | + |
| 80 | + String queryString = "FOR t IN " + COLLECTION_NAME |
| 81 | + + " FILTER t.age >= 20 && t.age < 30 && t.gender == @gender RETURN t"; |
| 82 | + System.out.println(queryString); |
| 83 | + |
| 84 | + @SuppressWarnings("rawtypes") |
| 85 | + CursorResult<Map> cursor = arangoDriver.executeAqlQuery(queryString, bindVars, aqlQueryOptions, Map.class); |
| 86 | + Assert.assertNotNull(cursor); |
| 87 | + |
| 88 | + @SuppressWarnings("rawtypes") |
| 89 | + Iterator<Map> iterator = cursor.iterator(); |
| 90 | + while (iterator.hasNext()) { |
| 91 | + Map<?, ?> map = iterator.next(); |
| 92 | + |
| 93 | + Assert.assertNotNull(map); |
| 94 | + Assert.assertNotNull(map.get("name")); |
| 95 | + Assert.assertNotNull(map.get("gender")); |
| 96 | + Assert.assertNotNull(map.get("age")); |
| 97 | + |
| 98 | + System.out.printf("%15s (%5s): %s%n", map.get("name"), map.get("gender"), map.get("age").toString()); |
| 99 | + } |
| 100 | + |
| 101 | + // |
| 102 | + printHeadline("get query results in a list"); |
| 103 | + // |
| 104 | + |
| 105 | + queryString = "FOR t IN " + COLLECTION_NAME |
| 106 | + + " FILTER t.age >= 20 && t.age < 30 && t.gender == @gender RETURN [t.name, t.gender, t.age]"; |
| 107 | + System.out.println(queryString); |
| 108 | + |
| 109 | + @SuppressWarnings("rawtypes") |
| 110 | + CursorResult<List> cursor2 = arangoDriver.executeAqlQuery(queryString, bindVars, aqlQueryOptions, |
| 111 | + List.class); |
| 112 | + Assert.assertNotNull(cursor2); |
| 113 | + |
| 114 | + @SuppressWarnings("rawtypes") |
| 115 | + Iterator<List> iterator2 = cursor2.iterator(); |
| 116 | + while (iterator2.hasNext()) { |
| 117 | + List<?> list = iterator2.next(); |
| 118 | + |
| 119 | + Assert.assertNotNull(list); |
| 120 | + Assert.assertNotNull(list.get(0)); |
| 121 | + Assert.assertNotNull(list.get(1)); |
| 122 | + Assert.assertNotNull(list.get(2)); |
| 123 | + |
| 124 | + System.out.printf("%15s (%5s): %s%n", list.get(0), list.get(1), list.get(2).toString()); |
| 125 | + } |
| 126 | + |
| 127 | + } catch (ArangoException e) { |
| 128 | + Assert.fail("Example failed. " + e.getMessage()); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + private void createExamples() throws ArangoException { |
| 134 | + // create some persons |
| 135 | + for (int i = 0; i < 100; i++) { |
| 136 | + SimplePerson person = new SimplePerson(); |
| 137 | + person.setName("TestUser" + i); |
| 138 | + person.setGender((i % 2) == 0 ? MALE : FEMALE); |
| 139 | + person.setAge((int) (Math.random() * 100) + 10); |
| 140 | + |
| 141 | + arangoDriver.createDocument(COLLECTION_NAME, person, true, null); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments