feat: Extensions for Kotlin

This commit is contained in:
kamosama 2023-08-07 23:28:49 +08:00
parent 7278337ec3
commit d5d3664174
7 changed files with 23 additions and 22 deletions

View File

@ -1,7 +1,9 @@
package com.mybatisflex.kotlin.db package com.mybatisflex.kotlin.db
import java.util.NoSuchElementException
import kotlin.properties.ReadWriteProperty import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
/** /**
* 数据库配置对象暂时未启用 * 数据库配置对象暂时未启用
* @author 卡莫sama(yuanjiashuai) * @author 卡莫sama(yuanjiashuai)
@ -20,15 +22,16 @@ class IfNullVar<T : Any>(private var init: (() -> T)?) : ReadWriteProperty<Any?,
private var _value: T? = null private var _value: T? = null
override fun getValue(thisRef: Any?, property: KProperty<*>): T { override fun getValue(thisRef: Any?, property: KProperty<*>): T {
if (_value == null) { if (_value == null && init != null) {
synchronized(this) { synchronized(this) {
if (_value == null) { if (_value == null) {
_value = init!!() _value = init?.invoke()
//释放引用
init = null init = null
} }
} }
} }
return this._value!! return this._value?:throw NoSuchElementException()
} }
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {

View File

@ -3,7 +3,7 @@ package com.mybatisflex.kotlin.entry
import com.mybatisflex.core.dialect.DialectFactory import com.mybatisflex.core.dialect.DialectFactory
import com.mybatisflex.core.table.TableInfoFactory import com.mybatisflex.core.table.TableInfoFactory
import com.mybatisflex.core.util.ArrayUtil import com.mybatisflex.core.util.ArrayUtil
import com.mybatisflex.kotlin.extend.db.DB import com.mybatisflex.kotlin.extensions.db.DB
import java.io.Serializable import java.io.Serializable
/** /**
* 实体类父类继承该类后将赋予实体简单增删改操作 * 实体类父类继承该类后将赋予实体简单增删改操作

View File

@ -1,4 +1,4 @@
package com.mybatisflex.kotlin.extend.db package com.mybatisflex.kotlin.extensions.db
import com.mybatisflex.core.BaseMapper import com.mybatisflex.core.BaseMapper
import com.mybatisflex.core.MybatisFlexBootstrap import com.mybatisflex.core.MybatisFlexBootstrap
@ -10,8 +10,8 @@ import com.mybatisflex.core.row.*
import com.mybatisflex.core.table.TableDef import com.mybatisflex.core.table.TableDef
import com.mybatisflex.core.table.TableInfoFactory import com.mybatisflex.core.table.TableInfoFactory
import com.mybatisflex.core.transaction.Propagation import com.mybatisflex.core.transaction.Propagation
import com.mybatisflex.kotlin.extend.entry.filter import com.mybatisflex.kotlin.extensions.entry.filter
import com.mybatisflex.kotlin.extend.entry.toEntities import com.mybatisflex.kotlin.extensions.entry.toEntities
import com.mybatisflex.kotlin.scope.QueryScope import com.mybatisflex.kotlin.scope.QueryScope
import com.mybatisflex.kotlin.scope.queryScope import com.mybatisflex.kotlin.scope.queryScope
import java.util.function.BiConsumer import java.util.function.BiConsumer

View File

@ -1,4 +1,4 @@
package com.mybatisflex.kotlin.extend.entry package com.mybatisflex.kotlin.extensions.entry
import com.mybatisflex.core.FlexConsts import com.mybatisflex.core.FlexConsts
import com.mybatisflex.core.dialect.DialectFactory import com.mybatisflex.core.dialect.DialectFactory
@ -10,7 +10,7 @@ import com.mybatisflex.core.table.TableDef
import com.mybatisflex.core.table.TableInfoFactory import com.mybatisflex.core.table.TableInfoFactory
import com.mybatisflex.core.util.ArrayUtil import com.mybatisflex.core.util.ArrayUtil
import com.mybatisflex.kotlin.entry.Entry import com.mybatisflex.kotlin.entry.Entry
import com.mybatisflex.kotlin.extend.db.DB import com.mybatisflex.kotlin.extensions.db.DB
import com.mybatisflex.kotlin.scope.QueryScope import com.mybatisflex.kotlin.scope.QueryScope
import java.util.Arrays import java.util.Arrays
@ -49,9 +49,7 @@ inline fun <reified E> TableDef.query(
) )
} }
inline fun <reified E> TableDef.all( inline fun <reified E> TableDef.all(): List<E> = DB.selectAll(schema, tableName).toEntities()
vararg columns: QueryColumn?
): List<E> = DB.selectAll(schema, tableName).toEntities()
inline fun <reified E> Collection<Row>.toEntities() = map { it to E::class.java }.toList() inline fun <reified E> Collection<Row>.toEntities() = map { it to E::class.java }.toList()

View File

@ -1,4 +1,4 @@
package com.mybatisflex.kotlin.extend.mapper package com.mybatisflex.kotlin.extensions.mapper
import com.mybatisflex.core.BaseMapper import com.mybatisflex.core.BaseMapper
import com.mybatisflex.core.query.QueryCondition import com.mybatisflex.core.query.QueryCondition

View File

@ -1,4 +1,4 @@
package com.mybatisflex.kotlin.extend.sql package com.mybatisflex.kotlin.extensions.sql
import com.mybatisflex.core.query.Joiner import com.mybatisflex.core.query.Joiner
import com.mybatisflex.core.query.QueryColumn import com.mybatisflex.core.query.QueryColumn

View File

@ -1,19 +1,19 @@
package com.myba package com.myba
import com.mybatisflex.kotlin.extend.entry.batchDeleteById import com.mybatisflex.kotlin.extensions.entry.batchDeleteById
import com.mybatisflex.core.BaseMapper import com.mybatisflex.core.BaseMapper
import com.mybatisflex.core.MybatisFlexBootstrap import com.mybatisflex.core.MybatisFlexBootstrap
import com.mybatisflex.core.audit.AuditManager import com.mybatisflex.core.audit.AuditManager
import com.mybatisflex.core.audit.ConsoleMessageCollector import com.mybatisflex.core.audit.ConsoleMessageCollector
import com.mybatisflex.kotlin.entry.Entry import com.mybatisflex.kotlin.entry.Entry
import com.mybatisflex.kotlin.extend.db.DB import com.mybatisflex.kotlin.extensions.db.DB
import com.mybatisflex.kotlin.extend.db.DB.filter import com.mybatisflex.kotlin.extensions.db.DB.filter
import com.mybatisflex.kotlin.extend.entry.all import com.mybatisflex.kotlin.extensions.entry.all
import com.mybatisflex.kotlin.extend.entry.batchInsert import com.mybatisflex.kotlin.extensions.entry.batchInsert
import com.mybatisflex.kotlin.extend.entry.batchUpdate import com.mybatisflex.kotlin.extensions.entry.batchUpdate
import com.mybatisflex.kotlin.extend.mapper.queryList import com.mybatisflex.kotlin.extensions.mapper.queryList
import com.mybatisflex.kotlin.extend.sql.* import com.mybatisflex.kotlin.extensions.sql.*
import com.mybatisflex.kotlintest.entry.Account import com.mybatisflex.kotlintest.entry.Account
import com.mybatisflex.kotlintest.entry.table.AccountTableDef.ACCOUNT import com.mybatisflex.kotlintest.entry.table.AccountTableDef.ACCOUNT
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder