Search in sources :

Example 1 with RQLParser

use of net.jazdw.rql.parser.RQLParser in project mica2 by obiba.

the class RQLCriteriaOpalConverter method parse.

public void parse(String rqlQuery) {
    RQLParser parser = new RQLParser();
    ASTNode queryNode = parser.parse(rqlQuery);
    parseNode(queryNode);
}
Also used : RQLParser(net.jazdw.rql.parser.RQLParser) ASTNode(net.jazdw.rql.parser.ASTNode)

Example 2 with RQLParser

use of net.jazdw.rql.parser.RQLParser in project ma-core-public by infiniteautomation.

the class DataPointEventsByDataPointRQLQueryDefinition method validateImpl.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.module.ModuleQueryDefinition#validateImpl(com.fasterxml.jackson.databind.JsonNode)
     */
@Override
protected void validateImpl(final User user, final JsonNode parameters, final RestValidationResult result) {
    if (parameters.get("rql") == null)
        result.addRequiredError("rql");
    else {
        try {
            JsonNode rqlNode = parameters.get("rql");
            ObjectReader reader = Common.objectMapper.getObjectReader(String.class);
            String rql = reader.readValue(rqlNode);
            if (rql != null && !rql.isEmpty()) {
                RQLParser parser = new RQLParser();
                parser.parse(rql);
            }
        } catch (IOException | RQLParserException | IllegalArgumentException e) {
            result.addInvalidValueError("rql");
        }
    }
}
Also used : RQLParser(net.jazdw.rql.parser.RQLParser) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) RQLParserException(net.jazdw.rql.parser.RQLParserException) IOException(java.io.IOException)

Example 3 with RQLParser

use of net.jazdw.rql.parser.RQLParser in project ma-modules-public by infiniteautomation.

the class WatchlistSqlVisitorTest method testRQL.

@Test
public void testRQL() throws IOException {
    // Create a User
    User user = new User();
    user.setUsername("test");
    user.setName("test");
    user.setEmail("test@test.com");
    user.setPassword("usernametest");
    user.setPermissions("user,test,permission1");
    validate(user);
    UserDao.instance.saveUser(user);
    // Insert some watchlists
    for (int i = 0; i < 120; i++) {
        WatchListVO wl = new WatchListVO();
        wl.setXid(WatchListDao.instance.generateUniqueXid());
        wl.setName("Watchilst " + i);
        wl.setUserId(user.getId());
        wl.setReadPermission("permission1");
        WatchListDao.instance.saveWatchList(wl);
    }
    String rql = "limit(100,0)";
    RQLParser parser = new RQLParser();
    ASTNode root = null;
    ASTNode queryNode = parser.parse(rql);
    // Combine the existing query with an AND node
    if (queryNode == null) {
        root = new ASTNode("eq", "userId", user.getId());
    } else {
        // Filter by Permissions
        Set<String> permissions = Permissions.explodePermissionGroups(user.getPermissions());
        ASTNode permRQL = new ASTNode("in", "readPermission", permissions);
        root = new ASTNode("or", new ASTNode("eq", "userId", user.getId()), permRQL, queryNode);
    }
    final AtomicLong selectCounter = new AtomicLong();
    final AtomicLong countValue = new AtomicLong();
    StreamableRowCallback<WatchListVO> selectCallback = new StreamableRowCallback<WatchListVO>() {

        @Override
        public void row(WatchListVO row, int index) throws Exception {
            selectCounter.incrementAndGet();
        }
    };
    StreamableRowCallback<Long> countCallback = new StreamableRowCallback<Long>() {

        @Override
        public void row(Long row, int index) throws Exception {
            countValue.set(row);
        }
    };
    StreamableSqlQuery<WatchListVO> query = WatchListDao.instance.createQuery(root, selectCallback, countCallback, modelMap, appenders, true);
    query.query();
    query.count();
    assertEquals(100L, selectCounter.get());
    assertEquals(120L, countValue.get());
}
Also used : User(com.serotonin.m2m2.vo.User) AtomicLong(java.util.concurrent.atomic.AtomicLong) RQLParser(net.jazdw.rql.parser.RQLParser) ASTNode(net.jazdw.rql.parser.ASTNode) AtomicLong(java.util.concurrent.atomic.AtomicLong) StreamableRowCallback(com.infiniteautomation.mango.db.query.StreamableRowCallback) Test(org.junit.Test)

Example 4 with RQLParser

use of net.jazdw.rql.parser.RQLParser in project ma-core-public by infiniteautomation.

the class DataPointEventsByDataPointRQLQueryDefinition method createQuery.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.module.ModuleQueryDefinition#createQuery(com.fasterxml.jackson.databind.JsonNode)
     */
@Override
public ASTNode createQuery(User user, JsonNode parameters) throws IOException {
    JsonNode rqlNode = parameters.get("rql");
    ObjectReader reader = Common.objectMapper.getObjectReader(String.class);
    String rql = reader.readValue(rqlNode);
    ASTNode rqlAstNode;
    if (rql == null || rql.isEmpty()) {
        rqlAstNode = new ASTNode("limit", AbstractBasicDao.DEFAULT_LIMIT);
    }
    RQLParser parser = new RQLParser();
    try {
        rqlAstNode = parser.parse(rql);
    } catch (RQLParserException | IllegalArgumentException e) {
        throw new IOException(e.getMessage());
    }
    // Lookup data points by tag
    List<Object> args = new ArrayList<>();
    args.add("typeRef1");
    DataPointDao.instance.rqlQuery(rqlAstNode, new MappedRowCallback<DataPointVO>() {

        @Override
        public void row(DataPointVO dp, int index) {
            if (Permissions.hasDataPointReadPermission(user, dp)) {
                args.add(Integer.toString(dp.getId()));
            }
        }
    });
    // Create Event Query for these Points
    if (args.size() > 0) {
        ASTNode query = new ASTNode("in", args);
        query = addAndRestriction(query, new ASTNode("eq", "userId", user.getId()));
        query = addAndRestriction(query, new ASTNode("eq", "typeName", "DATA_POINT"));
        return query;
    } else {
        return new ASTNode("limit", 0, 0);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ASTNode(net.jazdw.rql.parser.ASTNode) RQLParser(net.jazdw.rql.parser.RQLParser) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) RQLParserException(net.jazdw.rql.parser.RQLParserException)

Aggregations

RQLParser (net.jazdw.rql.parser.RQLParser)4 ASTNode (net.jazdw.rql.parser.ASTNode)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)2 IOException (java.io.IOException)2 RQLParserException (net.jazdw.rql.parser.RQLParserException)2 StreamableRowCallback (com.infiniteautomation.mango.db.query.StreamableRowCallback)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 User (com.serotonin.m2m2.vo.User)1 ArrayList (java.util.ArrayList)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.Test)1