SQL in Scala, where jOOQ could go
I have recently blogged about how simple it is to integrate jOOQ into Scala. See the full blog post here:
I’m more and more thrilled by that option, as Scala is one of the fastest emerging JVM languages nowadays. The plain integration of a Java library in Scala leaves some open questions. jOOQ knows the “val()” operator or method, to create bind values. See the manual here:
This operator cannot be used in Scala, as Scala declares “val” as a reserved word. I’ve had similar issues in Java before, when trying to use “case” or “else” in the API, which is not possible either. The path of least resistance is to overload or re-name those methods. “val” was overloaded as “value” in jOOQ 2.0.1. “case” and “else” were re-named a long time ago as “decode” (from Oracle’s DECODE function), and “otherwise” (as in XSL).
So for a full-fledged integration in Scala, jOOQ should be wrapped in a new API called scOOQ ;-). This new API should take Scala’s language features into account in order to make working with jOOQ a lot easier. This could be the chance to re-engineer some of the API and make all API methods uppercase, as is usual with SQL. With Scala’s ability of omitting syntax elements, such as “.” and “()”, the API could then declare one-word methods, such as in this Java example:
MERGE().INTO(MY_TABLE) .USING(SOURCE_TABLE) .ON(MY_TABLE.ID.equal(SOURCE_TABLE.ID)) .WHEN().MATCHED().THEN().UPDATE() .SET(ID, 1) .SET(DATA, "Data") .WHEN().NOT().MATCHED().THEN().INSERT(MY_TABLE.ID, MY_TABLE.DATA) .VALUES(1, "Data")
While in Java, this looks quite nasty and verbose, in Scala it could be very nice! The below statement should compile in Scala if the API was declared as such:
MERGE INTO MY_TABLE USING (SOURCE_TABLE) ON (MY_TABLE.ID equal SOURCE_TABLE.ID) WHEN MATCHED THEN UPDATE SET (ID, 1) SET (DATA, "Data") WHEN NOT MATCHED THEN INSERT (MY_TABLE.ID, MY_TABLE.DATA) VALUES (1, "Data")
Convinced? Contributions very welcome! :-)
That was awesome, right? Share it with your friends!
- 879,692 hits
- Super excited to announce #jOOQ 3.5.0 with tons of new features! groups.google.com/forum/#!topic/… #DataTypes #StoredProcedures #Informix #DDL #UNION 1 day ago
- Within 2015, #jOOQ will be more popular than #EclipseLink: buff.ly/1xVgPfU. Next goal: Become more popular than #Hibernate 1 day ago
Get Back in Control of Your SQL
Top Posts & Pages
- Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT?
- Add Some Entropy to Your JVM
- 10 Subtle Best Practices when Coding Java
- SQL Trick: row_number() is to SELECT what dense_rank() is to SELECT DISTINCT
- 10 Common Mistakes Java Developers Make when Writing SQL
- The 10 Most Popular DB Engines (SQL and NoSQL)
- 10 More Common Mistakes Java Developers Make when Writing SQL
- 10 Things You Didn't Know About Java
- Use MySQL's Strict Mode on all new Projects!
- Java Auto-Unboxing Gotcha. Beware!
- Use MySQL’s Strict Mode on all new Projects! November 20, 2014
- jOOQ Tip of the Day: Discover all Primary Keys November 14, 2014
- Don’t Migrate to MariaDB just yet. MySQL is Back! November 12, 2014
- Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT? November 11, 2014
- Don’t Miss out on Awesome SQL Power with FIRST_VALUE(), LAST_VALUE(), LEAD(), and LAG() November 7, 2014
- Painless Access from Java to PL/SQL Procedures with jOOQ November 4, 2014
- 10 Things You Didn’t Know About Java November 3, 2014
- A RESTful JDBC HTTP Server built on top of jOOQ October 31, 2014
- Let’s Stream a Map in Java 8 with jOOλ October 23, 2014
- Stop Claiming that you’re Using a Schemaless Database October 20, 2014