=====Jython Scripting für die Oracle Datenbank mit SQLcl=====
===== Integration in SQLcl====
Mehr zu SQLcl siehe [[dba:sqlcl_oracle_command_line_in_12c|SQLcl - Quo vadis SQL*Plus? - Das neue SQL*Plus in der Praxis - Der neue SQL Kommando Interpreter für die Oracle Datenbank]]
Um Jython einzubinden muss nur die Jar Datei in den Klassenpfad beim Aufruf von SQLcl aufgenommen werden.
- Download der Jar Datei jython-standalone-2.7.0.jar von http://www.jython.org/downloads.html
- Kopieren nach $SQLCL_HOME/lib
- Einbinden in den Klassen Pfad, über die Umgebung oder über die $SQLCL_HOME/bin/sql.bat bzw. sql
- Linux: export CLASSPATH=$CLASSPATH:/opt/oracle/products/sqlcl/lib/jython-standalone-2.7.0.jar
- Erstellen des ersten Jython scripts helloWorld.pyprint "Hello World"
- Aufruf mit „SQL> script helloWorld.py“ => „Hello World“
D.h. die ScriptEngine erkennt über die Datei Endung, was für ein Interpreter verwandt werden soll!
NUR SQL Scripte werden in SQLcl mit @ aufgerufen, js und py Scripte immer mit "script" !
==Problem: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp65001.==
Leider führt der erste Test unter Windows gleich zu einem „console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp65001.“
Das lässt sich zwar über den Java Start Parameter „-Dpython.console.encoding=UTF-8“ bekämpfen, zeigt aber wieder auf, dass hier noch viel Arbeit für Oracle notwendig ist um das Konsolenwerkzeug unter Windows wirklich lauffähig zu bekommen.
----
==== Einsatz Beispiele ====
Auch innerhalb des Scopes des Scripts sind die “äußern” Objekte der SQLcl Umgebung erreichbar und können aufgerufen werden
Folgende Objekte können in Script (default Java Script Syntax ! ) direkt angesprochen werden:
* **sqlcl** - SQLCL selbst wie sqlcl.setStmt(„select * from dual“) und sqlcl.run() um das dann auszuführen
* Methods: **setStmt()** - Ein SQL Statment oder einen SQLcl Befehl in den Buffer schrieben
* Methods: **run** - Den Buffer ausführen
* **ctx** - Object vom Typ ScriptContext, kann direkt angesprochen werden - wie ctx.write(„String“)
* Methods: **write()** - Auf Standard Out schreiben
* Methods: **getProperty()** - Wert im Session Context setzen
* Methods: **setProperty(,