Skip to content

Commit

Permalink
Merge pull request #764 from line/hotfix/support-limit-in-repository
Browse files Browse the repository at this point in the history
feat: support limit in spring data repository
  • Loading branch information
shouwn authored Sep 3, 2024
2 parents 025f6c1 + e65f28c commit 6c3f67c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface KotlinJdslJpqlExecutor {
*/
@SinceJdsl("3.0.0")
fun <T : Any> findAll(
offset: Int? = null,
limit: Int? = null,
init: Jpql.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?>

Expand All @@ -29,6 +31,8 @@ interface KotlinJdslJpqlExecutor {
@SinceJdsl("3.0.0")
fun <T : Any, DSL : JpqlDsl> findAll(
dsl: JpqlDsl.Constructor<DSL>,
offset: Int? = null,
limit: Int? = null,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?>

Expand All @@ -38,6 +42,8 @@ interface KotlinJdslJpqlExecutor {
@SinceJdsl("3.4.0")
fun <T : Any, DSL : JpqlDsl> findAll(
dsl: DSL,
offset: Int? = null,
limit: Int? = null,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,33 @@ open class KotlinJdslJpqlExecutorImpl(
private val metadata: CrudMethodMetadata?,
) : KotlinJdslJpqlExecutor {
override fun <T : Any> findAll(
offset: Int?,
limit: Int?,
init: Jpql.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?> {
return findAll(Jpql, init)
return findAll(Jpql, offset = offset, limit = limit, init)
}

override fun <T : Any, DSL : JpqlDsl> findAll(
dsl: JpqlDsl.Constructor<DSL>,
offset: Int?,
limit: Int?,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?> {
val query: SelectQuery<T> = jpql(dsl, init)
val jpaQuery = createJpaQuery(query, query.returnType)

return jpaQuery.resultList
return findAll(dsl.newInstance(), offset = offset, limit = limit, init)
}

override fun <T : Any, DSL : JpqlDsl> findAll(
dsl: DSL,
offset: Int?,
limit: Int?,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?> {
val query: SelectQuery<T> = 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
}
Expand All @@ -74,9 +80,7 @@ open class KotlinJdslJpqlExecutorImpl(
pageable: Pageable,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?> {
val query: SelectQuery<T> = jpql(dsl, init)

return createList(query, query.returnType, pageable)
return findAll(dsl.newInstance(), pageable, init)
}

override fun <T : Any, DSL : JpqlDsl> findAll(
Expand All @@ -101,9 +105,7 @@ open class KotlinJdslJpqlExecutorImpl(
pageable: Pageable,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): Page<T?> {
val query: SelectQuery<T> = jpql(dsl, init)

return createPage(query, query.returnType, pageable)
return findPage(dsl.newInstance(), pageable, init)
}

override fun <T : Any, DSL : JpqlDsl> findPage(
Expand All @@ -128,9 +130,7 @@ open class KotlinJdslJpqlExecutorImpl(
pageable: Pageable,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): Slice<T?> {
val query: SelectQuery<T> = jpql(dsl, init)

return createSlice(query, query.returnType, pageable)
return findSlice(dsl.newInstance(), pageable, init)
}

override fun <T : Any, DSL : JpqlDsl> findSlice(
Expand All @@ -155,10 +155,7 @@ open class KotlinJdslJpqlExecutorImpl(
dsl: JpqlDsl.Constructor<DSL>,
init: DSL.() -> JpqlQueryable<UpdateQuery<T>>,
): Int {
val query: UpdateQuery<T> = jpql(dsl, init)
val jpaQuery = createJpaQuery(query)

return jpaQuery.executeUpdate()
return update(dsl.newInstance(), init)
}

@Transactional
Expand All @@ -184,10 +181,7 @@ open class KotlinJdslJpqlExecutorImpl(
dsl: JpqlDsl.Constructor<DSL>,
init: DSL.() -> JpqlQueryable<DeleteQuery<T>>,
): Int {
val query: DeleteQuery<T> = jpql(dsl, init)
val jpaQuery = createJpaQuery(query)

return jpaQuery.executeUpdate()
return delete(dsl.newInstance(), init)
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ class KotlinJdslJpqlExecutorImplTest : WithAssertions {
@MockK
private lateinit var page1: Page<String>

private val offset1 = 10

private val limit1 = 20

private val lockModeType1 = LockModeType.READ

private val queryHint1 = "queryHintName1" to "queryHintValue1"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
Expand All @@ -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)
Expand All @@ -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
}
}
Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface KotlinJdslJpqlExecutor {
*/
@SinceJdsl("3.0.0")
fun <T : Any> findAll(
offset: Int? = null,
limit: Int? = null,
init: Jpql.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?>

Expand All @@ -29,6 +31,8 @@ interface KotlinJdslJpqlExecutor {
@SinceJdsl("3.0.0")
fun <T : Any, DSL : JpqlDsl> findAll(
dsl: JpqlDsl.Constructor<DSL>,
offset: Int? = null,
limit: Int? = null,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?>

Expand All @@ -38,6 +42,8 @@ interface KotlinJdslJpqlExecutor {
@SinceJdsl("3.4.0")
fun <T : Any, DSL : JpqlDsl> findAll(
dsl: DSL,
offset: Int? = null,
limit: Int? = null,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,33 @@ open class KotlinJdslJpqlExecutorImpl(
private val metadata: CrudMethodMetadata?,
) : KotlinJdslJpqlExecutor {
override fun <T : Any> findAll(
offset: Int?,
limit: Int?,
init: Jpql.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?> {
return findAll(Jpql, init)
return findAll(Jpql, offset = offset, limit = limit, init)
}

override fun <T : Any, DSL : JpqlDsl> findAll(
dsl: JpqlDsl.Constructor<DSL>,
offset: Int?,
limit: Int?,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?> {
val query: SelectQuery<T> = jpql(dsl, init)
val jpaQuery = createJpaQuery(query, query.returnType)

return jpaQuery.resultList
return findAll(dsl.newInstance(), offset = offset, limit = limit, init)
}

override fun <T : Any, DSL : JpqlDsl> findAll(
dsl: DSL,
offset: Int?,
limit: Int?,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?> {
val query: SelectQuery<T> = 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
}
Expand All @@ -76,9 +82,7 @@ open class KotlinJdslJpqlExecutorImpl(
pageable: Pageable,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): List<T?> {
val query: SelectQuery<T> = jpql(dsl, init)

return createList(query, query.returnType, pageable)
return findAll(dsl.newInstance(), pageable, init)
}

override fun <T : Any, DSL : JpqlDsl> findAll(
Expand All @@ -103,9 +107,7 @@ open class KotlinJdslJpqlExecutorImpl(
pageable: Pageable,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): Page<T?> {
val query: SelectQuery<T> = jpql(dsl, init)

return createPage(query, query.returnType, pageable)
return findPage(dsl.newInstance(), pageable, init)
}

override fun <T : Any, DSL : JpqlDsl> findPage(
Expand All @@ -130,9 +132,7 @@ open class KotlinJdslJpqlExecutorImpl(
pageable: Pageable,
init: DSL.() -> JpqlQueryable<SelectQuery<T>>,
): Slice<T?> {
val query: SelectQuery<T> = jpql(dsl, init)

return createSlice(query, query.returnType, pageable)
return findSlice(dsl.newInstance(), pageable, init)
}

override fun <T : Any, DSL : JpqlDsl> findSlice(
Expand All @@ -157,10 +157,7 @@ open class KotlinJdslJpqlExecutorImpl(
dsl: JpqlDsl.Constructor<DSL>,
init: DSL.() -> JpqlQueryable<UpdateQuery<T>>,
): Int {
val query: UpdateQuery<T> = jpql(dsl, init)
val jpaQuery = createJpaQuery(query)

return jpaQuery.executeUpdate()
return update(dsl.newInstance(), init)
}

@Transactional
Expand All @@ -186,10 +183,7 @@ open class KotlinJdslJpqlExecutorImpl(
dsl: JpqlDsl.Constructor<DSL>,
init: DSL.() -> JpqlQueryable<DeleteQuery<T>>,
): Int {
val query: DeleteQuery<T> = jpql(dsl, init)
val jpaQuery = createJpaQuery(query)

return jpaQuery.executeUpdate()
return delete(dsl.newInstance(), init)
}

@Transactional
Expand Down
Loading

0 comments on commit 6c3f67c

Please sign in to comment.