神刀安全网

jOOQ 4.0’s New API Will Use Annotations Only for Truly Declarative Java/SQL Programming

This post was originally published over at  jooq.org , a blog focusing on all things open source, Java and software development from the perspective of  jOOQ .

SQL is the only really popular and mature 4GL (Fourth Generation Programming Language) . I.e. it is the only popular declarative language.

At the same time, SQL has proven that turing completeness is not reserved to lesser languages like C, C++, or Java. Since SQL:1999 and its hierarchical common table expressions, SQL can be safely considered “turing complete” . This means that any program can be written in SQL. Don’t believe it? Take, for instance, this SQL Mandelbrot set calculation as can be seen in this Stack Overflow question .

jOOQ 4.0’s New API Will Use Annotations Only for Truly Declarative Java/SQL Programming

Source: User Elie on http://stackoverflow.com/q/314864/521799

Wonderful! No more need for procedural, and object oriented cruft.

How we’ve been wrong so far…

At Data Geekery ( the company behind jOOQ ), we love SQL. And we love Java. But one thing has always bothered us in the past. Java is not really a purely declarative language. A lot of Java language constructs are real anti patterns for the enlightened declarative programmer. For instance:

// This is bad for(String string : strings)     System.out.println(string);  // This is even worse try{     someSQLStatements(); } catch(SQLException e) {     someRecovery(); } 

The imperative style of the above code is hardly ever useful. Programmers need to tediously tell the Java compiler and the JVM what algorithm they meant to implement, down to the single statement, when in reality, using the JIT and other advanced optimization techniques, they don’t really have to.

Luckily, there are annotations

Since Java 5, however, there have been farsighted people in expert groups who have added a powerful new concept to the Java language: Annotations ( more info here ). At first, experiments were made with only a handful of limited-use annotations, like:

  • @Override
  • @SuppressWarnings

But then, even more farsighted people have then proceeded in combining these annotations to form completely declaratively things like a component :

@Path("/MonsterRest") @Stateless @WebServlet(urlPatterns ="/MonsterServlet") @Entity @Table(name ="MonsterEntity") @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @NamedQuery(name ="findAll", query ="SELECT c FROM Book c") publicclassBookextendsHttpServlet {       // ======================================     // =             Attributes             =     // ======================================       @Id     @GeneratedValue     privateLong id;     privateString isbn;     privateInteger nbOfPage;     privateBoolean illustrations;     privateString contentLanguage;     @Column(nullable =false)     @Size(min =5, max =50)     @XmlElement(nillable =false)     privateString title;     privateFloat price;     @Column(length =2000)     @Size(max =2000)     privateString description;     @ElementCollection     @CollectionTable(name ="tags")     privateList<String> tags =newArrayList<>(); 

Look at this beauty. Credits to Antonio Goncalves

However, we still think that there is a lot of unnecessary object oriented bloat in the above. Luckily, recent innovations that make Java annotations turing complete ( or even sentient? ) will now finally allow us to improve upon this situation, specifically for jOOQ, which aims to model the declarative SQL language in Java. Finally, annotations are a perfect fit!

Those innovations are:

These innovations allow us to completely re-implement the entire jOOQ 4.0 API in order to allow for users writing SQL as follows:

@Select({     @Column("FIRST_NAME"),     @Column("LAST_NAME") }) @From(     table =@Table("AUTHOR"),     join =@Join("BOOK"),     predicate =@On(         left =@Column("AUTHOR.ID"),         op =@Eq,         right =@Column("BOOK.AUTHOR_ID")     ) ) @Where(     predicate =@Predicate(         left =@Column("BOOK.TITLE"),         op =@Like,         right =@Value("%Annotations in a Nutshell%")     ) ) classSQLStatement {} 

Just like JPA, this makes jOOQ now fully transparent and declarative, by using annotations. Developers will now be able to completely effortlessly translate their medium to highly complex SQL queries into the exact equivalent in jOOQ annotations.

Don’t worry, we’ll provide migration scripts to upgrade your legacy jOOQ 3.x application to 4.0. A working prototype is on the way and is expected to be released soon, early adopter feedback is very welcome, so stay tuned for more exciting SQL goodness!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » jOOQ 4.0’s New API Will Use Annotations Only for Truly Declarative Java/SQL Programming

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮