| 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 | 0 | public class VirginsDaoJdbc implements VirginsDao { |
| 35 | |
|
| 36 | |
private final static boolean DEBUG = false; |
| 37 | |
private final static boolean DEBUGCONNECTIONLEAK = false; |
| 38 | |
|
| 39 | 0 | 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 | 0 | this.ds = ds; |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
@SuppressWarnings("unchecked") |
| 56 | |
public List<Virgin> getVirgins() throws DataAccessException { |
| 57 | |
if (DEBUG) |
| 58 | |
logger.info("Getting Virgins"); |
| 59 | 0 | VirginsQuery vq = new VirginsQuery(ds); |
| 60 | 0 | 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 | 0 | VirginsQuery vq = new VirginsQuery(ds, max); |
| 72 | 0 | return(vq.execute(max)); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public boolean isVirgin(String picName) { |
| 79 | 0 | VirginsQuery vq = new VirginsQuery(ds, picName); |
| 80 | 0 | Virgin v = (Virgin)vq.findObject(picName); |
| 81 | |
if (DEBUG) |
| 82 | |
logger.info("IsVirgin: " + picName + ": " + v != null ? "true" : "false"); |
| 83 | 0 | 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 | 0 | VirginInsert vi = new VirginInsert(ds); |
| 93 | 0 | Virgin v = new Virgin(); |
| 94 | 0 | v.setName(picName); |
| 95 | 0 | vi.insert(v); |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public void deleteVirgin(String picName) throws DataAccessException { |
| 102 | |
if (DEBUG) |
| 103 | |
logger.info("deleteVirgin: " + picName); |
| 104 | 0 | VirginDelete vd = new VirginDelete(ds); |
| 105 | 0 | Virgin v = new Virgin(); |
| 106 | 0 | v.setName(picName); |
| 107 | 0 | vd.delete(v); |
| 108 | 0 | } |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public int getVirginCount() { |
| 114 | |
if (DEBUG) |
| 115 | |
logger.info("getVirginCount:"); |
| 116 | |
if (DEBUGCONNECTIONLEAK) |
| 117 | |
return(69); |
| 118 | 0 | if (sql_getVirginCountQUERYr == null) |
| 119 | 0 | sql_getVirginCountQUERYr = new VirginsCount(ds); |
| 120 | 0 | return(sql_getVirginCountQUERYr.run()); |
| 121 | |
} |
| 122 | |
private VirginsCount sql_getVirginCountQUERYr; |
| 123 | |
|
| 124 | |
|
| 125 | |
class VirginsQuery extends MappingSqlQuery { |
| 126 | |
|
| 127 | 0 | VirginsQuery(DataSource ds) { |
| 128 | 0 | super(ds, "SELECT * from virgins"); |
| 129 | 0 | compile(); |
| 130 | 0 | } |
| 131 | |
|
| 132 | 0 | VirginsQuery(DataSource ds, int max) { |
| 133 | 0 | super(ds, "SELECT * from virgins LIMIT ?"); |
| 134 | 0 | declareParameter(new SqlParameter(Types.INTEGER)); |
| 135 | 0 | compile(); |
| 136 | 0 | } |
| 137 | |
|
| 138 | 0 | VirginsQuery(DataSource ds, String picName) { |
| 139 | 0 | super(ds, "SELECT * from virgins WHERE pic = ?"); |
| 140 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 141 | 0 | compile(); |
| 142 | 0 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 146 | 0 | Virgin v = new Virgin(); |
| 147 | 0 | v.setName(rs.getString("pic")); |
| 148 | 0 | return(v); |
| 149 | |
} |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
class VirginsCount extends SqlFunction { |
| 156 | |
|
| 157 | 0 | VirginsCount(DataSource ds) { |
| 158 | 0 | super(ds, "SELECT COUNT(*) from virgins"); |
| 159 | 0 | compile(); |
| 160 | 0 | } |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
protected class VirginInsert extends SqlUpdate { |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | 0 | protected VirginInsert(DataSource ds) { |
| 173 | 0 | super(ds, "INSERT INTO virgins VALUES(?)"); |
| 174 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 175 | 0 | compile(); |
| 176 | 0 | } |
| 177 | |
|
| 178 | |
protected void insert(Virgin v) { |
| 179 | 0 | Object[] objs = new Object[] { |
| 180 | 0 | v.getName()}; |
| 181 | 0 | super.update(objs); |
| 182 | |
|
| 183 | 0 | } |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
protected class VirginDelete extends SqlUpdate { |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | 0 | protected VirginDelete(DataSource ds) { |
| 196 | 0 | super(ds, "DELETE FROM virgins WHERE pic = (?)"); |
| 197 | 0 | declareParameter(new SqlParameter(Types.VARCHAR)); |
| 198 | 0 | compile(); |
| 199 | 0 | } |
| 200 | |
|
| 201 | |
protected void delete(Virgin v) { |
| 202 | 0 | Object[] objs = new Object[] { |
| 203 | 0 | v.getName()}; |
| 204 | 0 | super.update(objs); |
| 205 | |
|
| 206 | 0 | } |
| 207 | |
} |
| 208 | |
} |