| AbstractSqlStatement.java |
1 package com.orindasoft.demo.generated.sql;
2
3
4 import java.sql.*;
5 import com.orindasoft.pub.LogInterface;
6 import com.orindasoft.pub.OracleResourceUser;
7 import com.orindasoft.pub.StatementParameters2;
8 import com.orindasoft.pub.CSException;
9
10 /**
11 * Abstract SQL statement class for all classes generated by JDBCWizard./a>
12 *
13 * <p>Generated by JDBCWizard./a> build 2776 at 2011/03/07 19:55:47.167 GMT
14 * <p>JDBCWizard./a> is made by Orinda Software Ltd, Dublin, Ireland
15 * <p>Target Database: Oracle 10.2.0
16 * <p> -------------------------------------------------------------------------
17 * <p> WARNING: This code will stop working around the time JDBCWizard./a> expires.
18 * <p> This restriction only exists in the demo version of JDBCWizard./a>.
19 * <p> -------------------------------------------------------------------------
20 * <p> JDBCWizard./a> Tips 10 of 19:
21 * <p> JDBCWizard./a> allows you to run complex stored procedures without
22 * <p> using a precompiler such as SQLJ
23 * <p> -------------------------------------------------------------------------
24 *
25 * <p>Copyright Orinda Software 2003-2009
26
27 * <p>Class ChangeFlightDates.sql - Run contents of SQL file ChangeFlightDates
28 * <p>Class CustcomerAdd.sql - Run contents of SQL file CustcomerAdd
29 * <p>Class CustomerDelete.sql - Run contents of SQL file CustomerDelete
30 * <p>Class CustomerUpdate.sql - Run contents of SQL file CustomerUpdate
31 * @author devteam60@orindabuild.com
32 * @version 1.1
33 */
34 public abstract class AbstractSqlStatement implements OracleResourceUser
35 {
36 /**
37 * Constant used for object that is in this users account
38 */
39 public static final int THIS_USERS_OBJECT = 0;
40
41 /**
42 * Constant used for object that is in an other users account
43 */
44 public static final int OTHER_USERS_OBJECT = 1;
45
46 /**
47 * Constant used for object that is a refered to by a PRIVATE SYNONYM
48 */
49 public static final int THIS_USERS_SYNONYM = 2;
50
51 /**
52 * Constant used for object that is a refered to by a PUBLIC SYNONYM
53 */
54 public static final int PUBLIC_SYNONYM = 3;
55
56 /**
57 * Database Connection
58 */
59 public Connection theConnection = null;
60
61 /**
62 * An instance of a class which implements com.orindasoft.pub.LogInterface
63 */
64 public com.orindasoft.pub.LogInterface theLog = null;
65
66 /**
67 * Prepared statment for SQL Statement
68 */
69 public PreparedStatement theStatement = null;
70
71 /**
72 * Parameters for prepared statement
73 */
74 public StatementParameters2 theParameters = null;
75
76 /**
77 * Build Number - Added to spot conflicts between version of generated code
78 * and version of com.orindasoft.pub Library.
79 * @since 6.0.2706
80 */
81 private static final int theBuildNumber = 2776;
82
83 /**
84 * Create an instance of AbstractSqlStatement using a database connection and an instance of LogInterface
85 * @param Connection theConnection A database connection
86 * @param LogInterface theLog An instance of a class that implements com.orindasoft.pub.LogInterface
87 */
88 public AbstractSqlStatement(Connection theConnection, LogInterface theLog)
89 {
90 this.theConnection = theConnection;
91 this.theLog = theLog;
92 theParameters = new StatementParameters2(getProcCallStatement(),theLog);
93 }
94
95 /**
96 * Create an instance of AbstractSqlStatement using an instance of LogInterface
97 * @param LogInterface theLog An instance of a class that implements com.orindasoft.pub.LogInterface
98 */
99 public AbstractSqlStatement(LogInterface theLog)
100 {
101 this.theLog = theLog;
102 theParameters = new StatementParameters2(getProcCallStatement(),theLog);
103 }
104
105 /**
106 * Set or reset the database connection
107 * @param Connection theConnection A database connection
108 */
109 public void setConnection(Connection theConnection)
110 {
111 theLog.debug("AbstractSqlStatement - Setting DB Connection"); //DEBUG
112 this.theConnection = theConnection;
113 }
114
115 /**
116 * Return a string containing the magic incantation needed to execute this.
117 */
118 public abstract String getProcCallStatement();
119
120 /**
121 * Prepare statement if needed
122 */
123 protected PreparedStatement preparePreparedStatement() throws CSException
124 {
125 PreparedStatement newPreparedStatement;
126
127 theLog.debug("AbstractSqlStatement - Preparing Statement"); //DEBUG
128 // Parse statement...
129 try
130 {
131 theLog.debug("AbstractSqlStatement - Starting to parse statement");
132 newPreparedStatement = theConnection.prepareStatement(getProcCallStatement());
133 theLog.debug("AbstractSqlStatement - Finished parsing statement");
134 }
135 catch (java.sql.SQLException e)
136 {
137 theLog.error("AbstractSqlStatement: Prepare Statement failed with " + e.toString());
138 throw (new CSException("Unable to parse statement: " + e.toString()));
139 }
140
141
142 return(newPreparedStatement);
143
144 }
145
146 /**
147 * Create Parameters object if needed.
148 */
149 protected void createParams(String sqlString)
150 {
151 theParameters = new StatementParameters2(sqlString,theLog);
152 }
153
154 /**
155 * Bind Parameters object if needed.
156 */
157 protected abstract void bindParams() throws CSException;
158
159 /**
160 * Make sure connected and statement prepared
161 * This method allows application code to create a live Statement
162 * before JDBCWizard./a> would normally do so.
163 * @throws CSException An error of some kind. See <a href=http://www.orindasoft.com/?pdsrc=GD2776csexeceptions>CSExceptions</a>
164 * @since 5.0.2271 connectAndPrepare added
165 */
166 public void connectAndPrepare() throws CSException
167 {
168 if (theConnection == null)
169 {
170 throw (new CSException("Not Connected - Prepared Statement can not be executed"));
171 }
172
173 if (theStatement == null)
174 {
175 // Parse statement...
176 theStatement = preparePreparedStatement();
177
178 // Make sure generated code version matches version of this code...
179 com.orindasoft.pub.StatementParameters2.checkBuild(theBuildNumber);
180 }
181 }
182
183 /**
184 * Issue statement
185 * @throws CSException An error of some kind. See <a href=http://www.orindasoft.com/?pdsrc=GD2776csexeceptions>CSExceptions</a>
186 * @since 4.0.2054 Better Error handling
187 * @since 5.0.2271 connectAndPrepare added
188 */
189 public void executeCall() throws CSException
190 {
191 theLog.debug("AbstractSqlStatement - Execute Statement"); //DEBUG
192
193 connectAndPrepare();
194
195 try
196 {
197 // See if our statement is usable. If somebody else who has the connection has called
198 // its commit method out prepared statement will no longer be usable...
199 bindParams();
200 issueCall();
201 }
202 catch (CSException e)
203 {
204 theLog.warning("First attempt at execution failed: " + e.toString());
205 // Re-parse and try again
206 theStatement = preparePreparedStatement();
207 bindParams();
208 issueCall();
209 }
210 catch (Error e)
211 {
212 // Log error and throw it - we don't know what it is but at least
213 // the log will show where it was created. This kind of error can occur if
214 // the wrong runtime libraries are in use.
215 theStatement = null;
216 theLog.syserror("Error thrown while trying to execute statement:" + e.getClass().getName() + ":" + e.getMessage());
217 throw(e);
218 }
219
220 }
221
222
223 /**
224 * Execute the statement, keeping track of stats if requested
225 * @since 4.0.1798 Now traps java.lang.OutOfMemoryError
226 * @since 4.0.2054 Better error handling
227 */
228 private void issueCall() throws CSException
229 {
230 theLog.debug("AbstractSqlStatement - Start Execute DB Call"); //DEBUG
231 try
232 {
233 theStatement.executeUpdate();
234 }
235 catch (SQLException e)
236 {
237 throw (new CSException(e.toString()));
238 }
239 theLog.debug("AbstractSqlStatement - End Execute DB Call"); //DEBUG
240 }
241
242
243 /**
244 * Release all db resources that we know to be in use. Further attempts to use this
245 * will fail until we are given another connection to play with.
246 * We explicitly rollback at the end of this method.
247 * @return boolean <code>true</code> if we didn't encounter any problems, otherwise <code>false</code>
248 */
249
250 public boolean releaseResources()
251 {
252 boolean returnCode = true;
253
254 theLog.debug("AbstractSqlStatement - Attempting to release connection");
255
256 try
257 {
258
259 if (theStatement != null)
260 {
261 theStatement.close();
262 }
263 }
264 catch (SQLException e)
265 {
266 theLog.warning(e.toString());
267 returnCode = false;
268 }
269
270 theConnection = null;
271 theStatement = null;
272 theLog.debug("AbstractSqlStatement- Finished releasing connection");
273
274 return(returnCode);
275 }
276
277 /**
278 * Check to see if we are using Oracle Resources
279 * will fail until we are given another connection to play with.
280 * We explicitly rollback at the end of this method.
281 * @return <code>true</code> if we are using resources
282 * @return <code>false</code> if we are not using resources
283 */
284 public boolean hasResources()
285 {
286 if (theConnection == null)
287 {
288 return (false);
289 }
290
291 return(true);
292 }
293 } // Generated by JDBCWizard./a> 6.0.2776
294