01: /*
02: * hgcommons 7
03: * Hammurapi Group Common Library
04: * Copyright (C) 2003 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21: * e-Mail: support@hammurapi.biz
22: */
23: package biz.hammurapi.sql;
24:
25: import java.sql.PreparedStatement;
26: import java.sql.SQLException;
27: import java.util.StringTokenizer;
28:
29: import biz.hammurapi.legacy.persistence.PersistenceException;
30: import biz.hammurapi.legacy.persistence.Storage;
31:
32: /**
33: * @author Pavel Vlasov
34: * @version $Revision: 1.1 $
35: */
36: public class JdbcStorage implements Storage {
37:
38: private SQLProcessor processor;
39:
40: public JdbcStorage(SQLProcessor processor) {
41: this .processor = processor;
42: }
43:
44: public SQLProcessor getProcessor() {
45: return processor;
46: }
47:
48: /**
49: * Key format should be:
50: * {table}:{key field}:{key value}:{projector}
51: */
52: public String put(Object o) throws PersistenceException {
53: if (o instanceof JdbcPersistable) {
54: return ((JdbcPersistable) o).store(this );
55: }
56: return null;
57: }
58:
59: public Object get(String key) throws PersistenceException {
60: final StringTokenizer st = new StringTokenizer(key, ":");
61: try {
62: return processor.projectSingleObject("DELETE FROM "
63: + st.nextToken() + " WHERE " + st.nextToken()
64: + "=?", new Parameterizer() {
65: public void parameterize(PreparedStatement ps)
66: throws SQLException {
67: ps.setString(1, st.nextToken());
68: }
69: }, (Projector) Class.forName(st.nextToken()).newInstance());
70: } catch (InstantiationException e) {
71: throw new PersistenceException(e);
72: } catch (IllegalAccessException e) {
73: throw new PersistenceException(e);
74: } catch (ClassNotFoundException e) {
75: throw new PersistenceException(e);
76: } catch (SQLException e) {
77: throw new PersistenceException(e);
78: }
79: }
80:
81: public void remove(String key) throws PersistenceException {
82: final StringTokenizer st = new StringTokenizer(key, ":");
83: try {
84: processor.processUpdate("DELETE FROM " + st.nextToken()
85: + " WHERE " + st.nextToken() + "=?",
86: new Parameterizer() {
87: public void parameterize(PreparedStatement ps)
88: throws SQLException {
89: ps.setString(1, st.nextToken());
90: }
91: });
92: } catch (SQLException e) {
93: throw new PersistenceException(e);
94: }
95: }
96:
97: }
|