• R/O
  • SSH
  • HTTPS

estocdb: Commit


Commit MetaInfo

Revisão31 (tree)
Hora2010-09-01 00:02:50
Autorshun_m

Mensagem de Log

(mensagem de log vazia)

Mudança Sumário

Diff

--- trunk/src/estoc/dbm/TableAccessMysql.java (revision 30)
+++ trunk/src/estoc/dbm/TableAccessMysql.java (revision 31)
@@ -113,7 +113,7 @@
113113 @Override
114114 protected String getStringType(int opt) {
115115 if (opt == -1) {
116- return "VARCHAR(125)"; // defaultでMAX125とする
116+ return "TEXT";
117117 } else {
118118 return "VARCHAR(" + opt + ")";
119119 }
--- trunk/src/estoc/dbm/DbmUtil.java (revision 30)
+++ trunk/src/estoc/dbm/DbmUtil.java (revision 31)
@@ -7,7 +7,6 @@
77 import java.sql.SQLException;
88 import java.sql.Statement;
99 import java.util.Date;
10-
1110 import estoc.dbm.annotate.Table;
1211
1312 /**
@@ -18,9 +17,12 @@
1817
1918 /**
2019 * to csv string
20+ *
2121 * @param objects
22- * @param sep Object separator
23- * @param quote quoting String
22+ * @param sep
23+ * Object separator
24+ * @param quote
25+ * quoting String
2426 * @return string
2527 */
2628 public static String toCommaString(Object[] objects, String sep, String quote) {
@@ -38,12 +40,15 @@
3840 return sb.toString();
3941 }
4042
41-
4243 /**
4344 * fill to TableObject from ResultSet
44- * @param <T> Class
45- * @param clz Table object
46- * @param result ResultSet
45+ *
46+ * @param <T>
47+ * Class
48+ * @param clz
49+ * Table object
50+ * @param result
51+ * ResultSet
4752 * @return filled object
4853 * @throws SQLException
4954 */
@@ -69,12 +74,15 @@
6974 }
7075 }
7176
72-
7377 /**
7478 * Set value to Field
75- * @param obj target object
76- * @param info target column
77- * @param val set value
79+ *
80+ * @param obj
81+ * target object
82+ * @param info
83+ * target column
84+ * @param val
85+ * set value
7886 */
7987 public static void setVlaueToField(Object obj, ColumnInfo info, Object val) {
8088 String setter = getSetterName(info.getName());
@@ -98,8 +106,11 @@
98106
99107 /**
100108 * Get value from Filed
101- * @param obj target object
102- * @param info target column
109+ *
110+ * @param obj
111+ * target object
112+ * @param info
113+ * target column
103114 * @return filed value
104115 */
105116 public static Object getVlaueFromField(Object obj, ColumnInfo info) {
@@ -119,7 +130,6 @@
119130 if (rtn == null) {
120131 rtn = new TypedNull(info.getType());
121132 }
122-
123133 return rtn;
124134 }
125135
@@ -137,30 +147,30 @@
137147 return sb.toString();
138148 }
139149
140-
141- public static void close(Statement statement) {
142- if (statement != null) {
150+ public static void close(ResultSet result) {
151+ if (result != null) {
143152 try {
144- statement.close();
153+ result.close();
145154 } catch (Exception e) {
146155 }
156+ }
157+ }
158+
159+ public static void close(Statement stmt) {
160+ if (stmt != null) {
147161 try {
148- Connection connection = statement.getConnection();
149- if (connection != null) {
150- connection.close();
151- }
152- } catch (Exception e) {
162+ stmt.close();
163+ } catch (SQLException e) {
153164 }
154165 }
155166 }
156167
157- public static void close(ResultSet result) {
158- if (result != null) {
168+ public static void close(Connection connection) {
169+ if (connection != null) {
159170 try {
160- result.close();
161- } catch (Exception e) {
171+ connection.close();
172+ } catch (SQLException e) {
162173 }
163174 }
164175 }
165-
166176 }
--- trunk/src/estoc/dbm/SqlCreatorImpl.java (revision 30)
+++ trunk/src/estoc/dbm/SqlCreatorImpl.java (revision 31)
@@ -15,7 +15,7 @@
1515 @Override
1616 public String getDeleteSql(String tableName, Set<String> pks) {
1717 StringBuilder sb = new StringBuilder();
18- sb.append("DELETE ").append(tableName);
18+ sb.append("DELETE FROM ").append(tableName);
1919 sb.append(" WHERE ");
2020 List<String> condition = new ArrayList<String>();
2121 for (String pk : pks) {
--- trunk/src/estoc/dbm/DataAccessImpl.java (revision 30)
+++ trunk/src/estoc/dbm/DataAccessImpl.java (revision 31)
@@ -28,7 +28,6 @@
2828 this.connectionProvider = connectionProvider;
2929 this.creator = creator;
3030 }
31-
3231 Map<Class<?>, List<ColumnInfo>> fieldMap = new HashMap<Class<?>, List<ColumnInfo>>();
3332
3433 private List<ColumnInfo> getDeclaredFields(Class<?> clz) {
@@ -50,20 +49,25 @@
5049 * @see dbm.DataAccess#select(java.lang.Class, java.lang.Object)
5150 */
5251 public <T> T select(Class<T> clz, Object... params) throws SQLException {
53- PreparedStatement stmt = createSelectStmt(clz);
52+ Connection connection = connectionProvider.getConnection();
5453 try {
55- setParams(stmt, params);
56- ResultSet result = stmt.executeQuery();
54+ PreparedStatement stmt = createSelectStmt(clz, connection);
5755 try {
58- if (result != null && result.next()) {
59- return DbmUtil.fillData(clz, result);
56+ setParams(stmt, params);
57+ ResultSet result = stmt.executeQuery();
58+ try {
59+ if (result != null && result.next()) {
60+ return DbmUtil.fillData(clz, result);
61+ }
62+ return null;
63+ } finally {
64+ DbmUtil.close(result);
6065 }
61- return null;
6266 } finally {
63- DbmUtil.close(result);
67+ DbmUtil.close(stmt);
6468 }
6569 } finally {
66- DbmUtil.close(stmt);
70+ DbmUtil.close(connection);
6771 }
6872 }
6973
@@ -77,9 +81,11 @@
7781 return fillter(clz, whereString, orderString, -1);
7882 }
7983
80- @Override public <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString, long maxCount)
84+ @Override
85+ public <T> ArrayList<T> fillter(Class<T> clz, String whereString, String orderString, long maxCount)
8186 throws SQLException {
82- Statement stmt = connectionProvider.getConnection().createStatement();
87+ Connection connection = connectionProvider.getConnection();
88+ Statement stmt = connection.createStatement();
8389 try {
8490 String tblName = DbmUtil.getTableName(clz);
8591 StringBuilder sql = new StringBuilder("SELECT * FROM ").append(tblName);
@@ -104,11 +110,12 @@
104110 DbmUtil.close(rs);
105111 }
106112 } finally {
107- stmt.close();
113+ DbmUtil.close(stmt);
114+ DbmUtil.close(connection);
108115 }
109116 }
110117
111- private <T> PreparedStatement createSelectStmt(Class<T> clz) throws SQLException {
118+ private <T> PreparedStatement createSelectStmt(Class<T> clz, Connection connection) throws SQLException {
112119 PreparedStatement stmt;
113120 Set<String> cols = new LinkedHashSet<String>();
114121 Set<String> pks = new LinkedHashSet<String>();
@@ -126,7 +133,7 @@
126133 }
127134 String tableName = DbmUtil.getTableName(clz);
128135 String selectSql = creator.getSelectSql(tableName, pks, cols);
129- stmt = connectionProvider.getConnection().prepareStatement(selectSql);
136+ stmt = connection.prepareStatement(selectSql);
130137 return stmt;
131138 }
132139
@@ -142,27 +149,32 @@
142149 try {
143150 connection.createStatement().executeUpdate("DELETE FROM " + tableName);
144151 } finally {
145- connection.close();
152+ DbmUtil.close(connection);
146153 }
147154 return true;
148155 }
149156 Class<?> clz = obj.getClass();
150- PreparedStatement stmt = createDeleteStmt(clz);
157+ Connection connection = connectionProvider.getConnection();
151158 try {
152- List<Object> params = new ArrayList<Object>();
153- for (ColumnInfo info : getDeclaredFields(clz)) {
154- if (info.isPk()) {
155- params.add(DbmUtil.getVlaueFromField(obj, info));
159+ PreparedStatement stmt = createDeleteStmt(clz, connection);
160+ try {
161+ List<Object> params = new ArrayList<Object>();
162+ for (ColumnInfo info : getDeclaredFields(clz)) {
163+ if (info.isPk()) {
164+ params.add(DbmUtil.getVlaueFromField(obj, info));
165+ }
156166 }
167+ setParams(stmt, params.toArray());
168+ return stmt.executeUpdate() == 1;
169+ } finally {
170+ DbmUtil.close(stmt);
157171 }
158- setParams(stmt, params.toArray());
159- return stmt.executeUpdate() == 1;
160172 } finally {
161- DbmUtil.close(stmt);
173+ DbmUtil.close(connection);
162174 }
163175 }
164176
165- private PreparedStatement createDeleteStmt(Class<?> clz) throws SQLException {
177+ private PreparedStatement createDeleteStmt(Class<?> clz, Connection connection) throws SQLException {
166178 PreparedStatement stmt;
167179 Set<String> pks = new LinkedHashSet<String>();
168180 for (ColumnInfo info : getDeclaredFields(clz)) {
@@ -176,7 +188,7 @@
176188 String tableName = DbmUtil.getTableName(clz);
177189 String sql = creator.getDeleteSql(tableName, pks);
178190 LOG.info("sql = " + sql);
179- stmt = connectionProvider.getConnection().prepareStatement(sql);
191+ stmt = connection.prepareStatement(sql);
180192 return stmt;
181193 }
182194
@@ -187,87 +199,96 @@
187199 */
188200 public <T> T insert(T obj) throws SQLException {
189201 Class<?> clz = obj.getClass();
190- PreparedStatement stmt = createInsertStmt(clz);
202+ Connection connection = connectionProvider.getConnection();
191203 try {
192- // set value
193- List<Object> params = new ArrayList<Object>();
194- Date now = new Date();
195- List<ColumnInfo> autoIncFields = new ArrayList<ColumnInfo>();
196- List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
197- for (ColumnInfo info : getDeclaredFields(clz)) {
198- if (info.isColumn()) {
199- if (info.isAutoInc()) {
200- autoIncFields.add(info);
201- continue;
204+ PreparedStatement stmt = createInsertStmt(clz, connection);
205+ try {
206+ // set value
207+ List<Object> params = new ArrayList<Object>();
208+ Date now = new Date();
209+ List<ColumnInfo> autoIncFields = new ArrayList<ColumnInfo>();
210+ List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
211+ for (ColumnInfo info : getDeclaredFields(clz)) {
212+ if (info.isColumn()) {
213+ if (info.isAutoInc()) {
214+ autoIncFields.add(info);
215+ continue;
216+ }
217+ if (info.isTimeStamp()) {
218+ timeFields.add(info);
219+ params.add(now);
220+ } else {
221+ params.add(DbmUtil.getVlaueFromField(obj, info));
222+ }
202223 }
203- if (info.isTimeStamp()) {
204- timeFields.add(info);
205- params.add(now);
206- } else {
207- params.add(DbmUtil.getVlaueFromField(obj, info));
224+ }
225+ setParams(stmt, params.toArray());
226+ // Do insert
227+ if (stmt.executeUpdate() == 1) {
228+ // AutoInc, TimeStamp の値埋め込み
229+ for (ColumnInfo info : timeFields) {
230+ DbmUtil.setVlaueToField(obj, info, now);
208231 }
209- }
210- }
211- setParams(stmt, params.toArray());
212- // Do insert
213- if (stmt.executeUpdate() == 1) {
214- // AutoInc, TimeStamp の値埋め込み
215- for (ColumnInfo info : timeFields) {
216- DbmUtil.setVlaueToField(obj, info, now);
217- }
218- ResultSet keys = stmt.getGeneratedKeys();
219- try {
220- for (ColumnInfo info : autoIncFields) {
221- if (keys.next()) {
222- Object object = keys.getObject(1);
223- if (object instanceof Integer) {
224- if (info.getType() == Integer.class) {
225- DbmUtil.setVlaueToField(obj, info, object);
226- } else if (info.getType() == Long.class) {
227- Long val = ((Integer) object).longValue();
228- DbmUtil.setVlaueToField(obj, info, val);
229- } else {
230- throw new UnsupportedOperationException("Unsupport type:" + info.getType());
231- }
232- } else if (object instanceof Long) {
233- if (info.getType() == Integer.class) {
234- Integer val = ((Long) object).intValue();
235- DbmUtil.setVlaueToField(obj, info, val);
236- } else if (info.getType() == Long.class) {
237- DbmUtil.setVlaueToField(obj, info, object);
238- } else {
239- throw new UnsupportedOperationException("Unsupport type:" + info.getType());
240- }
241- } else if (object instanceof BigDecimal) {
242- if (info.getType() == Integer.class) {
243- Integer val = ((BigDecimal) object).intValue();
244- DbmUtil.setVlaueToField(obj, info, val);
245- } else if (info.getType() == Long.class) {
246- Long val = ((BigDecimal) object).longValue();
247- DbmUtil.setVlaueToField(obj, info, val);
248- } else {
249- throw new UnsupportedOperationException("Unsupport type:" + info.getType());
250- }
232+ ResultSet keys = stmt.getGeneratedKeys();
233+ try {
234+ for (ColumnInfo info : autoIncFields) {
235+ if (keys.next()) {
236+ Object object = keys.getObject(1);
237+ fill(obj, info, object);
251238 } else {
252- throw new UnsupportedOperationException("Unsupport result type:" + object.getClass());
239+ break;
253240 }
254- } else {
255- break;
256241 }
242+ } finally {
243+ DbmUtil.close(keys);
257244 }
258- } finally {
259- DbmUtil.close(keys);
245+ return obj;
246+ } else {
247+ return null;
260248 }
261- return obj;
262- } else {
263- return null;
249+ } finally {
250+ DbmUtil.close(stmt);
264251 }
265252 } finally {
266- DbmUtil.close(stmt);
253+ DbmUtil.close(connection);
267254 }
268255 }
269256
270- private PreparedStatement createInsertStmt(Class<?> clz) throws SQLException {
257+ private <T> void fill(T obj, ColumnInfo info, Object object) {
258+ if (object instanceof Integer) {
259+ if (info.getType() == Integer.class) {
260+ DbmUtil.setVlaueToField(obj, info, object);
261+ } else if (info.getType() == Long.class) {
262+ Long val = ((Integer) object).longValue();
263+ DbmUtil.setVlaueToField(obj, info, val);
264+ } else {
265+ throw new UnsupportedOperationException("Unsupport type:" + info.getType());
266+ }
267+ } else if (object instanceof Long) {
268+ if (info.getType() == Integer.class) {
269+ Integer val = ((Long) object).intValue();
270+ DbmUtil.setVlaueToField(obj, info, val);
271+ } else if (info.getType() == Long.class) {
272+ DbmUtil.setVlaueToField(obj, info, object);
273+ } else {
274+ throw new UnsupportedOperationException("Unsupport type:" + info.getType());
275+ }
276+ } else if (object instanceof BigDecimal) {
277+ if (info.getType() == Integer.class) {
278+ Integer val = ((BigDecimal) object).intValue();
279+ DbmUtil.setVlaueToField(obj, info, val);
280+ } else if (info.getType() == Long.class) {
281+ Long val = ((BigDecimal) object).longValue();
282+ DbmUtil.setVlaueToField(obj, info, val);
283+ } else {
284+ throw new UnsupportedOperationException("Unsupport type:" + info.getType());
285+ }
286+ } else {
287+ throw new UnsupportedOperationException("Unsupport result type:" + object.getClass());
288+ }
289+ }
290+
291+ private PreparedStatement createInsertStmt(Class<?> clz, Connection connection) throws SQLException {
271292 PreparedStatement stmt;
272293 Set<String> columns = new LinkedHashSet<String>();
273294 for (ColumnInfo info : getDeclaredFields(clz)) {
@@ -280,7 +301,7 @@
280301 String tableName = DbmUtil.getTableName(clz);
281302 String sql = creator.getInsertSql(tableName, columns);
282303 LOG.info("sql= " + sql);
283- stmt = connectionProvider.getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
304+ stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
284305 return stmt;
285306 }
286307
@@ -291,46 +312,51 @@
291312 */
292313 public <T> T update(T obj) throws SQLException {
293314 Class<?> clz = obj.getClass();
294- PreparedStatement stmt = createUpdateStmt(clz);
315+ Connection connection = connectionProvider.getConnection();
295316 try {
296- // set value
297- List<Object> params = new ArrayList<Object>();
298- List<Object> pkParams = new ArrayList<Object>();
299- List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
300- Date now = new Date();
301- for (ColumnInfo info : getDeclaredFields(clz)) {
302- if (info.isPk()) {
303- pkParams.add(DbmUtil.getVlaueFromField(obj, info));
304- } else {
305- if (info.isColumn()) {
306- if (info.isTimeStamp()) {
307- timeFields.add(info);
308- params.add(now);
309- } else {
310- params.add(DbmUtil.getVlaueFromField(obj, info));
317+ PreparedStatement stmt = createUpdateStmt(clz, connection);
318+ try {
319+ // set value
320+ List<Object> params = new ArrayList<Object>();
321+ List<Object> pkParams = new ArrayList<Object>();
322+ List<ColumnInfo> timeFields = new ArrayList<ColumnInfo>();
323+ Date now = new Date();
324+ for (ColumnInfo info : getDeclaredFields(clz)) {
325+ if (info.isPk()) {
326+ pkParams.add(DbmUtil.getVlaueFromField(obj, info));
327+ } else {
328+ if (info.isColumn()) {
329+ if (info.isTimeStamp()) {
330+ timeFields.add(info);
331+ params.add(now);
332+ } else {
333+ params.add(DbmUtil.getVlaueFromField(obj, info));
334+ }
311335 }
312336 }
313337 }
314- }
315- List<Object> tmp = new ArrayList<Object>();
316- tmp.addAll(params);
317- tmp.addAll(pkParams);
318- setParams(stmt, tmp.toArray());
319- // do update
320- if (stmt.executeUpdate() == 1) {
321- for (ColumnInfo info : timeFields) {
322- DbmUtil.setVlaueToField(obj, info, now);
338+ List<Object> tmp = new ArrayList<Object>();
339+ tmp.addAll(params);
340+ tmp.addAll(pkParams);
341+ setParams(stmt, tmp.toArray());
342+ // do update
343+ if (stmt.executeUpdate() == 1) {
344+ for (ColumnInfo info : timeFields) {
345+ DbmUtil.setVlaueToField(obj, info, now);
346+ }
347+ return obj;
348+ } else {
349+ return null;
323350 }
324- return obj;
325- } else {
326- return null;
351+ } finally {
352+ DbmUtil.close(stmt);
327353 }
328354 } finally {
329- DbmUtil.close(stmt);
355+ DbmUtil.close(connection);
330356 }
331357 }
332358
333- private PreparedStatement createUpdateStmt(Class<?> clz) throws SQLException {
359+ private PreparedStatement createUpdateStmt(Class<?> clz, Connection connection) throws SQLException {
334360 PreparedStatement stmt;
335361 Set<String> pks = new LinkedHashSet<String>();
336362 Set<String> columns = new LinkedHashSet<String>();
@@ -349,7 +375,7 @@
349375 String tableName = DbmUtil.getTableName(clz);
350376 String sql = creator.getUpdateSql(tableName, pks, columns);
351377 LOG.info("sql= " + sql);
352- stmt = connectionProvider.getConnection().prepareStatement(sql);
378+ stmt = connection.prepareStatement(sql);
353379 return stmt;
354380 }
355381
Show on old repository browser