01: package de.ixdb.squirrel_sql.plugins.cache;
02:
03: import com.intersys.cache.jbind.JBindDatabase;
04: import com.intersys.objects.CacheDatabase;
05: import com.intersys.objects.CacheQuery;
06: import com.intersys.objects.Database;
07: import net.sourceforge.squirrel_sql.client.session.ISession;
08: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
09: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
10:
11: import java.awt.event.ActionEvent;
12: import java.sql.ResultSet;
13: import java.util.Vector;
14:
15: public class ScriptViewCommand {
16: private ISession _session;
17:
18: private static final String PREFIX_SQLUSER = "SQLUser.";
19:
20: public ScriptViewCommand(ISession session) {
21: _session = session;
22: }
23:
24: public void execute() {
25: try {
26:
27: String[] selectedViews = getSelectedViews();
28:
29: Database cacDb = (JBindDatabase) CacheDatabase
30: .getDatabase(_session.getSQLConnection()
31: .getConnection());
32: StringBuffer script = new StringBuffer();
33:
34: for (int i = 0; i < selectedViews.length; i++) {
35: CacheQuery qry = new CacheQuery(cacDb,
36: "%Library.SQLCatalog", "SQLViewInfo");
37: ResultSet viewInfo = qry.execute(selectedViews[i]);
38:
39: viewInfo.next();
40: script.append("CREATE VIEW ").append(
41: selectedViews[i].substring(PREFIX_SQLUSER
42: .length())).append(" AS\n");
43: script.append(viewInfo.getString(1)).append(
44: getStatementSeparator()).append("\n");
45: viewInfo.close();
46:
47: }
48:
49: _session.getSessionInternalFrame().getSQLPanelAPI()
50: .appendSQLScript(script.toString());
51: _session
52: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
53: } catch (Exception e) {
54: throw new RuntimeException(e);
55: }
56: }
57:
58: private String[] getSelectedViews() {
59: IDatabaseObjectInfo[] dbObjs = _session
60: .getSessionInternalFrame().getObjectTreeAPI()
61: .getSelectedDatabaseObjects();
62:
63: Vector ret = new Vector();
64: for (int i = 0; i < dbObjs.length; i++) {
65: if (dbObjs[i] instanceof ITableInfo) {
66: ITableInfo ti = (ITableInfo) dbObjs[i];
67: String sTable = PREFIX_SQLUSER + ti.getSimpleName();
68: ret.add(sTable);
69: }
70: }
71: return (String[]) ret.toArray(new String[0]);
72: }
73:
74: private String getStatementSeparator() {
75: String statementSeparator = _session.getProperties()
76: .getSQLStatementSeparator();
77:
78: if (1 < statementSeparator.length()) {
79: statementSeparator = "\n" + statementSeparator + "\n";
80: }
81:
82: return statementSeparator;
83: }
84:
85: }
|