KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > rdbms > RdbmsSession


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  * Redistribution and use in source and binary forms, with or without
4  * modification, is permitted provided that the following conditions are met: -
5  * Redistributions of source code must retain the above copyright notice, this
6  * list of conditions and the following disclaimer. - Redistributions in binary
7  * form must reproduce the above copyright notice, this list of conditions and
8  * the following disclaimer in the documentation and/or other materials
9  * provided with the distribution. - All advertising materials mentioning
10  * features or use of this software must display the following acknowledgment:
11  * "This product includes Djeneric." - Products derived from this software may
12  * not be called "Djeneric" nor may "Djeneric" appear in their names without
13  * prior written permission of Genimen BV. - Redistributions of any form
14  * whatsoever must retain the following acknowledgment: "This product includes
15  * Djeneric." THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND
16  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
17  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV,
19  * DJENERIC.ORG, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */

27 package com.genimen.djeneric.repository.rdbms;
28
29 import java.math.BigDecimal JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Types JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import com.genimen.djeneric.language.Messages;
38 import com.genimen.djeneric.repository.DjContext;
39 import com.genimen.djeneric.repository.DjCursor;
40 import com.genimen.djeneric.repository.DjDomain;
41 import com.genimen.djeneric.repository.DjExtent;
42 import com.genimen.djeneric.repository.DjList;
43 import com.genimen.djeneric.repository.DjObject;
44 import com.genimen.djeneric.repository.DjObjectMatcher;
45 import com.genimen.djeneric.repository.DjOql;
46 import com.genimen.djeneric.repository.DjPersistenceManager;
47 import com.genimen.djeneric.repository.DjProperty;
48 import com.genimen.djeneric.repository.DjQueryByExample;
49 import com.genimen.djeneric.repository.DjSession;
50 import com.genimen.djeneric.repository.exceptions.DjenericException;
51 import com.genimen.djeneric.repository.exceptions.ObjectDeletedException;
52 import com.genimen.djeneric.repository.oql.core.ParseException;
53 import com.genimen.djeneric.util.DjLogger;
54
55 /**
56  * Description of the Class
57  *
58  * @author Wido Riezebos @created 28 mei 2002
59  */

60 public class RdbmsSession extends DjSession
61 {
62   Connection JavaDoc _conn;
63
64   /**
65    * Constructor for the RdbmsSession object
66    *
67    * @param mgr
68    * Description of the Parameter
69    * @param conn
70    * Description of the Parameter
71    */

72   protected RdbmsSession(DjPersistenceManager mgr)
73   {
74     super(mgr);
75   }
76
77   /**
78    * Returns the rdbmsPersistenceManager of the RdbmsSession object
79    *
80    * @return The rdbmsPersistenceManager value
81    */

82   protected RdbmsPersistenceManager getRdbmsPersistenceManager()
83   {
84     return (RdbmsPersistenceManager) getPersistenceManager();
85   }
86
87   public void releaseDatasources()
88   {
89     if (_conn != null) try
90     {
91       _conn.rollback();
92       getRdbmsPersistenceManager().releaseToPool(_conn);
93     }
94     catch (SQLException JavaDoc x)
95     {
96       // connection damanged? rely on implicit rollback to do the job then
97
// handle this as a warning only and do not place the (damaged) connection
98
// back in the pool
99
DjLogger.log(x);
100     }
101     _conn = null;
102   }
103
104   protected void assureConnection() throws DjenericException
105   {
106     if (_conn == null) _conn = getRdbmsPersistenceManager().getConnection();
107   }
108
109   /**
110    * Returns the connection of the RdbmsSession object
111    *
112    * @return The connection value
113    * @exception DjenericException
114    * Description of the Exception
115    */

116   protected Connection JavaDoc getConnection() throws DjenericException
117   {
118     if (!isValid()) throw new DjenericException(Messages.getString("global.SessionClosed"));
119     assureConnection();
120     return _conn;
121   }
122
123   /**
124    * Description of the Method
125    *
126    * @exception DjenericException
127    * Description of the Exception
128    */

129   public void commit() throws DjenericException
130   {
131     try
132     {
133       assureConnection();
134       postChanges();
135       if (shouldTrace(DjPersistenceManager.TRACE_FLOW)) trace("Database committed");
136       _conn.commit();
137       notifyApplySuccesfull();
138     }
139     catch (SQLException JavaDoc x)
140     {
141       rollback();
142       throw new DjenericException(x);
143     }
144   }
145
146   /**
147    * Description of the Method
148    *
149    * @exception DjenericException
150    * Description of the Exception
151    */

152   public void rollback() throws DjenericException
153   {
154     if (!isValid()) throw new DjenericException(Messages.getString("global.SessionClosed"));
155     try
156     {
157       assureConnection();
158       _conn.rollback();
159     }
160     catch (SQLException JavaDoc x)
161     {
162       throw new DjenericException(x);
163     }
164   }
165
166   /**
167    * Description of the Method
168    */

169   public void close()
170   {
171     if (!isValid()) return;
172     // already closed; ignore secondary close
173
releaseDatasources();
174     setValid(false);
175     // release some memory now
176
reset();
177     if (shouldTrace(DjPersistenceManager.TRACE_FLOW)) trace(Messages.getString("global.SessionClosed"));
178   }
179
180   /**
181    * Returns the internalSqlStatement of the RdbmsSession object
182    *
183    * @param stmt
184    * Description of the Parameter
185    * @return The internalSqlStatement value
186    * @exception DjenericException
187    * Description of the Exception
188    */

189   public SqlStatement getInternalSqlStatement(String JavaDoc stmt) throws DjenericException
190   {
191     if (!isValid()) throw new DjenericException(Messages.getString("global.SessionClosed"));
192     try
193     {
194       assureConnection();
195       SqlStatement ss = new SqlStatement(_conn, stmt, stmt);
196       RdbmsPersistenceManager mgr = (RdbmsPersistenceManager) getPersistenceManager();
197       DjContext context = mgr.getCurrentContext();
198       if (context != null && ss.hasParameter(RdbmsPersistenceManager.CONTEXT_PARAM_NAME))
199       {
200         ss.setLong(RdbmsPersistenceManager.CONTEXT_PARAM_NAME, context.getId());
201       }
202       return ss;
203     }
204     catch (SQLException JavaDoc x)
205     {
206       throw new DjenericException(x);
207     }
208   }
209
210   /**
211    * Returns the sqlStatement of the RdbmsSession object
212    *
213    * @param stmt
214    * Description of the Parameter
215    * @return The sqlStatement value
216    * @exception DjenericException
217    * Description of the Exception
218    */

219   public SqlStatement getSqlStatement(String JavaDoc stmt) throws DjenericException
220   {
221     if (!isValid()) throw new DjenericException(Messages.getString("global.SessionClosed"));
222     try
223     {
224       assureConnection();
225       RdbmsPersistenceManager mgr = (RdbmsPersistenceManager) getPersistenceManager();
226       SqlStatement ss = new SqlStatement(_conn, mgr.external2internalStatement(stmt), stmt);
227       ss.setCommitAllowed(isCommitAllowed());
228
229       DjContext context = mgr.getCurrentContext();
230       if (context != null && ss.hasParameter(RdbmsPersistenceManager.CONTEXT_PARAM_NAME))
231       {
232         ss.setLong(RdbmsPersistenceManager.CONTEXT_PARAM_NAME, context.getId());
233       }
234
235       return ss;
236     }
237     catch (SQLException JavaDoc x)
238     {
239       throw new DjenericException(x);
240     }
241
242   }
243
244   /**
245    * Description of the Method
246    *
247    * @param extent
248    * Description of the Parameter
249    * @return Description of the Return Value
250    * @exception DjenericException
251    * Description of the Exception
252    */

253   public DjObject createObject(DjExtent extent) throws DjenericException
254   {
255     if (!isValid()) throw new DjenericException(Messages.getString("global.SessionClosed"));
256     RdbmsDjenericObject po = new RdbmsDjenericObject(this, extent);
257
258     po.setTransient(extent.isTransient());
259
260     if (shouldTrace(DjPersistenceManager.TRACE_FLOW)) trace("Object of type " + extent.getObjectType() + " created");
261     return po;
262   }
263
264   /**
265    * Returns the object from the persistent store
266    *
267    * @param extent
268    * The extent to query
269    * @param id
270    * The id of the object
271    * @return The object
272    * @exception ObjectDeletedException
273    * Thrown if the object is not found in the store
274    */

275   public DjObject getObject(DjExtent extent, long id) throws DjenericException
276   {
277     DjObject po = doGetObject(extent, id);
278     if (po == null) throw new ObjectDeletedException(Messages.getString("RdbmsSession.probablyDeleted", extent
279         .getNameSingular(), String.valueOf(id)), po);
280     return po;
281   }
282
283   private DjObject doGetObject(DjExtent extent, long id) throws DjenericException
284   {
285     if (!isValid()) throw new DjenericException(Messages.getString("global.SessionClosed"));
286     try
287     {
288       // See if it's in the session already, if so return THAT object, not a
289
// new copy
290
DjObject loadedAlready = getFromSession(id);
291       if (loadedAlready != null)
292       {
293         // if we found it in the session, but it is marked for delete then
294
// return null: it's gone!
295
if (!loadedAlready.isMarkedForDelete()) return loadedAlready;
296         return null;
297       }
298
299       RdbmsDjenericObject po = null;
300
301       // Some optimization here:
302
// If in Polymorph mode we can use one single select statement to find an
303
// object of any type
304
// (Everything is stored in one table so this is possible)
305
// If NOT in polymorph mode we might need to fire more than one select
306
// statement because the
307
// object might be stored in an extent that is a subclass extent of the
308
// one specified by the
309
// extent parameter.
310
if (getRdbmsPersistenceManager().isInPolymorphMode())
311       {
312         SqlStatement stmt = getInternalSqlStatement("select * from " + RdbmsPersistenceManager.POLYMORPH_TABLE
313                                                     + " where " + RdbmsPersistenceManager.MAPPING_OBJECT_ID + " = :id");
314         stmt.setLong("id", id);
315         ResultSet JavaDoc rs = null;
316
317         try
318         {
319           rs = stmt.executeQuery();
320           if (rs.next())
321           {
322             String JavaDoc tableAlias = rs.getString(RdbmsPersistenceManager.INTERNAL_TYPE_COLUMN);
323             po = (RdbmsDjenericObject) createObject(getPersistenceManager().getExtentByInternalCode(tableAlias));
324             po.setFromRecord(rs, false);
325           }
326         }
327         finally
328         {
329           if (rs != null) rs.close();
330           stmt.close();
331         }
332       }
333       else
334       {
335         SqlStatement stmt = getSqlStatement("select * from " + extent.getName() + " where "
336                                             + extent.getIdProperty().getName() + " = :id");
337         stmt.setLong("id", id);
338         ResultSet JavaDoc rs = null;
339
340         try
341         {
342           rs = stmt.executeQuery();
343           if (rs.next())
344           {
345             po = (RdbmsDjenericObject) createObject(extent);
346             po.setFromRecord(rs, true);
347           }
348           else
349           {
350             DjExtent[] specs = extent.getSpecializations();
351             for (int i = 0; i < specs.length; i++)
352             {
353               DjObject obj = doGetObject(specs[i], id);
354               if (obj != null) return obj;
355             }
356           }
357         }
358         finally
359         {
360           if (rs != null) rs.close();
361           stmt.close();
362         }
363       }
364       return po;
365     }
366     catch (SQLException JavaDoc x)
367     {
368       throw new DjenericException(x);
369     }
370
371   }
372
373   private String JavaDoc translateOperator(String JavaDoc operator, boolean valueIsNull)
374   {
375     if (valueIsNull)
376     {
377       if (operator.equals("!=")) return " is not ";
378       if (operator.equals("==")) return " is ";
379     }
380
381     if (operator.equals("!=")) return "<>";
382     if (operator.equals("==")) return "=";
383     return operator;
384   }
385
386   private DjList getObjects(DjExtent ext, DjObjectMatcher qbe, boolean forceReloadFromDB, boolean addFromSession)
387       throws DjenericException
388   {
389     if (!isValid()) throw new DjenericException(Messages.getString("global.SessionClosed"));
390     try
391     {
392       DjList results = new DjList();
393       results.setStoredTypeName(ext);
394       // First add all matching objects that are (modified/new) in the session
395
// but not yet committed..
396
if (addFromSession) addModifiedObjectsFromSession(ext, qbe, results, false);
397
398       SqlStatement stmt = setupStatement(ext, qbe);
399
400       ResultSet JavaDoc rs = null;
401       String JavaDoc idColumnName = ext.getIdProperty().getName();
402       try
403       {
404         rs = stmt.executeQuery();
405         while (rs.next())
406         {
407           long id = rs.getLong(idColumnName);
408           RdbmsDjenericObject po = (RdbmsDjenericObject) getFromSession(id);
409           if (po == null)
410           {
411             po = (RdbmsDjenericObject) createObject(ext);
412             po.setFromRecord(rs, true);
413             results.add(po);
414           }
415           else
416           {
417             // Ignore the object if it's marked for delete
418
if (!po.isMarkedForDelete())
419             {
420               // Modified objects are already in the result
421
// ignore them (but still process the reload)
422
if (addFromSession && po.isModified())
423               { // ignore this one; already in the set!
424
}
425               else
426               {
427                 // go and add it to the set!
428
results.add(po);
429               }
430               if (forceReloadFromDB) po.reload();
431             }
432           }
433         }
434       }
435       finally
436       {
437         if (rs != null) rs.close();
438         stmt.close();
439       }
440
441       // Now add all objects that are sub types of the given extent
442
// Do this simply by recursing downwards the inheritance tree:
443
DjExtent[] specializations = ext.getSpecializations();
444       for (int i = 0; i < specializations.length; i++)
445       {
446         results.addAll(getObjects(specializations[i], qbe, forceReloadFromDB, false));
447       }
448       return results;
449     }
450     catch (SQLException JavaDoc x)
451     {
452       throw new DjenericException(x);
453     }
454   }
455
456   /**
457    * Returns the objects of the RdbmsSession object
458    *
459    * @param qbe
460    * Description of the Parameter
461    * @param forceReloadFromDB
462    * Description of the Parameter
463    * @return The objects value
464    * @exception DjenericException
465    * Description of the Exception
466    */

467   public DjList getObjects(DjObjectMatcher qbe, boolean forceReloadFromDB) throws DjenericException
468   {
469     DjList result = getObjects(qbe.getExtent(), qbe, forceReloadFromDB, true);
470     result.setStoredTypeName(qbe.getExtent());
471
472     return result;
473   }
474
475   /**
476    * Returns the objects of the RdbmsSession object
477    *
478    * @param ext
479    * Description of the Parameter
480    * @param forceReloadFromDB
481    * Description of the Parameter
482    * @return The objects value
483    * @exception DjenericException
484    * Description of the Exception
485    */

486   public DjList getObjects(DjExtent ext, boolean forceReloadFromDB) throws DjenericException
487   {
488     DjList result = getObjects(ext, null, forceReloadFromDB, true);
489     result.setStoredTypeName(ext);
490
491     return result;
492   }
493
494   private DjCursor getObjectsCursor(DjExtent ext, DjObjectMatcher qbe, boolean forceReloadFromDB, boolean addFromSession)
495       throws DjenericException
496   {
497     if (!isValid()) throw new DjenericException(Messages.getString("global.SessionClosed"));
498     try
499     {
500       DjList sessionObjects = new DjList();
501       sessionObjects.setStoredTypeName(ext);
502       // First add all objects that are (modified) in the session but not yet
503
// committed..
504
if (addFromSession) addModifiedObjectsFromSession(ext, qbe, sessionObjects, false);
505
506       SqlStatement stmt = setupStatement(ext, qbe);
507
508       RdbmsCursor result = new RdbmsCursor(this, ext, stmt, qbe, sessionObjects, forceReloadFromDB);
509
510       DjExtent specs[] = ext.getSpecializations();
511       for (int i = 0; i < specs.length; i++)
512       {
513         result.chain(getObjectsCursor(specs[i], qbe, forceReloadFromDB, false));
514       }
515
516       return result;
517     }
518     catch (SQLException JavaDoc x)
519     {
520       throw new DjenericException(x);
521     }
522   }
523
524   /**
525    * Returns the objectsCursor of the RdbmsSession object
526    *
527    * @param ext
528    * Description of the Parameter
529    * @param forceReloadFromDB
530    * Description of the Parameter
531    * @return The objectsCursor value
532    * @exception DjenericException
533    * Description of the Exception
534    */

535   public DjCursor getObjectsCursor(DjExtent ext, boolean forceReloadFromDB) throws DjenericException
536   {
537     return getObjectsCursor(ext, null, forceReloadFromDB, true);
538   }
539
540   /**
541    * Returns the objectsCursor of the RdbmsSession object
542    *
543    * @param qbe
544    * Description of the Parameter
545    * @param forceReloadFromDB
546    * Description of the Parameter
547    * @return The objectsCursor value
548    * @exception DjenericException
549    * Description of the Exception
550    */

551   public DjCursor getObjectsCursor(DjObjectMatcher qbe, boolean forceReloadFromDB) throws DjenericException
552   {
553     return getObjectsCursor(qbe.getExtent(), qbe, forceReloadFromDB, true);
554   }
555
556   /**
557    * Adds a feature to the ValidMasterObjects attribute of the RdbmsSession
558    * object
559    *
560    * @param masterExtent
561    * The feature to be added to the ValidMasterObjects attribute
562    * @param restrictingOid
563    * The feature to be added to the ValidMasterObjects attribute
564    * @param restrictingPropertyName
565    * The feature to be added to the ValidMasterObjects attribute
566    * @param forceReloadFromDB
567    * The feature to be added to the ValidMasterObjects attribute
568    * @param results
569    * The feature to be added to the ValidMasterObjects attribute
570    * @exception DjenericException
571    * Description of the Exception
572    */

573   protected void addValidMasterObjects(DjExtent masterExtent, long restrictingOid, String JavaDoc restrictingPropertyName,
574                                        boolean forceReloadFromDB, DjList results) throws DjenericException
575   {
576     try
577     {
578       String JavaDoc masterExtentName = masterExtent.getName();
579       SqlStatement stmt = getSqlStatement("select * from " + masterExtentName + " where " + restrictingPropertyName
580                                           + " = :restrictingOid");
581
582       stmt.setLong("restrictingOid", restrictingOid);
583
584       ResultSet JavaDoc rs = null;
585
586       try
587       {
588         rs = stmt.executeQuery();
589         while (rs.next())
590         {
591           RdbmsDjenericObject po = (RdbmsDjenericObject) getFromSession(rs.getLong(masterExtent.getIdProperty()
592               .getName()));
593           if (po == null)
594           {
595             po = (RdbmsDjenericObject) createObject(masterExtent);
596             po.setFromRecord(rs, true);
597             results.add(po);
598           }
599           else
600           {
601             // Ignore the object if it's marked for delete
602
if (!po.isMarkedForDelete())
603             {
604               results.add(po);
605               if (forceReloadFromDB) po.reload();
606             }
607
608           }
609         }
610       }
611       finally
612       {
613         if (rs != null) rs.close();
614         stmt.close();
615       }
616       DjExtent[] specializations = masterExtent.getSpecializations();
617       for (int i = 0; i < specializations.length; i++)
618       {
619         addValidMasterObjects(specializations[i], restrictingOid, restrictingPropertyName, forceReloadFromDB, results);
620       }
621     }
622     catch (SQLException JavaDoc x)
623     {
624       throw new DjenericException(x);
625     }
626   }
627
628   /**
629    * Defined at this level to render access to classes in this package;
630    *
631    * @param id
632    * Description of the Parameter
633    * @return The fromSession value
634    * @exception DjenericException
635    * Description of the Exception
636    */

637   protected DjObject getFromSession(long id) throws DjenericException
638   {
639     return super.getFromSession(id);
640   }
641
642   private void setQbeParameters(SqlStatement stmt, DjQueryByExample qbe) throws SQLException JavaDoc
643   {
644     for (int i = 0; i < qbe.getPropertyCount(); i++)
645     {
646       if (!qbe.isNull(i))
647       {
648         DjProperty prop = qbe.getProperty(i);
649         if (prop.getTypeCode() == DjDomain.INT_TYPE) stmt.setInt(prop.getName(), qbe.getInt(i));
650         else if (prop.getTypeCode() == DjDomain.BIGDECIMAL_TYPE) stmt.setBigDecimal(prop.getName(), qbe
651             .getBigDecimal(i));
652         else if (prop.getTypeCode() == DjDomain.DATE_TYPE)
653         {
654           if (qbe.getDate(i) instanceof java.sql.Timestamp JavaDoc) stmt.setTimestamp(prop.getName(), (java.sql.Timestamp JavaDoc) qbe
655               .getDate(i));
656           else stmt.setTimestamp(prop.getName(), new java.sql.Timestamp JavaDoc(((java.util.Date JavaDoc) qbe.getDate(i)).getTime()));
657         }
658         else if (prop.getTypeCode() == DjDomain.LONG_TYPE) stmt.setLong(prop.getName(), qbe.getLong(i));
659         else if (prop.getTypeCode() == DjDomain.STRING_TYPE) stmt.setString(prop.getName(), qbe.getString(i));
660       }
661     }
662   }
663
664   private SqlStatement setupStatement(DjExtent ext, DjObjectMatcher qbe) throws SQLException JavaDoc, DjenericException
665   {
666     if (qbe == null)
667     {
668       return setupDefaultStatement(ext);
669     }
670     else if (qbe instanceof DjQueryByExample)
671     {
672       return setupStatementUsingQbe(ext, (DjQueryByExample) qbe);
673     }
674
675     if (qbe instanceof DjOql)
676     {
677       return setupStatementUsingOql(ext, (DjOql) qbe);
678     }
679
680     throw new DjenericException(Messages.getString("RdbmsSession.unableToHandle", qbe.getClass().getName()));
681   }
682
683   private SqlStatement setupStatementUsingQbe(DjExtent ext, DjQueryByExample qbe) throws SQLException JavaDoc,
684       DjenericException
685   {
686
687     String JavaDoc sqlStmt = "select * from " + ext.getName();
688
689     boolean first = true;
690     for (int i = 0; i < qbe.getPropertyCount(); i++)
691     {
692       if (!qbe.isNull(i) || qbe.hasOperatorFor(i))
693       {
694         if (first) sqlStmt += " where ";
695         else sqlStmt += " and ";
696         first = false;
697
698         DjProperty prop = qbe.getProperty(i);
699
700         // Handle QBE with null values and an operator that is set: user "is
701
// null" and "is not null"
702
if (qbe.isNull(i))
703         {
704           sqlStmt += prop.getName() + " " + translateOperator(qbe.getOperator(i), true) + " null";
705         }
706         else
707         {
708           sqlStmt += prop.getName() + " " + translateOperator(qbe.getOperator(i), false) + " :" + prop.getName();
709         }
710       }
711     }
712
713     SqlStatement stmt = getSqlStatement(sqlStmt);
714     setQbeParameters(stmt, qbe);
715
716     return stmt;
717   }
718
719   private SqlStatement setupDefaultStatement(DjExtent ext) throws DjenericException
720   {
721     String JavaDoc sqlStmt = "select * from " + ext.getName();
722     SqlStatement stmt = getSqlStatement(sqlStmt);
723     return stmt;
724   }
725
726   private SqlStatement setupStatementUsingOql(DjExtent ext, DjOql oql) throws SQLException JavaDoc, DjenericException
727   {
728     String JavaDoc sqlStmt;
729     try
730     {
731       // We might be performing a subsequent query in a sub class extent;
732
// update the from clause of the OQL:
733
if (oql.getExtent() != ext) oql.setBaseClassExtent(ext);
734
735       String JavaDoc fromWhere = oql.getFromWhereClause();
736       sqlStmt = "select ";
737       if (!oql.isResultUnique()) sqlStmt += "distinct ";
738       sqlStmt += oql.getAlias() + ".* " + fromWhere;
739     }
740     catch (ParseException e)
741     {
742       throw new DjenericException(e);
743     }
744
745     SqlStatement stmt = getSqlStatement(sqlStmt);
746     setOqlParameters(stmt, oql);
747     return stmt;
748   }
749
750   private void setOqlParameters(SqlStatement stmt, DjOql oql) throws SQLException JavaDoc, DjenericException
751   {
752     HashMap JavaDoc parameters = oql.getParameters();
753     Iterator JavaDoc it = parameters.keySet().iterator();
754
755     while (it.hasNext())
756     {
757       String JavaDoc name = it.next().toString();
758       Object JavaDoc value = parameters.get(name);
759
760       if (value instanceof Integer JavaDoc) stmt.setInt(name, ((Integer JavaDoc) value).intValue());
761       else if (value instanceof BigDecimal JavaDoc) stmt.setBigDecimal(name, (BigDecimal JavaDoc) value);
762       else if (value instanceof java.sql.Timestamp JavaDoc) stmt.setTimestamp(name, (java.sql.Timestamp JavaDoc) value);
763       else if (value instanceof java.util.Date JavaDoc) stmt.setTimestamp(name, new java.sql.Timestamp JavaDoc(((java.util.Date JavaDoc) value)
764           .getTime()));
765       else if (value instanceof Long JavaDoc) stmt.setLong(name, ((Long JavaDoc) value).longValue());
766       else if (value instanceof String JavaDoc) stmt.setString(name, (String JavaDoc) value);
767       else if (value instanceof DjObject) stmt.setLong(name, ((DjObject) value).getObjectId());
768       else if (value == null) stmt.setNull(name, Types.NULL);
769       else throw new SQLException JavaDoc(Messages
770           .getString("RdbmsSession.unsupportedOqlParameter", value.getClass().getName()));
771     }
772   }
773 }
Popular Tags