Scriptella and (mysql) Stored Procedures "OUT" Parameter

831 views Asked by At

I'm new to scriptella and have found it to be very useful thus far but have hit the following roadblock. I have an SQL procedure that already exists within a MYSQL database similar to the following.

DELIMITER //
CREATE PROCEDURE FOO(IN input_param INT, OUT output_param1 INT, OUT output_param2 INT)
BEGIN
      /* real FOO sets output_params (conditionally) based on input_param */
      /*     AND updates an internal table */
      SET output_param1 = 1;
      SET output_param2 = 2;
END//
DELIMITER ;

I would like to invoke/call this procedure from within Scriptella, similar to the following:

    <query connection-id="in">
      SELECT SomeColumn FROM SomeTable;
      <query connection-id="out1">
          CALL FOO(SomeColumn, @OUT1, @OUT2);   // ERROR1 exception from this line, see below
          <script connection-id="jexl">
              etl.globals['OUT1'] = @OUT1;      // ERROR2 doesn't like "@"
              etl.globals['OUT2'] = @OUT2;
          </script>
      </query>
      <query connection-id="out2">
          INSERT INTO AnotherTable (col1, col2) 
                VALUES (${etl.globals['OUT1']}, ${etl.globals['OUT2']});
      </query>
    </query>    

ERROR1 - Scriptella expects the line "CALL FOO(SomeColumn, @OUT1, @OUT2);" to return a resultSet.
Driver exception: java.sql.SQLException: ResultSet is from UPDATE. No Data. ** If I add "SELECT 1" to the end of the SQL stored procedure, it returns a value and eliminates this error.

ERROR2 - The code above attempts to store the OUT parameter(s) as temporary SQL variables, which don't work in Scriptella.
Driver exception: org.apache.commons.jexl2.JexlException: scriptella.driver.jexl.JexlConnection.run@80 tokenization failed

** It doesn't like the @ symbols or use of temporary variables here.

QUESTIONS Does Scriptella support invoking stored procedures and returning OUT parameters? I've tried a number of workarounds/hacks for this but haven't made any progress to date.

1

There are 1 answers

0
ejboy On

The following script worked for me on mysql:

<!--
Example of calling the following procedure:
CREATE PROCEDURE demoSp(IN inputParam VARCHAR(255),
                    INOUT inOutParam INT, OUT outParam VARCHAR(255))
BEGIN
    DECLARE z INT;
    SET z = inOutParam + 1;
    SET inOutParam = z;
    SET outParam = 'Out param';

    SELECT inputParam;
   END
//
-->

<!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd">
<etl>
    <connection id="in" url="jdbc:mysql://localhost/test" user="root"/>
    <connection id="out" driver="text"/>

    <!-- Calling a stored procedure -->
    <script connection-id="in">
       CALL demoSp('abc3', @a, @b);
    </script>
    <!-- Using output parameters -->
    <query connection-id="in">
        select @a,@b;
        <script connection-id="out">
            $1,$2
        </script>
    </query>
    <!-- Calling a stored procedure and iterating resultset (if any) -->
    <query connection-id="in">
        CALL demoSp('abc3', @a, @b);
        <script connection-id="out">
            Row: $1
        </script>
    </query>
</etl>