1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.sql;
20
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.SQLException;
24
25 import net.sf.jameleon.exception.JameleonScriptException;
26 import net.sf.jameleon.function.FunctionTag;
27 import net.sf.jameleon.util.JameleonUtility;
28
29 import org.apache.commons.jelly.JellyTagException;
30 import org.apache.commons.jelly.MissingAttributeException;
31
32 /***
33 * An abstract class used to execute SQL.
34 */
35 public abstract class AbstractSqlTag extends FunctionTag {
36
37 protected String sqlVarName;
38 /***
39 * The jdbc driver
40 * @jameleon.attribute required="true" contextName="jdbc.driver"
41 */
42 protected String jdbcDriver;
43 /***
44 * The jdbc url
45 * @jameleon.attribute required="true" contextName="jdbc.url"
46 */
47 protected String jdbcUrl;
48 /***
49 * The jdbc username
50 * @jameleon.attribute required="true" contextName="jdbc.username"
51 */
52 protected String jdbcUsername;
53 /***
54 * The jdbc password
55 * @jameleon.attribute required="false" contextName="jdbc.password"
56 */
57 protected String jdbcPassword;
58
59 public AbstractSqlTag(){
60 super();
61 }
62
63 public void testBlock() {
64 init();
65 String sql = getSql();
66 Connection conn = null;
67 try{
68 conn = getConnection();
69 executeSql(conn, sql);
70 assertTrue(true);
71 }catch (SQLException sqle){
72 JameleonScriptException jse = new JameleonScriptException("problem with: "+sql, sqle, this);
73 getFunctionResults().setError(jse);
74 }finally{
75 try{
76 if (conn != null && !conn.isClosed()) {
77 conn.close();
78 }
79 }catch (SQLException sqle){
80
81 }
82 }
83 destroy();
84 }
85
86 protected void validate() throws MissingAttributeException {
87 String sql = getSql();
88 if (sql == null || sql.trim().length() == 0) {
89 throw new MissingAttributeException(sqlVarName);
90 }
91 }
92
93 protected abstract void executeSql(Connection conn, String sql) throws SQLException;
94
95 /***
96 * Gets a connection to the database desired
97 * or a CSV file.
98 * This properties file should define an entry for each of the following:
99 * <ol>
100 * <li>jdbc.driver -- The driver using to connect to the datasource.
101 * <li>jdbc.username -- The username used to connect to the datasource
102 * <li>jdbc.password -- the password used to connect the username to the datasource
103 * <li>jdbc.url -- The url where the database exists.
104 * </ol>
105 */
106 protected Connection getConnection() throws SQLException{
107 Connection conn;
108
109 try {
110 Class.forName( jdbcDriver );
111 conn = DriverManager.getConnection( jdbcUrl, jdbcUsername, jdbcPassword );
112 conn.setAutoCommit( false );
113 } catch ( SQLException sqle ) {
114 String error = "Couldn't connect to database!!\n";
115 throw new SQLException( error + sqle.toString() +" " );
116 } catch ( ClassNotFoundException cnfe){
117 String error = "Couldn't load class "+jdbcDriver+"!!\n";
118 throw new SQLException( error + cnfe.toString());
119 }
120 return conn;
121 }
122
123
124 public String getSql(){
125 String sql = null;
126 if (isContextVariableNull(sqlVarName)) {
127 try{
128 sql = (String)getBodyText();
129 }catch(JellyTagException jte){
130 throw new JameleonScriptException(jte.getMessage(), jte, this);
131 }
132 }else{
133 sql = getVariableAsString(sqlVarName);
134 }
135 sql = JameleonUtility.decodeXMLToText(sql);
136 return sql;
137 }
138
139 protected void init(){}
140 protected void destroy(){}
141
142 }