java - CQEngine In 子句与 MultiValueNullableAttribute

标签 java sql cqengine

我有一个类 Object1,它有一个称为标签的长整型列表。我有另一个名为tagsToSearch 的多头列表。如何使用 CQEngine 构造如下查询:

Select * from Object1 Where tags in (tagsToSearch)

如果有人知道使用 CQEngine 会是什么样子,请告诉我。

最佳答案

这应该可以解决问题:

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
    }
}

关于java - CQEngine In 子句与 MultiValueNullableAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459357/

相关文章:

java - CQEngine可以查询另一个对象中的一个对象吗

java - 如何使用 NavigableIndex 从 cqengine IndexedCollection 获取第一个或最后一个项目

java - cqengine IndexedCollection 添加导致空指针

java - C++ 与 Java 中的名称隐藏

java - LeetCode正则表达式问题中重叠子问题的可视化以及DP的使用

sql - select语句是否可能无法识别早期查询中存在的行?

sql - SQL 查询中过多的空格会影响性能吗?

java - 返回在响应的特定条件下首先执行的 future

java - 使用鼠标单击 JComponent 时出现问题

SQL 返回表的前两列