sqlUpdate's statement doesn't appear to accept a variable argument

j13elliott
j13elliott's picture
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 03/13/2009
Points: 80

<mashup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.jackbe.com/2008-03-01/EMMLSchema../src/schemas/EMMLSpec.xsd"
    xmlns="http://www.jackbe.com/2008-03-01/EMMLSchema" xmlns:macro="http://www.jackbe.com/2008-03-01/EMMLMacro"
    name="TestIfExistsUpdateElseInsert">

    <operation name="invoke">

        <datasource url="jdbc:postgresql://framework:5432/JackBeDev"
            username="user" password="password" name="dataSource" />

        <input name="numRecords" type="number" default="50" />
        <input name="sortField" type="string" default="what?" />
        <input name="DN" type="string" default="admin" />
        <output name="result" type="string" default="" />

        <template
            expr="update usersearchprefs set userdn = {$DN},sortfield = '{$sortField}',
        numrecords = {$numRecords} where userdn = {$DN}"
            outputvariable="sqlUpdate" />

        <display message="sqlUpdate" variable="sqlUpdate" />

        <variables>
            <variable name="testString" type="string "
                default="update usersearchprefs set userdn = 'admin',sortfield = 'what?', numrecords = 50 where userdn = 'admin'"></variable>
        </variables>

        <sqlUpdate name="dataSource"
            statement="testString"
            outputvariable="result" />
    </operation>
</mashup>

The above fails with the following error:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [testString]; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "testString" : EMML Line # 30 : EMML Line # : 6

If statement ="testString" is changed to:

statement="update usersearchprefs set userdn = 'admin',sortfield = 'what?', numrecords = 50 where userdn = 'admin'" 

the update is successful.

Is there another approach that allows for passing a 'dynamic string' to the update?

0
Your rating: None
raj
raj's picture
User offline. Last seen 1 week 5 days ago. Offline
Joined: 09/22/2008
Points: 4

 Hi,

 

testString variable reference needs to be prefixed with "$"

<sqlUpdate name="dataSource"
            statement="$testString"
            outputvariable="result" />

raj.  chief masher @ jackbe

j13elliott
j13elliott's picture
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 03/13/2009
Points: 80

Thanks for the quick response:

I've  made the following change:

        <sqlUpdate name="dataSource"
            statement="$testString"
            outputvariable="result" />

and run the mashup again with the following results:

Error execution Mashup Script : org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [$testString]; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "$" : EMML Line # 30 : EMML Line # : 6

raj
raj's picture
User offline. Last seen 1 week 5 days ago. Offline
Joined: 09/22/2008
Points: 4

Which presto version are you using ?

Also, can u try using template and check the behavior...i.e.,

         <variable name="testString" type="string"></variable>

        <template expr="update usersearchprefs set userdn = 'admin',sortfield = 'what?', numrecords = 50 where userdn = 'admin'" outputvariable="testString"/>

        <sqlUpdate statement="$testString" name="mydb" />

 

 

raj.  chief masher @ jackbe

j13elliott
j13elliott's picture
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 03/13/2009
Points: 80

I'm using Version 2.7.0

I've attached the mashup that uses the template and returns the following error:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [$testString]; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "$" : EMML Line # 35 : EMML Line # : 6

 

raj
raj's picture
User offline. Last seen 1 week 5 days ago. Offline
Joined: 09/22/2008
Points: 4

 Hi,

my bad, dynamic variable ($variable) support in sqlUpdate is available only in yet-to-be released next version of Presto.

In 2.7, only static statements with named parameters are supported. 

 

raj.  chief masher @ jackbe

raj
raj's picture
User offline. Last seen 1 week 5 days ago. Offline
Joined: 09/22/2008
Points: 4

 To clarify named params,

you could do something like...

 

<mashup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.jackbe.com/2008-03-01/EMMLSchema../src/schemas/EMMLSpec.xsd"

    xmlns="http://www.jackbe.com/2008-03-01/EMMLSchema" xmlns:macro="http://www.jackbe.com/2008-03-01/EMMLMacro"

    name="TestIfExistsUpdateElseInsert">

        

    <operation name="invoke">

        

        <datasource name="dataSource" url="jdbc:hsqldb:hsql://localhost:9001" username="sa" password="" />

 

        <input name="numRecords" type="number" default="50" />

        <input name="sortField" type="string" default="what?" />

        <input name="DN" type="string" default="admin" />

        <input name="newDN" type="string" default="newadmin" />

        <output name="result" type="string" default="" />

        

 

    <variables>

    <variable name="testString" type="string"></variable>

    </variables>

            

        

        <sqlUpdate name="dataSource"

            statement="update usersearchprefs set userdn = :newDN, sortfield = :sortField,  numrecords = 50 where userdn = :DN "  outputvariable="result" />

 

    </operation>

</mashup>                                                                                                                                                                                                    

raj.  chief masher @ jackbe

nzblue_fish
nzblue_fish's picture
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 09/30/2009
Points: 1165

Hi Raj,

I've tried using a bound parameter in the sqlUpdate statement attribute as suggested but that doesn't appear to work either.

Here's the error message I get:

373 ERROR [com.jackbe.jbp.jems.moe.runtime.JEMSDesignerImpl] - <Error execution Mashup Script : org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [DROP TABLE ?]; nested exception is java.sql.SQLException: Incorrect syntax near '@P0'. : EMML Line # 126 : EMML Line # : 6>

and this was from the emml statement:

                <sqlUpdate name="$dsName" statement="DROP TABLE :table"
                    outputvariable="sqlCmdResult" />

where the table variable holds the actual table name.

Any thoughts?

Cheers, Innes

nzblue_fish
nzblue_fish's picture
User offline. Last seen 2 weeks 4 days ago. Offline
Joined: 09/30/2009
Points: 1165

Hi mashers,

Just confirming that in Presto 3.0, the <sqlUpdate> statement now supports dynamic variables via a <template> assigned variable. 

For example:

...

<variable name="dbTable" type="string" default="myTable" />

<variable name="colNames" type="string" default="mycol1,mcol2" />

<variable name="varBindings" type="string" default=":mycol1,:mycol2" />

...

<template expr="INSERT INTO {$dbTable} ({$colNames}) VALUES ({$varBindings})" outputvariable="sqlStmt" />

<sqlUpdate name="myDataSrc" statement="$sqlStmt" outputvariable="sqlRC"/>

...

now works as it did in Presto 2.7 for the <sql> statement.

Thanks to everyone at JackBe for including this in 3.0 as promised. You have just saved me a whole lot of hours and coding headaches on a major SQL database population project I'm working on.

Cheers everyone,

Mash on!!

Innes (NZ)

 

raj
raj's picture
User offline. Last seen 1 week 5 days ago. Offline
Joined: 09/22/2008
Points: 4

 That's great to know Innes !

raj.  chief masher @ jackbe