user6053979
user6053979

Reputation: 1

How to write this SQL query to jOOQ?

SELECT 
    feature_name, 
    ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) 
      * cos( radians( long ) - radians(-122) ) + sin( radians(37) ) 
      * sin( radians( lat ) ) ) ) AS distance 
FROM 
    geo_features 
HAVING 
    distance < 25 
ORDER BY 
    distance

Upvotes: 0

Views: 178

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220762

Assuming this static import:

import static org.jooq.impl.DSL.*;

Here's how to write your query:

GeoFeatures f = Tables.GEO_FEATURES;
Field<Double> distance =
    val(3959.0).mul(acos(cos(rad(37.0)))).mul(cos(rad(f.LAT)))
               .mul(rad(f.LONG).sub(rad(-122.0)).add(sin(rad(37.0)))
               .mul(sin(rad(f.LAT))).as("distance")

DSL.using(configuration)
   .select(
       f.FEATURE_NAME,
       distance
   )
   .from(f)
   .having(distance.lt(25.0))
   .orderBy(distance)

These are key things to keep in mind:

  • Every SQL function translates to a function in org.jooq.impl.DSL
  • Arithmetic operations are accessible via "infix" notation, but the operators have to be written as method names, such as * = mul(), + = add() or plus(), - = sub() or minus()
  • Numbers that are on the left hand side of an arithmetic operation have to be wrapped by jOOQ API explicitly, using val() (for bind variables) or inline() (for inlining)

Upvotes: 1

Related Questions