Created
May 30, 2019 09:47
-
-
Save nochmu/3d5d6b37aa25fc2d1f36471ef3cc63b5 to your computer and use it in GitHub Desktop.
FETCH Command for SQLcl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* FETCH Command for SQLcl | |
* FETCH is like EXEC but for functions instead of procedures. | |
* Usage: | |
* SQL> script fetch.js | |
* SQL> fetch SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') | |
* SYS_CONTEXT('USERENV','CURRENT_SCHEMA') | |
* --------------------------------------- | |
* CMU | |
*/ | |
// see also: https://github.com/oracle/oracle-db-tools/blob/master/sqlcl/SCRIPTING.md | |
function trimln(str) | |
{ | |
if(str[0] == "\n") | |
return trimln(str.slice(1)); | |
if(str[str.length-1] == "\n") | |
return trimln(str.slice(0, -1)); | |
else | |
return str; | |
} | |
var command = {}; | |
// Command handler | |
// @return true if the command was handled by this handler - otherwise false. | |
command.handle = function (conn,ctx,cmd) | |
{ | |
var name = 'FETCH'; | |
var sql = cmd.getSql(); | |
// Current Command begins with FETCH? | |
if ( sql.toUpperCase().indexOf(name) == 0 ) | |
{ | |
sql = sql.slice(name.length()); | |
sql = trimln(sql); | |
sql = "SELECT "+sql+" FROM dual;"; | |
sqlcl.setStmt(sql); | |
sqlcl.run(); | |
return true; | |
} | |
return false; | |
} | |
// fired before ANY command | |
command.begin = function(conn,ctx,cmd) { | |
} | |
// fired after ANY Command | |
command.end = function(conn,ctx,cmd) { | |
} | |
// Extend the Java CommandListener | |
var CommandListener = Java.type("oracle.dbtools.raptor.newscriptrunner.CommandListener"); | |
var MyCmd = Java.extend(CommandListener, { | |
handleEvent: command.handle , | |
beginEvent: command.begin , | |
endEvent: command.end | |
}); | |
// Add the new command to the Registry | |
var CommandRegistry = Java.type("oracle.dbtools.raptor.newscriptrunner.CommandRegistry"); | |
CommandRegistry.addForAllStmtsListener(MyCmd.class); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment