Skip to content

Commit

Permalink
feat: add selectFrom spec
Browse files Browse the repository at this point in the history
  • Loading branch information
kihwankim committed Oct 4, 2024
1 parent 565e71b commit 8f29c10
Show file tree
Hide file tree
Showing 16 changed files with 361 additions and 0 deletions.
14 changes: 14 additions & 0 deletions dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.linecorp.kotlinjdsl.dsl.jpql.join.impl.AssociationJoinDsl
import com.linecorp.kotlinjdsl.dsl.jpql.join.impl.FetchJoinDsl
import com.linecorp.kotlinjdsl.dsl.jpql.join.impl.JoinDsl
import com.linecorp.kotlinjdsl.dsl.jpql.select.SelectQueryFromStep
import com.linecorp.kotlinjdsl.dsl.jpql.select.SelectQueryWhereStep
import com.linecorp.kotlinjdsl.dsl.jpql.select.impl.SelectQueryFromStepDsl
import com.linecorp.kotlinjdsl.dsl.jpql.sort.SortNullsStep
import com.linecorp.kotlinjdsl.dsl.jpql.sort.impl.SortDsl
Expand All @@ -33,6 +34,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressionable
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Subquery
import com.linecorp.kotlinjdsl.querymodel.jpql.from.Fromable
import com.linecorp.kotlinjdsl.querymodel.jpql.join.JoinType
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Path
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Pathable
Expand Down Expand Up @@ -3119,6 +3121,18 @@ open class Jpql : JpqlDsl {
)
}

/**
* Creates a select clause in a select from query.
*/
@SinceJdsl("3.5.3")
inline fun <reified T : Any> selectFrom(
expr: Entityable<T>,
vararg froms: Fromable?,
): SelectQueryWhereStep<T> {
return select(expr)
.from(expr, *froms)
}

/**
* Creates a select clause in a select query.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,59 @@ class FromDslTest : WithAssertions {

assertThat(actual).isEqualTo(expected)
}

@Test
fun `selectFrom() with entity`() {
// when
val selectFrom = queryPart {
selectFrom(
entity1,
)
}.toQuery()
val actual: SelectQuery<Book> = selectFrom

// then
val expected = SelectQueries.selectQuery(
returnType = Book::class,
distinct = false,
select = listOf(entity1),
from = listOf(
entity1,
),
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun `selectFrom() with froms`() {
// when
val selectFrom = queryPart {
selectFrom(
entity1,
null,
entity2,
null,
join1,
null,
join2,
)
}.toQuery()
val actual: SelectQuery<Book> = selectFrom

// then
val expected = SelectQueries.selectQuery(
returnType = Book::class,
distinct = false,
select = listOf(entity1),
from = listOf(
entity1,
entity2,
join1,
join2,
),
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ class SelectExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = entityManager.createQuery(query, context).resultList

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ class SelectExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = entityManager.createQuery(query, context).resultList

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ class SelectExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = entityManager.createQuery(query, context).resultList

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun books() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ class SelectMutinySessionExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = sessionFactory.withSession {
it.createQuery(query, context).resultList
}.await().indefinitely()

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ class SelectMutinyStatelessSessionExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = sessionFactory.withStatelessSession {
it.createQuery(query, context).resultList
}.await().indefinitely()

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ class SelectStageSessionExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = sessionFactory.withSession {
it.createQuery(query, context).resultList
}.toCompletableFuture().get()

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ class SelectStageStatelessSessionExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = sessionFactory.withStatelessSession {
it.createQuery(query, context).resultList
}.toCompletableFuture().get()

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ class SelectMutinySessionExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = sessionFactory.withSession {
it.createQuery(query, context).resultList
}.await().indefinitely()

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ class SelectMutinyStatelessSessionExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = sessionFactory.withStatelessSession {
it.createQuery(query, context).resultList
}.await().indefinitely()

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ class SelectStageSessionExample : WithAssertions {
assertThat(actual).isEqualTo(listOf(4L))
}

@Test
fun `authors who haven't written a book with selectFrom`() {
// when
val query = jpql {
selectFrom(
entity(Author::class),
leftJoin(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).where(
path(BookAuthor::authorId).isNull(),
).orderBy(
path(Author::authorId).asc(),
)
}

val actual = sessionFactory.withSession {
it.createQuery(query, context).resultList
}.toCompletableFuture().get()

// then
assertThat(actual.map { it.authorId }).isEqualTo(listOf(4L))
}

@Test
fun `the book with the most authors`() {
// when
Expand Down
Loading

0 comments on commit 8f29c10

Please sign in to comment.