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 }
65
66 //*********************************************************************
67 // Accessor methods
68
69 /**
70 * Sets the name of the variable to hold the
71 * result.
72 */
73 public void setVar(String var) {
74 this.var = var;
75 }
76
77 /***
78 * Sets the scope of the variable to hold the
79 * result.
80 */
81 public void setScope(String scopeName) {
82 this.scope = scopeName;
83 }
84
85 /***
86 * Sets the SQL DataSource. DataSource can be
87 * a String or a DataSource object.
88 */
89 public void setDataSource(Object dataSource) {
90 this.rawDataSource = dataSource;
91 this.dataSourceSpecified = true;
92 }
93
94 /***
95 * Sets the SQL statement to use for the
96 * query. The statement may contain parameter markers
97 * (question marks, ?). If so, the parameter values must
98 * be set using nested value elements.
99 */
100 public void setSql(String sql) {
101 this.sql = sql;
102 }
103
104
105 //**********************************************************************
106 // Public utility methods
107
108 /**
109 * Called by nested parameter elements to add PreparedStatement
110 * parameter values.
111 */
112 public void addSQLParameter(Object o) {
113 if (parameters == null) {
114 parameters = new ArrayList();
115 }
116 parameters.add(o);
117 }
118
119 //**********************************************************************
120 // Protected utility methods
121
122 /**
123 * @return true if there are SQL parameters
124 */
125 protected boolean hasParameters() {
126 return parameters != null && parameters.size() > 0;
127 }
128
129 protected void clearParameters() {
130 parameters = null;
131 }
132
133 protected Connection getConnection() throws JellyTagException, SQLException {
134
135 Connection conn = null;
136 isPartOfTransaction = false;
137
138 TransactionTag parent =
139 (TransactionTag) findAncestorWithClass(TransactionTag.class);
140 if (parent != null) {
141 if (dataSourceSpecified) {
142 throw new JellyTagException(Resources.getMessage("ERROR_NESTED_DATASOURCE"));
143 }
144 conn = parent.getSharedConnection();
145 isPartOfTransaction = true;
146 }
147 else {
148 if ((rawDataSource == null) && dataSourceSpecified) {
149 throw new JellyTagException(Resources.getMessage("SQL_DATASOURCE_NULL"));
150 }
151 DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, context);
152 try {
153 conn = dataSource.getConnection();
154 }
155 catch (Exception ex) {
156 throw new JellyTagException(
157 Resources.getMessage("DATASOURCE_INVALID", ex.getMessage()));
158 }
159 }
160
161 return conn;
162 }
163
164 protected void setParameters(PreparedStatement ps)
165 throws SQLException {
166 if (parameters != null) {
167 for (int i = 0; i < parameters.size(); i++) {
168
169 ps.setObject(i + 1, parameters.get(i));
170 }
171 }
172 }
173 }