Search in sources :

Example 6 with FetchCase

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;
}
Also used : FetchCase(app.hongs.db.util.FetchCase)

Example 7 with FetchCase

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);
}
Also used : FetchCase(app.hongs.db.util.FetchCase) Map(java.util.Map) Action(app.hongs.action.anno.Action) IAction(app.hongs.dh.IAction)

Example 8 with FetchCase

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) FetchCase(app.hongs.db.util.FetchCase) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 9 with FetchCase

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FetchCase(app.hongs.db.util.FetchCase) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Collection(java.util.Collection) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with FetchCase

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FetchCase(app.hongs.db.util.FetchCase) Collection(java.util.Collection)

Aggregations

FetchCase (app.hongs.db.util.FetchCase)17 Map (java.util.Map)14 Action (app.hongs.action.anno.Action)8 HashMap (java.util.HashMap)8 NaviMap (app.hongs.action.NaviMap)5 RoleMap (app.hongs.serv.auth.RoleMap)5 Table (app.hongs.db.Table)4 HashSet (java.util.HashSet)4 HongsException (app.hongs.HongsException)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 DB (app.hongs.db.DB)2 IAction (app.hongs.dh.IAction)2 Collection (java.util.Collection)2 CoreLocale (app.hongs.CoreLocale)1 CommitSuccess (app.hongs.action.anno.CommitSuccess)1 Verify (app.hongs.action.anno.Verify)1 RoleSet (app.hongs.serv.auth.RoleSet)1 Iterator (java.util.Iterator)1