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

[WIP] Add if function into ktorm-support-mysql #163

Merged
merged 9 commits into from
Jul 6, 2020
Merged

[WIP] Add if function into ktorm-support-mysql #163

merged 9 commits into from
Jul 6, 2020

Conversation

hangingman
Copy link
Contributor

@hangingman hangingman commented Jul 1, 2020

Whats' this PR do ?

  • Add sum dsl functions into ktorm-support-mysql

    • sumIf: generate sum( if(column condition, ifTrue, ifFalse) )
    • sum: generate sum( column condition )
  • In MySQL, sum( boolean ) works as conditional sum

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)
    }

@hangingman hangingman marked this pull request as ready for review July 1, 2020 13:13
@hangingman
Copy link
Contributor Author

@vincentlauvlwj Hi, I try to add sum and sumIf function in ktorm-support-mysql.
I would appreciate it if you could review this PR.

Comment on lines 174 to 176
condition: ColumnDeclaring<Boolean>,
then: ColumnDeclaring<T>,
otherwise: ColumnDeclaring<T>
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment on lines +180 to +182
functionName = "if",
arguments = listOf(condition, then, otherwise).map { it.asExpression() },
sqlType = then.sqlType
Copy link
Member

Choose a reason for hiding this comment

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

Fix the indent.

Comment on lines 243 to 245
sum(IF((Employees.salary greaterEq 100L).cast(BooleanSqlType),
then = ArgumentExpression(1, IntSqlType),
otherwise = ArgumentExpression(0, IntSqlType)))
Copy link
Member

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.

Copy link
Contributor Author

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.

* without annotation, error occurred "have the same JVM signature"
https://stackoverflow.com/a/51581951/2565527
@hangingman hangingman changed the title Add sum with condition and sumIf function into ktorm-support-mysql [WIP] Add if function into ktorm-support-mysql Jul 5, 2020
@vincentlauvlwj vincentlauvlwj changed the base branch from master to v3.1.x July 6, 2020 14:41
@vincentlauvlwj vincentlauvlwj merged commit 16afc2e into kotlin-orm:v3.1.x Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants