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
1 Answer
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) orinline()(for inlining)
Query query = create.query("SELECT ...")like so? There should be helpful info at vendor site of JOOQ - or do I mix this up with something else?