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;
}
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);
}
}
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());
}
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);
}
}
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());
}
Aggregations