You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SelectQuery
├── Field (BOOK.ID)
├── Table (BOOK)
└── Condition (ID = 1)
2. Visitor 패턴 (Context)
Visitor 패턴이란?
객체 구조는 그대로 두고, 그 구조를 처리하는 로직을 바깥으로 분리하는 패턴
객체들이 이런 식으로 있다고 해보자.
Field
Table
Condition
이 객체들 각각에 대해 할 수 있는 작업이 많다 → 객체 안에 다 넣으면 객체가 너무 많은 책임을 가짐
SQL 문자열 만들기
bind 값 모으기
디버깅 출력하기
통계 내기
그래서
객체는 “나는 Field다 / Table이다 / Condition이다” 같은 구조
Visitor는 “그걸 어떻게 처리할지”라는 작업 처리
Context는 QueryPart를 순회하면서 처리하는 Visitor이다.
ctx.visit(queryPart);
/* ctx: Visitor 역할을 하는 객체 visit(...): 방문 동작 ctx.visit(queryPart): Visitor가 어떤 노드를 방문하기 시작하는 코드*/
역할:
QueryPart를 하나씩 방문
타입에 따라 다른 처리 수행
구조와 로직을 분리하기 위해 Visitor 패턴 사용
QueryPart = 구조
Context = 행위
5. 렌더링 과정
ctx.visit(query);
visitor가 query를 방문
실제 내부구현
visit(field) { // 컨텍스트 레벨 공통 처리
// 공통 처리 (before)
visit0(field);
// 공통 처리 (after)
}
visit0(internal) { // 각 노드별 처리
internal.accept(this);
}
↓
DefaultRenderContext(SQL 문자열을 생성하는 Visitor 구현체) 내부
query.accept(ctx); // 구조 만들기
query에게 스스로 어떻게 렌더링할지 실행해보라고 명령
↓
query 내부
ctx.sql("select ");
ctx.visit(field); // 내부적으로 field.accept(ctx); -> 실제 값 하나 출력ctx.sql(" from ");
ctx.visit(table); // 내부에서 acceptctx.sql(" where ");
ctx.visit(condition);
↓
재귀 반복
visit → accept → visit → accept → ...
↓
최종 결과
selectbook.idfrom book
wherebook.id= ?
결론: jOOQ의 쿼리 렌더링은
QueryPart 기반의 AST를 Context(Visitor)가 순회하면서 SQL 문자열로 변환하는 과정이다.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
[전체 흐름]
jOOQ의 쿼리 생성 과정
1. 쿼리 렌더링이란?
jOOQ에서는 SQL을 처음부터 문자열로 만들지 않는다.
이 객체 구조를 SQL 문자열로 바꾸기 위해서는?
= 쿼리 렌더링(Query Rendering)
2. QueryPart 구조
QueryPart는 SQL을 구성하는 모든 요소의 공통 타입이다.
즉 QueryPart는 SQL의 구조(AST)를 표현한다
2. Visitor 패턴 (Context)
Visitor 패턴이란?
객체 구조는 그대로 두고, 그 구조를 처리하는 로직을 바깥으로 분리하는 패턴
객체들이 이런 식으로 있다고 해보자.
이 객체들 각각에 대해 할 수 있는 작업이 많다 → 객체 안에 다 넣으면 객체가 너무 많은 책임을 가짐
그래서
Context는 QueryPart를 순회하면서 처리하는 Visitor이다.
역할:
구조와 로직을 분리하기 위해 Visitor 패턴 사용
QueryPart= 구조Context= 행위5. 렌더링 과정
visitor가 query를 방문
실제 내부구현
↓
DefaultRenderContext(SQL 문자열을 생성하는 Visitor 구현체) 내부query에게 스스로 어떻게 렌더링할지 실행해보라고 명령
↓
query 내부
↓
재귀 반복
↓
최종 결과
결론: jOOQ의 쿼리 렌더링은
QueryPart 기반의 AST를 Context(Visitor)가 순회하면서 SQL 문자열로 변환하는 과정이다.
Beta Was this translation helpful? Give feedback.
All reactions