Skip to content

Make casting work #415

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

Merged
merged 2 commits into from
Jul 22, 2022
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
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/ktorm.maven-publish.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ publishing {
name.set("ccr")
email.set("2938137849@qq.com")
}
developer {
id.set("svenallers")
name.set("Sven Allers")
email.set("sven.allers@gmx.de")
}
}
}
}
Expand Down
66 changes: 23 additions & 43 deletions ktorm-core/src/main/kotlin/org/ktorm/expression/SqlFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public abstract class SqlFormatter(
_builder.append(value)
}

protected fun writeExpression(expr: SqlExpression) {
if (expr.removeBrackets) {
visit(expr)
} else {
write("(")
visit(expr)
removeLastBlank()
write(") ")
}
}

protected fun writeKeyword(keyword: String) {
when (database.generateSqlInUpperCase) {
true -> {
Expand Down Expand Up @@ -210,53 +221,20 @@ public abstract class SqlFormatter(

override fun <T : Any> visitUnary(expr: UnaryExpression<T>): UnaryExpression<T> {
if (expr.type == UnaryExpressionType.IS_NULL || expr.type == UnaryExpressionType.IS_NOT_NULL) {
if (expr.operand.removeBrackets) {
visit(expr.operand)
} else {
write("(")
visit(expr.operand)
removeLastBlank()
write(") ")
}

writeExpression(expr.operand)
writeKeyword("${expr.type} ")
} else {
writeKeyword("${expr.type} ")

if (expr.operand.removeBrackets) {
visit(expr.operand)
} else {
write("(")
visit(expr.operand)
removeLastBlank()
write(") ")
}
writeExpression(expr.operand)
}

return expr
}

override fun <T : Any> visitBinary(expr: BinaryExpression<T>): BinaryExpression<T> {
if (expr.left.removeBrackets) {
visit(expr.left)
} else {
write("(")
visit(expr.left)
removeLastBlank()
write(") ")
}

writeExpression(expr.left)
writeKeyword("${expr.type} ")

if (expr.right.removeBrackets) {
visit(expr.right)
} else {
write("(")
visit(expr.right)
removeLastBlank()
write(") ")
}

writeExpression(expr.right)
return expr
}

Expand Down Expand Up @@ -314,13 +292,8 @@ public abstract class SqlFormatter(
if (expr.declaredName != null && expr.declaredName.isNotBlank()) {
checkColumnName(expr.declaredName)
write("${expr.declaredName.quoted} ")
} else if (expr.expression.removeBrackets) {
visit(expr.expression)
} else {
write("(")
visit(expr.expression)
removeLastBlank()
write(") ")
writeExpression(expr.expression)
}

return expr
Expand Down Expand Up @@ -418,6 +391,13 @@ public abstract class SqlFormatter(
return expr
}

override fun <T : Any> visitCasting(expr: CastingExpression<T>): CastingExpression<T> {
writeKeyword("cast(")
writeExpression(expr.expression)
writeKeyword("as ${expr.sqlType.typeName}) ")
return expr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the writeKeyword function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

override fun visitUnion(expr: UnionExpression): UnionExpression {
when (expr.left) {
is SelectExpression -> visitQuerySource(expr.left)
Expand Down
20 changes: 20 additions & 0 deletions ktorm-core/src/test/kotlin/org/ktorm/dsl/QueryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package org.ktorm.dsl

import org.junit.Test
import org.ktorm.BaseTest
import org.ktorm.database.use
import org.ktorm.expression.ScalarExpression
import org.ktorm.schema.TextSqlType
import java.sql.Clob
import kotlin.random.Random
import kotlin.test.assertContentEquals

/**
* Created by vince on Dec 07, 2018.
Expand Down Expand Up @@ -203,6 +207,22 @@ class QueryTest : BaseTest() {
println(names)
}

@Test
fun testCast() {
val salaries = database
.from(Employees)
.select(Employees.salary.cast(TextSqlType))
.where { Employees.salary eq 200 }
.map { row ->
when(val value = row.getObject(1)) {
is Clob -> value.characterStream.use { it.readText() }
else -> value
}
}

assertContentEquals(listOf("200"), salaries)
}

@Test
fun testInList() {
val query = database
Expand Down
22 changes: 21 additions & 1 deletion ktorm-global/src/test/kotlin/org/ktorm/global/GlobalQueryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package org.ktorm.global

import org.junit.Test
import org.ktorm.database.DialectFeatureNotSupportedException
import org.ktorm.database.use
import org.ktorm.dsl.*
import org.ktorm.expression.ScalarExpression
import org.ktorm.schema.TextSqlType
import java.sql.Clob
import kotlin.test.assertContentEquals

/**
* Created by vince at Apr 05, 2020.
Expand Down Expand Up @@ -195,6 +199,22 @@ class GlobalQueryTest : BaseGlobalTest() {
println(names)
}

@Test
fun testCast() {
val salaries = database
.from(Employees)
.select(Employees.salary.cast(TextSqlType))
.where { Employees.salary eq 200 }
.map { row ->
when(val value = row.getObject(1)) {
is Clob -> value.characterStream.use { it.readText() }
else -> value
}
}

assertContentEquals(listOf("200"), salaries)
}

@Test
fun testInList() {
val query = Employees
Expand Down Expand Up @@ -256,4 +276,4 @@ class GlobalQueryTest : BaseGlobalTest() {
assert(query.rowSet.size() == 2)
println(query.sql)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.ktorm.support.mysql

import org.junit.Test
import org.ktorm.dsl.*
import org.ktorm.schema.FloatSqlType
import kotlin.test.assertContentEquals

class QueryTest : BaseMySqlTest() {

@Test
fun testCast() {
val salaries = database
.from(Employees)
.select(Employees.salary.cast(FloatSqlType))
.where { Employees.salary eq 200 }
.map { it.getObject(1) }

assertContentEquals(listOf(200.0f), salaries)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class CommonTest : BaseOracleTest() {
}

assert(database.sequenceOf(t).filter { it.id eq 1 }.mapTo(HashSet()) { it.name } == setOf("test"))
assert(database.sequenceOf(t.aliased("t")).mapTo(HashSet()) { it.name } == setOf("test", "finance"))
assert(database.sequenceOf(t.aliased("t")).mapTo(HashSet()) { it.name } == setOf("test", "finance", "ai"))
}

@Test
Expand Down Expand Up @@ -149,4 +149,4 @@ class CommonTest : BaseOracleTest() {
assert("too long" in e.message!!)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.ktorm.support.oracle

import org.junit.Test
import org.ktorm.dsl.*
import org.ktorm.schema.IntSqlType
import java.math.BigDecimal
import kotlin.test.assertContentEquals

class QueryTest : BaseOracleTest() {

@Test
fun testCast() {
val mixedCases = database
.from(Departments)
.select(Departments.mixedCase.cast(IntSqlType))
.where { Departments.mixedCase eq "123" }
.map { it.getObject(1)}

assertContentEquals(listOf(BigDecimal(123)), mixedCases)
}
}
3 changes: 2 additions & 1 deletion ktorm-support-oracle/src/test/resources/init-oracle-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ create table "t_employee"(

insert into "t_department"("id", "name", "location") values (1, 'tech', 'Guangzhou');
insert into "t_department"("id", "name", "location") values (2, 'finance', 'Beijing');
insert into "t_department"("id", "name", "location", "mixedCase") values (3, 'ai', 'Hamburg', '123');

insert into "t_employee"("id", "name", "job", "manager_id", "hire_date", "salary", "department_id")
values (1, 'vince', 'engineer', null, to_date('2018-01-01', 'yyyy-MM-dd'), 100, 1);
Expand Down Expand Up @@ -48,4 +49,4 @@ insert into "t_employee0"("id", "name", "job", "manager_id", "hire_date", "salar
insert into "t_employee0"("id", "name", "job", "manager_id", "hire_date", "salary", "department_id")
values (3, 'tom', 'director', null, to_date('2018-01-01', 'yyyy-MM-dd'), 200, 2);
insert into "t_employee0"("id", "name", "job", "manager_id", "hire_date", "salary", "department_id")
values (4, 'penny', 'assistant', 3, to_date('2019-01-01', 'yyyy-MM-dd'), 100, 2);
values (4, 'penny', 'assistant', 3, to_date('2019-01-01', 'yyyy-MM-dd'), 100, 2);
Original file line number Diff line number Diff line change
Expand Up @@ -147,74 +147,23 @@ public open class PostgreSqlFormatter(
}

protected open fun visitILike(expr: ILikeExpression): ILikeExpression {
if (expr.left.removeBrackets) {
visit(expr.left)
} else {
write("(")
visit(expr.left)
removeLastBlank()
write(") ")
}

writeExpression(expr.left)
writeKeyword("ilike ")

if (expr.right.removeBrackets) {
visit(expr.right)
} else {
write("(")
visit(expr.right)
removeLastBlank()
write(") ")
}

writeExpression(expr.right)
return expr
}

protected open fun <T : Any> visitHStore(expr: HStoreExpression<T>): HStoreExpression<T> {
if (expr.left.removeBrackets) {
visit(expr.left)
} else {
write("(")
visit(expr.left)
removeLastBlank()
write(") ")
}

writeExpression(expr.left)
writeKeyword("${expr.type} ")

if (expr.right.removeBrackets) {
visit(expr.right)
} else {
write("(")
visit(expr.right)
removeLastBlank()
write(") ")
}

writeExpression(expr.right)
return expr
}

protected open fun <T : Any> visitCube(expr: CubeExpression<T>): CubeExpression<T> {
if (expr.left.removeBrackets) {
visit(expr.left)
} else {
write("(")
visit(expr.left)
removeLastBlank()
write(") ")
}

writeExpression(expr.left)
writeKeyword("${expr.type} ")

if (expr.right.removeBrackets) {
visit(expr.right)
} else {
write("(")
visit(expr.right)
removeLastBlank()
write(") ")
}

writeExpression(expr.right)
return expr
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.ktorm.support.postgresql

import org.junit.Test
import org.ktorm.database.use
import org.ktorm.dsl.*
import org.ktorm.schema.TextSqlType
import java.sql.Clob
import kotlin.test.assertEquals

class QueryTest : BasePostgreSqlTest() {

@Test
fun testCast() {
val salaries = database
.from(Employees)
.select(Employees.salary.cast(TextSqlType))
.where { Employees.salary eq 200 }
.map { row ->
when(val value = row.getObject(1)) {
is Clob -> value.characterStream.use { it.readText() }
else -> value
}
}

assertEquals(listOf("200"), salaries)
}
}
Loading