OrmLite:分组加入条款

时间:2016-03-17 12:37:17

标签: java android sqlite ormlite

我在Android应用程序中使用ORMLite,它使用数据库来创建/更新/删除对象。 我有以下表结构:

餐酒

  • 国家(自己的桌子/道)
  • 酒庄(自己的桌子/道)
  • 注释
  • PurchasePlace
  • 更多列,与此
  • 无关

我在做什么(只有关键部分):

用语言: 我想展示所有葡萄酒,其中某些字段与用户输入相匹配,但是尊重葡萄酒类型,它与常量相关。

准备加入查询生成器:

queryBuilderWinery.where().like("name", "%Value%");
queryBuilderCountry.where().like("name", "%Value%");

queryBuilderWine
.join(queryBuilderWinery)
.join(queryBuilderCountry)
.where().like("name", "%Value%")
.or().like("purchasePlace", "%Value%").and().eq("wine_type_id", 1)

生产:

FROM wine
INNER JOIN winery
   ON wine.winery_id = winery.id
INNER JOIN country
   ON wine.country_id = country.id
WHERE ((wine.name LIKE '%in%' OR wine.comment2 LIKE '%in%') AND wine.wine_type_id = 1)
OR (winery.winery_name LIKE '%in%')
OR (country.country_name LIKE '%in%')

需要的Where子句:

WHERE (wine.comment like '%Value%' OR 
        wine.purchasePlace like '%Value%' OR 
        winery.name like '%Value%' OR 
        country.name like '%Value%') 
    AND wine.wine_type_id = 1

or 

WHERE  wine.wine_type_id = 1
    AND (wine.comment like '%Value%' OR 
        wine.purchasePlace like '%Value%' OR 
        winery.name like '%Value%' OR 
        country.name like '%Value%') 

我的问题是,这可以吗?我已经阅读了关于此的文档,我知道可以使用RPN(反向波兰表示法)或使用组创建where子句,但我不确定如何将组合在哪里 正确的条款,因为所有连接也将有一个OR子句,我需要单独分组的AND子句。 如果我会使用Where Grouping,这对Join-Conditions也有影响吗?

任何帮助表示感谢。

更新 我尝试了一个原始的Where子句,但同样的问题在这里,我无法想象一种方式,我可以将WhereClause分组到多个表...

WineDAO wineDAO = new DatabaseHelperProvider().get(this).getWineDAO();
CountryDAO countryDAO = new DatabaseHelperProvider().get(this).getCountryDAO();
WineryDAO wineryDAO = new DatabaseHelperProvider().get(this).getWineryDAO();

QueryBuilder<Wine, Long> wineQueryBuilder = wineDAO.queryBuilder();
wineQueryBuilder = wineQueryBuilder.join(countryDAO.queryBuilder()).join(wineryDAO.queryBuilder());

Where<Wine, Long> where = wineQueryBuilder.where().raw("(winery.name = ? OR country.name = ?) AND wine_type_id = ?",
        new SelectArg("winery.name", "in"),
        new SelectArg("country.name", "in"),
        new SelectArg("wine_type_id", 1));

String prepStatement = wineQueryBuilder.prepareStatementString();
List<Wine> query = where.query();
int i = query.size();

这不起作用,因为列winery.name显然不包含在表wine中...

0 个答案:

没有答案