1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.data;
20
21 import org.apache.log4j.Logger;
22 /***
23 * Used to iterate over all tags it is surrounding one time per row in a SQL ResultSet.
24 * This tag should work with any RDBMS that provides a JDBC driver.
25 * One thing to remember is each JDBC driver seems to act a bit differently when returning
26 * the column names returned. Some JDBC drivers will returned the column names in all caps
27 * some will return them as undercase, while others may return them as defined when the table
28 * was created.
29 * <pre><source>
30 * <testcase xmlns="jelly:jameleon">
31 * <sql query="SELECT ID, col1 as NAME, col2 as GENDER FROM TEST"
32 * jdbcUsername="sa"
33 * jdbcPassword=""
34 * jdbcUrl="jdbc:hsqldb:hsqldb_sample"
35 * jdbcDriver="org.hsqldb.jdbcDriver"
36 * countRow="true">
37 *
38 * <some-session application="someApp" beginSession="true">
39 * <some-tag-that-uses-context-variables
40 * functionId="Verify successful navigation, using a different variable."
41 * id="${ID}"
42 * name="${NAME}"
43 * gender="${GENDER}"/>
44 * </some-session>
45 * </sql>
46 * </testcase>
47 * </source></pre>
48
49 * @jameleon.function name="sql"
50 */
51
52 public class SqlTag extends AbstractDataDrivableTag {
53
54 protected SqlDataDriver sqld;
55 protected String query;
56 protected String id = "-";
57 protected String jdbcDriver;
58 protected String jdbcUrl;
59 protected String jdbcUsername;
60 protected String jdbcPassword;
61
62 /***
63 * Gets the logger used for this tag
64 * @return the logger used for this tag.
65 */
66 protected Logger getLogger() {
67 return Logger.getLogger(SqlTag.class.getName());
68 }
69
70 /***
71 * Gets the DataDriver used for this tag.
72 * @return the DataDriver used for this tag.
73 */
74 protected DataDriver getDataDriver() {
75 if (sqld == null) {
76 sqld = new SqlDataDriver();
77 }
78 return sqld;
79 }
80
81 /***
82 * Gets an error message to be displayed when a error occurs due to the DataDriver.
83 * @return an error message to be displayed when a error occurs due to the DataDriver.
84 */
85 protected String getDataExceptionMessage() {
86 return "Trouble querying database with '"+getQuery()+"'";
87 }
88
89 /***
90 * Sets up the DataDriver by calling any implementation-dependent
91 * methods.
92 */
93 protected void setupDataDriver() {
94 sqld.setJDBCDriver(jdbcDriver);
95 sqld.setJDBCPassword(jdbcPassword);
96 sqld.setJDBCUrl(jdbcUrl);
97 sqld.setJDBCUsername(jdbcUsername);
98 sqld.setQuery(getQuery());
99 }
100
101 /***
102 * Gets the query to be used for this tag
103 * @return the query used by this tag
104 */
105 public String getQuery(){
106 return query;
107 }
108
109 /***
110 * Gets the trace message when the execution is beginning and ending.
111 * The message displayed will already start with BEGIN: or END:
112 * @return the trace message when the execution is just beginning and ending.
113 */
114 protected String getTagTraceMsg() {
115 return "executing sql " + getQuery();
116 }
117
118 /***
119 * Describe the tag when error messages occur.
120 * The most appropriate message might be the tag name itself.
121 * @return A brief description of the tag or the tag name itself.
122 */
123 public String getTagDescription() {
124 return "sql tag";
125 }
126
127 /***
128 * Calculates the location of the state to be stored for any tags under this tag.
129 * The result should be a relative path that simply has the row number in it along
130 * with some unique indentifier for this tag like the handle name or something.
131 * @return the location of the state to be stored for any tags under this tag minus the baseDir calculation stuff.
132 */
133 protected String getNewStateStoreLocation(int rowNum) {
134 return "sql-"+id+"-"+rowNum;
135 }
136
137 /***
138 * Set the JDBC Driver needed for the Database Connection
139 * @jameleon.attribute required="true"
140 */
141 public void setJdbcDriver (String jdbcDriver) {
142 this.jdbcDriver = jdbcDriver;
143 }
144
145 /***
146 * Set the JDBC URL for the Database Connection
147 * @jameleon.attribute required="true"
148 */
149 public void setJdbcUrl (String jdbcUrl) {
150 this.jdbcUrl = jdbcUrl;
151 }
152
153 /***
154 * Set the JDBC UserName for the Database Connection
155 * @jameleon.attribute required="true"
156 */
157 public void setJdbcUsername (String jdbcUsername) {
158 this.jdbcUsername = jdbcUsername;
159 }
160
161 /***
162 * Set the JDBC Password for the Database Connection
163 * @jameleon.attribute required="false"
164 */
165 public void setJdbcPassword (String jdbcPassword) {
166 this.jdbcPassword = jdbcPassword;
167 }
168
169 /***
170 * Set the SQL Query for the Database Connection
171 * @jameleon.attribute required="true"
172 */
173 public void setQuery (String query) {
174 this.query = query;
175 }
176
177 /***
178 * Set the SQL ID number
179 * This is primarily used for debugging.
180 * It also provides a means to distinguish between nested <code>SqlTag</code> tags.
181 * @jameleon.attribute required="false"
182 */
183 public void setId (String id) {
184 this.id = id;
185 }
186
187 }