Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add String functions(lower) #507

Merged
merged 7 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}