18
18
*/
19
19
public abstract class Dao <Model , Id > {
20
20
21
+ public static final Object [] EMPTY_ARRAY = new Object [0 ];
22
+
23
+ public static Object [] toArray (Object value )
24
+ throws SQLException {
25
+ if (value == null ) {
26
+ return EMPTY_ARRAY ;
27
+ }
28
+ if (value instanceof Object []) {
29
+ return (Object []) value ;
30
+ } else {
31
+ return new Object [] { value };
32
+ }
33
+ }
34
+
21
35
/**
22
36
* Busca un registro por su identificador
23
37
*
@@ -29,7 +43,7 @@ public abstract class Dao<Model, Id> {
29
43
*/
30
44
public Model findById (Id id ) throws SQLException {
31
45
String sql = "SELECT * FROM " + getTableName () + " WHERE " + whereClause (id );
32
- return findById (sql , id );
46
+ return findById (sql , toArray ( id ) );
33
47
}
34
48
35
49
/**
@@ -49,7 +63,7 @@ public Model findById(String sql, Object... params) throws SQLException {
49
63
rs = db .query (sql , params );
50
64
return rs .next () ? onRead (rs ) : null ;
51
65
} finally {
52
- db .close (rs );
66
+ DataBase .close (rs );
53
67
}
54
68
}
55
69
@@ -91,7 +105,7 @@ public List<Model> find(String sql, Object... params) throws SQLException {
91
105
return list ;
92
106
93
107
} finally {
94
- db .close (rs );
108
+ DataBase .close (rs );
95
109
}
96
110
}
97
111
@@ -114,7 +128,7 @@ public long count() throws SQLException {
114
128
* @throws SQLException
115
129
*/
116
130
public boolean exists (Id id ) throws SQLException {
117
- return getDataBase ().count (getTableName (), whereClause (id ), id ) > 0 ;
131
+ return getDataBase ().count (getTableName (), whereClause (id ), toArray ( id ) ) > 0 ;
118
132
}
119
133
120
134
/**
@@ -146,7 +160,7 @@ public boolean update(Model m) throws SQLException {
146
160
}
147
161
Id id = getId (m );
148
162
return getDataBase ().update (getTableName (), values ,
149
- whereClause (id ), id ) == 1 ;
163
+ whereClause (id ), toArray ( id ) ) == 1 ;
150
164
}
151
165
152
166
/**
@@ -192,7 +206,7 @@ public boolean delete(Model m) throws SQLException {
192
206
*/
193
207
public boolean deleteById (Id id ) throws SQLException {
194
208
return getDataBase ().delete (getTableName (),
195
- whereClause (id ), id ) == 1 ;
209
+ whereClause (id ), toArray ( id ) ) == 1 ;
196
210
}
197
211
198
212
/**
@@ -289,6 +303,8 @@ protected Map<String, Object> onWrite(Model m) {
289
303
* @param m modelo
290
304
* @param id insertado
291
305
*/
292
- protected abstract void insertId (Model m , long id );
306
+ protected void insertId (Model m , long id ) {
307
+
308
+ }
293
309
294
310
}
0 commit comments