跨多个表的ORMLite或Query

时间:2022-10-21 20:58:48

I am trying to query a table based on criteria from a join table using ORMLite.

我试图使用ORMLite根据连接表中的条件查询表。

Here is how I would express the query I am trying to write in tsql:

这是我将如何表达我试图在tsql中编写的查询:

select * from media m inner join file f on m.fileId = f.fileId
where m.isNew = 1 OR f.isNew = 1

The result should be a list of media records where either the media record or the corresponding file record has isNew = 1.

结果应该是媒体记录列表,其中媒体记录或相应的文件记录具有isNew = 1。

I have read through the documentation on using OR in ORMLite, but all of the examples use a single table, not joining. Likewise I have read the documentation on joins, but none of the examples include a where clause that spans both tables. Is there any way besides a raw query to do this?

我已阅读有关在ORMLite中使用OR的文档,但所有示例都使用单个表,而不是连接。同样,我已经阅读了有关连接的文档,但是没有一个示例包含跨越两个表的where子句。除了原始查询之外还有什么方法可以做到这一点吗?

I had a look at this question: https://*.com/a/12629645/874782 and it seems to ask the same thing, but the accepted answer produces an AND query, not an OR. Here is my code that I used to test that theory:

我看了一下这个问题:https://*.com/a/12629645/874782似乎问了同样的问题,但是接受的答案产生了一个AND查询,而不是OR。这是我用来测试该理论的代码:

public List<Media> getNewMedia() {
    Session session = getSession();
    Account account = session.getSelectedAccount();
    ContentGroup contentGroup = account.getSelectedContentGroup();

    List<Media> results = null;

    try {

        QueryBuilder<Category, Integer> categoryQueryBuilder = getHelper().getCategoryDao().queryBuilder();
        categoryQueryBuilder.where().eq("group_id", contentGroup.getId());

        QueryBuilder<MediaCategory, Integer> mediaCatQb = getHelper().getMediaCategoryDao().queryBuilder();
        mediaCatQb = mediaCatQb.join(categoryQueryBuilder);

        QueryBuilder<FileRecord, Integer> fileQueryBuilder = getHelper().getFileDao().queryBuilder();
        fileQueryBuilder.where().ge("lastUpdated", contentGroup.getLastDownload());

        QueryBuilder<Media, Integer> mediaQb = getHelper().getMediaDao().queryBuilder();
        mediaQb.where().eq("seen", false);
        // join with the media query
        results = mediaQb.join(fileQueryBuilder).join(mediaCatQb).query();

    } catch (SQLException e) {
        Log.e(TAG, "Sql Exception", e);
    }

    return results;
}

For the sake of completion, this is querying for a slightly more complex example than the one I gave above, this one expressed in tsql would be

为了完成,这是查询一个比我上面给出的更复杂的例子,这个在tsql中表示将是

select * from Media m join FileRecord f on m.fileRecordId = f.fileRecordId
where m.seen = false OR f.lastUpdated >= lastUpdateDate

When I run it, it is actually doing an AND query, which is what I would expect based on two joins with independent where clauses.

当我运行它时,它实际上正在进行AND查询,这是我期望的基于两个连接的独立where子句。

I think the key issue is that a where clause is inherently tied to a table, because it is performed on a QueryBuilder object which comes from a Dao that is specific to that table. How can I get around this?

我认为关键问题是where子句固有地绑定到表,因为它是在一个QueryBuilder对象上执行的,该对象来自特定于该表的Dao。我怎么能绕过这个?

1 个解决方案

#1


0  

I think what you are looking for is joinOr search for it in the ORMLite docs.

我认为您正在寻找的是joinOr在ORMLite文档中搜索它。

Like join(QueryBuilder) but this combines the WHERE statements of two query builders with a SQL "OR".

与join(QueryBuilder)类似,但它将两个查询构建器的WHERE语句与SQL“OR”组合在一起。

#1


0  

I think what you are looking for is joinOr search for it in the ORMLite docs.

我认为您正在寻找的是joinOr在ORMLite文档中搜索它。

Like join(QueryBuilder) but this combines the WHERE statements of two query builders with a SQL "OR".

与join(QueryBuilder)类似,但它将两个查询构建器的WHERE语句与SQL“OR”组合在一起。