All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.option.sql.SQLUpdate Maven / Gradle / Ivy

There is a newer version: 10.5
Show newest version
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Saxonica Limited.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package net.sf.saxon.option.sql;

import net.sf.saxon.expr.Expression;
import net.sf.saxon.expr.SimpleExpression;
import net.sf.saxon.expr.StringLiteral;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.expr.instruct.Executable;
import net.sf.saxon.om.AxisInfo;
import net.sf.saxon.om.Item;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.style.Declaration;
import net.sf.saxon.style.ExtensionInstruction;
import net.sf.saxon.trans.SaxonErrorCode;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.tree.iter.AxisIterator;
import net.sf.saxon.tree.util.FastStringBuffer;
import net.sf.saxon.type.Type;
import net.sf.saxon.value.*;
import net.sf.saxon.value.StringValue;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* An sql:update element in the stylesheet.
*  @author Mathias Payer 
*  @author Michael Kay
* 

* For example: *

*   <sql:update connection="$connection" table="table-name" where="{$where}"
*                 xsl:extension-element-prefixes="sql">
*       <sql:column name="column-name" select="$new_value" />
*   </sql:update>
* 

*

*/ public class SQLUpdate extends ExtensionInstruction { Expression connection; String table; Expression where; public void prepareAttributes() throws XPathException { table = getAttributeList().getValue("", "table"); if (table==null) { reportAbsence("table"); } table = SQLConnect.quoteSqlName(table); String dbWhere = getAttributeList().getValue("", "where"); if (dbWhere == null) { where = new StringLiteral(StringValue.EMPTY_STRING); } else { where = makeAttributeValueTemplate(dbWhere); } String connectAtt = getAttributeList().getValue("", "connection"); if (connectAtt==null) { reportAbsence("connection"); } else { connection = makeExpression(connectAtt); } } public void validate(Declaration decl) throws XPathException { super.validate(decl); where = typeCheck("where", where); connection = typeCheck("connection", connection); AxisIterator kids = iterateAxis(AxisInfo.CHILD); while(true) { NodeInfo curr = (NodeInfo)kids.next(); if (curr == null) { break; } if (curr instanceof SQLColumn) { // OK } else if (curr.getNodeKind() == Type.TEXT && Whitespace.isWhite(curr.getStringValueCS())) { // OK } else { compileError("Only sql:column is allowed as a child of sql:update", "XTSE0010"); } } } public Expression compile(Executable exec, Declaration decl) throws XPathException { // Collect names of columns to be added FastStringBuffer statement = new FastStringBuffer(FastStringBuffer.MEDIUM); statement.append("UPDATE " + table + " SET "); AxisIterator kids = iterateAxis(AxisInfo.CHILD); NodeInfo child; int cols = 0; while (true) { child = (NodeInfo)kids.next(); if (child == null) { break; } if (child instanceof SQLColumn) { if (cols++ > 0) statement.append(','); String colname = ((SQLColumn)child).getColumnName(); statement.append(colname); statement.append("=?"); } } return new UpdateInstruction(connection, statement.toString(), getColumnInstructions(exec, decl), where); } /*@NotNull*/ public List getColumnInstructions(Executable exec, Declaration decl) throws XPathException { List list = new ArrayList(10); AxisIterator kids = iterateAxis(AxisInfo.CHILD); NodeInfo child; while (true) { child = (NodeInfo)kids.next(); if (child == null) { break; } if (child instanceof SQLColumn) { list.add(((SQLColumn)child).compile(exec, decl)); } } return list; } private static class UpdateInstruction extends SimpleExpression { private static final long serialVersionUID = -4234440812734827279L; public static final int CONNECTION = 0; public static final int WHERE = 1; public static final int FIRST_COLUMN = 2; String statement; public UpdateInstruction(Expression connection, String statement, List columnInstructions, Expression where) { Expression[] sub = new Expression[columnInstructions.size() + 2]; sub[CONNECTION] = connection; sub[WHERE] = where; for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy