001: package net.sourceforge.squirrel_sql.plugins.userscript.kernel;
002:
003: public class TemplateCode {
004: public static final String CODE = "package pack;\n"
005: + "\n"
006: + "import java.sql.Connection;\n"
007: + "import java.sql.DriverManager;\n"
008: + "import java.io.PrintStream;\n"
009: + "import java.lang.reflect.Method;\n"
010: + "\n"
011: + "public class TemplateUserScript\n"
012: + "{\n"
013: + " public static final String DB_OBJECT_TYPE_TABLE = \"TABLE\";\n"
014: + " public static final String DB_OBJECT_TYPE_VIEW = \"VIEW\";\n"
015: + " public static final String DB_OBJECT_TYPE_PROCEDURE = \"PROCEDURE\";\n"
016: + " public static final String DB_OBJECT_TYPE_CONNECTION = \"CONNECTION\";\n"
017: + " public static final String DB_OBJECT_TYPE_SQL_STATEMENT = \"SQL\";\n"
018: + "\n"
019: + " /**\n"
020: + " * Will be set from inside SQuirreL via reflection.\n"
021: + " */\n"
022: + " public Object environment;\n"
023: + "\n"
024: + " /**\n"
025: + " *\n"
026: + " * Place your script code in this method.\n"
027: + " *\n"
028: + " * @param dbObjectType one of the DB_OBJECT_TYPE_... constants\n"
029: + " * @param info\n"
030: + " * table name if DB_OBJECT_TYPE_TABLE.equals(DB_OBJECT_TYPE_TABLE)\n"
031: + " * view name if DB_OBJECT_TYPE_TABLE.equals(DB_OBJECT_TYPE_VIEW)\n"
032: + " * procedure name if DB_OBJECT_TYPE_TABLE.equals(DB_OBJECT_TYPE_PROCEDURE)\n"
033: + " * null if DB_OBJECT_TYPE_TABLE.equals(DB_OBJECT_TYPE_CONNECTION)\n"
034: + " * SQL String if DB_OBJECT_TYPE_TABLE.equals(DB_OBJECT_TYPE_SQL_STATEMENT)\n"
035: + " * @param con The connection of the DB session. If you work with transactions your\n"
036: + " * script will executed in the same transaction. Closing the the connection will break the session.\n"
037: + " */\n"
038: + " public void execute(String dbObjectType, String info, Connection con)\n"
039: + " throws Exception\n"
040: + " {\n"
041: + " PrintStream ps;\n"
042: + " ps = createPrintStream();\n"
043: + " ps.println(\"PS 1\");\n"
044: + " ps.println(\"type: \" + dbObjectType);\n"
045: + " ps.println(\"info: \" + info);\n"
046: + " ps.println(\"URL: \" + con.getMetaData().getURL());\n"
047: + "\n"
048: + " ps = createPrintStream(\"Bean\");\n"
049: + " ps.println(\"PS 2\");\n"
050: + " ps.println(\"type: \" + dbObjectType);\n"
051: + " ps.println(\"info: \" + info);\n"
052: + " ps.println(\"URL: \" + con.getMetaData().getURL());\n"
053: + "\n"
054: + " ps = getSQLAreaPrintStream();\n"
055: + " ps.println(\"To SQL Area: \" + dbObjectType);\n"
056: + " ps.println(\"type: \" + dbObjectType);\n"
057: + " ps.println(\"info: \" + info);\n"
058: + " ps.println(\"URL: \" + con.getMetaData().getURL());\n"
059: + "\n"
060: + " }\n"
061: + "\n"
062: + "\n"
063: + " //////////////////////////////////////////////////////////////////////////////\n"
064: + " // Service Methods\n"
065: + " //////////////////////////////////////////////////////////////////////////////\n"
066: + "\n"
067: + "\n"
068: + " /**\n"
069: + " * The output to these print streams will be presented in a tabbed window.\n"
070: + " */\n"
071: + " PrintStream createPrintStream()\n"
072: + " {\n"
073: + " return createPrintStream(null);\n"
074: + " }\n"
075: + "\n"
076: + " /**\n"
077: + " * The output to these print streams will be presented in a tabbed window.\n"
078: + " */\n"
079: + " PrintStream createPrintStream(String tabTitle)\n"
080: + " {\n"
081: + " try\n"
082: + " {\n"
083: + " Method m = environment.getClass().getMethod(\"createPrintStream\", new Class[]{String.class});\n"
084: + " return (PrintStream) m.invoke(environment, new Object[]{tabTitle});\n"
085: + " }\n"
086: + " catch (Exception e)\n"
087: + " {\n"
088: + " throw new RuntimeException(e);\n"
089: + " }\n"
090: + " }\n"
091: + "\n"
092: + " /**\n"
093: + " * The output to this print stream will be appended to the SQL text area of the db session\n"
094: + " */\n"
095: + " PrintStream getSQLAreaPrintStream()\n"
096: + " {\n"
097: + " try\n"
098: + " {\n"
099: + " Method m = environment.getClass().getMethod(\"getSQLAreaPrintStream\", new Class[0]);\n"
100: + " return (PrintStream) m.invoke(environment, new Object[0]);\n"
101: + " }\n"
102: + " catch (Exception e)\n"
103: + " {\n"
104: + " throw new RuntimeException(e);\n"
105: + " }\n"
106: + " }\n"
107: + "\n"
108: + "\n"
109: + "\n"
110: + "\n"
111: + " ///////////////////////////////////////////////////////////////////////////\n"
112: + " // To test the script outside SQuirreL\n"
113: + " //////////////////////////////////////////////////////////////////////////\n"
114: + " public static void main(String[] args)\n"
115: + " throws Exception\n"
116: + " {\n"
117: + " Class.forName(\"COM.ibm.db2.jdbc.net.DB2Driver\");\n"
118: + " Connection con = DriverManager.getConnection(\"jdbc:db2://localhost/TestDB\", \"db2inst1\", \"db2inst1\");\n"
119: + "\n" + "\n"
120: + " TemplateUserScript s = new TemplateUserScript();\n"
121: + " s.environment = new TestEnvironment();\n"
122: + " s.execute(DB_OBJECT_TYPE_TABLE, \"MyTable\", con);\n"
123: + " }\n" + "\n" + "}\n" + "\n" + "\n"
124: + "class TestEnvironment\n" + "{\n" + "\n"
125: + " public PrintStream createPrintStream()\n" + " {\n"
126: + " return System.out;\n" + " }\n" + "\n"
127: + " public PrintStream getSQLAreaPrintStream()\n" + " {\n"
128: + " return System.out;\n" + " }\n" + "} ";
129:
130: }
|