Skip to content

Commit

Permalink
feat(query): support RAW to Operator. (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Mar 5, 2024
1 parent 4d5214b commit 1637dd7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
8 changes: 7 additions & 1 deletion wow-api/src/main/kotlin/me/ahoo/wow/api/query/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ enum class Operator {
/**
* 匹配字段值在指定值为`false`的所有文档
*/
FALSE
FALSE,

/**
* 原始操作符,将条件值直接作为原始的数据库查询条件
*/
RAW
}

data class Condition(
Expand Down Expand Up @@ -181,6 +186,7 @@ data class Condition(
fun id(value: String) = Condition(field = EMPTY_VALUE, operator = Operator.ID, value = value)
fun ids(value: List<String>) = Condition(field = EMPTY_VALUE, operator = Operator.IDS, value = value)
fun ids(vararg value: String) = Condition(field = EMPTY_VALUE, operator = Operator.IDS, value = value.toList())
fun raw(value: Any) = Condition(field = EMPTY_VALUE, operator = Operator.RAW, value = value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import me.ahoo.wow.api.query.Operator
import me.ahoo.wow.api.query.Projection
import me.ahoo.wow.api.query.Sort
import me.ahoo.wow.mongo.Documents
import org.bson.Document
import org.bson.conversions.Bson

object MongoFilterConverter {
Expand Down Expand Up @@ -66,6 +67,14 @@ object MongoFilterConverter {
}
Filters.or(children.map { it.toMongoFilter() })
}

Operator.RAW -> {
if (value is Bson) {
value as Bson
} else {
Document.parse(value as String)
}
}
}
if (!not) return filter
return Filters.not(filter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ class MongoFilterConverterTest {
Condition.or(listOf(Condition.isFalse("id"))),
Filters.or(Filters.eq("id", false))
),
Arguments.of(
Condition.or(listOf(Condition.raw(Filters.eq("id", false)))),
Filters.or(Filters.eq("id", false))
),
Arguments.of(
Condition.raw(Filters.eq("id", false)),
Filters.eq("id", false)
),
Arguments.of(
Condition.raw("{id:false}"),
Filters.eq("id", false)
)
)
}

Expand Down
4 changes: 4 additions & 0 deletions wow-query/src/main/kotlin/me/ahoo/wow/query/ConditionDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ class ConditionDsl {
condition(Condition.isFalse(this))
}

fun raw(value: Any) {
condition(Condition.raw(value))
}

fun build(): Condition {
if (conditions.isEmpty()) {
return Condition.all()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ConditionDslTest {
"field3" eq "value3"
"field4" eq "value4"
}
raw("1=1")
}
assertThat(
condition,
Expand Down Expand Up @@ -86,7 +87,8 @@ class ConditionDslTest {
Condition.eq("field3", "value3"),
Condition.eq("field4", "value4")
)
)
),
Condition.raw("1=1")
)
)
)
Expand Down

0 comments on commit 1637dd7

Please sign in to comment.