Skip to content

Commit

Permalink
docs: add description for dsl object
Browse files Browse the repository at this point in the history
  • Loading branch information
jbl428 committed Feb 18, 2024
1 parent 1c5ffe9 commit dd434b7
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions docs/ko/jpql-with-kotlin-jdsl/custom-dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
그리고 [`Expression`](expressions.md) 혹은 [`Predicate`](predicates.md)를 구현한 나만의 `Model` 클래스를 만들고, 이를 반환하는 함수를 만들 수 있습니다.
이 경우 [`JpqlSerializer`](custom-dsl.md#serializer)를 구현하여 `Model`을 String으로 랜더링하는 방법을 Kotlin JDSL에게 알려줄 수 있습니다.

{% hint style="info" %}
`jpql()`이 DSL을 인식하기 위해서 `JpqlDsl.Constructor`를 companion object로 구현해야 합니다.
{% endhint %}
나만의 DSL을 `jpql()`에 전달하기 위한 두 가지 방법이 있습니다.

- `JpqlDsl.Constructor`를 companion object로 구현

```kotlin
class MyJpql : Jpql() {
Expand Down Expand Up @@ -39,6 +39,34 @@ val query = jpql(MyJpql) {
}
```

- `Jpql` 인스턴스 전달

이 방법을 사용하면 쿼리 생성에 하나의 인스턴스를 재활용할 수 있으며 의존성 주입을 활용할 수 있습니다.

```kotlin
class MyJpql(
private val encryptor: Encryptor,
) : Jpql() {

fun myFunction(value: String): Expression<String> {
val encrypted = encryptor.encrypt(value)
return function(String::class, "myFunction", listOf(value(encrypted)))
}
}

val encryptor = Encryptor()
val instance = MyJpql(encryptor)
val query = jpql(instance) {
select(
entity(Book::class)
).from(
entity(Book::class)
).where(
myFunction("test").regexLike(".*")
)
}
```

### Serializer

나만의 `Model`을 String을 랜더링하기 위해 `JpqlSerializer`를 구현하고 이를 `RenderContext`에 추가해야 합니다.
Expand Down

0 comments on commit dd434b7

Please sign in to comment.