-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
default value support #431
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22eabd2
add postgresql default value support
lookup-cat 6ab35fb
add mysql default value support
lookup-cat fcae846
fix DefaultValueTest
lookup-cat 86adc81
fix mysql default value test
lookup-cat 441760a
rename DefaultValueExpression file
lookup-cat f432ded
fix DefaultValueExpression
lookup-cat dbbf7cf
add developer
lookup-cat bdb9d76
Merge branch 'v3.6.x' into master
vincentlauvlwj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
ktorm-support-mysql/src/main/kotlin/org/ktorm/support/mysql/DefaultValueExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2018-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.ktorm.support.mysql | ||
|
||
import org.ktorm.expression.ScalarExpression | ||
import org.ktorm.schema.Column | ||
import org.ktorm.schema.SqlType | ||
import java.util.Collections.emptyMap | ||
|
||
/** | ||
* DefaultValue expression, translated to the `default` keyword in MySQL. | ||
*/ | ||
public data class DefaultValueExpression<T : Any>( | ||
override val sqlType: SqlType<T>, | ||
override val isLeafNode: Boolean = true, | ||
override val extraProperties: Map<String, Any> = emptyMap() | ||
) : ScalarExpression<T>() | ||
|
||
/** | ||
* DefaultValue expression, translated to the `default` keyword in MySQL. | ||
*/ | ||
public fun <T : Any> Column<T>.defaultValue(): DefaultValueExpression<T> = DefaultValueExpression(this.sqlType) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
ktorm-support-mysql/src/test/kotlin/org/ktorm/support/mysql/DefaultValueTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.ktorm.support.mysql | ||
|
||
import org.junit.Test | ||
import org.ktorm.database.Database | ||
import org.ktorm.dsl.eq | ||
import org.ktorm.dsl.inList | ||
import org.ktorm.dsl.insertAndGenerateKey | ||
import org.ktorm.entity.* | ||
import org.ktorm.schema.Table | ||
import org.ktorm.schema.int | ||
import org.ktorm.schema.varchar | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
class DefaultValueTest : BaseMySqlTest() { | ||
|
||
object Users : Table<User>("t_user") { | ||
val id = int("id").primaryKey().bindTo { it.id } | ||
val username = varchar("username").bindTo { it.username } | ||
val age = int("age").bindTo { it.age } | ||
} | ||
|
||
interface User : Entity<User> { | ||
var id: Int | ||
var username: String | ||
var age: Int? | ||
} | ||
|
||
private val Database.users: EntitySequence<User, Users> get() = this.sequenceOf(Users) | ||
|
||
@Test | ||
fun insert() { | ||
val id = database.insertAndGenerateKey(Users) { | ||
set(it.id, it.id.defaultValue()) | ||
set(it.username, it.username.defaultValue()) | ||
set(it.age, it.age.defaultValue()) | ||
} | ||
assertIs<Int>(id) | ||
val entity = database.users.firstOrNull { it.id eq id } | ||
assertNotNull(entity) | ||
assertNotNull(entity.id) | ||
assertEquals(entity.username, "default") | ||
assertNull(entity.age) | ||
} | ||
|
||
@Test | ||
fun bulkInsert() { | ||
database.bulkInsert(Users) { | ||
item { | ||
set(it.id, 10) | ||
set(it.username, it.username.defaultValue()) | ||
set(it.age, 10) | ||
} | ||
item { | ||
set(it.id, 11) | ||
set(it.username, it.username.defaultValue()) | ||
set(it.age, 11) | ||
} | ||
} | ||
val defaultValues = database.users.filter { it.id inList (10..11).toList() }.toList() | ||
assertEquals(defaultValues.size, 2) | ||
for (defaultValue in defaultValues) { | ||
assertEquals(defaultValue.username, "default") | ||
} | ||
|
||
database.bulkInsertOrUpdate(Users) { | ||
item { | ||
set(it.id, 10) | ||
set(it.age, 10) | ||
} | ||
onDuplicateKey { | ||
set(it.age, it.age.defaultValue()) | ||
} | ||
} | ||
val user = database.users.first { it.id eq 10 } | ||
assertNull(user.age) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
drop table if exists t_department; | ||
drop table if exists t_employee; | ||
drop table if exists t_employee; | ||
drop table if exists t_user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...support-postgresql/src/main/kotlin/org/ktorm/support/postgresql/DefaultValueExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2018-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.ktorm.support.postgresql | ||
|
||
import org.ktorm.expression.ScalarExpression | ||
import org.ktorm.schema.Column | ||
import org.ktorm.schema.SqlType | ||
import java.util.Collections.emptyMap | ||
|
||
/** | ||
* DefaultValue expression, translated to the `default` keyword in PostgreSQL. | ||
*/ | ||
public data class DefaultValueExpression<T : Any>( | ||
override val sqlType: SqlType<T>, | ||
override val isLeafNode: Boolean = true, | ||
override val extraProperties: Map<String, Any> = emptyMap() | ||
) : ScalarExpression<T>() | ||
|
||
/** | ||
* DefaultValue expression, translated to the `default` keyword in PostgreSQL. | ||
*/ | ||
public fun <T : Any> Column<T>.defaultValue(): DefaultValueExpression<T> = DefaultValueExpression(this.sqlType) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ public open class PostgreSqlFormatter( | |
is ILikeExpression -> visitILike(expr) | ||
is HStoreExpression -> visitHStore(expr) | ||
is CubeExpression -> visitCube(expr) | ||
is DefaultValueExpression -> visitDefaultValue(expr) | ||
else -> super.visitScalar(expr) | ||
} | ||
|
||
|
@@ -167,6 +168,11 @@ public open class PostgreSqlFormatter( | |
return expr | ||
} | ||
|
||
protected open fun <T : Any> visitDefaultValue(expr: DefaultValueExpression<T>): DefaultValueExpression<T> { | ||
writeKeyword("default ") | ||
return expr | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同理,新的表达式类型需要在 |
||
protected open fun visitInsertOrUpdate(expr: InsertOrUpdateExpression): InsertOrUpdateExpression { | ||
writeKeyword("insert into ") | ||
visitTable(expr.table) | ||
|
@@ -259,6 +265,7 @@ public open class PostgreSqlExpressionVisitor : SqlExpressionVisitor() { | |
is ILikeExpression -> visitILike(expr) | ||
is HStoreExpression -> visitHStore(expr) | ||
is CubeExpression -> visitCube(expr) | ||
is DefaultValueExpression -> visitDefaultValue(expr) | ||
else -> super.visitScalar(expr) | ||
} | ||
|
||
|
@@ -368,4 +375,8 @@ public open class PostgreSqlExpressionVisitor : SqlExpressionVisitor() { | |
|
||
return if (changed) result else assignments | ||
} | ||
|
||
protected open fun <T : Any> visitDefaultValue(expr: DefaultValueExpression<T>): DefaultValueExpression<T> { | ||
return expr | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
ktorm-support-postgresql/src/test/kotlin/org/ktorm/support/postgresql/DefaultValueTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.ktorm.support.postgresql | ||
|
||
import org.junit.Test | ||
import org.ktorm.database.Database | ||
import org.ktorm.dsl.eq | ||
import org.ktorm.dsl.inList | ||
import org.ktorm.entity.* | ||
import org.ktorm.schema.Table | ||
import org.ktorm.schema.int | ||
import org.ktorm.schema.varchar | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
class DefaultValueTest : BasePostgreSqlTest() { | ||
|
||
object Users : Table<User>("t_user") { | ||
val id = int("id").primaryKey().bindTo { it.id } | ||
val username = varchar("username").bindTo { it.username } | ||
val age = int("age").bindTo { it.age } | ||
} | ||
|
||
interface User : Entity<User> { | ||
var id: Int | ||
var username: String | ||
var age: Int? | ||
} | ||
|
||
private val Database.users: EntitySequence<User, Users> get() = this.sequenceOf(Users) | ||
|
||
@Test | ||
fun insert() { | ||
val id = database.insertReturning(Users, returning = Users.id) { | ||
set(it.id, it.id.defaultValue()) | ||
set(it.username, it.username.defaultValue()) | ||
set(it.age, it.age.defaultValue()) | ||
} | ||
assertNotNull(id) | ||
val entity = database.users.firstOrNull { it.id eq id } | ||
assertNotNull(entity) | ||
assertNotNull(entity.id) | ||
assertEquals(entity.username, "default") | ||
assertNull(entity.age) | ||
} | ||
|
||
@Test | ||
fun bulkInsert() { | ||
database.bulkInsert(Users) { | ||
item { | ||
set(it.id, 10) | ||
set(it.username, it.username.defaultValue()) | ||
set(it.age, 10) | ||
} | ||
item { | ||
set(it.id, 11) | ||
set(it.username, it.username.defaultValue()) | ||
set(it.age, 11) | ||
} | ||
} | ||
val defaultValues = database.users.filter { it.id inList (10..11).toList() }.toList() | ||
assertEquals(defaultValues.size, 2) | ||
for (defaultValue in defaultValues) { | ||
assertEquals(defaultValue.username, "default") | ||
} | ||
|
||
database.bulkInsertOrUpdate(Users) { | ||
item { | ||
set(it.id, 10) | ||
set(it.age, 10) | ||
} | ||
onConflict(it.id) { | ||
set(it.age, it.age.defaultValue()) | ||
} | ||
} | ||
val user = database.users.first { it.id eq 10 } | ||
assertNull(user.age) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
增加新的表达式类型后,除了此处,还需要在
MySqlExpressionVisitor
增加相应的 visit 函数,方便第三方扩展,否则新的表达式会默认派发到visitUnknown