+
Skip to content

Add support for mixed case column names in PostgreSQL #166

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

Conversation

lyndsysimon
Copy link
Contributor

I'm building a small service using an existing PostgreSQL database, and came across a bug where column names defined with mixed case (e.g. "columnName") did not generate the correct SQL for PostgreSQL - the column names were not quoted.

PostgreSQL transforms unquoted column names to their lowercase equivalent: columnName becomes columnname, which is not defined as a column.

I resolved this by quoting all column names in the PostgreSQL dialect. To do this, I had to open the implementation of String.quoted in SqlDialect. I could have done it by overriding PostgreSqlDialect.visitColumn and PostgreSqlDialect.visitInsert, but that seemed both unnecessarily verbose and duplicated much of the logic from the parent class.

Note that I am very new to Kotlin, so please take care to ensure that my changes are appropriate.

@@ -73,7 +73,7 @@ open class SqlFormatter(
}
}

protected val String.quoted: String get() {
open protected val String.quoted: String get() {
if (this.toUpperCase() in database.keywords || !this.isIdentifier) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what isIdentifier signifies - while I don't believe there are any cases where quoting the column name in PostgreSQL will result in invalid syntax, there may be an edge case of which I'm unaware.

@@ -47,6 +47,10 @@ open class PostgreSqlFormatter(database: Database, beautifySql: Boolean, indentS
return result
}

override val String.quoted: String get() {
return "${database.identifierQuoteString}${this}${database.identifierQuoteString}".trim()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am quoting all column names. I believe quoting all columns for consistency is the right approach, but additional logic could be added to only quote column names which contain one or more uppercase characters.

Copy link
Member

@vincentlauvlwj vincentlauvlwj left a comment

Choose a reason for hiding this comment

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

Thank you for finding the bug and fixing it. Your solution is great. Always quoting the column names is the way that never get wrong.

I will merge your code. But before I merge that, please also update the build.gradle file, add your GitHub ID to the developers info (line 88).

@lyndsysimon
Copy link
Contributor Author

Awesome - done!

@vincentlauvlwj vincentlauvlwj changed the base branch from master to v3.1.x July 10, 2020 16:32
@vincentlauvlwj vincentlauvlwj changed the base branch from v3.1.x to master July 10, 2020 16:41
@vincentlauvlwj vincentlauvlwj changed the base branch from master to v3.1.x July 10, 2020 16:42
@vincentlauvlwj vincentlauvlwj merged commit 056a1b0 into kotlin-orm:v3.1.x Jul 10, 2020
@lyndsysimon
Copy link
Contributor Author

lyndsysimon commented Jul 10, 2020

Thanks for fixing my typo in build.gradle. I just discovered it as I was trying to include it in my project.

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
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载