package com.awsle.aibatis.parser;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import com.awsle.aibatis.client.SqlMapClient;
|
import com.awsle.aibatis.client.SqlMapClientImpl;
|
import com.awsle.aibatis.xml.aibatis.node.Delete;
|
import com.awsle.aibatis.xml.aibatis.node.Insert;
|
import com.awsle.aibatis.xml.aibatis.node.Select;
|
import com.awsle.aibatis.xml.aibatis.node.SqlMap;
|
import com.awsle.aibatis.xml.aibatis.node.SqlMapConfig;
|
import com.awsle.aibatis.xml.aibatis.node.SqlNode;
|
import com.awsle.aibatis.xml.aibatis.node.Update;
|
import com.awsle.aibatis.xml.utils.XmlSqlMapConfig;
|
|
/**
|
*
|
* @author 席有芳
|
* @url http://code.awsle.com/index.php/p/aibatis/
|
* @mail 951868171@qq.com
|
* @version 1.0
|
* @since aibatis-Alpha1.0.zip
|
*/
|
public class SqlMapConfigParser {
|
|
/**
|
* 解析Xml
|
* @param xml
|
* @return
|
*/
|
public SqlMapClient parser(String xml) {
|
SqlMapClientImpl sqlMap = new SqlMapClientImpl();
|
SqlMapConfig config = XmlSqlMapConfig.xmlToConfig(xml);
|
sqlMap.setSqlNodeMap(parserSqlNode(config));
|
sqlMap.setDataSource(config.getDataSource());
|
return sqlMap;
|
}
|
|
/**
|
* 解析Sql语句的节点
|
* @param config
|
* @return
|
*/
|
private Map<String, SqlNode> parserSqlNode(SqlMapConfig config){
|
Map<String, SqlNode> map = new HashMap<String, SqlNode>();
|
List<SqlMap> sqlMaps = config.getSqlMaps();
|
//如果为空
|
if(sqlMaps == null){
|
sqlMaps = new ArrayList<SqlMap>();
|
}
|
//引入的外部
|
List<String> includes = config.getIncludes();
|
if(includes != null){
|
for (String include : includes) {
|
SqlMapParser sqlMapParser = new SqlMapParser();
|
sqlMaps.add(sqlMapParser.parser(this.getClass().getResourceAsStream(include)));
|
}
|
}
|
//遍历sqlMap
|
for (SqlMap sqlMap : sqlMaps) {
|
String namespace = sqlMap.getNamespace();
|
//遍历插入语句
|
List<Insert> inserts = sqlMap.getInserts();
|
if(inserts != null){
|
for (Insert insert : inserts) {
|
map.put(insert.getId(), insert);
|
if(namespace != null){
|
map.put(namespace+"."+insert.getId(), insert);
|
}
|
}
|
}
|
//遍历删除语句
|
List<Delete> deletes = sqlMap.getDeletes();
|
if(deletes != null){
|
for (Delete delete : deletes) {
|
map.put(delete.getId(), delete);
|
if(namespace != null){
|
map.put(namespace+"."+delete.getId(), delete);
|
}
|
}
|
}
|
//遍历更新语句
|
List<Update> updates = sqlMap.getUpdates();
|
if(updates != null){
|
for (Update update : updates) {
|
map.put(update.getId(), update);
|
if(namespace != null){
|
map.put(namespace+"."+update.getId(), update);
|
}
|
}
|
}
|
//遍历查询语句
|
List<Select> selects = sqlMap.getSelects();
|
if(selects != null){
|
for (Select select : selects) {
|
map.put(select.getId(), select);
|
if(namespace != null){
|
map.put(namespace+"."+select.getId(), select);
|
}
|
}
|
}
|
}
|
return map;
|
}
|
|
|
|
}
|