Search in sources :

Example 1 with StreamableRowCallback

use of com.infiniteautomation.mango.db.query.StreamableRowCallback 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)

Aggregations

StreamableRowCallback (com.infiniteautomation.mango.db.query.StreamableRowCallback)1 User (com.serotonin.m2m2.vo.User)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ASTNode (net.jazdw.rql.parser.ASTNode)1 RQLParser (net.jazdw.rql.parser.RQLParser)1 Test (org.junit.Test)1