SQLcl is a bundle of JAR files. These JAR files contain all the Java code to run SQLsl.
In our scripts, we can refer to the Java Classes and use their methods:
// Load the class
var StringUtils = Java.type("oracle.dbtools.common.utils.StringUtils");
print(StringUtils.initCapSingle("sqlcl is awesome"));
// Result: Sqlcl is awesome
You can use these tools to open and explore the JAR files:
It's pretty cool to have a look inside but only a couple of classes and methods are worth mentioning. Most of the tasks we want our script to do can be done with the globals provided by SQLcl.
These objects are globally exposed in SQLcl.
More info:
You can get the corresponding class with this JavaScript command:
print(sqlcl.getClass());
print(ctx.getClass());
print(util.getClass());
oracle.dbtools.raptor.newscriptrunner.ScriptExecutor
Located in oracle.dbtools-common.jar
oracle.dbtools.raptor.newscriptrunner.ScriptRunnerContext
Located in oracle.dbtools-common.jar
oracle.dbtools.db.OracleUtil
Located in oracle.dbtools-common.jar
Purpose: Dealing with the filesystem
- Read directories
- Get file properties
Docs: https://docs.oracle.com/javase/8/docs/api/java/io/File.html
My library to work with the filesystem and read file contentd
Purpose: Reading file contents
Docs: https://docs.oracle.com/javase/8/docs/api/java/io/FileReader.html
Again my library to work with the filesystem and read file contentd
Purpose: File utilities for SQLcl
- Useful to get the current working directory
var FileUtils = Java.type("oracle.dbtools.common.utils.FileUtils");
var cwd = FileUtils.getCWD(ctx);
Purpose: JDBC connections
- Useful to get a JDBC connection
var DriverManager = Java.type("java.sql.DriverManager");
// Create a new connection to use for monitoring
// Grab the connect URL from the base connection in sqlcl
var jdbc = conn.getMetaData().getURL();
var user = 'hr';
var pass = 'hr';
//connect
var conn2 = DriverManager.getConnection(jdbc,user,pass);
Purpose: Adding custom commands to SQLcl
Purpose: Listening for custom commands in SQLcl
See above