Source code: nectar/data/mysql/MySqlAdapterService.java
1 /*
2 Copyright (C) 2003 Kai Schutte
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 * MySqlAdapter.java
19 *
20 * Created on March 13, 2003, 12:26 PM
21 */
22
23 package nectar.data.mysql;
24
25 import nectar.ServicesUtil;
26 import nectar.data.mysql.MysqlQueryRenderer;
27 import nectar.data.*;
28
29 import nectar.record.Record;
30
31 import java.sql.Connection;
32 import java.sql.Statement;
33 import java.sql.ResultSet;
34 import java.util.HashMap;
35 import java.util.Vector;
36 import java.util.Collection;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.LinkedList;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 /*
45 *
46 * @author Kai Schutte skander@skander.com
47 */
48 public class MySqlAdapterService extends DataAdapterService {
49 protected MysqlQueryRenderer queryRenderer = new MysqlQueryRenderer();
50 private static Log log = LogFactory.getLog(MySqlAdapterService.class);
51
52 /** Creates a new instance of MySqlAdapter */
53 public MySqlAdapterService() {
54 }
55
56 protected Statement getStatement() throws DataException {
57 Connection c = ServicesUtil.getConnection();
58 Statement s;
59 try {
60 s = c.createStatement();
61 } catch (Exception e) {
62 throw new DataException("Couldn't create Statement!", e);
63 }
64 return s;
65 }
66
67 protected void closeStatement(Statement s) throws DataException {
68 if (s == null) return;
69 try {
70 Connection c = s.getConnection();
71 s.close();
72 c.close();
73 } catch (Exception e) {
74 throw new DataException("Couldn't close Statement!", e);
75 }
76 }
77
78 public Object readSingleFieldSingleRow(Query q) throws DataException {
79 Statement stmt = getStatement();
80 String query = queryRenderer.renderSelect(q);
81 if(log.isTraceEnabled())
82 log.trace("QUERY: "+query);
83 Object obj = null;
84 try {
85 ResultSet rs = stmt.executeQuery(query);
86 if (rs.first())
87 obj = rs.getObject(1);
88 } catch (Exception e) {
89 closeStatement(stmt);
90 throw new DataException("Error while retrieving Data", e);
91 }
92 closeStatement(stmt);
93 return obj;
94 }
95
96
97 public HashMap readMultipleFieldSingleRow(Query q) throws DataException {
98 Statement stmt = getStatement();
99 String query = queryRenderer.renderSelect(q);
100 if(log.isTraceEnabled())
101 log.trace("QUERY: "+query);
102 HashMap result = null;
103 try {
104 ResultSet rs = stmt.executeQuery(query);
105 if (rs.first()) {
106 result = new HashMap(q.getFields().size()+ q.getExtraFields().size());
107 for (Iterator iter = q.getFields().iterator(); iter.hasNext();) {
108 String fn = (String)iter.next();
109 result.put(fn, rs.getObject(fn));
110 }
111 for (Iterator iter = q.getExtraFields().iterator(); iter.hasNext();) {
112 String fn = (String)iter.next();
113 result.put(fn, rs.getObject(fn));
114 }
115 }
116 } catch (Exception e) {
117 closeStatement(stmt);
118 throw new DataException("Error while retrieving Data", e);
119 }
120 closeStatement(stmt);
121 return result;
122 }
123
124 public List readMultipleFieldMultipleRow(Query q) throws DataException {
125 String query = queryRenderer.renderSelect(q);
126 if(log.isTraceEnabled())
127 log.trace("QUERY: "+query);
128 LinkedList list = new LinkedList();
129 Statement stmt = getStatement();
130 try {
131 ResultSet rs = stmt.executeQuery(query);
132 while (rs.next()) {
133 HashMap result = new HashMap(q.getFields().size() + q.getExtraFields().size());
134 Iterator iter = q.getFields().iterator();
135 while (iter.hasNext()) {
136 String fn = (String)iter.next();
137 result.put(fn, rs.getObject(fn));
138 }
139 iter = q.getExtraFields().iterator();
140 while (iter.hasNext()) {
141 String fn = (String)iter.next();
142 result.put(fn, rs.getObject(fn));
143 }
144 list.addLast(result);
145 }
146 } catch(java.sql.SQLException e) {
147 closeStatement(stmt);
148 throw new DataException("Error while retrieving Data", e);
149 }
150 closeStatement(stmt);
151 return list;
152 }
153
154 public List readSingleFieldMultipleRow(Query q) throws DataException {
155 Statement stmt = getStatement();
156 String query = queryRenderer.renderSelect(q);
157 if(log.isTraceEnabled())
158 log.trace("QUERY: "+query);
159 LinkedList list = new LinkedList();
160 try {
161 ResultSet rs = stmt.executeQuery(query);
162 while (rs.next()) {
163 list.add(rs.getObject(1));
164 }
165 } catch (Exception e) {
166 closeStatement(stmt);
167 throw new DataException("Error while retrieving Data", e);
168 }
169 closeStatement(stmt);
170 return list;
171 }
172
173 /** Performs an auto-commited insert.
174 */
175 public void insert(Insert i) throws DataException {
176 String query = queryRenderer.renderInsert(i);
177 if(log.isTraceEnabled())
178 log.trace("QUERY: "+query);
179 Statement stmt = getStatement();
180 try {
181 stmt.executeUpdate(query, Statement.NO_GENERATED_KEYS);
182 } catch (java.sql.SQLException e) {
183 closeStatement(stmt);
184 throw new DataException(e.toString());
185 }
186 closeStatement(stmt);
187 }
188
189 public void insert(ConnectionHandle ch, Insert i) throws DataException {
190 insert(ch, i, false);
191 }
192
193 public void insert(ConnectionHandle ch, Insert i, boolean returnLastInsertId) throws DataException {
194 Connection c = (Connection)ch.getConnection();
195 String query = queryRenderer.renderInsert(i);
196 if(log.isTraceEnabled())
197 log.trace("QUERY: "+query);
198 try {
199 Statement stmt = c.createStatement();
200 int flag;
201 if (returnLastInsertId)
202 flag = Statement.RETURN_GENERATED_KEYS;
203 else
204 flag = Statement.NO_GENERATED_KEYS;
205 stmt.executeUpdate(query, flag);
206 if (returnLastInsertId) {
207 ResultSet rs = stmt.getGeneratedKeys();
208 rs.first();
209 Long id = new Long(rs.getLong(1));
210 ch.setLastInsertId(id);
211 }
212 } catch (java.sql.SQLException e) {
213 throw new DataException(e.toString());
214 }
215 }
216
217 private void updateInternal(Statement stmt, Update update) throws java.sql.SQLException {
218 String query = queryRenderer.renderUpdate(update);
219 if(log.isTraceEnabled())
220 log.trace("QUERY: "+query);
221 stmt.executeUpdate(query);
222 }
223
224 public void update(Update update) throws DataException {
225 Statement stmt = getStatement();
226 try {
227 updateInternal(stmt, update);
228 } catch (java.sql.SQLException e) {
229 closeStatement(stmt);
230 throw new DataException(e.toString());
231 }
232 closeStatement(stmt);
233 }
234
235 public void update(ConnectionHandle ch, Update update) throws DataException {
236 Connection c = (Connection)ch.getConnection();
237 try {
238 Statement stmt = c.createStatement();
239 updateInternal(stmt, update);
240 } catch (java.sql.SQLException e) {
241 throw new DataException(e.toString());
242 }
243 }
244
245 public ConnectionHandle beginTransaction() throws DataException {
246 Connection c = ServicesUtil.getConnection();
247 try {
248 c.setAutoCommit(false);
249 } catch (java.sql.SQLException e) {
250 throw new DataException(e.toString());
251 }
252 ConnectionHandle ch = new ConnectionHandle(c);
253 ch.setTransactionCurrent(true);
254 return ch;
255 }
256
257 public void commitTransaction(ConnectionHandle ch) throws DataException {
258 if (!ch.isTransactionCurrent()) return;
259 Connection c = (Connection)ch.getConnection();
260 try {
261 c.commit();
262 c.setAutoCommit(true);
263 c.close();
264 } catch (java.sql.SQLException e) {
265 throw new DataException(e.toString());
266 }
267 ch.setTransactionCurrent(false);
268 }
269
270 public void rollbackTransaction(ConnectionHandle ch) throws DataException {
271 if (!ch.isTransactionCurrent()) return;
272 Connection c = (Connection)ch.getConnection();
273 try {
274 c.rollback();
275 c.setAutoCommit(true);
276 c.close();
277 } catch (java.sql.SQLException e) {
278 throw new DataException(e.toString());
279 }
280 ch.setTransactionCurrent(false);
281 }
282
283 private void deleteInternal(Statement stmt, Delete delete) throws java.sql.SQLException {
284 String query = queryRenderer.renderDelete(delete);
285 if(log.isTraceEnabled())
286 log.trace("QUERY: "+query);
287 stmt.executeUpdate(query);
288 }
289
290 public void delete(Delete delete) throws DataException {
291 Statement stmt = getStatement();
292 try {
293 deleteInternal(stmt, delete);
294 } catch (java.sql.SQLException e) {
295 closeStatement(stmt);
296 throw new DataException(e.toString());
297 }
298 closeStatement(stmt);
299 }
300
301 public void delete(ConnectionHandle ch, Delete delete) throws DataException {
302 Connection c = (Connection)ch.getConnection();
303 try {
304 Statement stmt = c.createStatement();
305 deleteInternal(stmt, delete);
306 } catch (java.sql.SQLException e) {
307 throw new DataException(e.toString());
308 }
309 }
310
311 }