use of app.hongs.HongsException in project HongsCORE by ihongs.
the class Model method exists.
/**
* 检查是否存在
*
* @param rd
* @param caze
* @return 存在为true, 反之为false
* @throws app.hongs.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(0x109a, "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(0x109b, "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 app.hongs.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(0x1094, "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 app.hongs.HongsException in project HongsCORE by ihongs.
the class Model method permit.
// ** 辅助过滤方法 **/
/**
* 操作确认
*
* <pre>
* 作用于 update,delete 上
*
* 如需添加过滤条件, 请重写此方法;
* 有不可操作的行时, 通过 caze 上的 MODEL_START 来区分异常:
* update 对应 0x1096
* delete 对应 0x1097
* 其他的 对应 0x1098
* 描述为 Can not update|delete|search by id: ID1, ID2, IDn
* </pre>
*
* @param caze
* @param rd
* @param id
* @throws HongsException
*/
protected void permit(FetchCase caze, Map rd, Set id) throws HongsException {
Map wh = new HashMap();
if (rd.containsKey(Cnst.AR_KEY)) {
wh.put(Cnst.AR_KEY, rd.get(Cnst.AR_KEY));
}
if (rd.containsKey(Cnst.OR_KEY)) {
wh.put(Cnst.OR_KEY, rd.get(Cnst.OR_KEY));
}
if (wh.isEmpty()) {
return;
}
// 组织查询
wh.put(table.primaryKey, id);
wh.put(Cnst.RB_KEY, Synt.setOf(table.primaryKey));
caze.use(db).from(table.tableName, table.name);
caze.setOption("OBJECT_MODE", false);
filter(caze, wh);
Set xd = new HashSet();
for (Map row : caze.oll()) {
xd.add(row.get(table.primaryKey).toString());
}
// 对比数量, 取出多余的部分作为错误消息抛出
if (xd.size() != id.size()) {
Set zd = new HashSet(id);
zd.removeAll(xd);
String er = zd.toString();
String mm = caze.getOption("MODEL_START", "");
if ("update".equals(mm)) {
throw new HongsException(0x1096, "Can not update by id: " + er);
} else if ("delete".equals(mm)) {
throw new HongsException(0x1097, "Can not delete by id: " + er);
} else {
throw new HongsException(0x1098, "Can not search by id: " + er);
}
}
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class Table method validateException.
private HongsException validateException(int code, String error, String fieldName, String... otherParams) {
List<String> trans = new ArrayList();
trans.add(db.name + "." + name);
trans.add(/*the*/
fieldName);
trans.addAll(Arrays.asList(otherParams));
return new HongsException(code, error + " (Table:" + name + ")").setLocalizedOptions(trans.toArray(new String[] {}));
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class Link method updates.
/**
* 更新方法
* @param sql
* @param params
* @return 更新的条数
* @throws HongsException
*/
public int updates(String sql, Object... params) throws HongsException {
this.ready();
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.updates: " + sb.toString());
}
PreparedStatement ps = this.prepareStatement(sql, params);
try {
return ps.executeUpdate();
} catch (SQLException ex) {
throw new HongsException(0x104e, ex);
} finally {
this.closeStatement(ps);
}
}
Aggregations