Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Last active April 20, 2020 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakamura-to/57cf013fa06bf6174db637a61ae6ce5b to your computer and use it in GitHub Desktop.
Save nakamura-to/57cf013fa06bf6174db637a61ae6ce5b to your computer and use it in GitHub Desktop.
KotlinのDSLでSQLっぽく書くために

selectするカラムの選択

ラムダで指定できなくはないが、型パラメータが面倒臭いことになる

DSLの利用コード

val query = select({ it.name }, from = ::_Emp) { e -> where { eq(e.id, 1) }}

DSLの定義

fun <ENTITY, ENTITY_TYPE : EntityType<ENTITY>, BASIC, PROPERTY_TYPE: EntityPropertyType<ENTITY, BASIC>> select(
        projection: (ENTITY_TYPE) -> BASIC,
        from: () -> ENTITY_TYPE,
        block: SelectDeclaration.(ENTITY_TYPE) -> Unit
): SelectStatement<ENTITY, ENTITY_TYPE> {
    return SelectStatement(from, block)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment