001: package de.ixdb.squirrel_sql.plugins.cache;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
005: import net.sourceforge.squirrel_sql.fw.sql.IProcedureInfo;
006: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
007: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
008: import org.w3c.dom.Document;
009: import org.w3c.dom.Element;
010: import org.w3c.dom.NodeList;
011:
012: import java.util.Vector;
013:
014: public class ScriptFunctionCommand {
015: private ISession _session;
016: private CachePlugin _plugin;
017:
018: private static ILogger s_log;
019:
020: public ScriptFunctionCommand(ISession session, CachePlugin plugin) {
021: _session = session;
022: _plugin = plugin;
023: if (s_log == null) {
024: s_log = LoggerController.createLogger(getClass());
025: }
026: }
027:
028: public void execute() {
029: String[] funcNames = getSelectedFunctions();
030:
031: if (0 == funcNames.length) {
032: return;
033: }
034:
035: String list = null;
036: for (int i = 0; i < funcNames.length; i++) {
037:
038: if (null == list) {
039: list = "User.func" + funcNames[i];
040: } else {
041: list += ",User.func" + funcNames[i];
042: }
043:
044: }
045:
046: CdlAccessor.getSearchStringForCdlAccess(_session
047: .getSessionInternalFrame().getObjectTreeAPI()
048: .getSelectedDatabaseObjects());
049:
050: Document doc = CdlAccessor.getDefinitionAsXmlDoc(list, _session
051: .getSQLConnection().getConnection());
052:
053: StringBuffer script = new StringBuffer();
054: for (int i = 0; i < funcNames.length; i++) {
055: String ddl = getDDL(doc, funcNames[i]);
056: script.append(ddl).append(getStatementSeparator()).append(
057: "\n");
058: }
059:
060: if (0 < script.length()) {
061: _session.getSessionInternalFrame().getSQLPanelAPI()
062: .appendSQLScript(script.toString());
063: _session
064: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
065: }
066:
067: }
068:
069: private String getDDL(Document doc, String funcName) {
070: Element documentElement = doc.getDocumentElement();
071:
072: NodeList elementsByTagName = documentElement
073: .getElementsByTagName("Class");
074:
075: StringBuffer ret = new StringBuffer("");
076: for (int i = 0; i < elementsByTagName.getLength(); i++) {
077: Element elem = (Element) elementsByTagName.item(i);
078:
079: if (elem.getAttribute("name").trim().endsWith(funcName)) {
080:
081: ret.append("CREATE FUNCTION ").append(funcName);
082:
083: Element methElem = (Element) elem.getElementsByTagName(
084: "Method").item(0);
085:
086: Element elemParams = (Element) methElem
087: .getElementsByTagName("FormalSpec").item(0);
088: String cdlParamString = elemParams.getFirstChild()
089: .getNodeValue();
090:
091: ret.append(getParams(cdlParamString));
092:
093: Element retElem = (Element) methElem
094: .getElementsByTagName("ReturnType").item(0);
095: String cdlRetString = retElem.getFirstChild()
096: .getNodeValue();
097: String retString = getType(cdlRetString);
098: ret.append("\nRETURNS ").append(retString).append(
099: "\nLANGUAGE COS\n{");
100:
101: Element implElem = (Element) methElem
102: .getElementsByTagName("Implementation").item(0);
103: String impl = implElem.getFirstChild().getNodeValue();
104: ret.append(impl);
105: ret.append("\n}");
106: }
107: }
108:
109: return ret.toString();
110: }
111:
112: private String getParams(String cdlParamString) {
113: String clean = removeBrackets(cdlParamString);
114:
115: String[] params = clean.split(",");
116:
117: String ret = "(";
118: for (int i = 0; i < params.length; i++) {
119: String[] pieces = params[i].split(":");
120:
121: String paramDef = pieces[0] + " " + getType(pieces[1]);
122: if (0 == i) {
123: ret += paramDef;
124: } else {
125: ret += ", " + paramDef;
126: }
127: }
128:
129: ret += ")";
130:
131: return ret;
132: }
133:
134: private String removeBrackets(String inParams) {
135: StringBuffer ret = new StringBuffer();
136: int openIndex = inParams.indexOf('(');
137: if (-1 == openIndex) {
138: return inParams;
139: }
140: while (-1 != openIndex) {
141: ret.append(inParams.substring(0, openIndex));
142: inParams = inParams.substring(inParams.indexOf(')') + 1,
143: inParams.length());
144: openIndex = inParams.indexOf('(');
145: }
146: return ret.toString();
147: }
148:
149: private String getType(String cacheType) {
150: if (cacheType.equals("%Library.TimeStamp")
151: || cacheType.equals("%TimeStamp")) {
152: return "DATETIME";
153: } else if (cacheType.startsWith("%Library.String")
154: || cacheType.startsWith("%String")) {
155: // Die 200 braucht nur SQL Server 7.0,
156: // SQL Server 2000 nicht mehr sagt SAN
157: return "VARCHAR(200)";
158: } else if (cacheType.startsWith("%Library.Integer")
159: || cacheType.startsWith("%Integer")) {
160: return "INTEGER";
161: } else if (cacheType.equals("%Library.Numeric")
162: || cacheType.equals("%Numeric")) {
163: return "NUMERIC(18,2)";
164: } else {
165: return "Unbekannter Parametertyp:>" + cacheType + "<";
166: }
167: }
168:
169: private String getStatementSeparator() {
170: String statementSeparator = _session.getProperties()
171: .getSQLStatementSeparator();
172:
173: if (1 < statementSeparator.length()) {
174: statementSeparator = "\n" + statementSeparator + "\n";
175: }
176:
177: return statementSeparator;
178: }
179:
180: private String[] getSelectedFunctions() {
181: IDatabaseObjectInfo[] dbObjs = _session
182: .getSessionInternalFrame().getObjectTreeAPI()
183: .getSelectedDatabaseObjects();
184:
185: Vector ret = new Vector();
186: for (int i = 0; i < dbObjs.length; i++) {
187: if (dbObjs[i] instanceof IProcedureInfo) {
188: IProcedureInfo pi = (IProcedureInfo) dbObjs[i];
189: String sFunc = pi.getSimpleName();
190: ret.add(sFunc);
191: }
192: }
193: return (String[]) ret.toArray(new String[0]);
194: }
195: }
|