sql:prepared-statement

Prepares a SQL statement for execution, returning a function which can be called with parameters to execute the statement, returning no result.

prepared-statement($connection as javatype:java.sql.Connection, $statement as xs:string) ➔ function(*)

Arguments

 

$connection

javatype:java.sql.Connection

A JDBC database connection established using sql:connect

 

$statement

xs:string

A SQL statement, typically containing question marks as place-holders for parameters

Result

function(*)

Namespace

http://saxon.sf.net/sql

Saxon availability

Implemented since Saxon 9.9. Requires Saxon-PE or Saxon-EE. Available for Java only.

Details

The function prepares a SQL statement for execution. The statement will typically be one that does not return a result, but which uses question marks as place-holders for parameters, for example INSERT INTO EMP VALUES (?, ?, ?). A call on sql:prepared-statement returns a function which can be called with parameters to execute the statement.

The arity of the returned function (the "invocation function") is equal to the number of question-marks appearing in the source statement.

The invocation function expects the supplied arguments to be atomic values. The data types of the arguments should correspond to the types of the values expected by the SQL statement: for example, xs:string for a VARCHAR column, xs:decimal for a SQL decimal, and so on.

The invocation function always returns an empty sequence, though to avoid over-aggressive optimization, it is declared as returning item()?. Because the invocation function is called for its side-effects, it is recommended to call it in the action expression of a saxon:do instruction.

Example:

<xsl:variable name="add-book" as="function(*)" select="sql:prepared-statement($connection, 'INSERT INTO book (isbn, title, author, category) VALUES (?, ?, ?, ?)')"/> <xsl:for-each select="//book"> <saxon:do action="$add-book(@isbn, @title, @author, @category)"/> </xsl:for-each>