Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

SQL Function 호출 본문

인프런/[인프런] QueryDsl

SQL Function 호출

Seung__ 2021. 9. 4. 00:25

1. SQL Function 호출


SQL Function은 JPA와 같이 Dialect에 등록된 내용만 호출이 가능함.

 

 

 

2. member를 M으로 변경하는 replace 함수 사용


    @Test
    public void sqlFunction(){
        List<String> result = queryFactory
                .select(Expressions.stringTemplate(
                        "function('replace', {0}, {1}, {2})",
                        member.username, "member", "M"))
                .from(member)
                .fetch();

        for (String s : result) {
            System.out.println("s = " + s);
        }
    }
select
        function('replace',
        member1.username,
        ?1,
        ?2) 
    from
        Member member1

s = M1
s = M2
s = M3
s = M4
  • H2Dialect
registerFunction( "replace", new StandardSQLFunction( "replace", StandardBasicTypes.STRING ) );

 

 

3. 소문자로 변경.


    @Test
    public void sqlFunction2(){
        List<String> result = queryFactory
                .select(member.username)
                .from(member)
                .where(member.username.eq(Expressions.stringTemplate("function('lower', {0})", member.username)))
                .fetch();

        for (String s : result) {
            System.out.println("s = " + s);
        }
    }

소문자로 변경하는 다른 방법

    @Test
    public void sqlFunction2(){
        List<String> result = queryFactory
                .select(member.username)
                .from(member)
//                .where(member.username.eq(Expressions.stringTemplate("function('lower', {0})", member.username)))
                .where(member.username.eq(member.username.lower()))
                .fetch();

        for (String s : result) {
            System.out.println("s = " + s);
        }
    }
select
        member1.username 
    from
        Member member1 
    where
        member1.username = lower(member1.username)

 

4. GitHub : 210903 SQL Function


 

GitHub - bsh6463/Querydsl

Contribute to bsh6463/Querydsl development by creating an account on GitHub.

github.com

 

Comments