Skip to content

Commit 92d3345

Browse files
committed
feat(ContainerOperator): 更新数据方法支持选择是否使用主键定位
1 parent 7f8b30d commit 92d3345

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

common-platform-api/src/main/kotlin/taboolib/common/platform/function/IO.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ fun debug(vararg message: Any?) {
3232
* @param message 日志内容
3333
*/
3434
fun info(vararg message: Any?) {
35-
PlatformFactory.getService<PlatformIO>().info(*message)
35+
val service = PlatformFactory.getServiceOrNull<PlatformIO>()
36+
if (service != null) {
37+
service.info(*message)
38+
} else {
39+
message.forEach { PrimitiveIO.println(it) }
40+
}
3641
}
3742

3843
/**
@@ -41,7 +46,12 @@ fun info(vararg message: Any?) {
4146
* @param message 日志内容
4247
*/
4348
fun severe(vararg message: Any?) {
44-
PlatformFactory.getService<PlatformIO>().severe(*message)
49+
val service = PlatformFactory.getServiceOrNull<PlatformIO>()
50+
if (service != null) {
51+
service.severe(*message)
52+
} else {
53+
message.forEach { PrimitiveIO.warning(it) }
54+
}
4555
}
4656

4757
/**
@@ -50,7 +60,12 @@ fun severe(vararg message: Any?) {
5060
* @param message 日志内容
5161
*/
5262
fun warning(vararg message: Any?) {
53-
PlatformFactory.getService<PlatformIO>().warning(*message)
63+
val service = PlatformFactory.getServiceOrNull<PlatformIO>()
64+
if (service != null) {
65+
service.warning(*message)
66+
} else {
67+
message.forEach { PrimitiveIO.warning(it) }
68+
}
5469
}
5570

5671
/**

module/database/database-ptc-object/src/main/kotlin/taboolib/expansion/ContainerOperator.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,17 @@ abstract class ContainerOperator {
214214
* 更新数据,借助 @Id 定位数据并更新
215215
*
216216
* @param data 数据类
217+
* @param usePrimaryKey 是否使用 @Id 定位数据,如果不使用则必须定义 filter 条件
217218
*/
218-
abstract fun update(data: Any, filter: Filter.() -> Unit = {})
219+
abstract fun update(data: Any, usePrimaryKey: Boolean = true, filter: Filter.() -> Unit = {})
219220

220221
/**
221222
* 更新数据,借助 @Id 和 @Key 定位数据并更新
222223
*
223224
* @param data 数据类
225+
* @param usePrimaryKey 是否使用 @Id 定位数据
224226
*/
225-
abstract fun updateByKey(data: Any)
227+
abstract fun updateByKey(data: Any, usePrimaryKey: Boolean = true)
226228

227229
/**
228230
* 插入数据

module/database/database-ptc-object/src/main/kotlin/taboolib/expansion/ContainerOperatorImpl.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,22 @@ class ContainerOperatorImpl(override val table: Table<*, *>, override val dataSo
6464
}.map { typeClass.createInstance(typeClass.read(this)) }
6565
}
6666

67-
override fun update(data: Any, filter: Filter.() -> Unit) {
67+
override fun update(data: Any, usePrimaryKey: Boolean, filter: Filter.() -> Unit) {
6868
val typeClass = AnalyzedClass.of(data::class.java)
6969
if (typeClass.members.none { !it.isFinal }) {
7070
error("No mutable field found.")
7171
}
72-
val name = typeClass.primaryMemberName ?: error("No primary id found.")
73-
val value = typeClass.getPrimaryMemberValue(data)
72+
val name = if (usePrimaryKey) typeClass.primaryMemberName ?: error("No primary id found.") else null
73+
val value = if (usePrimaryKey) typeClass.getPrimaryMemberValue(data) else null
7474
// 检查是否存在
7575
if (table.find(dataSource) {
7676
limit(1)
77-
where(name eq value.value())
77+
if (name != null) where(name eq value?.value())
7878
where(filter)
7979
}) {
8080
// 更新数据
8181
table.update(dataSource) {
82-
where(name eq value.value())
82+
if (name != null) where(name eq value?.value())
8383
where(filter)
8484
// 获取可变字段
8585
typeClass.members.filter { !it.isFinal }.forEach { member ->
@@ -91,12 +91,12 @@ class ContainerOperatorImpl(override val table: Table<*, *>, override val dataSo
9191
}
9292
}
9393

94-
override fun updateByKey(data: Any) {
94+
override fun updateByKey(data: Any, usePrimaryKey: Boolean, ) {
9595
val typeClass = AnalyzedClass.of(data::class.java)
9696
if (typeClass.members.none { !it.isFinal }) {
9797
error("No mutable field found.")
9898
}
99-
update(data) {
99+
update(data, usePrimaryKey) {
100100
typeClass.members.filter { it.isKey }.forEach { member ->
101101
member.name eq typeClass.getValue(data, member).value()
102102
}

module/database/database-ptc-object/src/main/kotlin/taboolib/expansion/ContainerSQLite.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ContainerSQLite(file: File) : Container<SQLite>(HostSQLite(file)) {
77

88
override fun createTableObject(type: AnalyzedClass, name: String): Table<*, *> {
99
return Table(name, host) {
10+
add { id() }
1011
type.members.forEach { member ->
1112
when {
1213
// 字符串

0 commit comments

Comments
 (0)