1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.db;
9
10 import java.util.List;
11
12 import javax.sql.DataSource;
13
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import java.sql.Types;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import org.springframework.dao.DataAccessException;
22 import org.springframework.jdbc.core.SqlParameter;
23 import org.springframework.jdbc.object.SqlFunction;
24 import org.springframework.jdbc.object.SqlUpdate;
25 import org.springframework.jdbc.object.MappingSqlQuery;
26
27 import com.buckosoft.PicMan.domain.Virgin;
28
29
30
31
32
33
34 public class VirginsDaoJdbc implements VirginsDao {
35
36 private final static boolean DEBUG = false;
37 private final static boolean DEBUGCONNECTIONLEAK = false;
38
39 protected final Log logger = LogFactory.getLog(getClass());
40
41
42 private DataSource ds;
43
44
45
46
47
48 public void setDataSource(DataSource ds) {
49 this.ds = ds;
50 }
51
52
53
54
55 @SuppressWarnings("unchecked")
56 public List<Virgin> getVirgins() throws DataAccessException {
57 if (DEBUG)
58 logger.info("Getting Virgins");
59 VirginsQuery vq = new VirginsQuery(ds);
60 return(vq.execute());
61 }
62
63
64
65
66
67 @SuppressWarnings("unchecked")
68 public List<Virgin> getVirgins(int max) throws DataAccessException {
69 if (DEBUG)
70 logger.info("Getting Virgins(" + max + ")");
71 VirginsQuery vq = new VirginsQuery(ds, max);
72 return(vq.execute(max));
73 }
74
75
76
77
78 public boolean isVirgin(String picName) {
79 VirginsQuery vq = new VirginsQuery(ds, picName);
80 Virgin v = (Virgin)vq.findObject(picName);
81 if (DEBUG)
82 logger.info("IsVirgin: " + picName + ": " + v != null ? "true" : "false");
83 return(v != null ? true : false);
84 }
85
86
87
88
89 public void addVirgin(String picName) throws DataAccessException {
90 if (DEBUG)
91 logger.info("addVirgin: " + picName);
92 VirginInsert vi = new VirginInsert(ds);
93 Virgin v = new Virgin();
94 v.setName(picName);
95 vi.insert(v);
96 }
97
98
99
100
101 public void deleteVirgin(String picName) throws DataAccessException {
102 if (DEBUG)
103 logger.info("deleteVirgin: " + picName);
104 VirginDelete vd = new VirginDelete(ds);
105 Virgin v = new Virgin();
106 v.setName(picName);
107 vd.delete(v);
108 }
109
110
111
112
113 public int getVirginCount() {
114 if (DEBUG)
115 logger.info("getVirginCount:");
116 if (DEBUGCONNECTIONLEAK)
117 return(69);
118 if (sql_getVirginCountQUERYr == null)
119 sql_getVirginCountQUERYr = new VirginsCount(ds);
120 return(sql_getVirginCountQUERYr.run());
121 }
122 private VirginsCount sql_getVirginCountQUERYr;
123
124
125 class VirginsQuery extends MappingSqlQuery {
126
127 VirginsQuery(DataSource ds) {
128 super(ds, "SELECT * from virgins");
129 compile();
130 }
131
132 VirginsQuery(DataSource ds, int max) {
133 super(ds, "SELECT * from virgins LIMIT ?");
134 declareParameter(new SqlParameter(Types.INTEGER));
135 compile();
136 }
137
138 VirginsQuery(DataSource ds, String picName) {
139 super(ds, "SELECT * from virgins WHERE pic = ?");
140 declareParameter(new SqlParameter(Types.VARCHAR));
141 compile();
142 }
143
144
145 protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
146 Virgin v = new Virgin();
147 v.setName(rs.getString("pic"));
148 return(v);
149 }
150 }
151
152
153
154
155 class VirginsCount extends SqlFunction {
156
157 VirginsCount(DataSource ds) {
158 super(ds, "SELECT COUNT(*) from virgins");
159 compile();
160 }
161 }
162
163
164
165
166 protected class VirginInsert extends SqlUpdate {
167
168
169
170
171
172 protected VirginInsert(DataSource ds) {
173 super(ds, "INSERT INTO virgins VALUES(?)");
174 declareParameter(new SqlParameter(Types.VARCHAR));
175 compile();
176 }
177
178 protected void insert(Virgin v) {
179 Object[] objs = new Object[] {
180 v.getName()};
181 super.update(objs);
182
183 }
184 }
185
186
187
188
189 protected class VirginDelete extends SqlUpdate {
190
191
192
193
194
195 protected VirginDelete(DataSource ds) {
196 super(ds, "DELETE FROM virgins WHERE pic = (?)");
197 declareParameter(new SqlParameter(Types.VARCHAR));
198 compile();
199 }
200
201 protected void delete(Virgin v) {
202 Object[] objs = new Object[] {
203 v.getName()};
204 super.update(objs);
205
206 }
207 }
208 }