Search in sources :

Example 1 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class Model method allow.

/**
 * 递归提取字段
 * @param table 顶层模型库表
 * @param assoc 当前关联库表
 * @param ac    当前下级关联
 * @param pc    当前层级参数
 * @param caze  当前查询用例
 * @param tn    当前表名
 * @param qn    层级名称
 * @param pn    相对层级
 * @param al    字段集合, 结构: {参数: [字段, 别名, 查询用例]}
 */
private void allow(Table table, Table assoc, Map ac, Map pc, FetchCase caze, String tn, String qn, String pn, Map al) {
    String tx, ax, az;
    tx = "`" + tn + "`.";
    if (null == qn) {
        qn = "";
        ax = "";
    } else if ("".equals(qn)) {
        qn = tn;
        ax = qn + ".";
    } else {
        qn = qn + "." + tn;
        ax = qn + ".";
    }
    if (null == pn) {
        pn = "";
        az = "";
    } else if ("".equals(pn)) {
        pn = tn;
        az = pn + ".";
    } else {
        pn = pn + "." + tn;
        az = pn + ".";
    }
    if (pc != null && pc.containsKey("fields")) {
        al.put(az + "*", new Object[] { null, null, caze });
    } else
        try {
            Map fs = assoc.getFields();
            for (Object n : fs.keySet()) {
                String f = (String) n;
                String k = f;
                String l = f;
                // 字段完整名
                f = tx + "`" + f + "`";
                // 字段别名
                l = az + /**/
                l;
                // 外部键
                k = ax + /**/
                k;
                al.put(k, new Object[] { f, l, caze });
            }
        } catch (HongsException e) {
            throw e.toExemption();
        }
    if (ac == null || ac.isEmpty()) {
        return;
    }
    Iterator it = ac.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry et = (Map.Entry) it.next();
        Map tc = (Map) et.getValue();
        String jn = (String) tc.get("join");
        // 不是 JOIN 的重置 pn, 层级名随之改变
        if (!"INNER".equals(jn) && !"LEFT".equals(jn) && !"RIGHT".equals(jn) && !"FULL".equals(jn)) {
            jn = null;
        } else {
            jn = pn;
        }
        tn = (String) et.getKey();
        ac = (Map) tc.get("assocs");
        pc = (Map) tc.get("params");
        // 获取真实的表名, 构建关联表实例
        String rn;
        rn = (String) tc.get("tableName");
        if (rn == null || "".equals(rn)) {
            rn = (String) tc.get("name");
        }
        FetchCase caxe;
        try {
            assoc = table.db.getTable(rn);
            caxe = caze.gotJoin(tn);
        } catch (HongsException e) {
            throw e.toExemption();
        }
        if (null == assoc) {
            throw new HongsExemption(1026, "Can not get table '" + rn + "' in DB '" + table.db.name + "'");
        }
        allow(table, assoc, ac, pc, caxe, tn, qn, jn, al);
    }
}
Also used : FetchCase(io.github.ihongs.db.util.FetchCase) HongsException(io.github.ihongs.HongsException) Iterator(java.util.Iterator) HongsExemption(io.github.ihongs.HongsExemption) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class Model method update.

/**
 * 更新记录
 *
 * @param rd
 * @param caze
 * @return 更新条数
 * @throws io.github.ihongs.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 xd = new HashMap(rd);
    xd.remove(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, xd);
    }
    return ids.size();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FetchCase(io.github.ihongs.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 3 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class UserAction method isUnique.

@Action("unique")
public void isUnique(ActionHelper helper) throws HongsException {
    Map rd = helper.getRequestData();
    FetchCase fc = model.fetchCase();
    fc.setOption("INCLUDE_REMOVED", Synt.declare(rd.get("include-removed"), false));
    boolean rv = model.unique(rd, fc);
    helper.reply(null, rv ? 1 : 0);
}
Also used : FetchCase(io.github.ihongs.db.util.FetchCase) HashMap(java.util.HashMap) NaviMap(io.github.ihongs.action.NaviMap) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action)

Example 4 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class RoleSet method expires.

@Override
protected byte expires(File f) throws HongsException {
    DB db;
    Table tb;
    Table td;
    FetchCase fc;
    Map rs;
    int st;
    long rt;
    long ot;
    long pt;
    db = DB.getInstance("master");
    tb = db.getTable("user");
    fc = new FetchCase(FetchCase.STRICT).from(tb.tableName, tb.name).select(tb.name + ".state, " + tb.name + ".rtime, " + tb.name + ".ptime").filter(tb.name + ".id = ?", userId);
    rs = db.fetchLess(fc);
    st = Synt.declare(rs.get("state"), 0);
    rt = Synt.declare(rs.get("rtime"), 0L);
    pt = Synt.declare(rs.get("ptime"), 0L);
    if (st <= 0) {
        // 用户不存在或已锁定,则删除
        return -1;
    }
    /**
     * 使用密码登录
     * 当密码变更时(登录时间小于密码修改时间)
     * 需要重新登录
     */
    USK: {
        ActionHelper ah;
        try {
            ah = ActionHelper.getInstance();
        } catch (UnsupportedOperationException e) {
            // 不理会非动作环境
            break USK;
        }
        if (!"*".equals(ah.getSessibute(Cnst.USK_SES))) {
            // 不理会非密码登录
            break USK;
        }
        ot = Synt.declare(ah.getSessibute(Cnst.UST_SES), 0L);
        if (ot < pt && 0 < ot && 0 < pt) {
            throw new HongsException(401, "Password changed").setLocalizedContent("core.password.changed").setLocalizedContext("master");
        }
    }
    tb = db.getTable("dept");
    td = db.getTable("dept_user");
    fc = new FetchCase(FetchCase.STRICT).from(tb.tableName, tb.name).join(td.tableName, td.name, td.name + ".dept_id = " + tb.name + ".id").select("MAX(" + tb.name + ".state) AS state, MAX(" + tb.name + ".rtime) AS rtime").filter(td.name + ".user_id = ?", userId).gather(td.name + ".user_id");
    rs = db.fetchLess(fc);
    st = Synt.declare(rs.get("state"), 1);
    ot = Synt.declare(rs.get("rtime"), 0L);
    if (st <= 0) {
        // 所在的分组均已锁定,则删除
        return -1;
    }
    /**
     * 比较文件修改时间和权限变更时间
     * 还没有过期则从缓存文件载入即可
     */
    if (rt < ot) {
        rt = ot;
    }
    if (f.exists() && f.lastModified() >= rt * 1000L) {
        return 1;
    } else {
        return 0;
    }
}
Also used : Table(io.github.ihongs.db.Table) FetchCase(io.github.ihongs.db.util.FetchCase) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) Map(java.util.Map) DB(io.github.ihongs.db.DB)

Example 5 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class RoleSet method imports.

@Override
protected void imports() throws HongsException {
    roles = new HashSet();
    DB db;
    Table tb;
    Table td;
    Table tt;
    FetchCase fc;
    List<Map> rz;
    db = DB.getInstance("master");
    // ** 查询用户权限 **/
    tb = db.getTable("user_role");
    fc = new FetchCase(FetchCase.STRICT).from(tb.tableName, tb.name).select(tb.name + ".role").filter(tb.name + ".user_id = ?", userId);
    rz = db.fetchMore(fc);
    for (Map rm : rz) {
        roles.add((String) rm.get("role"));
    }
    // ** 查询部门权限 **/
    tb = db.getTable("dept_role");
    td = db.getTable("dept_user");
    tt = db.getTable("dept");
    fc = new FetchCase(FetchCase.STRICT).from(tb.tableName, tb.name).join(td.tableName, td.name, tb.name + ".dept_id = " + td.name + ".dept_id").join(tt.tableName, tt.name, td.name + ".dept_id = " + tt.name + ".id").select(tb.name + ".role").filter(td.name + ".user_id = ?", userId).filter(tt.name + ".state > 0");
    rz = db.fetchMore(fc);
    for (Map rm : rz) {
        roles.add((String) rm.get("role"));
    }
}
Also used : Table(io.github.ihongs.db.Table) FetchCase(io.github.ihongs.db.util.FetchCase) Map(java.util.Map) DB(io.github.ihongs.db.DB) HashSet(java.util.HashSet)

Aggregations

FetchCase (io.github.ihongs.db.util.FetchCase)15 Map (java.util.Map)12 HashMap (java.util.HashMap)7 Action (io.github.ihongs.action.anno.Action)5 LinkedHashMap (java.util.LinkedHashMap)4 DB (io.github.ihongs.db.DB)3 Table (io.github.ihongs.db.Table)3 LinkedHashSet (java.util.LinkedHashSet)3 HongsException (io.github.ihongs.HongsException)2 NaviMap (io.github.ihongs.action.NaviMap)2 IAction (io.github.ihongs.dh.IAction)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Test (org.junit.Test)2 CoreConfig (io.github.ihongs.CoreConfig)1 HongsExemption (io.github.ihongs.HongsExemption)1 ActionHelper (io.github.ihongs.action.ActionHelper)1 CommitSuccess (io.github.ihongs.action.anno.CommitSuccess)1 Verify (io.github.ihongs.action.anno.Verify)1