diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/db/DbConfig.kt b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/db/DbConfig.kt index 52a36578..0b5a7fe3 100644 --- a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/db/DbConfig.kt +++ b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/db/DbConfig.kt @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlin.db import java.util.NoSuchElementException diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/entry/Entry.kt b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/entry/Entry.kt index e24d8606..baad83bd 100644 --- a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/entry/Entry.kt +++ b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/entry/Entry.kt @@ -1,9 +1,24 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlin.entry import com.mybatisflex.core.dialect.DialectFactory +import com.mybatisflex.core.row.Db.* import com.mybatisflex.core.table.TableInfoFactory import com.mybatisflex.core.util.ArrayUtil -import com.mybatisflex.kotlin.extensions.db.DB import java.io.Serializable /** * 实体类父类,继承该类后将赋予实体简单增删改操作 @@ -24,7 +39,7 @@ open class Entry :Serializable{ tableInfo.invokeOnInsertListener(this) val values = tableInfo.buildInsertSqlArgs(this, ignoreNulls) val sql = DialectFactory.getDialect().forInsertEntity(tableInfo, this, ignoreNulls) - return DB.insertBySql(sql, *values) == 1 + return insertBySql(sql, *values) == 1 } fun update(ignoreNulls: Boolean = true): Boolean { @@ -35,7 +50,7 @@ open class Entry :Serializable{ val primaryValues = tableInfo.buildPkSqlArgs(this) val tenantIdArgs = tableInfo.buildTenantIdArgs() val sql = DialectFactory.getDialect().forUpdateEntity(tableInfo, this, ignoreNulls) - return DB.updateBySql(sql, *ArrayUtil.concat(updateValues, primaryValues, tenantIdArgs)) == 1 + return updateBySql(sql, *ArrayUtil.concat(updateValues, primaryValues, tenantIdArgs)) == 1 } fun deleteById(): Boolean { @@ -43,7 +58,7 @@ open class Entry :Serializable{ val primaryValues = tableInfo.buildPkSqlArgs(this) val allValues = ArrayUtil.concat(primaryValues, tableInfo.buildTenantIdArgs()) val sql = DialectFactory.getDialect().forDeleteEntityById(tableInfo) - return DB.deleteBySql(sql, *allValues) == 1 + return deleteBySql(sql, *allValues) == 1 } } diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt index 0d0dffbd..6ba8a771 100644 --- a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt +++ b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt @@ -1,460 +1,116 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlin.extensions.db import com.mybatisflex.core.BaseMapper import com.mybatisflex.core.MybatisFlexBootstrap -import com.mybatisflex.core.paginate.Page import com.mybatisflex.core.query.QueryColumn import com.mybatisflex.core.query.QueryCondition -import com.mybatisflex.core.query.QueryWrapper -import com.mybatisflex.core.row.* +import com.mybatisflex.core.row.Db.selectListByQuery +import com.mybatisflex.core.row.Db.selectOneByQuery +import com.mybatisflex.core.row.Row import com.mybatisflex.core.table.TableDef import com.mybatisflex.core.table.TableInfoFactory -import com.mybatisflex.core.transaction.Propagation import com.mybatisflex.kotlin.extensions.entry.filter import com.mybatisflex.kotlin.extensions.entry.toEntities import com.mybatisflex.kotlin.scope.QueryScope import com.mybatisflex.kotlin.scope.queryScope -import java.util.function.BiConsumer -import java.util.function.Supplier + /** - * 数据库操作对象 + * 数据库简单操作扩展 * @author 卡莫sama(yuanjiashuai) * @date 2023/8/7 */ -object DB { - - fun invoker(): RowMapperInvoker = Db.invoker() - - fun invoker(environmentId: String): RowMapperInvoker = Db.invoker(environmentId) - - inline fun > mapper(): M = MybatisFlexBootstrap.getInstance().getMapper(M::class.java) - - inline fun queryOne( - vararg columns: QueryColumn, - schema: String? = null, - tableName: String? = null, - noinline init: QueryScope.() -> Unit - ): T = queryRow(schema = schema, tableName = tableName, columns = columns, init = init).toEntity(T::class.java) - fun queryRow( - vararg columns: QueryColumn?, - schema: String? = null, - tableName: String? = null, - init: QueryScope.() -> Unit - ): Row = - selectOneByQuery( - schema = schema, - tableName = tableName, - queryWrapper = queryScope(columns = columns, init = init) - ) - inline fun query( - vararg columns: QueryColumn?, - schema: String? = null, - tableName: String? = null, - noinline init: QueryScope.() -> Unit - ): List = - queryRows(schema = schema, tableName = tableName, columns = columns, init = init) - .toEntities() +inline fun > mapper(): M = MybatisFlexBootstrap.getInstance().getMapper(M::class.java) - fun queryRows( - vararg columns: QueryColumn?, - schema: String? = null, - tableName: String? = null, - init: QueryScope.() -> Unit - ): List = selectListByQuery( - schema = schema, tableName = tableName, - queryWrapper = queryScope(columns = columns, init = init) - ) +inline fun queryOne( + vararg columns: QueryColumn, + schema: String? = null, + tableName: String? = null, + noinline init: QueryScope.() -> Unit +): T = queryRow(schema = schema, tableName = tableName, columns = columns, init = init).toEntity(T::class.java) - // filter----------- - inline fun filter( - tableName: String, - schema: String, - vararg columns: QueryColumn?, - queryCondition: QueryCondition = QueryCondition.createEmpty() - ): List = selectListByQuery( + +fun queryRow( + vararg columns: QueryColumn?, + schema: String? = null, + tableName: String? = null, + init: QueryScope.() -> Unit +): Row = + selectOneByQuery( schema, tableName, - queryScope(*columns).where(queryCondition) - ).toEntities() - - inline fun filter( - vararg columns: QueryColumn?, - init: () -> QueryCondition - ): List { - val tableInfo = TableInfoFactory.ofEntityClass(E::class.java) - return filter( - columns = columns, - schema = tableInfo.schema, - tableName = tableInfo.tableName, - queryCondition = init() - ) - } - - inline fun filter( - tableDef: T, - vararg columns: QueryColumn?, - init: T.() -> QueryCondition - ): List = tableDef.filter(columns = columns, init = init) - - - // ---------------------- - fun insert(schema: String?, tableName: String?, row: Row?): Int { - return Db.insert(schema, tableName, row) - } - - fun insert(tableName: String?, row: Row?): Int { - return Db.insert(null as String?, tableName, row) - } - - fun insertBySql(sql: String?, vararg args: Any?): Int { - return Db.insertBySql(sql, *args) - } - - fun insertBatch(schema: String?, tableName: String?, rows: Collection, batchSize: Int = rows.size): IntArray { - return Db.insertBatch(schema, tableName, rows, batchSize) - } - - - fun insertBatchWithFirstRowColumns(schema: String?, tableName: String?, rows: List?): Int { - return Db.insertBatchWithFirstRowColumns(schema, tableName, rows) - } - - fun deleteBySql(sql: String?, vararg args: Any?): Int { - return Db.deleteBySql(sql, *args) - } - - fun deleteById(schema: String?, tableName: String?, row: Row?): Int { - return Db.deleteById(schema, tableName, row) - } - - fun deleteById(tableName: String?, row: Row?): Int { - return Db.deleteById(null as String?, tableName, row) - } - - fun deleteById(schema: String?, tableName: String?, primaryKey: String?, id: Any?): Int { - return Db.deleteById(schema, tableName, primaryKey, id) - } - - fun deleteById(tableName: String?, primaryKey: String?, id: Any?): Int { - return Db.deleteById(null as String?, tableName, primaryKey, id) - } - - fun deleteBatchByIds(schema: String?, tableName: String?, primaryKey: String?, ids: Collection<*>?): Int { - return Db.deleteBatchByIds(schema, tableName, primaryKey, ids) - } - - - fun deleteByMap(schema: String?, tableName: String?, whereColumns: Map?): Int { - return Db.deleteByQuery( - schema, tableName, QueryWrapper() - .where(whereColumns) - ) - } - - fun deleteByCondition(schema: String?, tableName: String?, condition: QueryCondition?): Int { - return Db.deleteByQuery( - schema, tableName, QueryWrapper() - .where(condition) - ) - } - - - fun deleteByQuery(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Int { - return Db.deleteByQuery(schema, tableName, queryWrapper) - } - - fun updateBySql(sql: String?, vararg args: Any?): Int { - return Db.updateBySql(sql, *args) - } - - fun updateBatch(sql: String?, batchArgsSetter: BatchArgsSetter): IntArray { - return Db.updateBatch(sql, batchArgsSetter) - } - - - fun updateById(schema: String?, tableName: String?, row: Row?): Int { - return Db.updateById(schema, tableName, row) - } - - fun updateByMap(schema: String?, tableName: String?, data: Row?, whereColumns: Map?): Int { - return Db.updateByQuery( - schema, tableName, data, QueryWrapper() - .where(whereColumns) - ) - } - - - fun updateByCondition(schema: String?, tableName: String?, data: Row?, condition: QueryCondition?): Int { - return Db.updateByQuery( - schema, tableName, data, QueryWrapper() - .where(condition) - ) - } - - fun updateByQuery(schema: String?, tableName: String?, data: Row?, queryWrapper: QueryWrapper?): Int { - return Db.updateByQuery(schema, tableName, data, queryWrapper) - } - - fun updateBatchById(schema: String?, tableName: String?, rows: List?): Int { - return Db.updateBatchById(schema, tableName, rows) - } - - - fun updateEntitiesBatch(entities: Collection?, batchSize: Int): Int { - return Db.updateEntitiesBatch(entities, batchSize) - } - - fun updateEntitiesBatch(entities: Collection?): Int { - return updateEntitiesBatch(entities, 1000) - } - - fun updateNumberAddByQuery( - schema: String?, - tableName: String?, - fieldName: String?, - value: Number?, - queryWrapper: QueryWrapper? - ): Int { - return Db.updateNumberAddByQuery(schema, tableName, fieldName, value, queryWrapper) - } - - fun executeBatch( - totalSize: Int, - batchSize: Int, - mapperClass: Class?, - consumer: BiConsumer? - ): IntArray { - return Db.executeBatch(totalSize, batchSize, mapperClass, consumer) - } - - fun selectOneBySql(sql: String?, vararg args: Any?): Row { - return Db.selectOneBySql(sql, *args) - } - - - fun selectOneById(schema: String?, tableName: String?, row: Row?): Row { - return Db.selectOneById(schema, tableName, row) - } - - fun selectOneById(schema: String?, tableName: String?, primaryKey: String?, id: Any?): Row { - return Db.selectOneById(schema, tableName, primaryKey, id) - } - - fun selectOneByMap(schema: String?, tableName: String?, whereColumns: Map?): Row { - return Db.selectOneByQuery( - schema, tableName, QueryWrapper() - .where(whereColumns).limit(1) - ) - } - - fun selectOneByCondition(schema: String?, tableName: String?, condition: QueryCondition?): Row { - return Db.selectOneByQuery( - schema, tableName, QueryWrapper() - .where(condition).limit(1) - ) - } - - fun selectOneByQuery(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Row { - return Db.selectOneByQuery(schema, tableName, queryWrapper) - } - - fun selectOneByQuery(queryWrapper: QueryWrapper?): Row { - return Db.selectOneByQuery(queryWrapper) - } - - fun selectListBySql(sql: String?, vararg args: Any?): List { - return Db.selectListBySql(sql, *args) - } - - fun selectListByMap(schema: String?, tableName: String?, whereColumns: Map?): List { - return Db.selectListByQuery( - schema, tableName, QueryWrapper() - .where(whereColumns) - ) - } - - fun selectListByMap(tableName: String?, whereColumns: Map?): List { - return Db.selectListByMap(tableName, whereColumns) - } - - fun selectListByMap(schema: String?, tableName: String?, whereColumns: Map?, count: Int): List { - return Db.selectListByMap(schema, tableName, whereColumns, count) - } - - fun selectListByMap(tableName: String?, whereColumns: Map?, count: Int): List { - return Db.selectListByMap(tableName, whereColumns, count) - } - - fun selectListByCondition(schema: String?, tableName: String?, condition: QueryCondition?): List { - return Db.selectListByCondition(schema, tableName, condition) - } - - fun selectListByCondition(tableName: String?, condition: QueryCondition?): List { - return Db.selectListByQuery( - null as String?, tableName, QueryWrapper() - .where(condition) - ) - } - - fun selectListByCondition(schema: String?, tableName: String?, condition: QueryCondition?, count: Int): List { - return Db.selectListByQuery( - schema, tableName, QueryWrapper() - .where(condition).limit(count) - ) - } - - fun selectListByQuery(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): List { - return Db.selectListByQuery(schema, tableName, queryWrapper) - } - - fun selectListByQuery(queryWrapper: QueryWrapper?): List { - return Db.selectListByQuery(queryWrapper) - } - - fun selectAll(schema: String?, tableName: String?): List { - return Db.selectAll(schema, tableName) - } - - - fun selectObject(sql: String?, vararg args: Any?): Any { - return Db.selectObject(sql, *args) - } - - fun selectObject(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Any { - return Db.selectObject(schema, tableName, queryWrapper) - } - - fun selectObject(queryWrapper: QueryWrapper?): Any { - return Db.selectObject(queryWrapper) - } - - fun selectObjectList(sql: String?, vararg args: Any?): List { - return Db.selectObjectList(sql, *args) - } - - fun selectObjectList(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Any { - return Db.selectObjectList(schema, tableName, queryWrapper) - } - - fun selectObjectList(tableName: String?, queryWrapper: QueryWrapper?): Any { - return Db.selectObjectList(tableName, queryWrapper) - } - - fun selectObjectList(queryWrapper: QueryWrapper?): Any { - return Db.selectObjectList(queryWrapper) - } - - fun selectCount(sql: String?, vararg args: Any?): Long { - return Db.selectCount(sql, *args) - } - - fun selectCountByCondition(schema: String?, tableName: String?, condition: QueryCondition?): Long { - return Db.selectCountByQuery( - schema, tableName, QueryWrapper() - .where(condition) - ) - } - - fun selectCountByCondition(tableName: String?, condition: QueryCondition?): Long { - return Db.selectCountByCondition( - tableName, condition - ) - } - - fun selectCountByQuery(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Long { - return Db.selectCountByQuery(schema, tableName, queryWrapper) - } - - fun selectCountByQuery(queryWrapper: QueryWrapper?): Long = Db.selectCountByQuery(queryWrapper) - - - fun paginate( - schema: String?, - tableName: String?, - pageNumber: Int, - pageSize: Int, - condition: QueryCondition? - ): Page = Db.paginate(schema, tableName, Page(pageNumber, pageSize), QueryWrapper.create().where(condition)) - - - fun paginate( - tableName: String?, - pageNumber: Int, - pageSize: Int, - condition: QueryCondition? - ): Page = Db.paginate(tableName, pageNumber, pageSize, condition) - - - fun paginate( - schema: String?, - tableName: String?, - pageNumber: Int, - pageSize: Int, - totalRow: Int, - condition: QueryCondition? - ): Page = Db.paginate(schema, tableName, pageNumber, pageSize, totalRow, condition) - - - fun paginate( - tableName: String?, - pageNumber: Int, - pageSize: Int, - totalRow: Int, - condition: QueryCondition? - ): Page = Db.paginate(tableName, pageNumber, pageSize, totalRow, condition) - - fun paginate( - schema: String?, - tableName: String?, - pageNumber: Int, - pageSize: Int, - queryWrapper: QueryWrapper? - ): Page = Db.paginate(schema, tableName, pageNumber, pageSize, queryWrapper) - - fun paginate(tableName: String?, - pageNumber: Int, - pageSize: Int, - queryWrapper: QueryWrapper? - ): Page = Db.paginate(tableName, pageNumber, pageSize, queryWrapper) - - fun paginate( - schema: String?, - tableName: String?, - pageNumber: Int, - pageSize: Int, - totalRow: Int, - queryWrapper: QueryWrapper? - ): Page = Db.paginate(schema, tableName, pageNumber, pageSize, totalRow, queryWrapper) - - - fun paginate( - tableName: String?, - pageNumber: Int, - pageSize: Int, - totalRow: Int, - queryWrapper: QueryWrapper? - ): Page = Db.paginate(tableName, pageNumber, pageSize, totalRow, queryWrapper) - - - fun paginate(schema: String?, tableName: String?, page: Page?, queryWrapper: QueryWrapper?): Page = Db.paginate(schema, tableName, page, queryWrapper) - - fun paginate(tableName: String?, page: Page?, queryWrapper: QueryWrapper?): Page = Db.paginate(tableName, page, queryWrapper) - - fun tx(supplier: Supplier?): Boolean = tx(supplier, Propagation.REQUIRED) - - fun tx(supplier: Supplier?, propagation: Propagation?): Boolean = Db.tx(supplier, propagation) - - fun txWithResult(supplier: Supplier?): T =txWithResult(supplier, Propagation.REQUIRED) - - fun txWithResult(supplier: Supplier?, propagation: Propagation?): T = Db.txWithResult(supplier, propagation) - + queryScope(columns = columns, init = init) + ) + + +inline fun query( + vararg columns: QueryColumn?, + schema: String? = null, + tableName: String? = null, + noinline init: QueryScope.() -> Unit +): List = + queryRows(schema = schema, tableName = tableName, columns = columns, init = init) + .toEntities() + +fun queryRows( + vararg columns: QueryColumn?, + schema: String? = null, + tableName: String? = null, + init: QueryScope.() -> Unit +): List = selectListByQuery( + schema,tableName,queryScope(columns = columns, init = init) +) + +// filter----------- +inline fun filter( + tableName: String, + schema: String, + vararg columns: QueryColumn?, + queryCondition: QueryCondition = QueryCondition.createEmpty() +): List = selectListByQuery( + schema, + tableName, + queryScope(*columns).where(queryCondition) +).toEntities() + +inline fun filter( + vararg columns: QueryColumn?, + init: () -> QueryCondition +): List { + val tableInfo = TableInfoFactory.ofEntityClass(E::class.java) + return filter( + columns = columns, + schema = tableInfo.schema, + tableName = tableInfo.tableName, + queryCondition = init() + ) } +inline fun filter( + tableDef: T, + vararg columns: QueryColumn?, + init: T.() -> QueryCondition +): List = tableDef.filter(columns = columns, init = init) + diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt index 5a26103c..e12cb259 100644 --- a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt +++ b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt @@ -1,16 +1,32 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlin.extensions.entry import com.mybatisflex.core.FlexConsts import com.mybatisflex.core.dialect.DialectFactory import com.mybatisflex.core.query.QueryColumn import com.mybatisflex.core.query.QueryCondition +import com.mybatisflex.core.row.Db.* +import com.mybatisflex.kotlin.extensions.db.* import com.mybatisflex.core.row.Row import com.mybatisflex.core.row.RowUtil import com.mybatisflex.core.table.TableDef import com.mybatisflex.core.table.TableInfoFactory import com.mybatisflex.core.util.ArrayUtil import com.mybatisflex.kotlin.entry.Entry -import com.mybatisflex.kotlin.extensions.db.DB import com.mybatisflex.kotlin.scope.QueryScope import java.util.Arrays @@ -29,7 +45,7 @@ inline fun T.filter( init: T.() -> QueryCondition ): List { val tableInfo = TableInfoFactory.ofEntityClass(E::class.java) - return DB.filter( + return filter( columns = columns, schema = tableInfo.schema, tableName = tableInfo.tableName, @@ -41,7 +57,7 @@ inline fun TableDef.query( vararg columns: QueryColumn?, noinline init: QueryScope.() -> Unit ): List { - return DB.query( + return query( columns = columns, schema = this.schema, tableName = this.tableName, @@ -49,7 +65,7 @@ inline fun TableDef.query( ) } -inline fun TableDef.all(): List = DB.selectAll(schema, tableName).toEntities() +inline fun TableDef.all(): List = selectAll(schema, tableName).toEntities() inline fun Collection.toEntities() = map { it to E::class.java }.toList() @@ -68,7 +84,7 @@ inline fun List.batchInsert(): Boolean { allValues = ArrayUtil.concat(allValues, tableInfo.buildInsertSqlArgs(entity, false)) } val sql = DialectFactory.getDialect().forInsertEntityBatch(tableInfo, entities) - return DB.insertBySql(sql,*allValues) > 1 + return insertBySql(sql,*allValues) > 1 } @@ -79,5 +95,5 @@ inline fun List. batchDeleteById(): Boolean { val primaryValues = this.map { tableInfo.buildPkSqlArgs(it) }.stream().flatMap(Arrays::stream).toArray() val tenantIdArgs = tableInfo.buildTenantIdArgs() val sql = DialectFactory.getDialect().forDeleteEntityBatchByIds(tableInfo, primaryValues) - return DB.deleteBySql(sql,*ArrayUtil.concat(primaryValues, tenantIdArgs)) > 1 + return deleteBySql(sql,*ArrayUtil.concat(primaryValues, tenantIdArgs)) > 1 } diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/mapper/MaaperExtensions.kt b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/mapper/MaaperExtensions.kt index 3b630a4b..342c4984 100644 --- a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/mapper/MaaperExtensions.kt +++ b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/mapper/MaaperExtensions.kt @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlin.extensions.mapper import com.mybatisflex.core.BaseMapper diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt index 00af55f0..c84e7005 100644 --- a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt +++ b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlin.extensions.sql import com.mybatisflex.core.query.Joiner diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/BootstrapScope.kt b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/BootstrapScope.kt new file mode 100644 index 00000000..870ec4c3 --- /dev/null +++ b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/BootstrapScope.kt @@ -0,0 +1,61 @@ +package com.mybatisflex.kotlin.scope +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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. + */ + +import com.mybatisflex.core.MybatisFlexBootstrap +import javax.sql.DataSource + +class BootstrapScope(private val instant: MybatisFlexBootstrap = MybatisFlexBootstrap.getInstance()) { + + fun dataSources(dataSourceScope: DataSourceScope.() -> Unit) = + dataSourceScope(DataSourceScope(instant)) + + + operator fun Class.unaryPlus(): MybatisFlexBootstrap = + instant.addMapper(this) + + operator fun DataSource.unaryPlus(): MybatisFlexBootstrap = + instant.setDataSource(this) + + infix fun String.of(dataSource: DataSource): MybatisFlexBootstrap = + instant.setDataSource(this, dataSource) + +} + +class DataSourceScope(private val bootstrap: MybatisFlexBootstrap) { + + + fun dataSource(dataSourceKey: String, dataSource: DataSource) = + bootstrap.addDataSource(dataSourceKey, dataSource) + +// infix fun String.of(dataSource: DataSource) = +// bootstrap.addDataSource(this, dataSource) + +} + + +fun buildBootstrap( + instant: MybatisFlexBootstrap = MybatisFlexBootstrap.getInstance(), + scope: BootstrapScope.(MybatisFlexBootstrap) -> Unit +): MybatisFlexBootstrap { + scope(BootstrapScope(instant), instant) + return instant +} + + + + + diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/QueryScope.kt b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/QueryScope.kt index 70a88d3d..035141f3 100755 --- a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/QueryScope.kt +++ b/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/QueryScope.kt @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlin.scope import com.mybatisflex.core.query.QueryColumn @@ -29,7 +44,7 @@ fun queryScope(vararg columns: QueryColumn?, init: (QueryScope.() -> Unit)? = nu val builder = QueryScope() if (columns.isNotEmpty()) { - builder.select(columns) + builder.select(*columns) } //用于嵌套查询拿到上层查询包装对象 init?.also { diff --git a/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/ExtensionTest.kt b/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/ExtensionTest.kt index d6bb492a..43139621 100755 --- a/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/ExtensionTest.kt +++ b/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/ExtensionTest.kt @@ -1,19 +1,34 @@ -package com.myba +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlintest import com.mybatisflex.core.BaseMapper -import com.mybatisflex.core.MybatisFlexBootstrap +import com.mybatisflex.core.FlexConsts import com.mybatisflex.core.audit.AuditManager import com.mybatisflex.core.audit.ConsoleMessageCollector import com.mybatisflex.kotlin.entry.Entry -import com.mybatisflex.kotlin.extensions.db.DB -import com.mybatisflex.kotlin.extensions.db.DB.filter +import com.mybatisflex.kotlin.extensions.db.* import com.mybatisflex.kotlin.extensions.entry.* import com.mybatisflex.kotlin.extensions.mapper.queryList import com.mybatisflex.kotlin.extensions.sql.* +import com.mybatisflex.kotlin.scope.buildBootstrap import com.mybatisflex.kotlintest.entry.Account -import com.mybatisflex.kotlintest.entry.table.AccountTableDef import com.mybatisflex.kotlintest.entry.table.AccountTableDef.ACCOUNT import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder + import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType import javax.sql.DataSource import kotlin.streams.toList @@ -28,23 +43,28 @@ fun main() { AuditManager.setAuditEnable(true) AuditManager.setMessageCollector(ConsoleMessageCollector()) - MybatisFlexBootstrap.getInstance() - .addMapper(AccountMapper::class.java) - .setDataSource(dataSource) - .start() + + buildBootstrap { + + AccountMapper::class.java + dataSources { +// dataSource(FlexConsts.NAME,dataSource) + FlexConsts.NAME of dataSource + } +// + dataSource + }.start() filter { ACCOUNT.AGE `=` 12 or `if`(true) { ACCOUNT.ID `in` listOf(1, 2) } }.stream().peek(::println).peek { it.id = it.id.plus(2) }.forEach(Entry::save) //使用表对象filter或者DB对象有两个泛型的filter方法时方法体内this为表对象无需XXX.AA调用,直接AA - ACCOUNT.filter { - AGE `=` 12 or - `if`(true) { ID `in` listOf(1, 2) } - }.stream().peek(::println).peek { it.id = it.id.plus(2) }.forEach(Entry::save) +// ACCOUNT.filter { +// AGE `=` 12 or +// `if`(true) { ID `in` listOf(1, 2) } +// }.stream().peek(::println).peek { it.id = it.id.plus(6) }.forEach(Entry::save) println("保存后————————") - DB.mapper().findByAge(18,1,2).stream().peek { println(it) }.forEach(Entry::deleteById) + mapper().findByAge(18,1,2).stream().peek { println(it) }.forEach(Entry::deleteById) println("删除后————————") ACCOUNT.all().stream().peek { println(it) }.map { it.userName = "sa" @@ -60,8 +80,8 @@ fun main() { ACCOUNT.all().stream().peek { println(it) }.toList().filter { it.id.rem(2) == 0 }.batchDeleteById() println("批量删除后————————") - //使用DB对象查询时需指定from表 - DB.query {from(ACCOUNT)}.stream().peek { println(it) }.toList().filter { it.id.rem(3) == 0 }.map { it.userName = "哈哈" + //直接使用函数查询时需指定from表 + query {from(ACCOUNT)}.stream().peek { println(it) }.toList().filter { it.id.rem(3) == 0 }.map { it.userName = "哈哈" it }.batchUpdate() println("批量更新后————————") @@ -69,6 +89,8 @@ fun main() { ACCOUNT.query {}.forEach(::println) } + + //接口里面写方法时打这个注解才能生成Default方法否则会单独生成一个类导致报错 @JvmDefaultWithCompatibility interface AccountMapper : BaseMapper { @@ -76,9 +98,6 @@ interface AccountMapper : BaseMapper { fun findByAge(age: Int, vararg ids: Int): List = queryList { select(ACCOUNT.ALL_COLUMNS) from(ACCOUNT) - from { - select() - } where(ACCOUNT) { (AGE `=` age) and `if`(true) { ID `in` ids.asList() diff --git a/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/entry/Account.kt b/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/entry/Account.kt index 4de61fee..8380f0ce 100755 --- a/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/entry/Account.kt +++ b/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/entry/Account.kt @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 com.mybatisflex.kotlintest.entry import com.mybatisflex.annotation.Column