package com.smtaiserver.smtaiserver.attach;
|
|
import java.util.ArrayList;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.dom4j.Element;
|
import org.dom4j.Node;
|
import org.dom4j.tree.DefaultText;
|
|
import com.smtservlet.util.SMTStatic;
|
|
public class SMTAttachMetricSqlXml
|
{
|
///////////////////////////////////////////////////////////////////////////////////////
|
private static class SQLXMLExecArg
|
{
|
public StringBuilder _sbSQLText = new StringBuilder();
|
public List<Object> _sqlParams = new ArrayList<>();
|
public Map<String, String> _mapArgs;
|
|
public SQLXMLExecArg(Map<String, String> mapArgs)
|
{
|
_mapArgs = mapArgs;
|
}
|
|
public Object getArgValue(String key) throws Exception
|
{
|
String value = _mapArgs.get(key);
|
if(value == null)
|
throw new Exception("can't find arg : " + key);
|
return value;
|
}
|
}
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
private static abstract class SQLXMLNode
|
{
|
public abstract void execute(SQLXMLExecArg execArg) throws Exception;
|
}
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
private static class SQLXMLNodeSQL extends SQLXMLNode
|
{
|
private List<Object> _listChildren = new ArrayList<>();
|
|
public SQLXMLNodeSQL(Element xmlRoot) throws Exception
|
{
|
for (Iterator<Node> iterInner = xmlRoot.nodeIterator(); iterInner.hasNext();)
|
{
|
Node nodeInner = iterInner.next();
|
if(nodeInner.getNodeType() == Node.TEXT_NODE)
|
{
|
String text = ((DefaultText)nodeInner).getText();
|
int lastPos = _listChildren.size() - 1;
|
if(lastPos >= 0 && _listChildren.get(lastPos) instanceof String)
|
{
|
_listChildren.set(lastPos, (String)_listChildren.get(lastPos) + text);
|
}
|
else
|
{
|
_listChildren.add(text);
|
}
|
}
|
else
|
{
|
_listChildren.add(createSQLXMLNode((Element)nodeInner));
|
}
|
}
|
}
|
|
@Override
|
public void execute(SQLXMLExecArg execArg) throws Exception
|
{
|
for(Object oxmlNode : _listChildren)
|
{
|
if(oxmlNode instanceof String)
|
{
|
execArg._sbSQLText.append(oxmlNode);
|
}
|
else if(oxmlNode instanceof SQLXMLNode)
|
{
|
((SQLXMLNode)oxmlNode).execute(execArg);
|
}
|
}
|
}
|
}
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
private static class SQLXMLNodePARAM extends SQLXMLNode
|
{
|
private String _key;
|
|
public SQLXMLNodePARAM(Element xmlRoot) throws Exception
|
{
|
_key = SMTStatic.getXmlAttr(xmlRoot, "key");
|
}
|
|
@Override
|
public void execute(SQLXMLExecArg execArg) throws Exception
|
{
|
execArg._sbSQLText.append("'" + SMTStatic.toString(execArg.getArgValue(_key)).replace("'", "''") + "'");
|
}
|
}
|
///////////////////////////////////////////////////////////////////////////////////////
|
private String _dsId;
|
private SQLXMLNode _SQLXMLNode;
|
|
private static SQLXMLNode createSQLXMLNode(Element xmlRoot) throws Exception
|
{
|
String name = xmlRoot.getName().toUpperCase();
|
if( "NAME_SQL".equals(name)
|
|| "VALUE_SQL".equals(name))
|
return new SQLXMLNodeSQL(xmlRoot);
|
else if("PARAM".equals(name))
|
return new SQLXMLNodePARAM(xmlRoot);
|
else
|
throw new Exception("unknow SQLXML : " + name);
|
}
|
|
public String getDSId()
|
{
|
return _dsId;
|
}
|
|
public SMTAttachMetricSqlXml(Element rootElement) throws Exception
|
{
|
_dsId = SMTStatic.getXmlAttr(rootElement, "ds_id", null);
|
|
_SQLXMLNode = createSQLXMLNode(rootElement);
|
}
|
|
public String createSQL(Map<String, String> mapArgs) throws Exception
|
{
|
// 生成原始SQL
|
SQLXMLExecArg execArg = new SQLXMLExecArg(mapArgs);
|
_SQLXMLNode.execute(execArg);
|
|
String sql = execArg._sbSQLText.toString();
|
if (mapArgs!=null){
|
sql = sql.replace("LIMIT\n" +
|
" 100", "LIMIT " + mapArgs.get("limit"));
|
}
|
return sql;
|
}
|
|
}
|