use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class AdministrationDao method countExampleQueryNodes.
/**
* Maps example queries to integer, which represents the amount of nodes of
* the aql query.
*/
private void countExampleQueryNodes(List<ExampleQuery> exampleQueries) {
for (ExampleQuery eQ : exampleQueries) {
QueryData query = getQueryDao().parseAQL(eQ.getExampleQuery(), null);
int count = 0;
for (List<QueryNode> qNodes : query.getAlternatives()) {
count += qNodes.size();
}
eQ.setNodes(count);
}
}
use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class TestAbstractSqlGenerator method shouldJoinMultipleFromClausesWithComma.
/**
* FROM clauses from multiple FromClauseSqlGenrators are separated by ",".
*/
@Test
public void shouldJoinMultipleFromClausesWithComma() {
// given
String selectClause = uniqueString();
String fromClause = uniqueString();
setupSelectAndFromClause(selectClause, fromClause);
String fromClause2 = uniqueString();
@SuppressWarnings("unchecked") FromClauseSqlGenerator<QueryData> fromClauseSqlGenerator2 = mock(FromClauseSqlGenerator.class);
given(fromClauseSqlGenerator2.fromClause(eq(queryData), eq(alternative), anyString())).willReturn(fromClause2);
fromClauseSqlGenerators.add(fromClauseSqlGenerator2);
// when
String sql = generator.toSql(queryData);
// then
String expected = createMinimalSqlStatement(selectClause, fromClause);
// strip last newline
expected = expected.substring(0, expected.length() - 1);
expected += ",\n" + " " + fromClause2 + "\n";
assertThat(sql, is(expected));
}
use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class TestAbstractSqlGenerator method shouldAppendGroupByClause.
/**
* Append GROUP BY clause.
*/
@Test
public void shouldAppendGroupByClause() {
// given
String selectClause = uniqueString();
String fromClause = uniqueString();
setupSelectAndFromClause(selectClause, fromClause);
String groupBy = uniqueString();
@SuppressWarnings("unchecked") GroupByClauseSqlGenerator<QueryData> groupByClauseSqlGenerator = mock(GroupByClauseSqlGenerator.class);
generator.setGroupByClauseSqlGenerator(groupByClauseSqlGenerator);
given(groupByClauseSqlGenerator.groupByAttributes(eq(queryData), eq(alternative))).willReturn(groupBy);
// when
String sql = generator.toSql(queryData);
// then
String expected = createMinimalSqlStatement(selectClause, fromClause);
expected += "GROUP BY " + groupBy + "\n";
assertThat(sql, is(expected));
}
use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class TestAbstractSqlGenerator method shouldAppendWithClause.
@Test
public void shouldAppendWithClause() {
LinkedList<String> clauses = new LinkedList<>();
clauses.add("A as (SELECT 1)");
clauses.add("B as (SELECT 2)");
clauses.add("C as (SELECT 3)");
when(withClauseSqlGenerator.withClauses(any(QueryData.class), anyListOf(QueryNode.class), anyString())).thenReturn(clauses);
String sql = generator.toSql(queryData);
String expected = "WITH\n" + "A as (SELECT 1),\n" + "B as (SELECT 2),\n" + "C as (SELECT 3)\n" + "SELECT null\nFROM\n \n";
assertThat(sql, is(expected));
}
use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class QueryServiceImpl method find.
@GET
@Path("search/find")
@Produces({ "application/xml", "text/plain" })
@Override
public Response find(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames, @DefaultValue("0") @QueryParam("offset") String offsetRaw, @DefaultValue("-1") @QueryParam("limit") String limitRaw, @DefaultValue("ascending") @QueryParam("order") String orderRaw) throws IOException {
requiredParameter(query, "q", "AnnisQL query");
requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");
Subject user = SecurityUtils.getSubject();
List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
for (String c : corpusNames) {
user.checkPermission("query:find:" + c);
}
int offset = Integer.parseInt(offsetRaw);
int limit = Integer.parseInt(limitRaw);
OrderType order;
try {
order = OrderType.valueOf(orderRaw.toLowerCase());
} catch (IllegalArgumentException ex) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN).entity("parameter 'order' has the invalid value '" + orderRaw + "'. It should be one of" + " 'ascending', 'random' or 'descending").build());
}
final QueryData data = queryDataFromParameters(query, rawCorpusNames);
data.setCorpusConfiguration(queryDao.getCorpusConfiguration());
data.addExtension(new LimitOffsetQueryData(offset, limit, order));
String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
if (acceptHeader == null || acceptHeader.trim().isEmpty()) {
acceptHeader = "*/*";
}
List<String> knownTypes = Lists.newArrayList("text/plain", "application/xml");
// find the best matching mime type
String bestMediaTypeMatch = MIMEParse.bestMatch(knownTypes, acceptHeader);
if ("text/plain".equals(bestMediaTypeMatch)) {
return Response.ok(findRaw(data, rawCorpusNames, query), "text/plain").build();
} else {
List<Match> result = findXml(data, rawCorpusNames, query);
return Response.ok().type("application/xml").entity(new GenericEntity<MatchGroup>(new MatchGroup(result)) {
}).build();
}
}
Aggregations