@@ -208,30 +208,9 @@ protected static Object _get(Class c, Object...args) throws SQLException {
208208 }
209209
210210
211- //Get tableName
212- String tableName = null ;
213- try { tableName = ((Model ) c .newInstance ()).tableName ; }
214- catch (Exception e ){}
215-
216211
217212 //Build sql to find the model id
218- StringBuilder str = new StringBuilder ("select id from " );
219- str .append (tableName );
220- str .append (" where " );
221- for (int i =0 ; i <args .length -1 ; i ++){
222- str .append (args [i ]);
223- i ++;
224- Object val = args [i ];
225- if (val instanceof String ){
226- str .append ("'" );
227- str .append (val .toString ().replace ("'" , "''" ));
228- str .append ("'" );
229- }
230- else {
231- str .append (val );
232- }
233- if (i <args .length -2 ) str .append (" and " );
234- }
213+ String sql = getSQL (c , args );
235214
236215
237216 //Execute query
@@ -240,7 +219,7 @@ protected static Object _get(Class c, Object...args) throws SQLException {
240219 try {
241220 conn = getConnection (c );
242221 javaxt .sql .Recordset rs = new javaxt .sql .Recordset ();
243- rs .open (str . toString () , conn );
222+ rs .open (sql , conn );
244223 if (!rs .EOF ) id = rs .getValue (0 ).toLong ();
245224 rs .close ();
246225 conn .close ();
@@ -259,18 +238,94 @@ protected static Object _get(Class c, Object...args) throws SQLException {
259238 return null ;
260239 }
261240
262-
241+
263242 //**************************************************************************
264243 //** _find
265244 //**************************************************************************
266245 /** Returns an array of models from the database using a given set of
267246 * constraints.
268247 */
269248 protected static Object [] _find (Class c , Object ...args ) throws SQLException {
249+
250+ //Build sql using args
251+ String sql = getSQL (c , args );
252+
253+
254+ //Execute query
255+ java .util .ArrayList <Long > ids = new java .util .ArrayList <Long >();
256+ Connection conn = null ;
257+ try {
258+ conn = getConnection (c );
259+ javaxt .sql .Recordset rs = new javaxt .sql .Recordset ();
260+ rs .open (sql , conn );
261+ while (rs .hasNext ()){
262+ ids .add (rs .getValue (0 ).toLong ());
263+ rs .moveNext ();
264+ }
265+ rs .close ();
266+ conn .close ();
267+ }
268+ catch (SQLException e ){
269+ if (conn !=null ) conn .close ();
270+ throw e ;
271+ }
272+
273+
274+ //Return model
275+ if (!ids .isEmpty ()){
276+ java .util .ArrayList arr = new java .util .ArrayList (ids .size ());
277+ for (long id : ids ){
278+ try {
279+ arr .add (c .getConstructor (long .class ).newInstance (id ));
280+ }
281+ catch (Exception e ){}
282+ }
283+ return arr .toArray ();
284+ }
285+
270286 return new Object [0 ];
271287 }
272288
273289
290+ //**************************************************************************
291+ //** getSQL
292+ //**************************************************************************
293+ /** Returns a sql statement used to generate a list of model IDs
294+ */
295+ private static String getSQL (Class c , Object ...args ){
296+
297+ //Get tableName
298+ String tableName = null ;
299+ try { tableName = ((Model ) c .newInstance ()).tableName ; }
300+ catch (Exception e ){}
301+
302+ StringBuilder str = new StringBuilder ("select " );
303+ str .append (tableName );
304+ str .append (".id from " );
305+ str .append (tableName );
306+
307+ if (args .length >1 ){
308+ str .append (" where " );
309+
310+ for (int i =0 ; i <args .length -1 ; i ++){
311+ str .append (args [i ]);
312+ i ++;
313+ Object val = args [i ];
314+ if (val instanceof String ){
315+ str .append ("'" );
316+ str .append (val .toString ().replace ("'" , "''" ));
317+ str .append ("'" );
318+ }
319+ else {
320+ str .append (val );
321+ }
322+ if (i <args .length -2 ) str .append (" and " );
323+ }
324+ }
325+ return str .toString ();
326+ }
327+
328+
274329 //**************************************************************************
275330 //** getValue
276331 //**************************************************************************
@@ -313,8 +368,8 @@ protected static Connection getConnection(Class c) throws SQLException {
313368 if (name.endsWith(".class")){
314369 name = name.substring(0, name.length()-6).replace("/", ".");
315370 Class c = Class.forName(name);
316- if (javaxt.utils .Model.class.isAssignableFrom(c)){
317- javaxt.utils .Model.init(c, database.getConnectionPool());
371+ if (javaxt.sql .Model.class.isAssignableFrom(c)){
372+ javaxt.sql .Model.init(c, database.getConnectionPool());
318373 }
319374 }
320375 }
0 commit comments