Skip to content

Commit 7328852

Browse files
author
a-brandt
committed
added executeAqlQueryRaw(...)
1 parent 82b9a73 commit 7328852

File tree

6 files changed

+270
-10
lines changed

6 files changed

+270
-10
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
v2.7.2 (2016-XX-XX)
22
---------------------------
3+
* added executeAqlQueryRaw(...). Example src/test/java/com/arangodb/example/document/RawDocumentExample.java
34

45
v2.7.1 (2016-01-21)
56
---------------------------

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,4 +5812,30 @@ public String getDocumentRaw(String documentHandle, Long ifNoneMatchRevision, Lo
58125812
ifMatchRevision);
58135813
}
58145814

5815+
/**
5816+
* This method executes an AQL query and returns a CursorRawResult.
5817+
*
5818+
* Use CursorRawResult.iterator() to get the raw JSON strings.
5819+
*
5820+
* @param query
5821+
* an AQL query as string
5822+
* @param bindVars
5823+
* a map containing all bind variables,
5824+
* @param aqlQueryOptions
5825+
* AQL query options
5826+
* @return CursorRawResult
5827+
* @throws ArangoException
5828+
*/
5829+
public CursorRawResult executeAqlQueryRaw(
5830+
String query,
5831+
Map<String, Object> bindVars,
5832+
AqlQueryOptions aqlQueryOptions) throws ArangoException {
5833+
5834+
if (aqlQueryOptions == null) {
5835+
aqlQueryOptions = getDefaultAqlQueryOptions();
5836+
}
5837+
5838+
return cursorDriver.executeAqlQueryRaw(getDefaultDatabase(), query, bindVars, aqlQueryOptions);
5839+
}
5840+
58155841
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright (C) 2012 tamtam180
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;
18+
19+
import java.util.ArrayList;
20+
import java.util.Iterator;
21+
import java.util.List;
22+
import java.util.NoSuchElementException;
23+
24+
import com.arangodb.entity.CursorEntity;
25+
import com.arangodb.entity.WarningEntity;
26+
import com.google.gson.JsonObject;
27+
28+
/**
29+
* @author tamtam180 - kirscheless at gmail.com
30+
*
31+
*/
32+
public class CursorRawResult implements Iterable<String> {
33+
34+
private String database;
35+
private transient InternalCursorDriver cursorDriver;
36+
private transient CursorEntity<JsonObject> entity;
37+
private transient int pos;
38+
private int count;
39+
private CursorIterator iter;
40+
41+
public CursorRawResult(String database, InternalCursorDriver cursorDriver, CursorEntity<JsonObject> entity,
42+
Class<?>... clazz) {
43+
this.database = database;
44+
this.cursorDriver = cursorDriver;
45+
this.entity = entity;
46+
this.count = entity == null ? 0 : entity.getCount();
47+
this.pos = 0;
48+
this.iter = null;
49+
}
50+
51+
@Override
52+
public Iterator<String> iterator() {
53+
if (iter == null) {
54+
iter = new CursorIterator();
55+
}
56+
57+
return iter;
58+
}
59+
60+
/**
61+
* Returns the objects as a list
62+
*
63+
* @return list of DocumentEntity objects
64+
*/
65+
public List<String> asList() {
66+
List<String> result = new ArrayList<String>();
67+
Iterator<String> iterator = iterator();
68+
69+
while (iterator.hasNext()) {
70+
result.add(iterator.next());
71+
}
72+
73+
return result;
74+
}
75+
76+
/**
77+
* Close cursor (removes cursor from database)
78+
*
79+
* @throws ArangoException
80+
*/
81+
public void close() throws ArangoException {
82+
long cursorId = entity.getCursorId();
83+
cursorDriver.finishQuery(database, cursorId);
84+
}
85+
86+
/**
87+
* Get total number of results (if requested)
88+
*
89+
* @return total number of results
90+
*/
91+
public int getCount() {
92+
return count;
93+
}
94+
95+
/**
96+
* Get total number of results for queries with LIMIT clause
97+
*
98+
* @return total number of results
99+
*/
100+
public int getFullCount() {
101+
return entity.getFullCount();
102+
}
103+
104+
/**
105+
* Return a single instance that matches the query, or null if the query
106+
* returns no results.
107+
*
108+
* Throws NonUniqueResultException (RuntimeException) if there is more than
109+
* one matching result
110+
*
111+
* @return the single result or null
112+
*/
113+
public String getUniqueResult() {
114+
return entity.getUniqueResult().toString();
115+
}
116+
117+
/**
118+
* read more values
119+
*
120+
* @throws ArangoException
121+
*/
122+
private void updateEntity() throws ArangoException {
123+
long cursorId = entity.getCursorId();
124+
this.entity = cursorDriver.continueQuery(database, cursorId, JsonObject.class);
125+
this.pos = 0;
126+
}
127+
128+
/**
129+
* internal iterator
130+
*/
131+
public class CursorIterator implements Iterator<String> {
132+
133+
@Override
134+
public boolean hasNext() {
135+
if (entity == null) {
136+
return false;
137+
}
138+
if (pos < entity.size()) {
139+
return true;
140+
}
141+
if (entity.hasMore()) {
142+
return true;
143+
}
144+
return false;
145+
}
146+
147+
@Override
148+
public String next() {
149+
if (hasNext()) {
150+
if (pos >= entity.size()) {
151+
try {
152+
updateEntity();
153+
} catch (ArangoException e) {
154+
throw new IllegalStateException(e);
155+
}
156+
}
157+
return entity.get(pos++).toString();
158+
}
159+
throw new NoSuchElementException();
160+
}
161+
162+
@Override
163+
public void remove() {
164+
throw new UnsupportedOperationException("remove is not supported!");
165+
}
166+
167+
}
168+
169+
/**
170+
* Returns true, if there are AQL warnings
171+
*
172+
* @return true, if there are AQL warnings
173+
*/
174+
public boolean hasWarning() {
175+
return entity.hasWarnings();
176+
}
177+
178+
/**
179+
* Returns a list of AQL warnings (code and message)
180+
*
181+
* @return list of AQL warnings
182+
*/
183+
public List<WarningEntity> getWarnings() {
184+
return entity.getWarnings();
185+
}
186+
187+
}

src/main/java/com/arangodb/InternalCursorDriver.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,19 @@ <T> CursorResult<T> executeAqlQuery(
4040
AqlQueryOptions aqlQueryOptions,
4141
Class<T> clazz) throws ArangoException;
4242

43+
// request a cursor without DocumentEntity
44+
CursorRawResult executeAqlQueryRaw(
45+
String database,
46+
String query,
47+
Map<String, Object> bindVars,
48+
AqlQueryOptions aqlQueryOptions) throws ArangoException;
49+
4350
// return the raw JSON response from server
4451
String executeAqlQueryJSON(
45-
String database,
46-
String query,
47-
Map<String, Object> bindVars,
48-
AqlQueryOptions aqlQueryOptions) throws ArangoException;
52+
String database,
53+
String query,
54+
Map<String, Object> bindVars,
55+
AqlQueryOptions aqlQueryOptions) throws ArangoException;
4956

5057
// request a cursor with DocumentEntity
5158
<T, S extends DocumentEntity<T>> DocumentCursorResult<T, S> executeBaseCursorQuery(

src/main/java/com/arangodb/impl/InternalCursorDriverImpl.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.arangodb.ArangoConfigure;
2323
import com.arangodb.ArangoException;
24+
import com.arangodb.CursorRawResult;
2425
import com.arangodb.CursorResult;
2526
import com.arangodb.CursorResultSet;
2627
import com.arangodb.DocumentCursorResult;
@@ -36,6 +37,7 @@
3637
import com.arangodb.util.AqlQueryOptions;
3738
import com.arangodb.util.MapBuilder;
3839
import com.arangodb.util.ShortestPathOptions;
40+
import com.google.gson.JsonObject;
3941

4042
/**
4143
* @author tamtam180 - kirscheless at gmail.com
@@ -56,13 +58,13 @@ public CursorEntity<?> validateQuery(String database, String query) throws Arang
5658

5759
@Override
5860
public String executeAqlQueryJSON(
59-
String database,
60-
String query,
61-
Map<String, Object> bindVars,
62-
AqlQueryOptions aqlQueryOptions) throws ArangoException {
61+
String database,
62+
String query,
63+
Map<String, Object> bindVars,
64+
AqlQueryOptions aqlQueryOptions) throws ArangoException {
6365

64-
return getJSONResponseText(getCursor(database, query, bindVars, aqlQueryOptions));
65-
}
66+
return getJSONResponseText(getCursor(database, query, bindVars, aqlQueryOptions));
67+
}
6668

6769
@SuppressWarnings("unchecked")
6870
@Override
@@ -146,6 +148,19 @@ public <T> CursorResult<T> executeAqlQuery(
146148
return new CursorResult<T>(database, this, entity, clazz);
147149
}
148150

151+
@Override
152+
public CursorRawResult executeAqlQueryRaw(
153+
String database,
154+
String query,
155+
Map<String, Object> bindVars,
156+
AqlQueryOptions aqlQueryOptions) throws ArangoException {
157+
158+
CursorEntity<JsonObject> entity = executeCursorEntityQuery(database, query, bindVars, aqlQueryOptions,
159+
JsonObject.class);
160+
161+
return new CursorRawResult(database, this, entity);
162+
}
163+
149164
@SuppressWarnings("unchecked")
150165
@Override
151166
public <V, E> ShortestPathEntity<V, E> getShortestPath(

src/test/java/com/arangodb/example/document/RawDocumentExample.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.arangodb.example.document;
1818

19+
import java.util.HashMap;
20+
import java.util.Iterator;
21+
1922
import org.json.JSONML;
2023
import org.json.JSONObject;
2124
import org.junit.Assert;
@@ -24,6 +27,7 @@
2427

2528
import com.arangodb.ArangoDriver;
2629
import com.arangodb.ArangoException;
30+
import com.arangodb.CursorRawResult;
2731
import com.arangodb.entity.DocumentEntity;
2832

2933
public class RawDocumentExample extends BaseExample {
@@ -141,6 +145,26 @@ public void ReadDocuments() {
141145
Assert.fail("Failed to read document. " + e.getMessage());
142146
}
143147

148+
//
149+
printHeadline("get query results");
150+
//
151+
152+
String queryString = "FOR t IN " + COLLECTION_NAME + " FILTER t.cook_time == \"3 hours\" RETURN t";
153+
System.out.println(queryString);
154+
HashMap<String, Object> bindVars = new HashMap<String, Object>();
155+
156+
try {
157+
CursorRawResult cursor = arangoDriver.executeAqlQueryRaw(queryString, bindVars, null);
158+
Assert.assertNotNull(cursor);
159+
Iterator<String> iter = cursor.iterator();
160+
while (iter.hasNext()) {
161+
JSONObject jsonObject2 = new JSONObject(iter.next());
162+
System.out.println("XML value: " + JSONML.toString(jsonObject2));
163+
}
164+
} catch (ArangoException e) {
165+
Assert.fail("Failed to query documents. " + e.getMessage());
166+
}
167+
144168
}
145169

146170
}

0 commit comments

Comments
 (0)