tools.c3p0.orm.OrmUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-autotest-tool Show documentation
Show all versions of java-autotest-tool Show documentation
This is an integration of autotest tools
package tools.c3p0.orm;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.xml.XmlUtil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class OrmUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(OrmUtil.class);
private static XmlUtil xmlInstance = null;
private static JSONObject jsonObject = null;
public static void setXmlMapperPath(String xmlMapperPath){
if (xmlMapperPath.startsWith("src")){
xmlInstance = XmlUtil.getXMLInstance(xmlMapperPath);
} else {
xmlInstance = XmlUtil.getNewInstance();
xmlInstance.loadFromResource(xmlMapperPath);
}
}
public static JSONObject analyzeDbXml(){
if (xmlInstance == null){
LOGGER.error("请先设置mapperXmlPath。");
return null;
}
Element xmlRootEle = xmlInstance.getRootElement();
LOGGER.info("xmlRootEle name is {}", xmlRootEle.getName());
String xmlNameSpace = xmlRootEle.attribute("namespace").getValue();
LOGGER.info("xmlRootEle namespace is {}", xmlNameSpace);
jsonObject = new JSONObject(true);
jsonObject.put("namespace", xmlNameSpace);
boolean selectFlag = xmlInstance.containsElement(xmlRootEle, "select");
boolean insertFlag = xmlInstance.containsElement(xmlRootEle, "insert");
boolean updateFlag = xmlInstance.containsElement(xmlRootEle, "update");
if (selectFlag){
List selectList = xmlInstance.getSubElementsByName(xmlRootEle, "select");
if (selectList != null && !selectList.isEmpty()){
jsonObject.put("select", eleListToJSONArray(selectList, xmlInstance));
} else {
LOGGER.warn("Xml 文件中没有 select 标签。");
}
}
if (insertFlag){
List insertList = xmlInstance.getSubElementsByName(xmlRootEle, "insert");
if (insertList != null && !insertList.isEmpty()){
jsonObject.put("insert", eleListToJSONArray(insertList, xmlInstance));
} else {
LOGGER.warn("Xml 文件中没有 insert 标签。");
}
}
if (updateFlag){
List updateList = xmlInstance.getSubElementsByName(xmlRootEle, "update");
if (updateList != null && !updateList.isEmpty()){
jsonObject.put("update", eleListToJSONArray(updateList, xmlInstance));
} else {
LOGGER.warn("Xml 文件中没有 update 标签。");
}
}
return jsonObject;
}
private static JSONArray eleListToJSONArray(List elementList, XmlUtil xmlInstance){
if (elementList != null && !elementList.isEmpty()){
JSONArray jsonArray = new JSONArray();
for (Element selectEle : elementList){
JSONObject selectObject = new JSONObject();
selectObject.put("attribute", JSON.parseObject(JSON.toJSONString(xmlInstance.getAttributeInfo(selectEle))));
selectObject.put("text", selectEle.getText());
selectObject.put("id", selectEle.attribute("id").getValue());
jsonArray.add(selectObject);
}
return jsonArray;
} else {
return null;
}
}
@Deprecated
public static String getSQL(String id, JSONObject jsonObject) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
if (id.startsWith("select")){
JSONArray selectJSONArray = jsonObject.getJSONArray("select");
for (JSONObject currJSONObj : selectJSONArray.toJavaList(JSONObject.class)){
if (id.contentEquals(currJSONObj.getString("id"))){
return currJSONObj.getString("text");
} else {
LOGGER.error("[错误]:您输入的方法{},未能在指定的XML文件中找到。", id);
return "";
}
}
} else if (id.startsWith("insert")){
JSONArray insertJSONArray = jsonObject.getJSONArray("insert");
for (JSONObject currJSONObj : insertJSONArray.toJavaList(JSONObject.class)){
if (id.contentEquals(currJSONObj.getString("id"))){
return currJSONObj.getString("text");
} else {
LOGGER.error("[错误]:您输入的方法{},未能在指定的XML文件中找到。", id);
return "";
}
}
} else if (id.startsWith("update")){
JSONArray updateJSONArray = jsonObject.getJSONArray("update");
for (JSONObject currJSONObj : updateJSONArray.toJavaList(JSONObject.class)){
if (id.contentEquals(currJSONObj.getString("id"))){
return currJSONObj.getString("text");
} else {
LOGGER.error("[错误]:您输入的方法{},未能在指定的XML文件中找到。", id);
return "";
}
}
} else {
LOGGER.warn("[警告]:您输入的方法名非法。(要求方法名必须以select、insert、update开头。)");
return "";
}
return "";
}
@Deprecated
private static String analyzeParam(String sql, Map paramMap){
if (!sql.contains("#{")){
LOGGER.info("SQL 不包括 参数。");
return sql.trim();
}else {
int index = 0;
String realSql = sql.trim();
String content = realSql;
StringBuilder finalSqlBuilder = new StringBuilder();
while (index < realSql.length()) {
// LOGGER.info("index={},realSql.length={}", index, realSql.length());
if (content.contains("#{")) {
int indexOfStart = realSql.indexOf("#{", index);
finalSqlBuilder.append(realSql.substring(index, indexOfStart));
int indexOfEnd = realSql.indexOf("}", indexOfStart);
String paramVal = realSql.substring(indexOfStart + 2, indexOfEnd);
String key = "";
if (paramVal.contains(",")) {
key = paramVal.trim().split(",")[0];
} else {
key = paramVal.trim();
}
String value = paramMap.getOrDefault(key, null).toString();
// LOGGER.info("key:value = {}:{}", key, value);
finalSqlBuilder.append(value);
index = indexOfEnd + 1;
content = realSql.substring(index);
} else {
finalSqlBuilder.append(realSql.substring(index));
}
LOGGER.info("index={},realSql.length={}", index, realSql.length());
}
return finalSqlBuilder.toString();
}
}
private static Map getStmtAndParamsForSelect(String sql, Map paramMap){
Map stmtParamMap = new HashMap<>();
List