nbcp.myoql.db.mongo.component.MongoQueryClip.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.mongo
//import nbcp.base.comm.*
import nbcp.base.comm.ListQueryModel
import nbcp.base.comm.ListResult
import nbcp.base.comm.SortIsAsc
import nbcp.base.exception.DbDataInvalidException
import nbcp.myoql.db.db
import nbcp.myoql.db.mongo.base.MongoColumnName
import nbcp.myoql.db.mongo.component.MongoBaseMetaCollection
import nbcp.myoql.db.mongo.extend.MyOqlMongoTreeData
import nbcp.myoql.db.mongo.extend.toDocument
import org.bson.Document
import org.springframework.data.mongodb.core.query.Criteria
import java.io.Serializable
/**
* MongoQuery
*/
class MongoQueryClip, E : Any>(var moerEntity: M) :
MongoBaseQueryClip(moerEntity.tableName) {
fun limit(skip: Int, take: Int): MongoQueryClip {
this.skip = skip;
this.take = take;
return this;
}
/**
* 升序
*/
fun orderByAsc(sortFunc: (M) -> MongoColumnName): MongoQueryClip {
return this.orderBy(sortFunc(this.moerEntity).toString(), true)
}
/**
* 降序
*/
fun orderByDesc(sortFunc: (M) -> MongoColumnName): MongoQueryClip {
return this.orderBy(sortFunc(this.moerEntity).toString(), false)
}
fun orderBy(field: String, asc: Boolean): MongoQueryClip {
var sortName = db.mongo.getMongoColumnName(field)
this.sort.put(sortName, if (asc) 1 else -1)
return this;
}
/**
* 快速绑定查询列表 ListQueryModel
*/
fun withQueryModel(queryModel: ListQueryModel): MongoQueryClip {
return this
.limit(queryModel.skip, queryModel.take)
.withSorts(queryModel.sorts);
}
/**
* 快速绑定查询列表 ListQueryModel.sorts 的排序
*/
private fun withSorts(sorts: List): MongoQueryClip {
sorts.forEach {
if (it.sort.isNullOrEmpty()) {
return@forEach
}
this.orderBy(it.sort, it.asc);
}
return this;
}
fun where(whereData: Criteria): MongoQueryClip {
this.whereData.putAll(whereData.criteriaObject);
return this;
}
// fun where(whereData: List): MongoQueryClip {
// this.whereData.addAll(whereData);
// return this;
// }
fun where(where: (M) -> Criteria): MongoQueryClip {
this.whereData.putAll(where(moerEntity).criteriaObject);
return this;
}
/**
* 从数组对象中查询,并返回数组中的第一个匹配项。
* @param where: mongoElemMatch 操作符返回的对象。
*/
fun whereSelectElemMatchFirstItem(where: (M) -> Criteria): MongoQueryClip {
this.where(where);
return this.selectElemMatchFirstItem(where)
}
/**
* 返回数组中的第一个匹配项。
* @param where: mongoElemMatch 操作符返回的对象。
*/
fun selectElemMatchFirstItem(where: (M) -> Criteria): MongoQueryClip {
var doc = where(moerEntity).toDocument();
this.selectProjections.putAll(doc)
return this;
}
/**
* 获取Array的某几个
*/
fun selectArrayFirst(select: (M) -> MongoColumnName, skip: Int, take: Int): MongoQueryClip {
return selectArraySlice(select(this.moerEntity).toString(), skip, take)
}
/**
* 从Array最后位置获取。
*/
fun selectArrayLast(select: (M) -> MongoColumnName, take: Int): MongoQueryClip {
return selectArraySlice(select(this.moerEntity).toString(), if (take < 0) take else (0 - take))
}
/**
* https://docs.mongodb.com/manual/reference/operator/projection/slice/#proj._S_slice
*/
private fun selectArraySlice(select: String, vararg values: Int): MongoQueryClip {
if (values.isEmpty() || values.size > 2) {
throw DbDataInvalidException()
}
var doc = Document();
var slice = Document();
if (values.size == 1) {
slice.put("\$slice", values.first())
} else {
slice.put("\$slice", values)
}
doc.put(select, slice)
this.selectProjections.putAll(doc)
return this;
}
/**
* 使用 or 连接各个 where条件
*/
fun linkOr(vararg wheres: (M) -> Criteria): MongoQueryClip {
whereOr(*wheres.map { it(moerEntity) }.toTypedArray())
return this;
}
/**
* 对同一个字段多个条件时使用。
*/
fun linkAnd(vararg wheres: (M) -> Criteria): MongoQueryClip {
whereAnd(*wheres.map { it(moerEntity) }.toTypedArray())
return this;
}
fun whereIf(whereIf: Boolean, whereData: ((M) -> Criteria)): MongoQueryClip {
if (whereIf == false) return this;
this.whereData.putAll(whereData(moerEntity).criteriaObject);
return this;
}
fun select(vararg columns: MongoColumnName): MongoQueryClip {
selectColumns.addAll(columns.map { it.toString() })
return this;
}
fun select(vararg columns: String): MongoQueryClip {
selectColumns.addAll(columns)
return this;
}
fun select(column: (M) -> MongoColumnName): MongoQueryClip {
this.selectColumns.add(column(moerEntity).toString());
return this;
}
fun unSelect(column: (M) -> MongoColumnName): MongoQueryClip {
unSelectColumns.add(column(this.moerEntity).toString());
return this;
}
@JvmOverloads
fun toList(mapFunc: ((Document) -> Unit)? = null): MutableList {
return toList(moerEntity.entityClass, mapFunc)
}
@JvmOverloads
fun toEntity(mapFunc: ((Document) -> Unit)? = null): E? {
this.take = 1;
return toList(moerEntity.entityClass, mapFunc).firstOrNull();
}
// inline fun toEntity(): R? {
// return toEntity(R::class.java);
// }
fun toTreeJson(
idColumn: ((M) -> MongoColumnName),
pidColumn: ((M) -> MongoColumnName),
pidValue: Serializable,
childrenFieldName: String = "children"
): List {
return MyOqlMongoTreeData(
this,
db.mongo.getEntityColumnName(idColumn(this.moerEntity).toString()),
pidColumn(this.moerEntity),
pidValue,
childrenFieldName
).toTreeJson();
}
@JvmOverloads
fun toEntity(type: Class, mapFunc: ((Document) -> Unit)? = null): R? {
this.take = 1;
return toList(type, mapFunc).firstOrNull();
}
@JvmOverloads
fun toListResult(mapFunc: ((Document) -> Unit)? = null): ListResult {
return toListResult(this.moerEntity.entityClass, mapFunc);
}
// ------------------------------
fun unSelect(column: MongoColumnName): MongoQueryClip {
unSelectColumns.add(column.toString());
return this;
}
}