+
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
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 build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ subprojects { project ->
name = "afezeria"
email = "zodal@outlook.com"
}
developer {
id = "lyndsysimon"
name = "Lyndsy Simon
email = "lyndsy@lyndsysimon.com"
}
}
scm {
url = "https://github.com/vincentlauvlwj/Ktorm.git"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return "${database.identifierQuoteString}${this}${database.identifierQuoteString}".trim()
} else {
Expand Down
2 changes: 2 additions & 0 deletions ktorm-core/src/test/kotlin/me/liuwj/ktorm/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ open class BaseTest {
val id: Int
var name: String
var location: LocationWrapper
var mixedCase: String?
}

interface Employee : Entity<Employee> {
Expand All @@ -78,6 +79,7 @@ open class BaseTest {
val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
val location = varchar("location").transform({ LocationWrapper(it) }, { it.underlying }).bindTo { it.location }
val mixedCase = varchar("mixedCase").bindTo { it.mixedCase }
}

open class Employees(alias: String?) : Table<Employee>("t_employee", alias) {
Expand Down
3 changes: 2 additions & 1 deletion ktorm-core/src/test/resources/init-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
create table t_department(
id int not null primary key auto_increment,
name varchar(128) not null,
location varchar(128) not null
location varchar(128) not null,
mixedCase varchar(128)
);

create table t_employee(
Expand Down
3 changes: 2 additions & 1 deletion ktorm-support-mysql/src/test/resources/init-mysql-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
create table t_department(
id int not null primary key auto_increment,
name varchar(128) not null,
location varchar(128) not null
location varchar(128) not null,
mixedCase varchar(128)
);

create table t_employee(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}

override fun <T : Any> visitScalar(expr: ScalarExpression<T>): ScalarExpression<T> {
val result = when (expr) {
is ILikeExpression -> visitILike(expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class PostgreSqlTest : BaseTest() {
return database.insert(Departments) {
it.name to "dept name"
it.location to LocationWrapper("dept location")
it.mixedCase to "value for mixed case"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ create extension if not exists hstore;
create table t_department(
id serial primary key,
name varchar(128) not null,
location varchar(128) not null
location varchar(128) not null,
"mixedCase" varchar(123)
);

create table t_employee(
Expand All @@ -22,8 +23,8 @@ create table t_metadata(
numbers text[] not null
);

insert into t_department(name, location) values ('tech', 'Guangzhou');
insert into t_department(name, location) values ('finance', 'Beijing');
insert into t_department(name, location, "mixedCase") values ('tech', 'Guangzhou', 'one');
insert into t_department(name, location, "mixedCase") values ('finance', 'Beijing', 'two');

insert into t_employee(name, job, manager_id, hire_date, salary, department_id)
values ('vince', 'engineer', null, '2018-01-01', 100, 1);
Expand Down
3 changes: 2 additions & 1 deletion ktorm-support-sqlite/src/test/resources/init-sqlite-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
create table t_department(
id integer primary key autoincrement,
name text not null,
location text not null
location text not null,
mixedCase text
);

create table t_employee(
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载