package org.noear.mongox;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.IndexOptions;
import org.bson.Document;
import org.noear.wood.DataItem;
import org.noear.wood.cache.CacheUsing;
import org.noear.wood.cache.ICacheController;
import org.noear.wood.cache.ICacheService;
import org.noear.wood.ext.Fun2;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Pattern;
/**
* @author noear 2021/2/5 created
*/
public class MgTableQuery implements ICacheController<MgTableQuery> {
private String table;
private Map<String, Object> whereMap;
private Map<String, Object> orderMap;
private Map<String, Object> dataItem;
private int limit_size;
private int limit_start;
private MongoX mongoX;
private void initWhereMap() {
if (whereMap == null) {
whereMap = new LinkedHashMap<>();
}
}
public MgTableQuery(MongoX mongoX) {
this.mongoX = mongoX;
}
public MgTableQuery table(String table) {
this.table = table;
return this;
}
public MgTableQuery table(Class<?> table) {
this.table = table.getSimpleName();
return this;
}
public MgTableQuery whereMap(Map<String, Object> map) {
this.whereMap = map;
return this;
}
//添加SQL where = 语句
public MgTableQuery whereTrue() {
initWhereMap();
return this;
}
/**
* <p><code>
* db.table("user").whereScript("this.age > 20 && this.age <= 40")
* </code></p>
* 添加SQL where script 语句,需要服务器开启脚本能力
*/
public MgTableQuery whereScript(String code) {
initWhereMap();
String fun = null;
if (code.contains("return ")) {
fun = "function (){" + code + "};";
} else {
fun = "function (){return " + code + "};";
}
whereMap.put("$where", fun);
return this;
}
//添加SQL where = 语句
public MgTableQuery whereEq(String field, Object val) {
initWhereMap();
whereMap.put(field, val);
return this;
}
//添加SQL where != 语句
public MgTableQuery whereNeq(String field, Object val) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$ne", val);
whereMap.put(field, tmp);
return this;
}
//添加SQL where < 语句
public MgTableQuery whereLt(String field, Object val) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$lt", val);
whereMap.put(field, tmp);
return this;
}
//添加SQL where <= 语句
public MgTableQuery whereLte(String field, Object val) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$lte", val);
whereMap.put(field, tmp);
return this;
}
//添加SQL where > 语句
public MgTableQuery whereGt(String field, Object val) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$gt", val);
whereMap.put(field, tmp);
return this;
}
//添加SQL where >= 语句
public MgTableQuery whereGte(String field, Object val) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$gte", val);
whereMap.put(field, tmp);
return this;
}
public MgTableQuery whereBtw(String field, Object start, Object end) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$gte", start);
tmp.put("$lte", end);
whereMap.put(field, tmp);
return this;
}
public MgTableQuery whereNbtw(String field, Object start, Object end) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$gte", start);
tmp.put("$lte", end);
Map<String, Object> tmp2 = new LinkedHashMap<>();
tmp2.put("$not",tmp);
whereMap.put(field, tmp2);
return this;
}
public MgTableQuery whereIn(String field, Iterable ary) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$in", ary);
whereMap.put(field, tmp);
return this;
}
public MgTableQuery whereNin(String field, Iterable ary) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$nin", ary);
whereMap.put(field, tmp);
return this;
}
public MgTableQuery whereLk(String field, String regex) {
initWhereMap();
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
whereMap.put(field, pattern);
return this;
}
public MgTableQuery whereNlk(String field, String regex) {
initWhereMap();
Pattern expr = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$not", expr);
whereMap.put(field, tmp);
return this;
}
public MgTableQuery whereMod(String field, long base, long val) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$mod", Arrays.asList(base, val));
whereMap.put(field, tmp);
return this;
}
public MgTableQuery whereNmod(String field, long base, long val) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$mod", Arrays.asList(base, val));
Map<String, Object> tmp2 = new LinkedHashMap<>();
tmp2.put("$not", tmp2);
whereMap.put(field, tmp2);
return this;
}
public MgTableQuery whereAll(String field, Iterable ary) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$all", ary);
whereMap.put(field, tmp);
return this;
}
public MgTableQuery whereSize(String field, long size) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$size", size);
whereMap.put(field, tmp);
return this;
}
public MgTableQuery whereExists(String field, boolean exists) {
initWhereMap();
Map<String, Object> tmp = new LinkedHashMap<>();
tmp.put("$exists", exists);
whereMap.put(field, tmp);
return this;
}
//
// for and
//
//添加SQL and = 语句
public MgTableQuery andEq(String field, Object val) {
return whereEq(field, val);
}
//添加SQL where != 语句
public MgTableQuery andNeq(String field, Object val) {
return whereNeq(field, val);
}
//添加SQL where < 语句
public MgTableQuery andLt(String field, Object val) {
return whereLt(field, val);
}
//添加SQL where <= 语句
public MgTableQuery andLte(String field, Object val) {
return whereLte(field, val);
}
//添加SQL where > 语句
public MgTableQuery andGt(String field, Object val) {
return whereGt(field, val);
}
//添加SQL where >= 语句
public MgTableQuery andGte(String field, Object val) {
return whereGte(field, val);
}
public MgTableQuery andBtw(String field, Object start, Object end) {
return whereBtw(field, start, end);
}
public MgTableQuery andExists(String field, boolean exists) {
return whereExists(field, exists);
}
public MgTableQuery andMod(String field, long base, long val) {
return whereMod(field, base, val);
}
public MgTableQuery andNmod(String field, long base, long val) {
return whereNmod(field, base, val);
}
public MgTableQuery andSize(String field, long size) {
return whereSize(field, size);
}
public MgTableQuery andAll(String field, Iterable ary) {
return whereAll