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