use of app.hongs.db.util.FetchCase in project HongsCORE by ihongs.
the class Table method fetchCase.
/**
* 调用 FetchCase 构建查询
* 可用 all, one 得到结果, 以及 delete, update 操作数据
* 但与 fetchMore,fetchLess 不同, 不会自动关联和排除已删
* @return 绑定了 db, table 的查询对象
* @throws app.hongs.HongsException
*/
public FetchCase fetchCase() throws HongsException {
FetchCase fc = new FetchCase().use(db).from(tableName, name);
AssocMore.checkCase(fc, getParams());
return fc;
}
use of app.hongs.db.util.FetchCase in project HongsCORE by ihongs.
the class DBAction method isExists.
@Action("exists")
public void isExists(ActionHelper helper) throws HongsException {
Model ett = getEntity(helper);
Map req = helper.getRequestData();
req = getReqMap(helper, ett, "exists", req);
FetchCase c = new FetchCase();
c.setOption("INCLUDE_REMOVED", Synt.declare(req.get("include-removed"), false));
boolean val = ett.exists(req, c);
helper.reply(null, val ? 1 : 0);
}
use of app.hongs.db.util.FetchCase in project HongsCORE by ihongs.
the class Model method field.
// ** 查询字段处理 **/
/**
* 绑定许可字段
* @param caze
* @param rb
*/
protected final void field(FetchCase caze, Set rb) {
if (rb == null) {
rb = new HashSet();
}
// 许可的字段
Map<String, Object[]> af = new LinkedHashMap();
// 通配符字段
Map<String, Set<String>> cf = new HashMap();
// 可关联的表
Set<String> tc = caze.getOption("ASSOCS", new HashSet());
// 包含字段
Set<String> ic = new LinkedHashSet();
// 排除字段
Set<String> ec = new LinkedHashSet();
Set<String> xc;
allow(caze, af);
// 整理出层级结构, 方便处理通配符
for (Map.Entry<String, Object[]> et : af.entrySet()) {
String fn = et.getKey();
int p = fn.lastIndexOf(".");
String k;
if (p > -1) {
k = fn.substring(0, p) + ".*";
} else {
k = "*";
}
Set<String> fs = cf.get(k);
if (fs == null) {
fs = new LinkedHashSet();
cf.put(k, fs);
// 当字段别名有点时表示是 JOIN 关联, 这种情况总是需要查询
Object[] fa = et.getValue();
// 别名
String ln = (String) fa[1];
// 用例
FetchCase fc = (FetchCase) fa[2];
if (ln.contains(".")) {
tc.add(fc.getName());
}
}
fs.add(fn);
}
for (Object fo : rb) {
String fn = fo.toString();
if (fn.startsWith("-")) {
fn = fn.substring(1);
xc = ec;
} else {
xc = ic;
}
if (cf.containsKey(fn)) {
xc.addAll(cf.get(fn));
} else if (af.containsKey(fn)) {
xc.add(fn);
// 排除时, 先在包含中增加全部
if (xc == ec) {
int p = fn.lastIndexOf(".");
if (p != -1) {
fn = fn.substring(0, p) + ".*";
} else {
fn = "*";
}
ic.addAll(cf.get(fn));
}
}
}
// 默认没给就是全部
if (ic.isEmpty() == true) {
ic.addAll(af.keySet());
}
// 排除字段即取差集
if (ec.isEmpty() == false) {
ic.removeAll(ec);
}
for (String kn : ic) {
Object[] fa = af.get(kn);
// 字段
String fn = (String) fa[0];
// 别名
String ln = (String) fa[1];
// 用例
FetchCase fc = (FetchCase) fa[2];
if (fn != null && ln != null) {
fc.select(fn + " AS `" + ln + "`");
}
tc.add(fc.getName());
}
caze.setOption("ASSOCS", tc);
}
use of app.hongs.db.util.FetchCase in project HongsCORE by ihongs.
the class Model method update.
/**
* 更新记录
*
* @param rd
* @param caze
* @return 更新条数
* @throws app.hongs.HongsException
*/
public int update(Map rd, FetchCase caze) throws HongsException {
Object idz = rd.get(Cnst.ID_KEY);
if (idz == null) {
idz = rd.get(table.primaryKey);
}
if (idz == null) {
return this.put(null, null);
}
Set<String> ids = new LinkedHashSet();
if (idz instanceof Collection) {
ids.addAll((Collection) idz);
} else {
ids.add(idz.toString());
}
Map dat = new HashMap(rd);
dat.remove(this.table.primaryKey);
// 检查是否可更新
FetchCase fc = caze != null ? caze.clone() : fetchCase();
fc.setOption("MODEL_START", "update");
permit(fc, rd, ids);
for (String id : ids) {
this.put(id, dat);
}
return ids.size();
}
use of app.hongs.db.util.FetchCase in project HongsCORE by ihongs.
the class Model method delete.
/**
* 删除记录
*
* @param rd
* @param caze
* @return 删除条数
* @throws app.hongs.HongsException
*/
public int delete(Map rd, FetchCase caze) throws HongsException {
Object idz = rd.get(Cnst.ID_KEY);
if (idz == null) {
idz = rd.get(table.primaryKey);
}
if (idz == null) {
return this.del(null, null);
}
Set<String> ids = new LinkedHashSet();
if (idz instanceof Collection) {
ids.addAll((Collection) idz);
} else {
ids.add(idz.toString());
}
// 检查是否可更新
FetchCase fc = caze != null ? caze.clone() : fetchCase();
fc.setOption("MODEL_START", "delete");
permit(fc, rd, ids);
for (String id : ids) {
this.del(id, caze);
}
return ids.size();
}
Aggregations