query.insert.NativeInsert.kt Maven / Gradle / Ivy
package query.insert
import ast.expr.SqlIdentifierExpr
import ast.statement.insert.SqlInsert
import database.DB
import dsl.Query
import query.ReviseQuery
import query.select.Select
import query.select.SelectQuery
import util.toSqlString
import visitor.getExpr
import visitor.getQueryExpr
import java.sql.Connection
/**
* 无实体类的insert语句dsl类
* @property db DB 数据库类型
* @property conn Connection? 数据库连接
* @property isTransaction Boolean 是否是事务
* @property sqlInsert SqlInsert insert语法树
* @property columns MutableList 插入字段名列表
* @property records MutableList> 插入记录列表
*/
class NativeInsert(
var db: DB = DB.MYSQL,
override var conn: Connection? = null,
override var isTransaction: Boolean = false
) : ReviseQuery() {
constructor(db: DB) : this(db, null, false)
private var sqlInsert = SqlInsert()
private var columns = mutableListOf()
private var records = mutableListOf>()
/**
* insert into子句
* 例如:NativeInsert() into "t1"
* @param table String 表名
* @return NativeInsert 插入dsl
*/
infix fun into(table: String): NativeInsert {
sqlInsert.table = SqlIdentifierExpr(table)
return this
}
/**
* insert语句的字段名
* 例如:NativeInsert() into "t1" columns listOf("c1", "c2")
* @param columns List 字段名列表
* @return NativeInsert 插入dsl
*/
infix fun columns(columns: List): NativeInsert {
this.columns = columns.toMutableList()
this.sqlInsert.columns.addAll(columns.map { SqlIdentifierExpr(it) })
return this
}
/**
* insert语句的字段名
* 例如:NativeInsert().into("t1").columns("c1", "c2")
* @param column Array 字段名列表
* @return NativeInsert 插入dsl
*/
fun columns(vararg column: String): NativeInsert {
this.columns = column.toMutableList()
this.sqlInsert.columns.addAll(columns.map { SqlIdentifierExpr(it) })
return this
}
/**
* insert语句的子查询
* @param select [@kotlin.ExtensionFunctionType] Function1