java - jOOQ - How to create a SQL query with WHERE having multiple conditions -


i want create query should work one:

select    sensor_id,   measurement_time,   measurement_value    public.measurement_pm2_5   (sensor_id = 1 , measurement_time between to_timestamp(123) , to_timestamp(100000))   or (sensor_id = 1 , measurement_time between to_timestamp(500000) , to_timestamp(99999999999))   or (sensor_id = 49 , measurement_time between to_timestamp(555) , to_timestamp(556))   or (sensor_id = 9 , measurement_time between to_timestamp(7654) , to_timestamp(999999299347))   or (sensor_id = 44 , measurement_time between to_timestamp(4252) , to_timestamp(999949999348))   or (sensor_id = 60 , measurement_time between to_timestamp(63452) , to_timestamp(999998999349))   ; 

the number of ors in query may vary.

is possible build such query using jooq type safe api or have create manually using plain sql?

i know if there wouldn't additional statement measurement_time - different every sensor_id - this:

set<integer> sensorids = new hashset<>(); sensorids.add(1); sensorids.add(49); sensorids.add(9); sensorids.add(44); sensorids.add(60); timestamp starttime = new timestamp(123l); timestamp endtime = new timestamp(999999999999l); try(java.sql.connection conn = connection.hikaridatasource.getconnection()) {     system.out.println("sql = " + dsl.using(conn).select()         .from(measurement_pm2_5)         .where(measurement_pm2_5.sensor_id.in(sensorids))         .and(measurement_pm2_5.measurement_time.between(starttime, endtime))         .getsql()); } catch (sqlexception e) {     e.printstacktrace(); } 

but unfortunately have different timestamps different sensor_ids.

private class timerange {     timestamp starttime;     timestamp endtime;      timestamp getstarttime() {         return starttime;     }      timestamp getendtime() {         return endtime;     } } ... map<integer, list<timerange>> sensoridswithtimerange 

it doable jooq.

you want create suitable condition , plug query.

the question how build condition... , there many ways build one.

for instance:

final condition c1 = somefield.eq(someotherfield); // or final condition c1 = somefield.lessthan(somevalue); 

the way obtain field reference depend on setup.

possibilites endless; , if have 2 conditions c1 , c2, build condition c, can:

final condition c = c1.and(c2); // or c1.or(c2) 

and plug condition c final query.

if use jooq, chances have generated metadata code already, , if not, jdbc url alone, jooq can infer table , column names you.


Comments