Revision: 6055
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6055&view=rev
Author: manningr
Date: 2010-12-25 23:00:14 +0000 (Sat, 25 Dec 2010)
Log Message:
-----------
Demonstrate the implementation and registration of ISQLExecutionListener to intercept SQL that is being executed.
Modified Paths:
--------------
trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExamplePlugin.java
Added Paths:
-----------
trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExampleSqlExecutionListener.java
Modified: trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExamplePlugin.java
===================================================================
--- trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExamplePlugin.java 2010-12-25 00:55:49 UTC (rev 6054)
+++ trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExamplePlugin.java 2010-12-25 23:00:14 UTC (rev 6055)
@@ -10,18 +10,23 @@
import net.sourceforge.squirrel_sql.client.session.ISession;
import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
+import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
/**
- * The Example plugin class.
+ * The Example plugin class. This plugin does the following: 1. If the database types is DB2, it registers a
+ * menu action in the popup menu for view and procedure nodes in the ObjectTree. For detailed information and
+ * usage of the Plugin API see the following:
+ * https://sourceforge.net/apps/trac/squirrel-sql/wiki/SQuirreLSQLClientPluginAPI
*/
public class ExamplePlugin extends DefaultSessionPlugin
{
private PluginResources _resources;
+
/**
* Return the internal name of this plugin.
- *
- * @return the internal name of this plugin.
+ *
+ * @return the internal name of this plugin.
*/
public String getInternalName()
{
@@ -30,8 +35,8 @@
/**
* Return the descriptive name of this plugin.
- *
- * @return the descriptive name of this plugin.
+ *
+ * @return the descriptive name of this plugin.
*/
public String getDescriptiveName()
{
@@ -40,8 +45,8 @@
/**
* Returns the current version of this plugin.
- *
- * @return the current version of this plugin.
+ *
+ * @return the current version of this plugin.
*/
public String getVersion()
{
@@ -50,8 +55,8 @@
/**
* Returns the authors name.
- *
- * @return the authors name.
+ *
+ * @return the authors name.
*/
public String getAuthor()
{
@@ -59,12 +64,10 @@
}
/**
- * Returns the name of the change log for the plugin. This should
- * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
- * directory.
- *
- * @return the changelog file name or <TT>null</TT> if plugin doesn't have
- * a change log.
+ * Returns the name of the change log for the plugin. This should be a text or HTML file residing in the
+ * <TT>getPluginAppSettingsFolder</TT> directory.
+ *
+ * @return the changelog file name or <TT>null</TT> if plugin doesn't have a change log.
*/
public String getChangeLogFileName()
{
@@ -72,12 +75,10 @@
}
/**
- * Returns the name of the Help file for the plugin. This should
- * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
- * directory.
- *
- * @return the Help file name or <TT>null</TT> if plugin doesn't have
- * a help file.
+ * Returns the name of the Help file for the plugin. This should be a text or HTML file residing in the
+ * <TT>getPluginAppSettingsFolder</TT> directory.
+ *
+ * @return the Help file name or <TT>null</TT> if plugin doesn't have a help file.
*/
public String getHelpFileName()
{
@@ -85,12 +86,10 @@
}
/**
- * Returns the name of the Licence file for the plugin. This should
- * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
- * directory.
- *
- * @return the Licence file name or <TT>null</TT> if plugin doesn't have
- * a licence file.
+ * Returns the name of the Licence file for the plugin. This should be a text or HTML file residing in the
+ * <TT>getPluginAppSettingsFolder</TT> directory.
+ *
+ * @return the Licence file name or <TT>null</TT> if plugin doesn't have a licence file.
*/
public String getLicenceFileName()
{
@@ -98,7 +97,7 @@
}
/**
- * @return Comma separated list of contributors.
+ * @return Comma separated list of contributors.
*/
public String getContributors()
{
@@ -107,8 +106,8 @@
/**
* Create preferences panel for the Global Preferences dialog.
- *
- * @return Preferences panel.
+ *
+ * @return Preferences panel.
*/
public IGlobalPreferencesPanel[] getGlobalPreferencePanels()
{
@@ -123,36 +122,48 @@
_resources = new PluginResources("net.sourceforge.squirrel_sql.plugins.example.example", this);
}
-
/**
- * Called when a session started. Add commands to popup menu
- * in object tree.
- *
- * @param session The session that is starting.
- *
- * @return An implementation of PluginSessionCallback or null to indicate
- * the plugin does not work with this session
+ * Called when a session started. Add commands to popup menu in object tree.
+ *
+ * @param session
+ * The session that is starting.
+ * @return An implementation of PluginSessionCallback or null to indicate the plugin does not work with this
+ * session
*/
public PluginSessionCallback sessionStarted(ISession session)
{
+ // Adds the view and procedure script actions if the session is DB2.
+ addTreeNodeMenuActionsForDB2(session);
+
+ // Register a custom ISQLExecutionListener implementation that simply prints all SQL being executed to
+ // the message panel.
+ IMessageHandler messageHandler = session.getApplication().getMessageHandler();
+ ExampleSqlExecutionListener sqlExecutionListener = new ExampleSqlExecutionListener(messageHandler);
+ session.getSessionSheet().getSQLPaneAPI().addSQLExecutionListener(sqlExecutionListener);
+
+ return new PluginSessionCallbackAdaptor(this);
+ }
+
+ private void addTreeNodeMenuActionsForDB2(ISession session)
+ {
try
{
- if (! DialectFactory.isDB2(session.getMetaData())) {
- // Plugin knows only how to script Views and Stored Procedures on DB2.
- // So if it's not a DB2 Session we tell SQuirreL the Plugin should not be used.
- return null;
+ if (DialectFactory.isDB2(session.getMetaData()))
+ {
+ // Plugin knows only how to script Views and Stored Procedures on DB2.
+ // So if it's not a DB2 Session we tell SQuirreL the Plugin should not be used.
+
+ // Add context menu items to the object tree's view and procedure nodes.
+ IObjectTreeAPI otApi = session.getSessionInternalFrame().getObjectTreeAPI();
+ otApi.addToPopup(DatabaseObjectType.VIEW, new ScriptDB2ViewAction(getApplication(), _resources,
+ session));
+ otApi.addToPopup(DatabaseObjectType.PROCEDURE, new ScriptDB2ProcedureAction(getApplication(),
+ _resources, session));
}
-
- // Add context menu items to the object tree's view and procedure nodes.
- IObjectTreeAPI otApi = session.getSessionInternalFrame().getObjectTreeAPI();
- otApi.addToPopup(DatabaseObjectType.VIEW, new ScriptDB2ViewAction(getApplication(), _resources, session));
- otApi.addToPopup(DatabaseObjectType.PROCEDURE, new ScriptDB2ProcedureAction(getApplication(), _resources, session));
-
- return new PluginSessionCallbackAdaptor(this);
}
- catch(Exception e)
+ catch (Exception e)
{
- throw new RuntimeException(e);
+ throw new RuntimeException(e);
}
}
Added: trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExampleSqlExecutionListener.java
===================================================================
--- trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExampleSqlExecutionListener.java (rev 0)
+++ trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExampleSqlExecutionListener.java 2010-12-25 23:00:14 UTC (rev 6055)
@@ -0,0 +1,52 @@
+package net.sourceforge.squirrel_sql.plugins.example;
+
+import net.sourceforge.squirrel_sql.client.session.event.ISQLExecutionListener;
+import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
+
+/*
+ * Copyright (C) 2010 Rob Manning
+ * man...@us...
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * A simple ISQLExecutionListener that displays the SQL as it is being executed in the main application
+ * message panel (at the bottom of the application).
+ */
+public class ExampleSqlExecutionListener implements ISQLExecutionListener
+{
+ private final IMessageHandler _messageHandler;
+
+ public ExampleSqlExecutionListener(IMessageHandler messageHandler) {
+ _messageHandler = messageHandler;
+ }
+
+ @Override
+ public void statementExecuted(String sql)
+ {
+ _messageHandler.showMessage("statementExecuted: "+sql);
+ }
+
+ @Override
+ public String statementExecuting(String sql)
+ {
+ _messageHandler.showMessage("statementExecuting: "+sql);
+
+ // We don't modify the SQL in this example. We could veto it's execution be returned null.
+ return sql;
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|