简体   繁体   English

如何将此SQL查询写入jOOQ?

[英]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

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 每个SQL函数都转换为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() 可通过“中缀”符号访问算术运算,但是运算符必须以方法名称的形式编写,例如* = 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) jOOQ API必须明确地包装算术运算左侧的数字,使用val() (用于绑定变量)或inline() (用于内联)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM