How to correctly define SelectArg in a raw query?

I need to implement CASE in the WHERE clause of my query. I wrote the following code:

QueryBuilder<Cars, String> carsQB = carsDao.queryBuilder(); carsQB.selectColumns("id"); carsQB.join(modelsDao.queryBuilder()); carsQB.where().raw("CASE WHEN future = true THEN date > ? ELSE endDate > ? END", new SelectArg("date"), new SelectArg("endDate")); 

But I get the following error:

 E/AndroidRuntime(4599): Caused by: java.lang.IllegalArgumentException: Either the column name or SqlType must be set on each argument 

Both dates and endDate are Date columns in my database.

What am I missing? Is there a better way to do this? Thanks!

+4
source share
1 answer

The problem is that (unfortunately) calling new SelectArg(String) calls the SelectArg(Object) constructor , which sets the value, not the column name. You must define the arguments as follows:

 new SelectArg("date", null); 

To go back, javadocs for Where.raw() :

args - Optional arguments that match any? specified in rawStatement. Each of the arguments must have either the corresponding columnName or a set of sql types.

You can also use SqlType constructors. I improved the Javadocs documentation on SelectArg to help with this. See this change on github .

Also, as I said before, I think you want to do something like:

 SelectArg dateArg = new SelectArg("date", null); SelectArg endDateArg = new SelectArg("endDate", null); carsQB.where().raw("CASE WHEN future = true THEN date > ? ELSE endDate > ? END", dateArg, endDateArg); ... dateArg.setValue(...); endDateArg.setValue(...); 
+6
source

Source: https://habr.com/ru/post/1469266/


All Articles