diff --git a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutor.kt b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutor.kt index 28c8bce40..56a500830 100644 --- a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutor.kt +++ b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutor.kt @@ -20,6 +20,8 @@ interface KotlinJdslJpqlExecutor { */ @SinceJdsl("3.0.0") fun findAll( + offset: Int? = null, + limit: Int? = null, init: Jpql.() -> JpqlQueryable>, ): List @@ -29,6 +31,8 @@ interface KotlinJdslJpqlExecutor { @SinceJdsl("3.0.0") fun findAll( dsl: JpqlDsl.Constructor, + offset: Int? = null, + limit: Int? = null, init: DSL.() -> JpqlQueryable>, ): List @@ -38,6 +42,8 @@ interface KotlinJdslJpqlExecutor { @SinceJdsl("3.4.0") fun findAll( dsl: DSL, + offset: Int? = null, + limit: Int? = null, init: DSL.() -> JpqlQueryable>, ): List diff --git a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImpl.kt b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImpl.kt index cfd634710..59ddd4e00 100644 --- a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImpl.kt +++ b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImpl.kt @@ -37,27 +37,33 @@ open class KotlinJdslJpqlExecutorImpl( private val metadata: CrudMethodMetadata?, ) : KotlinJdslJpqlExecutor { override fun findAll( + offset: Int?, + limit: Int?, init: Jpql.() -> JpqlQueryable>, ): List { - return findAll(Jpql, init) + return findAll(Jpql, offset = offset, limit = limit, init) } override fun findAll( dsl: JpqlDsl.Constructor, + offset: Int?, + limit: Int?, init: DSL.() -> JpqlQueryable>, ): List { - val query: SelectQuery = jpql(dsl, init) - val jpaQuery = createJpaQuery(query, query.returnType) - - return jpaQuery.resultList + return findAll(dsl.newInstance(), offset = offset, limit = limit, init) } override fun findAll( dsl: DSL, + offset: Int?, + limit: Int?, init: DSL.() -> JpqlQueryable>, ): List { val query: SelectQuery = jpql(dsl, init) - val jpaQuery = createJpaQuery(query, query.returnType) + val jpaQuery = createJpaQuery(query, query.returnType).apply { + offset?.let { setFirstResult(it) } + limit?.let { setMaxResults(it) } + } return jpaQuery.resultList } @@ -74,9 +80,7 @@ open class KotlinJdslJpqlExecutorImpl( pageable: Pageable, init: DSL.() -> JpqlQueryable>, ): List { - val query: SelectQuery = jpql(dsl, init) - - return createList(query, query.returnType, pageable) + return findAll(dsl.newInstance(), pageable, init) } override fun findAll( @@ -101,9 +105,7 @@ open class KotlinJdslJpqlExecutorImpl( pageable: Pageable, init: DSL.() -> JpqlQueryable>, ): Page { - val query: SelectQuery = jpql(dsl, init) - - return createPage(query, query.returnType, pageable) + return findPage(dsl.newInstance(), pageable, init) } override fun findPage( @@ -128,9 +130,7 @@ open class KotlinJdslJpqlExecutorImpl( pageable: Pageable, init: DSL.() -> JpqlQueryable>, ): Slice { - val query: SelectQuery = jpql(dsl, init) - - return createSlice(query, query.returnType, pageable) + return findSlice(dsl.newInstance(), pageable, init) } override fun findSlice( @@ -155,10 +155,7 @@ open class KotlinJdslJpqlExecutorImpl( dsl: JpqlDsl.Constructor, init: DSL.() -> JpqlQueryable>, ): Int { - val query: UpdateQuery = jpql(dsl, init) - val jpaQuery = createJpaQuery(query) - - return jpaQuery.executeUpdate() + return update(dsl.newInstance(), init) } @Transactional @@ -184,10 +181,7 @@ open class KotlinJdslJpqlExecutorImpl( dsl: JpqlDsl.Constructor, init: DSL.() -> JpqlQueryable>, ): Int { - val query: DeleteQuery = jpql(dsl, init) - val jpaQuery = createJpaQuery(query) - - return jpaQuery.executeUpdate() + return delete(dsl.newInstance(), init) } @Transactional diff --git a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImplTest.kt b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImplTest.kt index 0d5458709..18f10efb5 100644 --- a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImplTest.kt +++ b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/repository/KotlinJdslJpqlExecutorImplTest.kt @@ -133,6 +133,10 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { @MockK private lateinit var page1: Page + private val offset1 = 10 + + private val limit1 = 20 + private val lockModeType1 = LockModeType.READ private val queryHint1 = "queryHintName1" to "queryHintValue1" @@ -268,12 +272,14 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { } returns stringTypedQuery1 every { stringTypedQuery1.setLockMode(any()) } returns stringTypedQuery1 every { stringTypedQuery1.setHint(any(), any()) } returns stringTypedQuery1 + every { stringTypedQuery1.setFirstResult(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.setMaxResults(any()) } returns stringTypedQuery1 every { stringTypedQuery1.resultList } returns list1 every { metadata.lockModeType } returns lockModeType1 every { metadata.queryHints } returns queryHints1 // when - val actual = sut.findAll(createSelectQuery1) + val actual = sut.findAll(offset1, limit1, createSelectQuery1) // then assertThat(actual).isEqualTo(list1) @@ -287,6 +293,8 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { stringTypedQuery1.setHint(queryHint1.first, queryHint1.second) stringTypedQuery1.setHint(queryHint2.first, queryHint2.second) stringTypedQuery1.setHint(queryHint3.first, queryHint3.second) + stringTypedQuery1.setFirstResult(offset1) + stringTypedQuery1.setMaxResults(limit1) stringTypedQuery1.resultList } } @@ -300,12 +308,14 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { } returns stringTypedQuery2 every { stringTypedQuery2.setLockMode(any()) } returns stringTypedQuery2 every { stringTypedQuery2.setHint(any(), any()) } returns stringTypedQuery2 + every { stringTypedQuery2.setFirstResult(any()) } returns stringTypedQuery2 + every { stringTypedQuery2.setMaxResults(any()) } returns stringTypedQuery2 every { stringTypedQuery2.resultList } returns list1 every { metadata.lockModeType } returns lockModeType1 every { metadata.queryHints } returns queryHints1 // when - val actual = sut.findAll(MyJpql, createSelectQuery2) + val actual = sut.findAll(MyJpql, offset1, limit1, createSelectQuery2) // then assertThat(actual).isEqualTo(list1) @@ -319,6 +329,8 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { stringTypedQuery2.setHint(queryHint1.first, queryHint1.second) stringTypedQuery2.setHint(queryHint2.first, queryHint2.second) stringTypedQuery2.setHint(queryHint3.first, queryHint3.second) + stringTypedQuery2.setFirstResult(offset1) + stringTypedQuery2.setMaxResults(limit1) stringTypedQuery2.resultList } } @@ -332,12 +344,14 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { } returns stringTypedQuery3 every { stringTypedQuery3.setLockMode(any()) } returns stringTypedQuery3 every { stringTypedQuery3.setHint(any(), any()) } returns stringTypedQuery3 + every { stringTypedQuery3.setFirstResult(any()) } returns stringTypedQuery3 + every { stringTypedQuery3.setMaxResults(any()) } returns stringTypedQuery3 every { stringTypedQuery3.resultList } returns list1 every { metadata.lockModeType } returns lockModeType1 every { metadata.queryHints } returns queryHints1 // when - val actual = sut.findAll(MyJpqlObject, createSelectQuery3) + val actual = sut.findAll(MyJpqlObject, offset1, limit1, createSelectQuery3) // then assertThat(actual).isEqualTo(list1) @@ -351,6 +365,8 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { stringTypedQuery3.setHint(queryHint1.first, queryHint1.second) stringTypedQuery3.setHint(queryHint2.first, queryHint2.second) stringTypedQuery3.setHint(queryHint3.first, queryHint3.second) + stringTypedQuery3.setFirstResult(offset1) + stringTypedQuery3.setMaxResults(limit1) stringTypedQuery3.resultList } } diff --git a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutor.kt b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutor.kt index dabc8f07b..c3e28613c 100644 --- a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutor.kt +++ b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutor.kt @@ -20,6 +20,8 @@ interface KotlinJdslJpqlExecutor { */ @SinceJdsl("3.0.0") fun findAll( + offset: Int? = null, + limit: Int? = null, init: Jpql.() -> JpqlQueryable>, ): List @@ -29,6 +31,8 @@ interface KotlinJdslJpqlExecutor { @SinceJdsl("3.0.0") fun findAll( dsl: JpqlDsl.Constructor, + offset: Int? = null, + limit: Int? = null, init: DSL.() -> JpqlQueryable>, ): List @@ -38,6 +42,8 @@ interface KotlinJdslJpqlExecutor { @SinceJdsl("3.4.0") fun findAll( dsl: DSL, + offset: Int? = null, + limit: Int? = null, init: DSL.() -> JpqlQueryable>, ): List diff --git a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImpl.kt b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImpl.kt index d3c2e052a..db74f6857 100644 --- a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImpl.kt +++ b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImpl.kt @@ -39,27 +39,33 @@ open class KotlinJdslJpqlExecutorImpl( private val metadata: CrudMethodMetadata?, ) : KotlinJdslJpqlExecutor { override fun findAll( + offset: Int?, + limit: Int?, init: Jpql.() -> JpqlQueryable>, ): List { - return findAll(Jpql, init) + return findAll(Jpql, offset = offset, limit = limit, init) } override fun findAll( dsl: JpqlDsl.Constructor, + offset: Int?, + limit: Int?, init: DSL.() -> JpqlQueryable>, ): List { - val query: SelectQuery = jpql(dsl, init) - val jpaQuery = createJpaQuery(query, query.returnType) - - return jpaQuery.resultList + return findAll(dsl.newInstance(), offset = offset, limit = limit, init) } override fun findAll( dsl: DSL, + offset: Int?, + limit: Int?, init: DSL.() -> JpqlQueryable>, ): List { val query: SelectQuery = jpql(dsl, init) - val jpaQuery = createJpaQuery(query, query.returnType) + val jpaQuery = createJpaQuery(query, query.returnType).apply { + offset?.let { setFirstResult(it) } + limit?.let { setMaxResults(it) } + } return jpaQuery.resultList } @@ -76,9 +82,7 @@ open class KotlinJdslJpqlExecutorImpl( pageable: Pageable, init: DSL.() -> JpqlQueryable>, ): List { - val query: SelectQuery = jpql(dsl, init) - - return createList(query, query.returnType, pageable) + return findAll(dsl.newInstance(), pageable, init) } override fun findAll( @@ -103,9 +107,7 @@ open class KotlinJdslJpqlExecutorImpl( pageable: Pageable, init: DSL.() -> JpqlQueryable>, ): Page { - val query: SelectQuery = jpql(dsl, init) - - return createPage(query, query.returnType, pageable) + return findPage(dsl.newInstance(), pageable, init) } override fun findPage( @@ -130,9 +132,7 @@ open class KotlinJdslJpqlExecutorImpl( pageable: Pageable, init: DSL.() -> JpqlQueryable>, ): Slice { - val query: SelectQuery = jpql(dsl, init) - - return createSlice(query, query.returnType, pageable) + return findSlice(dsl.newInstance(), pageable, init) } override fun findSlice( @@ -157,10 +157,7 @@ open class KotlinJdslJpqlExecutorImpl( dsl: JpqlDsl.Constructor, init: DSL.() -> JpqlQueryable>, ): Int { - val query: UpdateQuery = jpql(dsl, init) - val jpaQuery = createJpaQuery(query) - - return jpaQuery.executeUpdate() + return update(dsl.newInstance(), init) } @Transactional @@ -186,10 +183,7 @@ open class KotlinJdslJpqlExecutorImpl( dsl: JpqlDsl.Constructor, init: DSL.() -> JpqlQueryable>, ): Int { - val query: DeleteQuery = jpql(dsl, init) - val jpaQuery = createJpaQuery(query) - - return jpaQuery.executeUpdate() + return delete(dsl.newInstance(), init) } @Transactional diff --git a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImplTest.kt b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImplTest.kt index 0d55083b8..654d7e27b 100644 --- a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImplTest.kt +++ b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/repository/KotlinJdslJpqlExecutorImplTest.kt @@ -133,6 +133,10 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { @MockK private lateinit var page1: Page + private val offset1 = 10 + + private val limit1 = 20 + private val lockModeType1 = LockModeType.READ private val queryHint1 = "queryHintName1" to "queryHintValue1" @@ -268,12 +272,14 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { } returns stringTypedQuery1 every { stringTypedQuery1.setLockMode(any()) } returns stringTypedQuery1 every { stringTypedQuery1.setHint(any(), any()) } returns stringTypedQuery1 + every { stringTypedQuery1.setFirstResult(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.setMaxResults(any()) } returns stringTypedQuery1 every { stringTypedQuery1.resultList } returns list1 every { metadata.lockModeType } returns lockModeType1 every { metadata.queryHints } returns queryHints1 // when - val actual = sut.findAll(createSelectQuery1) + val actual = sut.findAll(offset1, limit1, createSelectQuery1) // then assertThat(actual).isEqualTo(list1) @@ -287,6 +293,8 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { stringTypedQuery1.setHint(queryHint1.first, queryHint1.second) stringTypedQuery1.setHint(queryHint2.first, queryHint2.second) stringTypedQuery1.setHint(queryHint3.first, queryHint3.second) + stringTypedQuery1.setFirstResult(offset1) + stringTypedQuery1.setMaxResults(limit1) stringTypedQuery1.resultList } } @@ -300,12 +308,14 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { } returns stringTypedQuery2 every { stringTypedQuery2.setLockMode(any()) } returns stringTypedQuery2 every { stringTypedQuery2.setHint(any(), any()) } returns stringTypedQuery2 + every { stringTypedQuery2.setFirstResult(any()) } returns stringTypedQuery2 + every { stringTypedQuery2.setMaxResults(any()) } returns stringTypedQuery2 every { stringTypedQuery2.resultList } returns list1 every { metadata.lockModeType } returns lockModeType1 every { metadata.queryHints } returns queryHints1 // when - val actual = sut.findAll(MyJpql, createSelectQuery2) + val actual = sut.findAll(MyJpql, offset1, limit1, createSelectQuery2) // then assertThat(actual).isEqualTo(list1) @@ -319,6 +329,8 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { stringTypedQuery2.setHint(queryHint1.first, queryHint1.second) stringTypedQuery2.setHint(queryHint2.first, queryHint2.second) stringTypedQuery2.setHint(queryHint3.first, queryHint3.second) + stringTypedQuery2.setFirstResult(offset1) + stringTypedQuery2.setMaxResults(limit1) stringTypedQuery2.resultList } } @@ -332,12 +344,14 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { } returns stringTypedQuery3 every { stringTypedQuery3.setLockMode(any()) } returns stringTypedQuery3 every { stringTypedQuery3.setHint(any(), any()) } returns stringTypedQuery3 + every { stringTypedQuery3.setFirstResult(any()) } returns stringTypedQuery3 + every { stringTypedQuery3.setMaxResults(any()) } returns stringTypedQuery3 every { stringTypedQuery3.resultList } returns list1 every { metadata.lockModeType } returns lockModeType1 every { metadata.queryHints } returns queryHints1 // when - val actual = sut.findAll(MyJpqlObject, createSelectQuery3) + val actual = sut.findAll(MyJpqlObject, offset1, limit1, createSelectQuery3) // then assertThat(actual).isEqualTo(list1) @@ -351,6 +365,8 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions { stringTypedQuery3.setHint(queryHint1.first, queryHint1.second) stringTypedQuery3.setHint(queryHint2.first, queryHint2.second) stringTypedQuery3.setHint(queryHint3.first, queryHint3.second) + stringTypedQuery3.setFirstResult(offset1) + stringTypedQuery3.setMaxResults(limit1) stringTypedQuery3.resultList } }