Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/main/java/org/mybatis/dynamic/sql/SqlTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
package org.mybatis.dynamic.sql;

import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class SqlTable implements TableExpression {

protected String tableName;
protected final List<SqlColumn<?>> columns = new ArrayList<>();

protected SqlTable(String tableName) {
this.tableName = Objects.requireNonNull(tableName);
Expand All @@ -31,21 +34,30 @@ public String tableName() {
return tableName;
}

public List<SqlColumn<?>> columns() {
return columns;
}

public BasicColumn allColumns() {
return SqlColumn.of("*", this); //$NON-NLS-1$
}

public <T> SqlColumn<T> column(String name) {
return SqlColumn.of(name, this);
SqlColumn<T> sqlColumn = SqlColumn.of(name, this);
columns.add(sqlColumn);
return sqlColumn;
}

public <T> SqlColumn<T> column(String name, JDBCType jdbcType) {
return SqlColumn.of(name, this, jdbcType);
SqlColumn<T> sqlColumn = SqlColumn.of(name, this, jdbcType);
columns.add(sqlColumn);
return sqlColumn;
}

public <T> SqlColumn<T> column(String name, JDBCType jdbcType, String typeHandler) {
SqlColumn<T> column = SqlColumn.of(name, this, jdbcType);
return column.withTypeHandler(typeHandler);
SqlColumn<T> column = SqlColumn.of(name, this, jdbcType).withTypeHandler(typeHandler);
columns.add(column);
return column;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ fun <T : Any> SqlTable.column(
withParameterTypeConverter(parameterTypeConverter)
withJavaType(javaType?.java)
withJavaProperty(javaProperty)
build()
val column = build()
columns().add(column)
column
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2016-2025 the original author or authors.
*
* 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
*
* https://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 org.mybatis.dynamic.sql.util.kotlin.mybatis3

import org.apache.ibatis.annotations.SelectProvider
import org.mybatis.dynamic.sql.BasicColumn
import org.mybatis.dynamic.sql.SqlTable
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
import org.mybatis.dynamic.sql.util.SqlProviderAdapter
import org.mybatis.dynamic.sql.util.kotlin.CountCompleter
import org.mybatis.dynamic.sql.util.kotlin.DeleteCompleter
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
import org.mybatis.dynamic.sql.util.kotlin.UpdateCompleter
import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper
import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper
import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper

/**
* @author lidiwei
* @create 2025-12-23 14:30
*/
interface KotlinDslMapper<out Table : SqlTable, Record : Any> : CommonCountMapper, CommonDeleteMapper,
CommonInsertMapper<Record>,
CommonUpdateMapper {
val table: Table

@SelectProvider(type = SqlProviderAdapter::class, method = "select")
fun doSelectList(selectStatement: SelectStatementProvider): List<Record>

@SelectProvider(type = SqlProviderAdapter::class, method = "select")
fun doSelectOne(selectStatement: SelectStatementProvider): Record?
}

fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insert(record: Record): Int =
insert(
mapper = this::insert,
row = record,
table = this.table
) {
table.columns().forEach { withMappedColumn(it) }
}

fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.update(completer: UpdateCompleter): Int =
update(this::update, table, completer)

fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.delete(completer: DeleteCompleter): Int =
deleteFrom(this::delete, table, completer)

fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.count(completer: CountCompleter): Long =
count(this::count, table.allColumns(), table = this.table, completer = completer)

fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.selectOne(
selectList: List<BasicColumn> = table.columns(),
completer: SelectCompleter
): Record? = selectOne(this::doSelectOne, selectList, table, completer)

fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.selectList(
selectList: List<BasicColumn> = table.columns(),
completer: SelectCompleter
): List<Record> = selectList(this::doSelectList, selectList, table, completer)


fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insertBatch(vararg records: Record): List<Int> =
insertBatch(records.toList())

fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insertBatch(records: Collection<Record>): List<Int> =
insertBatch(this::insert, records, table) {
table.columns().forEach { withMappedColumn(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.apache.ibatis.annotations.SelectProvider
import org.apache.ibatis.type.JdbcType
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
import org.mybatis.dynamic.sql.util.SqlProviderAdapter
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.KotlinDslMapper
import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper
import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper
Expand Down Expand Up @@ -66,3 +67,9 @@ interface PersonMapper : CommonCountMapper, CommonDeleteMapper, CommonInsertMapp
@ResultMap("PersonResult")
fun selectOne(selectStatement: SelectStatementProvider): PersonRecord?
}

@Mapper
interface PersonDslMapper: KotlinDslMapper<PersonDynamicSqlSupport.Person, PersonRecord> {
override val table: PersonDynamicSqlSupport.Person
get() = PersonDynamicSqlSupport.person
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import org.mybatis.dynamic.sql.util.kotlin.elements.isIn
import org.mybatis.dynamic.sql.util.kotlin.elements.sortColumn
import org.mybatis.dynamic.sql.util.kotlin.elements.stringConstant
import org.mybatis.dynamic.sql.util.kotlin.elements.sum
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.count
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insert
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertInto
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertSelect
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.multiSelect
Expand Down Expand Up @@ -226,6 +228,15 @@ class PersonMapperTest {
val rows = mapper.insert(row)
assertThat(rows).isEqualTo(1)
}

sqlSessionFactory.openSession().use { session ->
val mapper = session.getMapper(PersonDslMapper::class.java)

val row = PersonRecord(100, "Joe", LastName("Jones"), Date(), true, "Developer", 1)

val rows = mapper.insert(row)
assertThat(rows).isEqualTo(1)
}
}

@Test
Expand Down Expand Up @@ -500,6 +511,19 @@ class PersonMapperTest {

assertThat(rows).isEqualTo(2L)
}

sqlSessionFactory.openSession().use { session ->
val mapper = session.getMapper(PersonDslMapper::class.java)

val rows = mapper.count {
where {
occupation.isNull()
and { employed.isFalse() }
}
}

assertThat(rows).isEqualTo(2L)
}
}

@Test
Expand Down