Search notes:

SQLcl: User defined command handlers

Print the name of an executed script in green

The following SQLcl command handler runs (start, @) an SQL script.
Before the script is executed, its name is colored in green to make debugging easier if lots of scripts are run.
script
Java.type("oracle.dbtools.raptor.newscriptrunner.CommandRegistry").addForAllStmtsListener(
   Java.extend(Java.type("oracle.dbtools.raptor.newscriptrunner.CommandListener"),
   {
      handleEvent: function(conn, ctx, cmd) {

         var stmt = cmd.getSql().trim();
         if ( stmt.startsWith('runscript') ) {

             var scriptName = stmt.substring("runscript".length).trim();
             print("Running \x1b[32m" + scriptName + "\x1b[0m"); // Embed the script name in ANSI escape sequences for green.
             sqlcl.setStmt('@ ' + scriptName); sqlcl.run();

             return true;
         }

         return false;
      },

      beginEvent: function(conn, ctx, cmd) {},
      endEvent :  function(conn, ctx, cmd) {}

   }).class
);
/
Update 2024-04-18: It turns out that using this command handler causes SQLcl to sometimes(!) raise an exception if the sourced script contains an anonymous PL/SQL block.
An alternative to achieve the desired effect is this attempt to color or highlight the name of executed scripts (it is an SQL script that uses colored prompts).

Index