use of app.hongs.HongsException in project HongsCORE by ihongs.
the class DB method getModel.
/**
* 通过表名获取表模型
* 表名可以为"库名.表名"
* @param tableName table 对象的 name
* @return 指定表模型
* @throws app.hongs.HongsException
*/
public Model getModel(String tableName) throws HongsException {
/**
* 表名可以是"数据库.表名"
* 用于引用另一个库中的表
*/
int pos = tableName.lastIndexOf('.');
if (pos > 0) {
String db = tableName.substring(0, pos);
tableName = tableName.substring(pos + 1);
return DB.getInstance(db).getModel(tableName);
}
if (this.modelObjects.containsKey(tableName)) {
return this.modelObjects.get(tableName);
}
if (!this.tableConfigs.containsKey(tableName)) {
throw new app.hongs.HongsException(0x1039, "Can not find config for table '" + this.name + "." + tableName + "'.");
}
/**
* 读取库指定的modelClass
*/
Map<String, String> tcfg = this.tableConfigs.get(tableName);
// this.tableConfigs.remove(tableName);
String mcls = this.modelClass;
/**
* 就近原则:
* 如果表配置中有设置model则采用表配置中的
*/
if (tcfg.containsKey("model")) {
mcls = tcfg.get("model");
}
/**
* 如果class为空则直接使用默认的Table
*/
if (mcls == null || mcls.length() == 0) {
Model mobj = new Model(this.getTable(tableName));
this.modelObjects.put(tableName, mobj);
return mobj;
}
if (0 < Core.DEBUG && 4 != (4 & Core.DEBUG)) {
CoreLogger.trace("DB: modelClass(" + mcls + ") for table(" + this.name + "." + tableName + ") has been defined, try to get it");
}
/**
* 获取指定的Table类
*/
Class cls;
try {
cls = Class.forName(mcls);
} catch (ClassNotFoundException ex) {
throw new app.hongs.HongsException(0x103d, ex);
}
/**
* 获取构造器
*/
Constructor cst;
try {
cst = cls.getConstructor(new Class[] { Table.class });
} catch (NoSuchMethodException ex) {
throw new app.hongs.HongsException(0x103e, ex);
} catch (SecurityException ex) {
throw new app.hongs.HongsException(0x103e, ex);
}
/**
* 获取表实例
*/
Model mobj;
try {
mobj = (Model) cst.newInstance(new Object[] { this.getTable(tableName) });
} catch (InstantiationException ex) {
throw new app.hongs.HongsException(0x103f, ex);
} catch (IllegalAccessException ex) {
throw new app.hongs.HongsException(0x103f, ex);
} catch (IllegalArgumentException ex) {
throw new app.hongs.HongsException(0x103f, ex);
} catch (InvocationTargetException ex) {
throw new app.hongs.HongsException(0x103f, ex);
}
this.modelObjects.put(tableName, mobj);
return mobj;
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class DB method getTable.
/**
* 通过表名获取表对象
* 表名可以为"库名.表名"
* @param tableName table 对象的 name
* @return 指定表对象
* @throws app.hongs.HongsException
*/
public Table getTable(String tableName) throws HongsException {
/**
* 表名可以是"数据库.表名"
* 用于引用另一个库中的表
*/
int pos = tableName.indexOf('.');
if (pos > 0) {
String db = tableName.substring(0, pos);
tableName = tableName.substring(pos + 1);
return DB.getInstance(db).getTable(tableName);
}
if (this.tableObjects.containsKey(tableName)) {
return this.tableObjects.get(tableName);
}
if (!this.tableConfigs.containsKey(tableName)) {
throw new app.hongs.HongsException(0x1039, "Can not find config for table '" + this.name + "." + tableName + "'.");
}
/**
* 读取库指定的tableClass
* 读取表对应的tableConfig
*/
Map<String, String> tcfg = this.tableConfigs.get(tableName);
// this.tableConfigs.remove(tableName);
String tcls = this.tableClass;
String tpfx = this.tablePrefix;
String tsfx = this.tableSuffix;
/**
* 就近原则:
* 如果表配置中有设置class则采用表配置中的
* 如果表配置中没有设置prefix则采用库配置中的
* 如果表配置中没有设置suffix则采用库配置中的
*/
if (tcfg.containsKey("class")) {
tcls = tcfg.get("class");
}
if (!tcfg.containsKey("prefix")) {
tcfg.put("prefix", tpfx);
}
if (!tcfg.containsKey("suffix")) {
tcfg.put("suffix", tsfx);
}
/**
* 如果class为空则直接使用默认的Table
*/
if (tcls == null || tcls.length() == 0) {
Table tobj = new Table(this, tcfg);
this.tableObjects.put(tableName, tobj);
return tobj;
}
if (0 < Core.DEBUG && 4 != (4 & Core.DEBUG)) {
CoreLogger.trace("DB: tableClass(" + tcls + ") for table(" + this.name + "." + tableName + ") has been defined, try to get it");
}
/**
* 获取指定的Table类
*/
Class cls;
try {
cls = Class.forName(tcls);
} catch (ClassNotFoundException ex) {
throw new app.hongs.HongsException(0x103a, ex);
}
/**
* 获取构造器
*/
Constructor cst;
try {
cst = cls.getConstructor(new Class[] { DB.class, Map.class });
} catch (NoSuchMethodException ex) {
throw new app.hongs.HongsException(0x103b, ex);
} catch (SecurityException ex) {
throw new app.hongs.HongsException(0x103b, ex);
}
/**
* 获取表实例
*/
Table tobj;
try {
tobj = (Table) cst.newInstance(new Object[] { this, tcfg });
} catch (InstantiationException ex) {
throw new app.hongs.HongsException(0x103c, ex);
} catch (IllegalAccessException ex) {
throw new app.hongs.HongsException(0x103c, ex);
} catch (IllegalArgumentException ex) {
throw new app.hongs.HongsException(0x103c, ex);
} catch (InvocationTargetException ex) {
throw new app.hongs.HongsException(0x103c, ex);
}
this.tableObjects.put(tableName, tobj);
return tobj;
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class DBConfig method parseByStream.
/**
* 根据输入流解析配置
*
* @param ds
* @return 配置对象
* @throws app.hongs.HongsException
*/
public static DBConfig parseByStream(InputStream ds) throws HongsException {
Document doc;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbn = dbf.newDocumentBuilder();
doc = dbn.parse(ds);
} catch (ParserConfigurationException ex) {
throw new app.hongs.HongsException(0x1063, ex);
} catch (SAXException ex) {
throw new app.hongs.HongsException(0x1063, ex);
} catch (IOException ex) {
throw new app.hongs.HongsException(0x1069, ex);
}
return new DBConfig(doc);
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class DBConfig method parseByFile.
/**
* 根据文件解析配置
*
* @param df
* @return 配置对象
* @throws app.hongs.HongsException
*/
public static DBConfig parseByFile(File df) throws HongsException {
Document doc;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbn = dbf.newDocumentBuilder();
doc = dbn.parse(df);
} catch (ParserConfigurationException ex) {
throw new app.hongs.HongsException(0x1063, ex);
} catch (SAXException ex) {
throw new app.hongs.HongsException(0x1063, ex);
} catch (IOException ex) {
throw new app.hongs.HongsException(0x1067, ex);
}
return new DBConfig(doc);
}
use of app.hongs.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.toExpedient();
}
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.toExpedient();
}
if (null == assoc) {
throw new HongsExpedient.Common("Can not get table '" + rn + "' in DB '" + table.db.name + "'");
}
allow(table, assoc, ac, pc, caxe, tn, qn, jn, al);
}
}
Aggregations