Search in sources :

Example 11 with QueryData

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);
    }
}
Also used : QueryData(annis.ql.parser.QueryData) QueryNode(annis.model.QueryNode) ExampleQuery(annis.examplequeries.ExampleQuery)

Example 12 with QueryData

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));
}
Also used : QueryData(annis.ql.parser.QueryData) TestUtils.uniqueString(annis.test.TestUtils.uniqueString) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 13 with QueryData

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));
}
Also used : QueryData(annis.ql.parser.QueryData) TestUtils.uniqueString(annis.test.TestUtils.uniqueString) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 14 with QueryData

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));
}
Also used : QueryData(annis.ql.parser.QueryData) QueryNode(annis.model.QueryNode) TestUtils.uniqueString(annis.test.TestUtils.uniqueString) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 15 with QueryData

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();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatrixQueryData(annis.sqlgen.MatrixQueryData) Subject(org.apache.shiro.subject.Subject) Match(annis.service.objects.Match) OrderType(annis.service.objects.OrderType) GenericEntity(javax.ws.rs.core.GenericEntity) MatchGroup(annis.service.objects.MatchGroup) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

QueryData (annis.ql.parser.QueryData)20 AnnotateQueryData (annis.sqlgen.extensions.AnnotateQueryData)13 LimitOffsetQueryData (annis.sqlgen.extensions.LimitOffsetQueryData)13 MatrixQueryData (annis.sqlgen.MatrixQueryData)6 Date (java.util.Date)6 Subject (org.apache.shiro.subject.Subject)6 TestUtils.uniqueString (annis.test.TestUtils.uniqueString)5 GET (javax.ws.rs.GET)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 Test (org.junit.Test)5 Matchers.anyString (org.mockito.Matchers.anyString)5 QueryNode (annis.model.QueryNode)4 Match (annis.service.objects.Match)4 LinkedList (java.util.LinkedList)3 SaltProject (org.corpus_tools.salt.common.SaltProject)3 ExampleQuery (annis.examplequeries.ExampleQuery)2 MatchGroup (annis.service.objects.MatchGroup)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2