Quantcast
Channel: Oracle Bloggers
Viewing all articles
Browse latest Browse all 19780

SQLcl running Python!(..actually, jython)

$
0
0

Our product manager wrote about Javascript support quite a long time ago.

Kris' blog: SQLcl - Oct 13th Edition

script command runs a script that can be any language that is JSR-223 enabled

Yesterday, I was helping customer's Weblogic env where their WLST (WebLogic Scripting Tool)
doesn't run well. WLST is Jython based so I learned a bit about jython and that lead me to this blog entry.

Jython 2.7.0 - Standalone Jar

For embedding Jython in Java applications

I don't know what will be the official way of running python but I just grabbed jar file above and it just runs.
For example, in Windows Cygwin, you can invoke SQLcl like...

$CLASSPATH=c:\\tmp\\jython-standalone-2.7.0.jar sqlcl/bin/sql hr/hr@localhost/xe
SQLcl: Release 4.2.0.15.349.0706 RC on 日 2 21 14:31:08 2016
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

I copy&pasted Kris's 1st javascript file to a new file with "py" extension, "kris.py".

Now, you may think I'm joking but let's just run this.
Javascript and Python are 2 different languages but in SQLcl case, both these 2 run on JVM
so some Java objects are common. In Kris's blog entry above, you can see a few Java objects'
names you can use in both Javascript and Python(and other) in the same way.

SQL>script kris.py                   
javax.script.ScriptException: SyntaxError: no viable alternative at input 'ps1' in <script> at line number 9 at column number 4
        at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:190)
        at org.python.jsr223.PyScriptEngine.compileScript(PyScriptEngine.java:75)
        at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
        at javax.script.AbstractScriptEngine.eval(Unknown Source)
        at oracle.dbtools.scripting.ScriptingUtils.runScript(ScriptingUtils.java:22)
        at oracle.dbtools.raptor.scriptrunner.commands.ScriptCommand.runScript(ScriptCommand.java:154)

It failed as you expected. It failed on line#9.
But at least, you can see that SQLcl tried to run the file as "python".
Javascript and python syntax are different so let's just fix it and run again.

SQL>!notepad kris.pySQL>script kris.py
Hold my beer, I'm going to try something
hi from python
This is a count of my objects
  COUNT(1)
----------
        47

This time it ran successfully. Here are the changes to the python file. I just changed the variable declaration which
is on line#9. And I apologize for not using "real-world" like sample today. I want to show BLOB sample and cx_Oracle converted sample in future.

$diff kris.js kris.py
3c3< ctx.write('hi from javascript\n');
--->ctx.write('hi from python\n');
9c9< var ps1= 'set sqlprompt "@|blue  _USER|@@@|green _CONNECT_IDENTIFIER|@@|blue ?? >|@"'
--->ps1= 'set sqlprompt "@|blue  _USER|@@@|green _CONNECT_IDENTIFIER|@@|blue ?? >|@"'
15c15< sqlcl.setStmt('@numbers.sql');
--->sqlcl.setStmt('@https://raw.githubusercontent.com/oracle/Oracle_DB_Tools/master/sqlcl/examples/numbers.sql');

Viewing all articles
Browse latest Browse all 19780

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>