Search in sources :

Example 81 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Async method close.

@Override
public void close() {
    if (!servs.isShutdown()) {
        servs.shutdownNow();
    }
    if (back == null) {
        if (!tasks.isEmpty()) {
            CoreLogger.error("There has {} task(s) not run.", tasks.size());
        }
        return;
    }
    File file;
    file = back;
    back = null;
    if (!tasks.isEmpty()) {
        try {
            save(file);
            CoreLogger.trace("There has {} task(s) not run, save to '{}'.", tasks.size(), file.getPath());
        } catch (HongsException ex) {
            CoreLogger.error(ex);
        }
    } else if (file.exists()) {
        file.delete();
    }
}
Also used : HongsException(io.github.ihongs.HongsException) File(java.io.File)

Example 82 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Model method get.

public Map get(String id, FetchCase caze) throws HongsException {
    if (id == null || id.length() == 0) {
        throw new HongsException(1094, "ID can not be empty for get");
    }
    Map rd = new HashMap();
    rd.put(this.table.primaryKey, id);
    // 调用filter进行过滤
    caze = caze != null ? caze.clone() : fetchCase();
    caze.setOption("MODEL_START", "get");
    this.filter(caze, rd);
    return this.table.fetchLess(caze);
}
Also used : HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 83 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Model method exists.

/**
 * 检查是否存在
 *
 * @param rd
 * @param caze
 * @return 存在为true, 反之为false
 * @throws io.github.ihongs.HongsException
 */
public boolean exists(Map rd, FetchCase caze) throws HongsException {
    if (rd == null) {
        rd = new HashMap();
    }
    if (caze == null) {
        caze = fetchCase();
    }
    // 默认不关联
    if (!caze.hasOption("ASSOCS") && !caze.hasOption("ASSOC_TYPES") && !caze.hasOption("ASSOC_JOINS")) {
        caze.setOption("ASSOCS", new HashSet());
    }
    // 是否缺少n或v参数
    if (!rd.containsKey("n") || !rd.containsKey("v")) {
        throw new HongsException(1085, "Param n or v can not be empty");
    }
    String n = (String) rd.get("n");
    String v = (String) rd.get("v");
    Map columns = this.table.getFields();
    // 是否缺少n对应的字段
    if (!columns.containsKey(n)) {
        throw new HongsException(1086, "Column " + n + " is not exists");
    }
    caze.filter("`" + this.table.name + "`.`" + n + "` = ?", v);
    Iterator it = rd.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        String field = (String) entry.getKey();
        String value = (String) entry.getValue();
        if (field.equals(this.table.primaryKey) || field.equals(Cnst.ID_KEY)) {
            if (value != null && !value.equals("")) {
                caze.filter("`" + this.table.name + "`.`" + this.table.primaryKey + "` != ?", value);
            }
        } else if (columns.containsKey(field)) {
            caze.filter("`" + this.table.name + "`.`" + field + "` = ?", value);
        }
    }
    Map row = this.table.fetchLess(caze);
    return !row.isEmpty();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HongsException(io.github.ihongs.HongsException) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 84 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Link method checkSQLParams.

/**
 * 检查SQL数据项
 * @param sql
 * @param params
 * @throws HongsException
 */
public static void checkSQLParams(StringBuilder sql, List params) throws HongsException {
    if (params == null) {
        params = new ArrayList();
    }
    int pos = 0;
    int num = 0;
    while ((pos = sql.indexOf("?", pos)) != -1) {
        if (num >= params.size()) {
            break;
        }
        /**
         * 如果参数是数组或List
         * 则将其全部转化为Set
         * 以供继续后面的处理
         */
        Object obj = params.get(num);
        if (obj != null && obj.getClass().isArray()) {
            obj = new LinkedHashSet(Arrays.asList((Object[]) obj));
        } else if (obj instanceof Map) {
            obj = new LinkedHashSet(((Map) obj).values());
        } else if (obj instanceof List) {
            obj = new LinkedHashSet((List) obj);
        }
        /**
         * 如果参数是Set,
         * 则视为"SQL IN"语句,
         * 将在当前问号后补充足量的问号,
         * 并将参数补充到当前参数列表中.
         */
        if (obj instanceof Collection) {
            Collection set = (Collection) obj;
            int off = num;
            // 加一个空参数防止语法错误
            if (set.isEmpty()) {
                set = Arrays.asList((Object) null);
            }
            // 从第二个参数开始补充问号
            for (int i = 1; i < set.size(); i++) {
                sql.insert(pos + 1, ",?");
                pos += 2;
                num += 1;
            }
            // 平铺到参数列表中
            params.remove(off);
            params.addAll(off, set);
        }
        pos += 1;
        num += 1;
    }
    if (num != params.size()) {
        throw new HongsException(1049, "The number of '?' and the number of parameters are inconsistent." + " ?s: " + num + " params: " + params.size() + " SQL: " + sql);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HongsException(io.github.ihongs.HongsException) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 85 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Link method update.

/**
 * 更新记录
 * <p>注: 调用update(sql, params...)实现</p>
 * @param table
 * @param values
 * @param where
 * @param params
 * @return 更新条数
 * @throws HongsException
 */
public int update(String table, Map<String, Object> values, String where, Object... params) throws HongsException {
    if (values == null || values.isEmpty()) {
        throw new HongsException(1047, "Update value can not be empty.");
    }
    if (where == null || where.isEmpty()) {
        throw new HongsException(1048, "Update where can not be empty.");
    }
    table = quoteField(table);
    /**
     * 组织语言 *
     */
    List paramz = new ArrayList(values.size());
    String vs = "";
    Iterator it = values.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        String field = (String) entry.getKey();
        paramz.add((Object) entry.getValue());
        vs += ", " + quoteField(field) + " = ?";
    }
    String sql = "UPDATE " + table + " SET " + vs.substring(2) + " WHERE " + where;
    if (params.length > 0) {
        paramz.addAll(Arrays.asList(params));
    }
    return this.updates(sql, paramz.toArray());
}
Also used : HongsException(io.github.ihongs.HongsException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

HongsException (io.github.ihongs.HongsException)138 Map (java.util.Map)77 HashMap (java.util.HashMap)61 LinkedHashMap (java.util.LinkedHashMap)31 IOException (java.io.IOException)29 Set (java.util.Set)26 HashSet (java.util.HashSet)25 ArrayList (java.util.ArrayList)24 List (java.util.List)20 HongsExemption (io.github.ihongs.HongsExemption)14 Action (io.github.ihongs.action.anno.Action)14 LinkedHashSet (java.util.LinkedHashSet)14 SQLException (java.sql.SQLException)13 FormSet (io.github.ihongs.action.FormSet)12 Table (io.github.ihongs.db.Table)12 FileNotFoundException (java.io.FileNotFoundException)11 CoreConfig (io.github.ihongs.CoreConfig)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 Iterator (java.util.Iterator)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9