Search in sources :

Example 6 with HongsException

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

the class Link method query.

// ** 查询语句 **/
/**
 * 查询方法
 * @param sql
 * @param start
 * @param limit
 * @param params
 * @return 查询结果
 * @throws HongsException
 */
public Loop query(String sql, int start, int limit, Object... params) throws HongsException {
    /**
     * 由于 SQLite 等不支持 absolute 方法
     * 故对这样的库采用组织语句的分页查询
     */
    if (limit == 0) {
        this.open();
    } else
        try {
            String dpn = this.open().getMetaData().getDatabaseProductName().toUpperCase();
            if ("SQLITE".equals(dpn)) {
                sql += " LIMIT ?,?";
                Object[] paramz = new Object[params.length + 2];
                System.arraycopy(params, 0, paramz, 0, params.length);
                paramz[params.length + 0] = start;
                paramz[params.length + 1] = limit;
                params = paramz;
                start = 0;
                limit = 0;
            }
        } catch (SQLException ex) {
            throw new HongsException.Common(ex);
        }
    if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
        StringBuilder sb = new StringBuilder(sql);
        List paramz = new ArrayList(Arrays.asList(params));
        checkSQLParams(sb, paramz);
        mergeSQLParams(sb, paramz);
        CoreLogger.debug("DB.query: " + sb.toString());
    }
    PreparedStatement ps = this.prepareStatement(sql, params);
    ResultSet rs;
    try {
        if (limit > 0) {
            ps.setFetchSize(limit);
            ps.setMaxRows(start + limit);
        }
        rs = ps.executeQuery();
        if (start > 0) {
            rs.absolute(start);
        }
    } catch (SQLException ex) {
        throw new HongsException(0x1043, ex);
    }
    Loop loop = new Loop(rs, ps);
    loop.inObjectMode(OBJECT_MODE);
    return loop;
}
Also used : SQLException(java.sql.SQLException) HongsException(app.hongs.HongsException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement)

Example 7 with HongsException

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

the class Link method mergeSQLParams.

/**
 * 绑定SQL数据项
 * 调用本方法前务必先调用内checkSQLParams
 * @param sql
 * @param params
 * @throws HongsException
 */
public static void mergeSQLParams(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;
        }
        /**
         * 如果参数是NULL,
         * 则直接加入NULL;
         * 如果参数是数字,
         * 则直接加入数字;
         * 如果参数是其他类型,
         * 则转换成字符串并加引号.
         */
        Object obj = params.get(num);
        String str;
        if (obj == null) {
            str = "NULL";
        } else if (obj instanceof Number) {
            str = obj.toString();
        } else {
            str = obj.toString();
            str = quoteValue(str);
        }
        sql.replace(pos, pos + 1, str);
        pos += str.length() - 1;
        num += 1;
    }
    if (num != params.size()) {
        throw new HongsException(0x1051, "The number of '?' and the number of parameters are inconsistent." + " ?s: " + num + " params: " + params.size() + " SQL: " + sql);
    }
}
Also used : HongsException(app.hongs.HongsException) ArrayList(java.util.ArrayList)

Example 8 with HongsException

use of app.hongs.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(0x104d, "Update value can not be empty.");
    }
    if (where == null || where.isEmpty()) {
        throw new HongsException(0x1052, "Update where can not be empty.");
    }
    table = quoteField(table);
    /**
     * 组织语言 *
     */
    List paramz = new ArrayList();
    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(app.hongs.HongsException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with HongsException

use of app.hongs.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 = new HashSet();
                set.add(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(0x1051, "The number of '?' and the number of parameters are inconsistent." + " ?s: " + num + " params: " + params.size() + " SQL: " + sql);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HongsException(app.hongs.HongsException) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with HongsException

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

the class Link method insert.

/**
 * 添加记录
 * <p>注: 调用update(sql, params...)实现</p>
 * @param table
 * @param values
 * @return 插入条数
 * @throws HongsException
 */
public int insert(String table, Map<String, Object> values) throws HongsException {
    if (values == null || values.isEmpty()) {
        throw new HongsException(0x104b, "Insert value can not be empty.");
    }
    table = quoteField(table);
    /**
     * 组织语句 *
     */
    List paramz = new ArrayList();
    String fs = "";
    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());
        fs += ", " + quoteField(field);
        vs += ", ?";
    }
    String sql = "INSERT INTO " + table + " (" + fs.substring(2) + ")" + " VALUES" + " (" + vs.substring(2) + ")";
    return this.updates(sql, paramz.toArray());
}
Also used : HongsException(app.hongs.HongsException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) 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