nbcp.myoql.db.sql.extend.MyOqlSqlTreeData.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktmyoql Show documentation
Show all versions of ktmyoql Show documentation
kotlin orm -- mysql,mongo , just like ktorm
The newest version!
package nbcp.myoql.db.sql.extend
import nbcp.base.comm.JsonMap
import nbcp.base.extend.AsString
import nbcp.base.extend.CloneObject
import nbcp.base.extend.ConvertType
import nbcp.myoql.db.sql.base.SqlBaseMetaTable
import nbcp.myoql.db.sql.base.SqlColumnName
import nbcp.myoql.db.sql.component.SqlQueryClip
import java.io.Serializable
/**
* 每个级别会查询一次。
*/
class MyOqlSqlTreeData, T : Serializable>(
var baseQuery: SqlQueryClip,
var pidValue: Serializable,
var idColumnName: String,
var pidColumn: SqlColumnName
) {
var list = mutableListOf();
init {
var pids = listOf(pidValue)
while (true) {
var entitys = loadSubsFromDb(pids);
if (entitys.any() == false) {
break;
}
pids = entitys.map { it.get(idColumnName) as Serializable }
list.addAll(entitys);
}
}
fun toList(): List {
var type = baseQuery.mainEntity.entityClass;
return list.map { it.ConvertType(type) as T }
}
fun toTreeJson(
childrenFieldName: String = "children"
): List {
return getChildren(pidValue, childrenFieldName);
}
private fun getChildren(pidValue: Serializable, childrenFieldName: String = "children"): List {
var level0s = list.filter { it.get(pidColumn.getAliasName()).AsString() == pidValue.AsString() }
level0s.forEach {
it.set(childrenFieldName, getChildren(it.get(idColumnName) as Serializable))
}
return level0s;
}
private fun loadSubsFromDb(pidValue: Collection): MutableList {
var ret = baseQuery.CloneObject()
ret.where { pidColumn sqlIn pidValue.toTypedArray() }
return ret.toMapList()
}
}