KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > sql > SQLInsert


1 package com.icl.saxon.sql;
2 import com.icl.saxon.*;
3 import com.icl.saxon.style.*;
4 import com.icl.saxon.om.NodeInfo;
5 import com.icl.saxon.expr.*;
6 import org.xml.sax.SAXException JavaDoc;
7 import org.xml.sax.AttributeList JavaDoc;
8 import org.w3c.dom.Node JavaDoc;
9 import java.sql.*;
10 //import java.util.*;
11
import javax.xml.transform.TransformerConfigurationException JavaDoc;
12 import javax.xml.transform.TransformerException JavaDoc;
13 /**
14 * An sql:insert element in the stylesheet.<BR>
15 */

16
17 public class SQLInsert extends StyleElement {
18
19     String JavaDoc table;
20
21     /**
22     * Determine whether this node is an instruction.
23     * @return true - it is an instruction
24     */

25
26     public boolean isInstruction() {
27         return true;
28     }
29
30     /**
31     * Determine whether this type of element is allowed to contain a template-body
32     * @return true: yes, it may contain a template-body (this is done only so that
33     * it can contain xsl:fallback)
34     */

35
36     public boolean mayContainTemplateBody() {
37         return true;
38     }
39     
40     public void prepareAttributes() throws TransformerConfigurationException JavaDoc {
41       
42         table = getAttribute("table");
43         if (table==null) reportAbsence("table");
44
45     }
46
47     public void validate() throws TransformerConfigurationException JavaDoc {
48         checkWithinTemplate();
49     }
50
51     public void process( Context context ) throws TransformerException JavaDoc {
52         
53         // Prepare the SQL statement (only do this once)
54

55         Controller controller = context.getController();
56         NodeInfo sourceDoc = (context.getCurrentNodeInfo()).getDocumentRoot();
57         Connection connection = (Connection)controller.getUserData(
58                                               sourceDoc, "sql:connection");
59         if (connection==null) {
60             throw styleError("No SQL connection has been established");
61         }
62
63         PreparedStatement ps =(PreparedStatement)controller.getUserData(
64                                               this, "sql:statement");
65                                               
66         try {
67             if (ps==null) {
68     
69                 StringBuffer JavaDoc statement = new StringBuffer JavaDoc();
70                 statement.append("INSERT INTO " + table + " (");
71         
72                 // Collect names of columns to be added
73

74                 Node JavaDoc child = getFirstChild();
75                 int cols = 0;
76                 while (child!=null) {
77                     if (child instanceof SQLColumn) {
78                         if (cols++ > 0) statement.append(',');
79                         String JavaDoc colname = ((SQLColumn)child).getColumnName();
80                         statement.append(colname);
81                     }
82                     child = child.getNextSibling();
83                 }
84                 statement.append(") VALUES (");
85             
86                 // Add "?" marks for the variable parameters
87

88                 for(int i=0; i<cols; i++) {
89                     if (i!=0)
90                         statement.append(',');
91                     statement.append('?');
92                 };
93                 
94                 statement.append(')');
95     
96                 // Prepare the SQL statement
97

98                 ps=connection.prepareStatement(statement.toString());
99                 controller.setUserData(this, "sql:statement", ps);
100             }
101
102             // Add the actual column values to be inserted
103

104             Node JavaDoc child2 = getFirstChild();
105             int i=1;
106             while (child2!=null) {
107                 if (child2 instanceof SQLColumn) {
108                                  
109                     // Get the column value: either from the select attribute or from content
110
Value v = ((SQLColumn)child2).getColumnValue(context);
111             
112                     // TODO: the values are all strings. There is no way of adding to a numeric column
113
String JavaDoc val = v.asString();
114             
115                     // another hack: setString() doesn't seem to like single-character string values
116
if (val.length()==1) val += " ";
117                     ps.setObject(i++, val);
118
119                 }
120                 
121                 // Get the next column and decide whether we've reached the last
122
child2 = child2.getNextSibling();
123             }
124                 
125             int num = ps.executeUpdate();
126             if (!connection.getAutoCommit()) {
127                 connection.commit();
128             }
129
130         } catch (SQLException ex) {
131             throw styleError("(SQL) " + ex.getMessage());
132         }
133     }
134
135
136 }
137
138 //
139
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
140
// you may not use this file except in compliance with the License. You may obtain a copy of the
141
// License at http://www.mozilla.org/MPL/
142
//
143
// Software distributed under the License is distributed on an "AS IS" basis,
144
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
145
// See the License for the specific language governing rights and limitations under the License.
146
//
147
// The Original Code is: all this file.
148
//
149
// The Initial Developer of the Original Code is
150
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
151
//
152
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
153
//
154
// Contributor(s): none.
155
//
156
Popular Tags