Last active
August 3, 2017 20:16
-
-
Save geowarin/eb21bf560c9d4d287b67fa04cb1d4e8c to your computer and use it in GitHub Desktop.
dialect specific SQL generation with jooq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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