|
| 1 | +/* |
| 2 | + * Copyright 2016-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.mybatis.dynamic.sql.util.kotlin.mybatis3 |
| 18 | + |
| 19 | +import org.apache.ibatis.annotations.SelectProvider |
| 20 | +import org.mybatis.dynamic.sql.BasicColumn |
| 21 | +import org.mybatis.dynamic.sql.SqlTable |
| 22 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider |
| 23 | +import org.mybatis.dynamic.sql.util.SqlProviderAdapter |
| 24 | +import org.mybatis.dynamic.sql.util.kotlin.CountCompleter |
| 25 | +import org.mybatis.dynamic.sql.util.kotlin.DeleteCompleter |
| 26 | +import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter |
| 27 | +import org.mybatis.dynamic.sql.util.kotlin.UpdateCompleter |
| 28 | +import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper |
| 29 | +import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper |
| 30 | +import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper |
| 31 | +import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper |
| 32 | + |
| 33 | +/** |
| 34 | + * @author lidiwei |
| 35 | + * @create 2025-12-23 14:30 |
| 36 | + */ |
| 37 | +interface KotlinDslMapper<out Table : SqlTable, Record : Any> : CommonCountMapper, CommonDeleteMapper, |
| 38 | + CommonInsertMapper<Record>, |
| 39 | + CommonUpdateMapper { |
| 40 | + val table: Table |
| 41 | + |
| 42 | + @SelectProvider(type = SqlProviderAdapter::class, method = "select") |
| 43 | + fun doSelectList(selectStatement: SelectStatementProvider): List<Record> |
| 44 | + |
| 45 | + @SelectProvider(type = SqlProviderAdapter::class, method = "select") |
| 46 | + fun doSelectOne(selectStatement: SelectStatementProvider): Record? |
| 47 | +} |
| 48 | + |
| 49 | +fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insert(record: Record): Int = |
| 50 | + insert( |
| 51 | + mapper = this::insert, |
| 52 | + row = record, |
| 53 | + table = this.table |
| 54 | + ) { |
| 55 | + table.columns().forEach { withMappedColumn(it) } |
| 56 | + } |
| 57 | + |
| 58 | +fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.update(completer: UpdateCompleter): Int = |
| 59 | + update(this::update, table, completer) |
| 60 | + |
| 61 | +fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.delete(completer: DeleteCompleter): Int = |
| 62 | + deleteFrom(this::delete, table, completer) |
| 63 | + |
| 64 | +fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.count(completer: CountCompleter): Long = |
| 65 | + count(this::count, table.allColumns(), table = this.table, completer = completer) |
| 66 | + |
| 67 | +fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.selectOne( |
| 68 | + selectList: List<BasicColumn> = table.columns(), |
| 69 | + completer: SelectCompleter |
| 70 | +): Record? = selectOne(this::doSelectOne, selectList, table, completer) |
| 71 | + |
| 72 | +fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.selectList( |
| 73 | + selectList: List<BasicColumn> = table.columns(), |
| 74 | + completer: SelectCompleter |
| 75 | +): List<Record> = selectList(this::doSelectList, selectList, table, completer) |
| 76 | + |
| 77 | + |
| 78 | +fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insertBatch(vararg records: Record): List<Int> = |
| 79 | + insertBatch(records.toList()) |
| 80 | + |
| 81 | +fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insertBatch(records: Collection<Record>): List<Int> = |
| 82 | + insertBatch(this::insert, records, table) { |
| 83 | + table.columns().forEach { withMappedColumn(it) } |
| 84 | + } |
0 commit comments