Blog do projektu Open Source JavaHotel

piątek, 7 października 2011

DB2 script launcher, Eclipse plugin


I created my first Eclipse plug-in. It is quite simple and allows running from Eclipse a script (sql) file against DB2 database.
It is based on CLP - Command Line Processor - and runs script which are valid in terms of this tool. It does not use JDBC connection.
In order to run  this plugin it is necessary to install DB2 client package. It is free and available from IBM web page. DB2 database (remote or local) should be cataloged and ready to use.

How it runs
Firstly we have to create DB2 script launcher.

"Database alias name" is an alias (catalog name) to DB2 database.
After having launcher defined we can simply right-click on any file (with .db2 or .sql extension) and run this script using database access connection defined.

More then one database configuration can be defined to have an access to more than one database. In this case the selection dialog will be displayed allowing choosing the database (configuration).
It is also possible to run script launcher in editor. The current content of the editor is copied to temporary file and CLP is launched.
If no configuration has been defined yet than launcher dialog will displayed allowing defining the first one.

Source code
Source code and plugin.xml file file available here. It is my first plugin and probably not everything is perfect.
Several problems have been especially troublesome and required scanning through the documentation.

How to launch the configuration programmatically to allow entering the very first configuration (source).
if (configurations.length == 0) {
    // Open launch configuration dialog for enter first configuration
    String name = manager.generateLaunchConfigurationName(NEW_CONFIGURATION);
    ILaunchConfigurationWorkingCopy wc = type.newInstance(null,name);
    configuration = wc.doSave();
    LaunchConfigurationManager lcm = DebugUIPlugin.getDefault().getLaunchConfigurationManager();
    ILaunchGroup group = lcm.getLaunchGroup(type, ILaunchManager.RUN_MODE);
    DebugUITools.openLaunchConfigurationPropertiesDialog(getShell(), configuration, group.getIdentifier());

How to extract file path name (necessary to launch CLP) from eclipse IPath (source)
 public void launch(ISelection arg0, String arg1) {
      ITreeSelection iSel = (ITreeSelection) arg0;
      TreePath[] t = iSel.getPaths();
      TreePath fPath = t[0];
      Object o = fPath.getLastSegment();
      File f = (File) o;
      URI locationURI = f.getLocationURI();
      URI u = locationURI;
      String fileName = u.getPath();
      runDB2(fileName, null);

How to extract content from the open editor (source)
public void launch(IEditorPart arg0, String arg1) {
  ITextEditor i = (ITextEditor) arg0;
  IDocumentProvider dProvider = i.getDocumentProvider();
  IDocument iDok = dProvider.getDocument(i.getEditorInput());
  String content = iDok.get();
  runDB2(null, content);
This launcher is very simple but can be the basis for future developing. For instance
  1. Enter the separator between different sql statements inside the file. Now ; is hardcoded.
  2. Keep the history of launching to match previous results.
  3. Catalog database (connection method) from plugin directly.
  4. etc.

Brak komentarzy:

Publikowanie komentarza