Skip to content

Commit

Permalink
Merge pull request #507 from waahhh/feature/add-string-functions-lower
Browse files Browse the repository at this point in the history
Add String functions(lower)
  • Loading branch information
shouwn authored Oct 23, 2023
2 parents b56fa24 + 05a1f75 commit f35656b
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 0 deletions.
16 changes: 16 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 @@ -1042,6 +1042,22 @@ open class Jpql : JpqlDsl {
return Expressions.type(path.toPath())
}

/**
* Creates an expression that represents the string in lowercase.
*/
@SinceJdsl("3.0.0")
fun lower(string: String): Expression<String> {
return Expressions.lower(Expressions.value(string))
}

/**
* Creates an expression that represents the string in lowercase.
*/
@SinceJdsl("3.0.0")
fun lower(string: Expressionable<String>): Expression<String> {
return Expressions.lower(string.toExpression())
}

/**
* Creates an expression that represents the length of the string as an integer.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.linecorp.kotlinjdsl.dsl.jpql.expression

import com.linecorp.kotlinjdsl.dsl.jpql.queryPart
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test

class LowerDslTest : WithAssertions {
private val string1 = "string1"

private val stringExpression1 = Expressions.value("string1")

@Test
fun `lower() with a string`() {
// when
val expression = queryPart {
lower(string1)
}.toExpression()

val actual: Expression<String> = expression // for type check

// then
val expected = Expressions.lower(
Expressions.value(string1),
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun `lower() with a string expression`() {
// when
val expression = queryPart {
lower(stringExpression1)
}.toExpression()

val actual: Expression<String> = expression // for type check

// then
val expected = Expressions.lower(
stringExpression1,
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFunction
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLength
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLiteral
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMax
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMin
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMinus
Expand Down Expand Up @@ -414,6 +415,14 @@ object Expressions {
return JpqlPathType(path)
}

/**
* Creates an expression that represents the string in lowercase.
*/
@SinceJdsl("3.0.0")
fun lower(string: Expression<String>): Expression<String> {
return JpqlLower(string)
}

/**
* Creates an expression that represents the length of the string as an integer.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression

@Internal
data class JpqlLower internal constructor(
val string: Expression<String>,
) : Expression<String>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFunction
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLength
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLiteral
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMax
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMin
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMinus
Expand Down Expand Up @@ -598,6 +599,19 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

@Test
fun lower() {
// when
val actual = Expressions.lower(stringExpression1)

// then
val expected = JpqlLower(
stringExpression1,
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun length() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLessThanSerialize
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLikeSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLiteralSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLocateSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLowerSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlMaxSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlMinSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlMinusSerializer
Expand Down Expand Up @@ -299,6 +300,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlLikeSerializer(),
JpqlLiteralSerializer(),
JpqlLocateSerializer(),
JpqlLowerSerializer(),
JpqlMaxSerializer(),
JpqlMinSerializer(),
JpqlMinusSerializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.render.RenderContext
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializer
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter
import kotlin.reflect.KClass

@Internal
class JpqlLowerSerializer : JpqlSerializer<JpqlLower> {
override fun handledType(): KClass<JpqlLower> {
return JpqlLower::class
}

override fun serialize(part: JpqlLower, writer: JpqlWriter, context: RenderContext) {
val delegate = context.getValue(JpqlRenderSerializer)

writer.write("LOWER")

writer.writeParentheses {
delegate.serialize(part.string, writer, context)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.render.TestRenderContext
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializerTest
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter
import io.mockk.impl.annotations.MockK
import io.mockk.verifySequence
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test

@JpqlSerializerTest
class JpqlLowerSerializerTest : WithAssertions {
private val sut = JpqlLowerSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

private val stringExpression1 = Expressions.value("string1")

@Test
fun handledType() {
// when
val actual = sut.handledType()

// then
assertThat(actual).isEqualTo(JpqlLower::class)
}

@Test
fun serialize() {
// given
val part = Expressions.lower(
stringExpression1,
)
val context = TestRenderContext(serializer)

// when
sut.serialize(part as JpqlLower, writer, context)

// then
verifySequence {
writer.write("LOWER")
writer.writeParentheses(any())
serializer.serialize(stringExpression1, writer, context)
}
}
}

0 comments on commit f35656b

Please sign in to comment.