Search in sources :

Example 81 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class Link method prepareStatement.

/**
 * 查询辅助 *
 */
/**
 * 预编译Statement并设置查询选项
 * <p>可使用cacheStatement开启缓存</p>
 * @param sql
 * @param params
 * @return PreparedStatement对象
 * @throws HongsException
 */
public PreparedStatement prepareStatement(String sql, Object... params) throws HongsException {
    /**
     * 检查SQL语句及Params
     * 以发现里面的Set对象
     */
    List paramz = new ArrayList(Arrays.asList(params));
    StringBuilder sb = new StringBuilder(sql);
    checkSQLParams(sb, paramz);
    sql = sb.toString();
    PreparedStatement ps = this.prepareStatement(sql);
    /**
     * 遍历params以执行PreparedStatement.setObject
     * 如果开启字符串模式, 则参数均以字符串形式绑定
     */
    try {
        int i = 0;
        for (Object x : paramz) {
            ps.setObject(++i, x);
        }
    } catch (SQLException ex) {
        throw new HongsException(0x1042, ex);
    }
    return ps;
}
Also used : SQLException(java.sql.SQLException) HongsException(app.hongs.HongsException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement)

Example 82 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class AssocCase method allow.

private void allow(Table table, Table assoc, Map ac, String tn, String qn, Map al) {
    /**
     * 三个变量: 层级名(qn), 名前缀(ax), 表前缀(tx)
     * 第一层时, 无需加名前缀, 无关联表前缀也不用加
     * 第二层时, 需将表名作为名前缀, 下级需带层级名
     */
    String tx, ax;
    tx = "`" + tn + "`.";
    if (null == qn) {
        qn = "";
        ax = "";
    } else if ("".equals(qn)) {
        qn = tn;
        ax = qn + ".";
    } else {
        qn = qn + "." + tn;
        ax = qn + ".";
    }
    try {
        Map fs = assoc.getFields();
        for (Object n : fs.keySet()) {
            String k = (String) n;
            String f = (String) n;
            k = ax + /**/
            k;
            f = tx + "`" + f + "`";
            al.put(k, f);
        }
    } catch (HongsException e) {
        throw e.toExpedient();
    }
    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 的不理会
        if (!"INNER".equals(jn) && !"LEFT".equals(jn) && !"RIGHT".equals(jn) && !"FULL".equals(jn)) {
            continue;
        }
        tn = (String) et.getKey();
        ac = (Map) tc.get("assocs");
        // 获取真实的表名, 构建关联表实例
        String rn;
        rn = (String) tc.get("tableName");
        if (rn == null || "".equals(rn)) {
            rn = (String) tc.get("name");
        }
        try {
            assoc = table.db.getTable(rn);
        } catch (HongsException e) {
            throw e.toExpedient();
        }
        if (null == assoc) {
            throw new HongsExpedient.Common("Can not get table '" + rn + "' in DB '" + table.db.name + "'");
        }
        allow(table, assoc, ac, tn, qn, al);
    }
}
Also used : HongsException(app.hongs.HongsException) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 83 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class AssocMore method fetchMore.

private static void fetchMore(Table table, FetchCase caze, List rows2, List lnks2) throws HongsException {
    Set tns = (Set) caze.getOption("ASSOCS");
    Set tps = (Set) caze.getOption("ASSOC_TYPES");
    FetchMore join = new FetchMore(rows2);
    while (!lnks2.isEmpty()) {
        List lnkz2 = new ArrayList();
        for (Map assoc : (List<Map>) lnks2) {
            String tp = (String) assoc.get("type");
            String jn = (String) assoc.get("join");
            String an = (String) assoc.get("name");
            String rn = (String) assoc.get("tableName");
            String pn = (String) assoc.get("assocName");
            if (rn == null || rn.length() == 0)
                rn = an;
            if (rn == null || rn.length() == 0)
                continue;
            // 检查是否许可关联
            if (tns != null && !tns.contains(rn) && !tns.contains(an)) {
                continue;
            }
            if (tps != null && !tps.contains(tp)) {
                continue;
            }
            Map assocs2 = (Map) assoc.get("assocs");
            String fk = (String) assoc.get("foreignKey");
            String pk = (String) assoc.get("primaryKey");
            Table table2 = table.db.getTable(rn);
            FetchCase caze2 = caze.gotJoin(an).from(table2.tableName);
            // 准备关联关系
            if ("BLS_TO".equals(tp)) {
                // 上级外键连接下级主键, 交换主外键
                String xk = fk;
                fk = pk;
                pk = xk;
                if (fk == null)
                    fk = table2.primaryKey;
                caze2.setOption("ASSOC_MULTI", false);
            } else if ("HAS_ONE".equals(tp)) {
                // 上级主键连接下级外键
                if (pk == null)
                    pk = table.primaryKey;
                caze2.setOption("ASSOC_MULTI", false);
            } else if ("HAS_MANY".equals(tp)) {
                // 上级主键连接下级外键
                if (pk == null)
                    pk = table.primaryKey;
                caze2.setOption("ASSOC_MULTI", true);
            } else if ("HAS_MORE".equals(tp)) {
                // 上级主键连接下级外键
                if (pk == null)
                    pk = table.primaryKey;
                caze2.setOption("ASSOC_MULTI", true);
                // 将下层数据合并到本层
                if (assocs2 != null) {
                    for (Map ass : (Collection<Map>) assocs2.values()) {
                        if (ass.containsKey("join") != true) {
                            ass.put("join", "MERGE");
                        }
                    }
                }
            } else {
                throw new HongsException(0x10c2, "Unrecognized assoc type '" + tp + "'");
            }
            caze2.setOption("ASSOC_MERGE", "MERGE".equals(jn));
            if (assocs2 != null) {
                fetchMore(table2, caze2, assocs2, lnkz2, null);
            }
            checkCase(caze2, (Map) assoc.get("params"), null, null, null);
            if (pn != null && !pn.equals("") && !pn.equals(caze.getName())) {
                pk = pn + "." + pk;
            }
            join.join(table2, caze2, pk, fk);
        }
        lnks2 = lnkz2;
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Table(app.hongs.db.Table) HongsException(app.hongs.HongsException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 84 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class FetchCase method getSQLDeep.

/**
 * 获取SQL组合
 * @return SQL组合
 */
private void getSQLDeep(StringBuilder t, StringBuilder f, StringBuilder w, StringBuilder g, StringBuilder h, StringBuilder o, String pn, String qn) {
    if (this.tableName == null || this.tableName.length() < 1) {
        throw new Error(new HongsException(0x10b4, "tableName can not be empty"));
    }
    // 有关联表
    boolean hasJoins = pn != null || !joinSet.isEmpty();
    // 补全语句
    boolean fixField = getOption("CLEVER_MODE", false);
    // 表名
    String tn;
    StringBuilder b = new StringBuilder();
    b.append("`").append(this.tableName).append("`");
    // 别名
    if (this.name != null && !this.name.equals("") && !this.name.equals(this.tableName)) {
        b.append(" AS `").append(this.name).append("`");
        tn = this.name;
    } else {
        tn = this.tableName;
    }
    // 关联
    if (pn != null) {
        switch(this.joinType) {
            case FetchCase.LEFT:
                b.insert(0, " LEFT JOIN ");
                break;
            case FetchCase.RIGHT:
                b.insert(0, " RIGHT JOIN ");
                break;
            case FetchCase.FULL:
                b.insert(0, " FULL JOIN ");
                break;
            case FetchCase.INNER:
                b.insert(0, " INNER JOIN ");
                break;
            case FetchCase.CROSS:
                b.insert(0, " CROSS JOIN ");
                break;
            default:
                return;
        }
        if (this.joinExpr != null && this.joinExpr.length() != 0) {
            CharSequence s = this.joinExpr;
            if (fixField) {
                s = fixSQLField(s, tn, pn);
            }
            b.append(" ON ").append(s);
        }
        /**
         * 别名层级
         * 第一层是不许前缀的
         * 第二层等同于与表名
         * 二层之后携带所有上级表名(不包含第一层表名)
         * 下面代码逻辑需从下往上看
         */
        if (!"".equals(qn)) {
            qn = qn + "." + tn;
        } else {
            qn = tn;
        }
    } else {
        qn = "";
    }
    t.append(b);
    // 字段
    if (this.fields.length() != 0) {
        CharSequence s = this.fields.toString().trim();
        // 为关联表的查询列添加层级名
        if (fixField && null != joinName) {
            if (0 < joinName.length()) {
                s = fixSQLAlias(s, joinName);
            } else if (0 < qn.length()) {
                s = fixSQLAlias(s, qn);
            }
        }
        if (fixField) {
            if (hasJoins) {
                s = fixSQLField(s, tn, pn);
            } else {
                s = fixSQLPoint(s, "", pn);
            }
        }
        f.append(" ").append(s);
    }
    // 条件
    if (this.wheres.length() != 0) {
        CharSequence s = this.wheres.toString().trim();
        if (fixField) {
            if (hasJoins) {
                s = fixSQLField(s, tn, pn);
            } else {
                s = fixSQLPoint(s, "", pn);
            }
        }
        w.append(" ").append(s);
    }
    // 分组
    if (this.groups.length() != 0) {
        CharSequence s = this.groups.toString().trim();
        if (fixField) {
            if (hasJoins) {
                s = fixSQLField(s, tn, pn);
            } else {
                s = fixSQLPoint(s, "", pn);
            }
        }
        g.append(" ").append(s);
    }
    // 筛选
    if (this.havins.length() != 0) {
        CharSequence s = this.havins.toString().trim();
        if (fixField) {
            if (hasJoins) {
                s = fixSQLField(s, tn, pn);
            } else {
                s = fixSQLPoint(s, "", pn);
            }
        }
        h.append(" ").append(s);
    }
    // 排序
    if (this.orders.length() != 0) {
        CharSequence s = this.orders.toString().trim();
        if (fixField) {
            if (hasJoins) {
                s = fixSQLField(s, tn, pn);
            } else {
                s = fixSQLPoint(s, "", pn);
            }
        }
        o.append(" ").append(s);
    }
    // 下级
    for (FetchCase caze : this.joinSet) {
        if (caze.joinType != 0) {
            caze.getSQLDeep(t, f, w, g, h, o, tn, qn);
        }
    }
}
Also used : HongsException(app.hongs.HongsException)

Example 85 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class JointGate method acting.

@Override
public void acting(ActionHelper helper, ActionRunner runner) throws HongsException {
    String act = runner.getHandle();
    String ent = runner.getEntity();
    String mod = runner.getModule();
    try {
        // 探测实体是否为独占模块, 方便自动机处理
        if (FormSet.hasConfFile(mod + "/" + ent)) {
            mod = mod + "/" + ent;
            runner.setModule(mod);
        }
        // 下划线开头的为内部资源, 不直接对外开放
        if (ent.startsWith("_")) {
            throw new HongsException(0x1100, "Unsupported Request!");
        }
        // 判断是否禁用了当前动作, 忽略表单不存在
        Map fa = FormSet.getInstance(mod).getForm(ent);
        Set ca = Synt.toSet(Dict.getDepth(fa, "@", "callable"));
        if (ca != null && !ca.contains(act)) {
            throw new HongsException(0x1100, "Unsupported Request.");
        }
    } catch (HongsException | HongsExpedient ex) {
        int ec = ex.getErrno();
        if (ec != 0x10e8 && ec != 0x10ea) {
            throw ex;
        }
    }
}
Also used : Set(java.util.Set) FormSet(app.hongs.action.FormSet) HongsException(app.hongs.HongsException) HongsExpedient(app.hongs.HongsExpedient) NaviMap(app.hongs.action.NaviMap) Map(java.util.Map)

Aggregations

HongsException (app.hongs.HongsException)89 Map (java.util.Map)42 HashMap (java.util.HashMap)34 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)15 LinkedHashMap (java.util.LinkedHashMap)15 Set (java.util.Set)15 List (java.util.List)13 File (java.io.File)11 SQLException (java.sql.SQLException)10 FileNotFoundException (java.io.FileNotFoundException)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 PreparedStatement (java.sql.PreparedStatement)8 Iterator (java.util.Iterator)8 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 HongsExpedient (app.hongs.HongsExpedient)7 FormSet (app.hongs.action.FormSet)7 Table (app.hongs.db.Table)7