-
-
Couldn't load subscription status.
- Fork 155
[WIP] Add if function into ktorm-support-mysql #163
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
[WIP] Add if function into ktorm-support-mysql #163
Conversation
|
@vincentlauvlwj Hi, I try to add sum and sumIf function in ktorm-support-mysql. |
ktorm-support-mysql/src/main/kotlin/me/liuwj/ktorm/support/mysql/Functions.kt
Outdated
Show resolved
Hide resolved
ktorm-support-mysql/src/main/kotlin/me/liuwj/ktorm/support/mysql/Functions.kt
Outdated
Show resolved
Hide resolved
| condition: ColumnDeclaring<Boolean>, | ||
| then: ColumnDeclaring<T>, | ||
| otherwise: ColumnDeclaring<T> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the existing code style, fix the indent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
| functionName = "if", | ||
| arguments = listOf(condition, then, otherwise).map { it.asExpression() }, | ||
| sqlType = then.sqlType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the indent.
| sum(IF((Employees.salary greaterEq 100L).cast(BooleanSqlType), | ||
| then = ArgumentExpression(1, IntSqlType), | ||
| otherwise = ArgumentExpression(0, IntSqlType))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can have a overloaded version for Int, fun IF(condition: ColumnDeclaring<Boolean>, then: Int, otherwise: Int), then we don't have to wrap the arguments as ArgumentExpression manually.
Also, overloaded versions for frequent-used types are neccesary, like Int, Long, Double, String, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vincentlauvlwj FYI, in this point, at first I created IF function (uppercase).
However, 'detekt' gave me error because of restriction that function name should be started with lower cases.
So, I changed IF to if, and use back-quote.
ktorm-support-mysql/src/test/kotlin/me/liuwj/ktorm/support/mysql/MySqlTest.kt
Outdated
Show resolved
Hide resolved
* without annotation, error occurred "have the same JVM signature" https://stackoverflow.com/a/51581951/2565527
Whats' this PR do ?
Add sum dsl functions into ktorm-support-mysql
sumIf: generatesum( if(column condition, ifTrue, ifFalse) )sum: generatesum( column condition )In MySQL,
sum( boolean )works as conditional sumBoolean type in MySQL is represented as INT 0 and 1, just like in C.use-case
@Test fun testSumIf() { val countRich = database.from(Employees) .select(sumIf(Employees.salary greaterEq 100L, 1, 0)) .map { row -> row.getInt(1) } assert(countRich.size == 1) assert(countRich.first() == 3) } @Test fun testSum() { val countRich = database.from(Employees) .select(sum(Employees.salary greaterEq 100L)) .map { row -> row.getInt(1) } assert(countRich.size == 1) assert(countRich.first() == 3) }