Package scriptella.driver.script

Scriptella bridge for the JSR 223: Scripting for the Java Platform.

See: Description

Package scriptella.driver.script Description

Scriptella bridge for the JSR 223: Scripting for the Java Platform.

Allows usage of JSR 223 compatible scripting languages in ETL <query> and <script> elements.

The driver relies on javax.script package from Java SE 6. The default language expected by the driver is JavaScript which is bundled with JRE.

General information

Driver class:scriptella.driver.script.Driver
URL:URL of the file to read from and send output to. URIs are resolved relative to a script file. If url is not specified console (System.in/out) is used.
Runtime dependencies:Java SE 6 is required. Additional dependencies are specific to a scripting language.

Driver Specific Properties

Name Description Required
language Language used in scripts and queries. No, the default language is JavaScript.
encoding Specifies charset encoding of a character stream specified by an url connection parameter. No, the system default encoding is used.

Query and Script Syntax

This driver does not impose any limitations on a language syntax. See JSR-223 script engines project for more details on supported languages.

Scripts and queries can reference variables from ancestor elements. Declared variables are exposed to nested elements.

Implicit variable query is available in <query> elements. This variable should be used to produce a result set:

    <query><![CDATA[
        var v1 = 'This variable is visible in a child script';
        for (var i = 0; i < 10; i++) {
            query.next(); // Triggers execution of a child script.
        }]]>
        <script>
            ......
            v2 = v1 + '!';
            ......
        </script>
    </query>
See ParametersCallbackMap class Javadoc for more details.

JDK 8 Nashorn JavaScript Engine notes

Important: Nashorn engine is not fully supported yet. See GitHub issue Fix JDK 8 Nashorn JavaScript engine integration #2 for status and additional details.

Examples

The following query executes a child script 10 times. As the result of execution 10 records are inserted into a database table. Additionally a log file log.txt is produced.
<connection id="script" driver="script"/>
<connection id="out" driver="oracle" url="jdbc:oracle:thin:@localhost:1521:DB"/>
<connection id="log" driver="script" url="log.txt"/>

<query connection-id="script">
    <![CDATA[
    for (var i = 0; i < 10; i++) {
        login = 'login' + i;
        //You can instantiate Java objects and invoke static methods
        var now = new java.sql.Timestamp(java.lang.System.currentTimeMillis());
        query.next(); //Executes a child script element
    }]]>

    <!-- Inserts a parameterized row into a database -->
    <script connection-id="out">
        INSERT INTO Table(ID, Login, Login_Time) VALUES (?i, ?login, ?now);
    </script>
    
    <!-- Logs the message using MessageFormat class and parent context variables -->
    <script connection-id="log">
        // create Java String array of 2 elements
        var a = java.lang.reflect.Array.newInstance(java.lang.Object, 2)
        a[0] = now;a[1] = i;
        println(format.format(a));
    >/script>
</query>

Copyright © Copyright 2006-2012 The Scriptella Project Team.