use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Model method permit.
// ** 辅助过滤方法 **/
/**
* 操作确认
*
* <pre>
* 作用于 update,delete 上
*
* 如需添加过滤条件, 请重写此方法;
* 有不可操作的行时, 通过 caze 上的 MODEL_START 来区分异常:
* update 对应 1096
* delete 对应 1097
* 其他的 对应 1098
* 描述为 Can not update|delete|search by id: ID1, ID2, IDn
* </pre>
*
* @param caze
* @param rd
* @param id
* @throws io.github.ihongs.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);
this.filter(caze, wh);
Set xd = new HashSet();
for (Map row : caze.select()) {
xd.add(Synt.asString(row.get(table.primaryKey)));
}
// 对比数量, 取出多余的部分作为错误消息抛出
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(1096, "Can not update by id: " + er);
} else if ("delete".equals(mm)) {
throw new HongsException(1097, "Can not delete by id: " + er);
} else {
throw new HongsException(1098, "Can not search by id: " + er);
}
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Model method allow.
/**
* 递归提取字段
* @param table 顶层模型库表
* @param assoc 当前关联库表
* @param ac 当前下级关联
* @param pc 当前层级参数
* @param caze 当前查询用例
* @param tn 当前表名
* @param qn 层级名称
* @param pn 相对层级
* @param al 字段集合, 结构: {参数: [字段, 别名, 查询用例]}
*/
private void allow(Table table, Table assoc, Map ac, Map pc, FetchCase caze, String tn, String qn, String pn, Map al) {
String tx, ax, az;
tx = "`" + tn + "`.";
if (null == qn) {
qn = "";
ax = "";
} else if ("".equals(qn)) {
qn = tn;
ax = qn + ".";
} else {
qn = qn + "." + tn;
ax = qn + ".";
}
if (null == pn) {
pn = "";
az = "";
} else if ("".equals(pn)) {
pn = tn;
az = pn + ".";
} else {
pn = pn + "." + tn;
az = pn + ".";
}
if (pc != null && pc.containsKey("fields")) {
al.put(az + "*", new Object[] { null, null, caze });
} else
try {
Map fs = assoc.getFields();
for (Object n : fs.keySet()) {
String f = (String) n;
String k = f;
String l = f;
// 字段完整名
f = tx + "`" + f + "`";
// 字段别名
l = az + /**/
l;
// 外部键
k = ax + /**/
k;
al.put(k, new Object[] { f, l, caze });
}
} catch (HongsException e) {
throw e.toExemption();
}
if (ac == null || ac.isEmpty()) {
return;
}
Iterator it = ac.entrySet().iterator();
while (it.hasNext()) {
Map.Entry et = (Map.Entry) it.next();
Map tc = (Map) et.getValue();
String jn = (String) tc.get("join");
// 不是 JOIN 的重置 pn, 层级名随之改变
if (!"INNER".equals(jn) && !"LEFT".equals(jn) && !"RIGHT".equals(jn) && !"FULL".equals(jn)) {
jn = null;
} else {
jn = pn;
}
tn = (String) et.getKey();
ac = (Map) tc.get("assocs");
pc = (Map) tc.get("params");
// 获取真实的表名, 构建关联表实例
String rn;
rn = (String) tc.get("tableName");
if (rn == null || "".equals(rn)) {
rn = (String) tc.get("name");
}
FetchCase caxe;
try {
assoc = table.db.getTable(rn);
caxe = caze.gotJoin(tn);
} catch (HongsException e) {
throw e.toExemption();
}
if (null == assoc) {
throw new HongsExemption(1026, "Can not get table '" + rn + "' in DB '" + table.db.name + "'");
}
allow(table, assoc, ac, pc, caxe, tn, qn, jn, al);
}
}
use of io.github.ihongs.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 (4 == (4 & 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(1045, ex);
} finally {
this.closeStatement(ps);
}
}
use of io.github.ihongs.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(ex);
}
if (4 == (4 & 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(1043, ex);
}
return new Loop(rs, ps);
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class DataCmdlet method revert.
public static void revert(String[] args, Inst df) throws HongsException, InterruptedException {
Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "form=s", "user:s", "memo:s", "time:i", "bufs:i", "drop:b", "includes:b", "cascades:b", "!A", "?Usage: revert --conf CONF_NAME --form FORM_NAME [--time TIMESTAMP] ID0 ID1 ..." });
String conf = (String) opts.get("conf");
String form = (String) opts.get("form");
String user = (String) opts.get("user");
String memo = (String) opts.get("memo");
long ct = Synt.declare(opts.get("time"), 0L);
int bn = Synt.declare(opts.get("bufs"), 1000);
Set<String> ds = Synt.asSet(opts.get(""));
Data dr = df.getInstance(conf, form);
dr.setUserId(Synt.defoult(user, Cnst.ADM_UID));
// user = dr.getUserId( );
form = dr.getFormId();
Map sd = new HashMap();
sd.put("memo", memo);
Table tb = dr.getTable();
String tn = tb.tableName;
// 查询迭代
Loop lp;
// 操作总数
int c = 0;
// 变更计数
int i = 0;
// 事务计数
int j = 0;
if (ct == 0) {
String fa = "`a`.*";
String fc = "COUNT(*) AS _cnt_";
String qa = "SELECT " + fa + " FROM `" + tn + "` AS `a` WHERE `a`.`form_id` = ? AND `a`.`etime` = ?";
String qc = "SELECT " + fc + " FROM `" + tn + "` AS `a` WHERE `a`.`form_id` = ? AND `a`.`etime` = ?";
if (!ds.isEmpty()) {
c = ds.size();
qa = qa + " AND a.id IN (?)";
lp = tb.db.query(qa, 0, 0, form, 0, ds);
} else {
lp = tb.db.query(qa, 0, 0, form, 0);
c = Synt.declare(tb.db.fetchOne(qc, form, 0).get("_cnt_"), 0);
}
} else {
String fx = "`x`.*";
String fa = "`a`.id, MAX(a.ctime) AS ctime";
String fc = "COUNT(DISTINCT a.id) AS _cnt_";
String qa = "SELECT " + fa + " FROM `" + tn + "` AS `a` WHERE `a`.`form_id` = ? AND `a`.`ctime` <= ?";
String qc = "SELECT " + fc + " FROM `" + tn + "` AS `a` WHERE `a`.`form_id` = ? AND `a`.`ctime` <= ?";
String qx = " WHERE x.id = b.id AND x.ctime = b.ctime AND x.`form_id` = ? AND x.`ctime` <= ?";
if (!ds.isEmpty()) {
c = ds.size();
qa = qa + " AND a.id IN (?)";
qx = qx + " AND x.id IN (?)";
qa = qa + " GROUP BY `a`.id";
qx = "SELECT " + fx + " FROM `" + tn + "` AS `x`, (" + qa + ") AS `b` " + qx;
lp = tb.db.query(qx, 0, 0, form, ct, ds, form, ct, ds);
} else {
qa = qa + " GROUP BY `a`.id";
qx = "SELECT " + fx + " FROM `" + tn + "` AS `x`, (" + qa + ") AS `b` " + qx;
lp = tb.db.query(qx, 0, 0, form, ct, form, ct);
c = Synt.declare(tb.db.fetchOne(qc, form, ct).get("_cnt_"), 0);
}
}
/**
* 清空全部数据
* 以便更新结构
*/
if (Synt.declare(opts.get("drop"), false)) {
IndexWriter iw = dr.getWriter();
/**/
String pd = dr.getPartId();
try {
if (pd != null && !pd.isEmpty()) {
iw.deleteDocuments(new Term("@" + Data.PART_ID_KEY, pd));
} else {
iw.deleteAll();
}
iw.commit();
iw.deleteUnusedFiles();
iw.maybeMerge();
} catch (IOException ex) {
throw new HongsException(ex);
}
}
/**
* 级联更新操作
* 默认不作级联
*/
Casc da = new Casc(dr, Synt.declare(opts.get("includes"), false), Synt.declare(opts.get("cascades"), false));
boolean pr = (Core.DEBUG == 0);
long tm = System.currentTimeMillis();
long tc = tm / 1000;
if (pr)
CmdletHelper.progres(tm, c, i);
dr.begin();
for (Map od : lp) {
String id = (String) od.get(Cnst.ID_KEY);
if (Synt.declare(od.get("etime"), 0L) != 0L) {
if (Synt.declare(od.get("state"), 1) >= 1) {
sd.put("rtime", od.get("ctime"));
dr.rev(id, sd, tc);
} else {
dr.del(id, sd, tc);
}
} else {
if (Synt.declare(od.get("state"), 1) >= 1) {
od = Synt.toMap(od.get("data"));
od.putAll(sd);
da.update(id, od);
} else {
dr.delDoc(id);
}
}
ds.remove(id);
i++;
j++;
if (j == bn) {
j = 0;
da.commit();
dr.begin();
if (pr) {
CmdletHelper.progres(tm, c, i);
}
}
}
da.commit();
dr.begin();
if (pr) {
CmdletHelper.progres(tm, c, i);
}
/**
* 删掉多余数据
*/
for (String id : ds) {
dr.delDoc(id);
}
da.commit();
if (pr) {
CmdletHelper.progres();
}
CmdletHelper.println("Revert " + i + " item(s) for " + form + " to " + dr.getDbName());
}
Aggregations