KtUpdateWrapper.kt 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.extension.kotlin
  17. import com.baomidou.mybatisplus.core.conditions.SharedString
  18. import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments
  19. import com.baomidou.mybatisplus.core.conditions.update.Update
  20. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils
  21. import com.baomidou.mybatisplus.core.toolkit.StringPool
  22. import com.baomidou.mybatisplus.core.toolkit.StringUtils
  23. import java.util.concurrent.atomic.AtomicInteger
  24. import java.util.stream.Collectors.joining
  25. import kotlin.reflect.KProperty
  26. /**
  27. * Kotlin Lambda 更新封装
  28. *
  29. * @author yangyuhan
  30. * @since 2018-11-02
  31. */
  32. class KtUpdateWrapper<T : Any> : AbstractKtWrapper<T, KtUpdateWrapper<T>>, Update<KtUpdateWrapper<T>, KProperty<*>> {
  33. /**
  34. * SQL 更新字段内容,例如:name='1', age=2
  35. */
  36. private val sqlSet = ArrayList<String>()
  37. constructor(entity: T) {
  38. this.setEntity(entity)
  39. this.initNeed()
  40. }
  41. constructor(entityClass: Class<T>) {
  42. this.entityClass = entityClass
  43. this.initEntityClass()
  44. this.initNeed()
  45. }
  46. internal constructor(entity: T, paramNameSeq: AtomicInteger, paramNameValuePairs: Map<String, Any>,
  47. mergeSegments: MergeSegments, lastSql: SharedString, sqlComment: SharedString) {
  48. this.setEntity(entity)
  49. this.paramNameSeq = paramNameSeq
  50. this.paramNameValuePairs = paramNameValuePairs
  51. this.expression = mergeSegments
  52. this.lastSql = lastSql
  53. this.sqlComment = sqlComment
  54. }
  55. override fun getSqlSet(): String? {
  56. return if (CollectionUtils.isEmpty(sqlSet)) null
  57. else sqlSet.stream().collect(joining(StringPool.COMMA))
  58. }
  59. override fun setSql(condition: Boolean, sql: String): KtUpdateWrapper<T> {
  60. if (condition && StringUtils.isNotBlank(sql)) {
  61. sqlSet.add(sql)
  62. }
  63. return typedThis
  64. }
  65. override fun set(condition: Boolean, column: KProperty<*>, value: Any): KtUpdateWrapper<T> {
  66. if (condition) {
  67. sqlSet.add(String.format("%s=%s", columnToString(column), formatSql("{0}", value)))
  68. }
  69. return typedThis
  70. }
  71. override fun instance(): KtUpdateWrapper<T> {
  72. return KtUpdateWrapper(entity, paramNameSeq, paramNameValuePairs, expression, SharedString.emptyString(),
  73. SharedString.emptyString())
  74. }
  75. }