2

I have a class Object1 which has a list of longs called tags. I have another list of longs called tagsToSearch. How can I construct a query using CQEngine that is the following:

Select * from Object1 Where tags in (tagsToSearch)

If anyone knows how this would look using CQEngine please let me know.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nath5
  • 1,665
  • 5
  • 28
  • 46

1 Answers1

0

This should do the trick:

package com.googlecode.cqengine;
import com.googlecode.cqengine.attribute.*;
import com.googlecode.cqengine.query.Query;
import com.googlecode.cqengine.query.option.QueryOptions;
import com.googlecode.cqengine.query.parser.sql.SQLParser;
import java.util.*;

import static com.googlecode.cqengine.codegen.AttributeBytecodeGenerator.*;
import static com.googlecode.cqengine.query.QueryFactory.*;
import static java.util.Arrays.asList;

public class TagsExample {

    static class MyObject {
        final String name;
        final List<Long> tags;

        public MyObject(String name, List<Long> tags) {
            this.name = name;
            this.tags = tags;
        }

        static final Attribute<MyObject, Long> TAGS = new MultiValueAttribute<MyObject, Long>("tags") {
            public Iterable<Long> getValues(MyObject object, QueryOptions queryOptions) { return object.tags; }
        };
    }

    public static void main(String[] args) {
        IndexedCollection<MyObject> collection = new ConcurrentIndexedCollection<>();
        collection.add(new MyObject("foo", asList(1L, 2L, 3L)));
        collection.add(new MyObject("bar", asList(4L, 5L, 6L)));
        collection.add(new MyObject("baz", asList(7L, 8L, 9L)));

        // Search via a programmatic query...
        Query<MyObject> nativeQuery = in(MyObject.TAGS, asList(3L, 9L));
        collection.retrieve(nativeQuery)
                .forEach(object -> System.out.println(object.name));
        // ..prints: foo, baz


        // Search via an SQL query...
        String sqlQuery = "SELECT * FROM collection WHERE tags IN (3, 9)";
        SQLParser<MyObject> parser = SQLParser.forPojoWithAttributes(MyObject.class, createAttributes(MyObject.class));
        parser.retrieve(collection, sqlQuery)
                .forEach(object -> System.out.println(object.name));
        // ..prints: foo, baz
    }
}
npgall
  • 2,979
  • 1
  • 24
  • 24
  • I will give that a shot, thanks for the quick reply. Is there anyway to represent that as a normal SQL query. I have a bunch of other queries stored as SQL and I can make and exception for this one but if I don't have to that would be better. – Nath5 Sep 13 '16 at 13:43
  • Yes, no problem. I've updated the answer to show how to do in SQL too. – npgall Sep 13 '16 at 21:02
  • CQEngine in scala have some issue but worked well in Java, in scala it's unable to pick where condition column (java.lang.IllegalStateException: No such attribute has been registered with the parser: empid) – sri hari kali charan Tummala Mar 19 '20 at 01:46