Skip to content

Commit

Permalink
feat: support filter by uuid on jetbrains exposed framework
Browse files Browse the repository at this point in the history
  • Loading branch information
magonxesp committed Oct 24, 2024
1 parent f6672b2 commit dd09fb2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.magonxesp.criteria.domain

import java.util.UUID

fun String.toUUID(): UUID = UUID.fromString(this)

fun String.isUUID() =
try {
this.toUUID()
true
} catch (_: IllegalArgumentException) {
false
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.magonxesp.criteria.infrastructure.exposed

import io.github.magonxesp.criteria.domain.Filter
import io.github.magonxesp.criteria.domain.FilterOperator
import io.github.magonxesp.criteria.domain.isUUID
import io.github.magonxesp.criteria.domain.toUUID
import io.github.magonxesp.criteria.infrastructure.Adapter
import io.github.magonxesp.criteria.infrastructure.map.FieldMap
import kotlinx.datetime.Instant
Expand All @@ -19,6 +21,7 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.neq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.notInList
import org.jetbrains.exposed.sql.SqlExpressionBuilder.notLike
import org.jetbrains.exposed.sql.SqlExpressionBuilder.regexp
import java.util.UUID

class FilterPredicateAdapter(
private val columns: Map<String, Column<Any>>,
Expand Down Expand Up @@ -67,6 +70,24 @@ class FilterPredicateAdapter(
}
}

private fun Filter.uuidPredicate(): Op<Boolean>? {
if (value !is String && value !is UUID) {
return null
}

if (value is String && !value.isUUID()) {
return null
}

val uuid: UUID = if (value is String) value.toUUID() else value as UUID

return when (operator) {
FilterOperator.EQUALS -> column eq uuid
FilterOperator.NOT_EQUALS -> column neq uuid
else -> null
}
}

private fun Filter.stringPredicate(): Op<Boolean>? {
if (value !is String) {
return null
Expand Down Expand Up @@ -130,9 +151,10 @@ class FilterPredicateAdapter(

private fun Filter.toPredicate(): Op<Boolean> =
numberPredicate()
?: uuidPredicate()
?: instantPredicate()
?: stringPredicate()
?: booleanPredicate()
?: instantPredicate()
?: listPredicate()
?: error("The filter operator ${operator.operator} is not supported by the given value type")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import org.jetbrains.exposed.sql.Query
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.insert
import java.util.UUID

object BookTable : Table() {
val id = varchar("id", length = 65)
val uuid = uuid("uuid")
val numericalId = integer("numerical_id")
val title = varchar("title", length = 255)
val author = varchar("author", length = 255)
Expand All @@ -17,6 +19,7 @@ object BookTable : Table() {

data class BookEntity(
val id: String,
val uuid: UUID,
val numericalId: Int,
val title: String,
val author: String,
Expand All @@ -25,6 +28,7 @@ data class BookEntity(
fun insert() {
BookTable.insert {
it[id] = this@BookEntity.id
it[uuid] = this@BookEntity.uuid
it[numericalId] = this@BookEntity.numericalId
it[title] = this@BookEntity.title
it[author] = this@BookEntity.author
Expand All @@ -35,6 +39,7 @@ data class BookEntity(

fun ResultRow.toEntity() = BookEntity(
id = get(BookTable.id),
uuid = get(BookTable.uuid),
numericalId = get(BookTable.numericalId),
title = get(BookTable.title),
author = get(BookTable.author),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class CriteriaExposedQueryAdapterTest : IntegrationTestCase() {
val books = (0..50).map {
BookEntity(
id = UUID.randomUUID().toString(),
uuid = UUID.randomUUID(),
numericalId = it,
title = random().book.title(),
author = random().book.author(),
Expand All @@ -44,6 +45,7 @@ abstract class CriteriaExposedQueryAdapterTest : IntegrationTestCase() {

private val columns = mapOf(
BookTable::id.name toColumn BookTable.id,
BookTable.uuid.name toColumn BookTable.uuid,
BookTable::title.name toColumn BookTable.title,
BookTable::author.name toColumn BookTable.author,
BookTable::stock.name toColumn BookTable.stock,
Expand Down Expand Up @@ -449,4 +451,46 @@ abstract class CriteriaExposedQueryAdapterTest : IntegrationTestCase() {

result shouldContain book
}

@Test
fun `it should find by equals uuid`() = transaction(db) {
val criteria = criteria {
filter(BookTable.uuid.name, book.uuid, FilterOperator.EQUALS)
}

val query = BookTable.selectAll()
CriteriaExposedQueryAdapter(columns).apply(criteria, query)

val result = query.toEntityList()

result shouldContain book
}

@Test
fun `it should find by equals uuid as string`() = transaction(db) {
val criteria = criteria {
filter(BookTable.uuid.name, book.uuid.toString(), FilterOperator.EQUALS)
}

val query = BookTable.selectAll()
CriteriaExposedQueryAdapter(columns).apply(criteria, query)

val result = query.toEntityList()

result shouldContain book
}

@Test
fun `it should find by not equals uuid`() = transaction(db) {
val criteria = criteria {
filter(BookTable.uuid.name, book.uuid, FilterOperator.NOT_EQUALS)
}

val query = BookTable.selectAll()
CriteriaExposedQueryAdapter(columns).apply(criteria, query)

val result = query.toEntityList()

result shouldNotContain book
}
}

0 comments on commit dd09fb2

Please sign in to comment.