Skip to content

Instantly share code, notes, and snippets.

@geowarin
Last active August 3, 2017 20:16
Show Gist options
  • Save geowarin/eb21bf560c9d4d287b67fa04cb1d4e8c to your computer and use it in GitHub Desktop.
Save geowarin/eb21bf560c9d4d287b67fa04cb1d4e8c to your computer and use it in GitHub Desktop.
dialect specific SQL generation with jooq
package geowarin.codegen
import org.amshove.kluent.shouldEqualTo
import org.jooq.SQLDialect
import org.jooq.impl.DSL
import org.jooq.impl.DefaultConfiguration
import org.junit.Test
class CodegenApplicationTests {
@Test
fun `should generate sql`() {
val originalQuery = "SELECT * FROM (VALUES (1, 'a'), (2, 'b')) t(a, b)"
generateSql(SQLDialect.H2, originalQuery) shouldEqualTo
"""
select
t.a,
t.b
from (
(
select
null a,
null b
where 1 = 0
)
union all (
select *
from (values
(1, 'a'),
(2, 'b')
) t
)
) t
""".trimIndent()
}
private fun generateSql(sqlDialect: SQLDialect, sql: String): String {
return DSL.using(DefaultConfiguration().set(sqlDialect))
.parser()
.parseQuery(sql).toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment