1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.db;
9
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.sql.Types;
13 import java.util.List;
14
15 import javax.sql.DataSource;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.springframework.jdbc.core.SqlParameter;
20 import org.springframework.jdbc.object.MappingSqlQuery;
21 import org.springframework.jdbc.object.SqlFunction;
22 import org.springframework.jdbc.object.SqlUpdate;
23
24 import com.buckosoft.PicMan.domain.MosaicTile;
25
26
27
28
29
30
31 public class MosaicTilesDaoJdbc implements MosaicTilesDao {
32 private static final boolean DEBUG = false;
33 protected final Log logger = LogFactory.getLog(getClass());
34
35 private DataSource ds;
36
37
38
39
40
41 public void setDataSource(DataSource ds) {
42 this.ds = ds;
43 }
44
45
46
47
48 public void deleteMosaicTiles(int mid) {
49 if (DEBUG)
50 logger.info("deleteMosaicTile: " + mid);
51 MosaicTileDelete mtd = new MosaicTileDelete(ds);
52 mtd.delete(mid);
53
54 }
55
56
57
58
59 @SuppressWarnings("unchecked")
60 public List<MosaicTile> getMosaicTiles(int mid) {
61 if (sql_getMosaicTilesQUERY == null)
62 sql_getMosaicTilesQUERY = new MosaicTilesQuery(ds, mid);
63 if (DEBUG)
64 logger.info("getMosaicTiles: " + mid);
65 return(sql_getMosaicTilesQUERY.execute(mid));
66 }
67 private MosaicTilesQuery sql_getMosaicTilesQUERY = null;
68
69
70
71
72 public int getTileCount(int mid) {
73 if (DEBUG)
74 logger.info("getTileCount:");
75 if (sql_getTileCountQUERYr == null)
76 sql_getTileCountQUERYr = new MosaicTilesCount(ds);
77 return(sql_getTileCountQUERYr.run(mid));
78 }
79 private MosaicTilesCount sql_getTileCountQUERYr;
80
81
82
83
84 public void storeMosaicTile(MosaicTile tile) {
85 MosaicTileInsert mti = new MosaicTileInsert(ds);
86 mti.insert(tile);
87 }
88
89
90
91
92
93 class MosaicTilesCount extends SqlFunction {
94
95 MosaicTilesCount(DataSource ds) {
96 super(ds, "SELECT COUNT(*) from mosaicTiles where mid=?");
97 declareParameter(new SqlParameter(Types.INTEGER));
98 compile();
99 }
100 }
101
102
103
104
105
106
107 class MosaicTilesQuery extends MappingSqlQuery {
108
109
110
111
112
113
114 MosaicTilesQuery(DataSource ds, int mid) {
115 super(ds, "SELECT * from mosaicTiles where mid = ?");
116 declareParameter(new SqlParameter(Types.INTEGER));
117 compile();
118 }
119
120 protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
121 MosaicTile mt = new MosaicTile();
122 mt.setMid(rs.getInt("mid"));
123 mt.setPid(rs.getInt("pid"));
124 mt.setX(rs.getInt("x"));
125 mt.setY(rs.getInt("y"));
126 mt.setWidth(rs.getInt("width"));
127 mt.setHeight(rs.getInt("height"));
128 return(mt);
129 }
130 }
131
132
133
134
135
136 protected class MosaicTileDelete extends SqlUpdate {
137
138
139
140
141
142 protected MosaicTileDelete(DataSource ds) {
143 super(ds, "DELETE FROM mosaicTiles WHERE mid = (?)");
144 declareParameter(new SqlParameter(Types.INTEGER));
145 compile();
146 }
147
148 protected void delete(int mid) {
149 super.update(mid);
150 }
151 }
152
153
154
155
156
157 protected class MosaicTileInsert extends SqlUpdate {
158
159
160
161
162
163 protected MosaicTileInsert(DataSource ds) {
164 super(ds, "REPLACE INTO mosaicTiles VALUES(?,?,?,?,?,?)");
165 declareParameter(new SqlParameter(Types.INTEGER));
166 declareParameter(new SqlParameter(Types.INTEGER));
167 declareParameter(new SqlParameter(Types.INTEGER));
168 declareParameter(new SqlParameter(Types.INTEGER));
169 declareParameter(new SqlParameter(Types.INTEGER));
170 declareParameter(new SqlParameter(Types.INTEGER));
171 compile();
172 }
173
174 protected void insert(MosaicTile mt) {
175 Object[] objs = new Object[] {
176 new Integer(mt.getMid()),
177 new Integer(mt.getPid()),
178 new Integer(mt.getX()),
179 new Integer(mt.getY()),
180 new Integer(mt.getWidth()),
181 new Integer(mt.getHeight()),
182 };
183 super.update(objs);
184
185 }
186 }
187
188
189 }