1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jelly.tags.sql;
18
19 import java.sql.Connection;
20 import java.sql.PreparedStatement;
21 import java.sql.SQLException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
26 import javax.sql.DataSource;
27
28 import org.apache.commons.jelly.JellyTagException;
29 import org.apache.commons.jelly.TagSupport;
30 import org.apache.commons.jelly.tags.Resources;
31
32 /***
33 * <p>Abstract base class for any SQL related tag in JSTL.
34 *
35 * @author Hans Bergsten
36 * @author Justyna Horwat
37 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
38 */
39
40 public abstract class SqlTagSupport extends TagSupport implements SQLExecutionTag {
41
42 protected String var;
43 protected String scope = "page";
44
45
46
47
48
49
50 protected Object rawDataSource;
51 protected boolean dataSourceSpecified;
52 protected String sql;
53
54
55
56
57 private List parameters;
58 protected boolean isPartOfTransaction;
59
60 //**********************************************************************
61 // Constructor and initialization
62
63 public SqlTagSupport() {
64 super.setEscapeText(false);
65 }
66
67 //*********************************************************************
68 // Accessor methods
69
70 /**
71 * Sets the name of the variable to hold the
72 * result.
73 */
74 public void setVar(String var) {
75 this.var = var;
76 }
77
78 /***
79 * Sets the scope of the variable to hold the
80 * result.
81 */
82 public void setScope(String scopeName) {
83 this.scope = scopeName;
84 }
85
86 /***
87 * Sets the SQL DataSource. DataSource can be
88 * a String or a DataSource object.
89 */
90 public void setDataSource(Object dataSource) {
91 this.rawDataSource = dataSource;
92 this.dataSourceSpecified = true;
93 }
94
95 /***
96 * Sets the SQL statement to use for the
97 * query. The statement may contain parameter markers
98 * (question marks, ?). If so, the parameter values must
99 * be set using nested value elements.
100 */
101 public void setSql(String sql) {
102 this.sql = sql;
103 }
104
105
106 //**********************************************************************
107 // Public utility methods
108
109 /**
110 * Called by nested parameter elements to add PreparedStatement
111 * parameter values.
112 */
113 public void addSQLParameter(Object o) {
114 if (parameters == null) {
115 parameters = new ArrayList();
116 }
117 parameters.add(o);
118 }
119
120 //**********************************************************************
121 // Protected utility methods
122
123 /**
124 * @return true if there are SQL parameters
125 */
126 protected boolean hasParameters() {
127 return parameters != null && parameters.size() > 0;
128 }
129
130 protected void clearParameters() {
131 parameters = null;
132 }
133
134 protected Connection getConnection() throws JellyTagException, SQLException {
135
136 Connection conn = null;
137 isPartOfTransaction = false;
138
139 TransactionTag parent =
140 (TransactionTag) findAncestorWithClass(TransactionTag.class);
141 if (parent != null) {
142 if (dataSourceSpecified) {
143 throw new JellyTagException(Resources.getMessage("ERROR_NESTED_DATASOURCE"));
144 }
145 conn = parent.getSharedConnection();
146 isPartOfTransaction = true;
147 }
148 else {
149 if ((rawDataSource == null) && dataSourceSpecified) {
150 throw new JellyTagException(Resources.getMessage("SQL_DATASOURCE_NULL"));
151 }
152 DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, context);
153 try {
154 conn = dataSource.getConnection();
155 }
156 catch (Exception ex) {
157 throw new JellyTagException(
158 Resources.getMessage("DATASOURCE_INVALID", ex.getMessage()));
159 }
160 }
161
162 return conn;
163 }
164
165 protected void setParameters(PreparedStatement ps)
166 throws SQLException {
167 if (parameters != null) {
168 for (int i = 0; i < parameters.size(); i++) {
169
170 ps.setObject(i + 1, parameters.get(i));
171 }
172 }
173 }
174 }