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

Conversation

svenallers
Copy link
Contributor

@svenallers svenallers commented Jul 21, 2022

Hi,
I realized during some implementation that a cast you make is actually not applied. I fixed that by overriding visitCasting() in SqlFormatter. During the implementation of visitCasting() , I also realized that

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

was a very common pattern used all over the place. I took the chance and refactored it into a single method writeExpression() to reduce the redundancy. I hope that's fine. Otherwise, I would revert it.

@svenallers
Copy link
Contributor Author

resolves #146

@vincentlauvlwj
Copy link
Member

vincentlauvlwj commented Jul 21, 2022

Thanks for your contribution, the code is perfect, but we have to ensure the cast syntax works for all supported dialects, could you please add unit tests for each of them?

write("CAST(")
writeExpression(expr.expression)
write("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

@svenallers
Copy link
Contributor Author

@vincentlauvlwj I added tests for each dialect. Was not as easy as I expected, because every dialect has its own restrictions as to what cast is allowed and MySql, Oracle as well as SQL Server do not allow casts from int to text...

@vincentlauvlwj vincentlauvlwj changed the base branch from master to v3.6.x July 22, 2022 03:03
@vincentlauvlwj vincentlauvlwj merged commit 22e7b30 into kotlin-orm:v3.6.x Jul 22, 2022
@vincentlauvlwj
Copy link
Member

Merged, will release in next version.

@svenallers svenallers deleted the implement-casting branch July 22, 2022 07:38
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