nbcp.myoql.code.generator.db.mysql.BaseEntityInfo.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktmyoql-code-generator Show documentation
Show all versions of ktmyoql-code-generator Show documentation
kotlin orm -- mysql,mongo , just like ktorm
The newest version!
package nbcp.myoql.code.generator.db.mysql
import nbcp.base.extend.AllFields
class BaseEntityInfo(var entity: EntityDbItemData, var baseEntityClass: Array>) {
fun getBaseInterfaces(): List> {
var list = mutableListOf>()
baseEntityClass.forEach {
if (it.isInterface == false) {
return@forEach
}
if (itMatch(entity, it) == false) {
return@forEach
}
list.add(it);
}
return list;
}
private fun itMatch(entity: EntityDbItemData, it: Class<*>): Boolean {
return it.AllFields.all { field ->
var fieldName = field.name;
var column = entity.columns.find { it.fieldName == fieldName };
if (column == null) {
return@all false;
}
if (column.dbType.javaType != field.type) {
return@all false;
}
return@all true
}
}
fun getBaseClasses(): List> {
var list = mutableListOf>()
baseEntityClass.forEach {
if (it.isInterface) {
return@forEach
}
if (itMatch(entity, it) == false) {
return@forEach
}
list.add(it);
}
return list;
}
fun getColumnsWithoutBaseClasses(): Set {
var set = mutableSetOf()
getBaseClasses().forEach { type ->
set.addAll(type.AllFields.map { it.name })
}
return set;
}
fun getColumnsWithoutBaseInterfaces(): Set {
var set = mutableSetOf()
getBaseInterfaces().forEach { type ->
set.addAll(type.AllFields.map { it.name })
}
return set;
}
fun getJpaStyleFields(): List {
var baseColumns = getColumnsWithoutBaseClasses();
var baseColumnsInInterface = getColumnsWithoutBaseInterfaces();
return this.entity.columns
.filter { column ->
return@filter baseColumns.contains(column.fieldName)
}
.map { column ->
"""/** ${column.simpleComment} */
@Cn("${column.simpleComment}")
@Column(name = "${column.name}")
@DbName(name = "${column.name}")
private ${if (baseColumnsInInterface.contains(column.fieldName)) "override" else ""}${column.javaType} ${column.fieldName} ;"""
}
}
fun getBaseClasseString(): String {
var list = getBaseClasses();
if (list.any() == false) return "";
return " extends ${list.joinToString(",")}"
}
fun getBaseInterfaceString(): String {
var list = getBaseInterfaces();
if (list.any() == false) return "";
return " implements ${list.joinToString(",")}"
}
}