-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #538 from waahhh/feature/add-string-functions-subs…
…tring Add String functions(substring)
- Loading branch information
Showing
8 changed files
with
354 additions
and
0 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
128 changes: 128 additions & 0 deletions
128
dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/SubstringDslTest.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,128 @@ | ||
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 SubstringDslTest : WithAssertions { | ||
private val string1 = "string1" | ||
private val int1 = 1 | ||
private val int2 = 2 | ||
|
||
private val stringExpression1 = Expressions.value("string1") | ||
private val intExpression1 = Expressions.value(1) | ||
private val intExpression2 = Expressions.value(2) | ||
|
||
@Test | ||
fun `substring() with a string and an int`() { | ||
// when | ||
val expression = queryPart { | ||
substring(string1, int1) | ||
}.toExpression() | ||
|
||
val actual: Expression<String> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.substring( | ||
value = Expressions.value(string1), | ||
start = Expressions.value(int1), | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `substring() with a string and ints`() { | ||
// when | ||
val expression = queryPart { | ||
substring(string1, int1, int2) | ||
}.toExpression() | ||
|
||
val actual: Expression<String> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.substring( | ||
value = Expressions.value(string1), | ||
start = Expressions.value(int1), | ||
length = Expressions.value(int2), | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `substring() with a string expression and an int`() { | ||
// when | ||
val expression = queryPart { | ||
substring(stringExpression1, int1) | ||
}.toExpression() | ||
|
||
val actual: Expression<String> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.substring( | ||
value = stringExpression1, | ||
start = Expressions.value(int1), | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `substring() with a string expression and ints`() { | ||
// when | ||
val expression = queryPart { | ||
substring(stringExpression1, int1, int2) | ||
}.toExpression() | ||
|
||
val actual: Expression<String> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.substring( | ||
value = stringExpression1, | ||
start = Expressions.value(int1), | ||
length = Expressions.value(int2), | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `substring() with a string expression and an int expression`() { | ||
// when | ||
val expression = queryPart { | ||
substring(stringExpression1, intExpression1) | ||
}.toExpression() | ||
|
||
val actual: Expression<String> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.substring( | ||
value = stringExpression1, | ||
start = intExpression1, | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `substring() with a string expression and int expressions`() { | ||
// when | ||
val expression = queryPart { | ||
substring(stringExpression1, intExpression1, intExpression2) | ||
}.toExpression() | ||
|
||
val actual: Expression<String> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.substring( | ||
value = stringExpression1, | ||
start = intExpression1, | ||
length = intExpression2, | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
.../src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlSubstring.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,11 @@ | ||
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
|
||
@Internal | ||
data class JpqlSubstring internal constructor( | ||
val value: Expression<String>, | ||
val start: Expression<Int>, | ||
val length: Expression<Int>?, | ||
) : Expression<String> |
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
38 changes: 38 additions & 0 deletions
38
...ain/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlSubstringSerializer.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,38 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlSubstring | ||
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 JpqlSubstringSerializer : JpqlSerializer<JpqlSubstring> { | ||
override fun handledType(): KClass<JpqlSubstring> { | ||
return JpqlSubstring::class | ||
} | ||
|
||
override fun serialize(part: JpqlSubstring, writer: JpqlWriter, context: RenderContext) { | ||
val delegate = context.getValue(JpqlRenderSerializer) | ||
|
||
writer.write("SUBSTRING") | ||
|
||
writer.writeParentheses { | ||
delegate.serialize(part.value, writer, context) | ||
|
||
writer.write(",") | ||
writer.write(" ") | ||
|
||
delegate.serialize(part.start, writer, context) | ||
|
||
part.length?.let { | ||
writer.write(",") | ||
writer.write(" ") | ||
|
||
delegate.serialize(it, writer, context) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.