001: package de.ixdb.squirrel_sql.plugins.cache;
002:
003: import org.xml.sax.InputSource;
004: import org.w3c.dom.Document;
005:
006: import javax.xml.parsers.DocumentBuilderFactory;
007: import javax.xml.parsers.DocumentBuilder;
008: import java.lang.reflect.Method;
009: import java.io.InputStream;
010: import java.io.ByteArrayInputStream;
011: import java.sql.Statement;
012: import java.sql.Connection;
013:
014: import com.intersys.cache.Dataholder;
015: import com.intersys.cache.CacheObject;
016: import com.intersys.objects.Database;
017: import com.intersys.objects.CacheDatabase;
018: import com.intersys.objects.CacheException;
019: import com.intersys.objects.CacheReader;
020: import com.intersys.classes.CharacterStream;
021: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
022: import net.sourceforge.squirrel_sql.fw.sql.IProcedureInfo;
023:
024: public class CdlAccessor {
025: static org.w3c.dom.Document getDocument(InputStream istream) {
026: try {
027: InputSource is = new InputSource(istream);
028:
029: DocumentBuilderFactory dbf = DocumentBuilderFactory
030: .newInstance();
031: dbf.setValidating(false);
032: dbf.setIgnoringElementContentWhitespace(true);
033:
034: dbf.setNamespaceAware(true);
035: DocumentBuilder db = dbf.newDocumentBuilder();
036: return db.parse(is);
037: } catch (Exception e) {
038: throw new RuntimeException(e);
039: }
040: }
041:
042: static String getDefinition(String list, Connection con) {
043: try {
044: Dataholder[] argv = new Dataholder[1];
045: argv[0] = Dataholder.create(list);
046:
047: Database conn = CacheDatabase.getDatabase(con);
048:
049: Dataholder res;
050:
051: try {
052: res = conn
053: .runClassMethod("CM.methgetClassDefinition",
054: "getClassDefinition", argv,
055: Database.RET_OBJECT);
056: } catch (CacheException e) {
057: Statement stat = con.createStatement();
058: stat
059: .executeUpdate(
060:
061: "CREATE METHOD CM.getClassDefinition(IN className %String)\n"
062: + "RETURNS Integer\n"
063: + "PROCEDURE\n"
064: + "LANGUAGE COS\n"
065: + "{\n"
066: + " new id,oref,oFile,file\n"
067: + " if $F($ZV,\"Linux\") \n"
068: + " set file = \"$HOME/CacheTemp\"_$job_\".xml\"\n"
069: + " else \n"
070: + " set file = \"c:\\\\temp\\\\$CacheTemp\"_$job_\".xml\" \n"
071: + " do $SYSTEM.OBJ.ExportCDL(className ,file, \"-d\")\n"
072: + " set oFile = ##class(%FileBinaryStream).%New()\n"
073: + " do oFile.LinkToFile(file)\n"
074: + "\n"
075: + " set outStream = ##class(%GlobalCharacterStream).%New()\n"
076: + " do outStream.CopyFrom(oFile)\n"
077: + " quit outStream\n" + "}");
078: stat.close();
079:
080: res = conn
081: .runClassMethod("CM.methgetClassDefinition",
082: "getClassDefinition", argv,
083: Database.RET_OBJECT);
084: }
085:
086: CacheObject cobj = res.getCacheObject();
087: CharacterStream characterStream = (CharacterStream) (cobj
088: .newJavaInstance());
089:
090: CacheReader reader = characterStream.getReader();
091:
092: StringBuffer sb = new StringBuffer();
093: sb.append("");
094:
095: char[] buf = new char[50];
096: int count = reader.read(buf);
097: while (true) {
098:
099: for (int i = 0; i < count; i++) {
100: sb.append(buf[i]);
101: }
102:
103: if (count < 50) {
104: break;
105: }
106:
107: count = reader.read(buf);
108: }
109: return sb.toString();
110: } catch (Exception e) {
111: throw new RuntimeException(e);
112: }
113: }
114:
115: static org.w3c.dom.Document getDefinitionAsXmlDoc(String list,
116: Connection connection) {
117: String cdl = getDefinition(list, connection);
118:
119: ByteArrayInputStream bis = new ByteArrayInputStream(cdl
120: .getBytes());
121:
122: return getDocument(bis);
123: }
124:
125: static String getSearchStringForCdlAccess(
126: IDatabaseObjectInfo[] dbObjs) {
127: String ret = null;
128:
129: for (int i = 0; i < dbObjs.length; i++) {
130: String prefix = "User.";
131:
132: if (false == "SQLUser".equalsIgnoreCase(dbObjs[i]
133: .getSchemaName())) {
134: prefix = dbObjs[i].getSchemaName() + ".";
135: }
136:
137: if (dbObjs[i] instanceof IProcedureInfo) {
138: IProcedureInfo pi = (IProcedureInfo) dbObjs[i];
139:
140: if (null == ret) {
141: ret = prefix + "func" + pi.getSimpleName();
142: ret += "," + prefix + "meth" + pi.getSimpleName();
143:
144: } else {
145: ret += "," + prefix + "func" + pi.getSimpleName();
146: ret += "," + prefix + "meth" + pi.getSimpleName();
147: }
148: } else {
149: if (null == ret) {
150: ret = prefix + dbObjs[i].getSimpleName();
151: } else {
152: ret += "," + prefix + dbObjs[i].getSimpleName();
153: }
154: }
155:
156: }
157: return ret;
158: }
159:
160: }
|