-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query-generation): add support for $sort INTELLIJ-196 (#131)
- Loading branch information
Showing
5 changed files
with
130 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...ngodb-dialects/mongosh/src/main/kotlin/com/mongodb/jbplugin/dialects/mongosh/aggr/Sort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mongodb.jbplugin.dialects.mongosh.aggr | ||
|
||
import com.mongodb.jbplugin.dialects.mongosh.backend.MongoshBackend | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasSorts | ||
|
||
internal fun <S> MongoshBackend.emitSortStage(node: Node<S>): MongoshBackend { | ||
val sorts = node.component<HasSorts<S>>()?.children ?: emptyList() | ||
val isLongSortChain = sorts.size > 3 | ||
|
||
emitObjectStart(long = isLongSortChain) | ||
emitObjectKey(registerConstant('$' + "sort")) | ||
emitObjectStart(long = isLongSortChain) | ||
emitAsFieldValueDocument(sorts, isLongSortChain) | ||
emitObjectEnd(long = isLongSortChain) | ||
emitObjectEnd(long = isLongSortChain) | ||
|
||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...b-dialects/mongosh/src/test/kotlin/com/mongodb/jbplugin/dialects/mongosh/aggr/SortTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.mongodb.jbplugin.dialects.mongosh.aggr | ||
|
||
import com.mongodb.jbplugin.dialects.mongosh.assertGeneratedQuery | ||
import com.mongodb.jbplugin.mql.BsonInt32 | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasAggregation | ||
import com.mongodb.jbplugin.mql.components.HasFieldReference | ||
import com.mongodb.jbplugin.mql.components.HasSorts | ||
import com.mongodb.jbplugin.mql.components.HasValueReference | ||
import com.mongodb.jbplugin.mql.components.IsCommand | ||
import com.mongodb.jbplugin.mql.components.Name | ||
import com.mongodb.jbplugin.mql.components.Named | ||
import org.junit.jupiter.api.Test | ||
|
||
class SortTest { | ||
@Test | ||
fun `can format a sort stage`() { | ||
assertGeneratedQuery( | ||
""" | ||
var collection = "" | ||
var database = "" | ||
db.getSiblingDB(database).getCollection(collection).aggregate([{"${"$"}sort": {"myField": 1, "myOtherField": -1, }}, ]) | ||
""".trimIndent() | ||
) { | ||
Node( | ||
Unit, | ||
listOf( | ||
IsCommand(IsCommand.CommandType.AGGREGATE), | ||
HasAggregation( | ||
listOf( | ||
Node( | ||
Unit, | ||
listOf( | ||
Named(Name.SORT), | ||
HasSorts( | ||
listOf( | ||
Node( | ||
Unit, | ||
listOf( | ||
HasFieldReference( | ||
HasFieldReference.FromSchema( | ||
Unit, | ||
"myField" | ||
) | ||
), | ||
HasValueReference( | ||
HasValueReference.Inferred( | ||
Unit, | ||
1, | ||
BsonInt32 | ||
) | ||
) | ||
) | ||
), | ||
Node( | ||
Unit, | ||
listOf( | ||
HasFieldReference( | ||
HasFieldReference.FromSchema( | ||
Unit, | ||
"myOtherField" | ||
) | ||
), | ||
HasValueReference( | ||
HasValueReference.Inferred( | ||
Unit, | ||
-1, | ||
BsonInt32 | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} |