KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > persistence > SQLStorage


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.persistence;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Vector JavaDoc;
31 import org.apache.log4j.Logger;
32 import org.objectweb.jac.core.rtti.ClassItem;
33 import org.objectweb.jac.core.rtti.CollectionItem;
34 import org.objectweb.jac.core.rtti.FieldItem;
35 import org.objectweb.jac.util.Strings;
36
37 /**
38  * Implements the storage to store within an SQL compliant database system.
39  *
40  * @see LongOID
41  */

42
43 public abstract class SQLStorage implements Storage,java.io.Serializable JavaDoc {
44     static Logger logger = Logger.getLogger("persistence.storage");
45     static Logger loggerSql = Logger.getLogger("persistence.sql");
46
47     /**
48      * The SQL connection to the database that is use by this storage. */

49     protected Connection JavaDoc db;
50
51     /**
52      * Default constructor. */

53     protected SQLStorage(PersistenceAC ac) throws SQLException JavaDoc {
54         this. ac = ac;
55     }
56
57     /**
58      * Creates a new SQL storage.<p>
59      *
60      * @param db the connection to the database
61      */

62     public SQLStorage(PersistenceAC ac, Connection JavaDoc db) throws SQLException JavaDoc {
63         this. ac = ac;
64         setConnection(db);
65     }
66
67     protected String JavaDoc id;
68     public String JavaDoc getId() {
69         return id;
70     }
71     public void setId(String JavaDoc id) {
72         this.id = id;
73     }
74
75     public void close() {}
76
77     /**
78      * Resets the connection to the database with the given
79      * connection.<p>
80      *
81      * @param db the new connection */

82
83     protected void setConnection(Connection JavaDoc db) throws SQLException JavaDoc {
84         this.db = db;
85         updateJacNames();
86     }
87
88     /**
89      * Tells wether a table with a given name exists
90      */

91     protected abstract boolean hasTable(String JavaDoc name) throws Exception JavaDoc;
92
93     /**
94      * Updates jac names from <classname><count> to <classname>#<count>
95      */

96     protected void updateJacNames() throws SQLException JavaDoc {
97         ResultSet JavaDoc rs = executeQuery(
98             "SELECT roots.id, roots.name, classes.classid from roots,classes where "+
99             "not roots.name like '%#%' and roots.id=classes.id");
100         execute("BEGIN TRANSACTION");
101         try {
102             while (rs.next()) {
103                 String JavaDoc classname = Strings.getShortClassName(rs.getString("classid")).toLowerCase();
104                 String JavaDoc name = rs.getString("name");
105                 if (name.startsWith(classname) &&
106                     name.length()>classname.length() &&
107                     name.charAt(classname.length())!='#')
108                 {
109                     String JavaDoc newName = classname+"#"+name.substring(classname.length());
110                     executeUpdate("update roots set name='"+newName+"' where name='"+name+"'");
111                 }
112             }
113             execute("COMMIT");
114         } catch (Exception JavaDoc e) {
115             execute("ROLLBACK");
116             logger.error("Failed to update jac names");
117         }
118     }
119
120     protected int executeUpdate(String JavaDoc query) throws SQLException JavaDoc {
121         try {
122             loggerSql.debug(query);
123             return db.createStatement().executeUpdate(query);
124         } catch (SQLException JavaDoc e) {
125             logger.error("executeUpdate query failed: "+query);
126             throw e;
127         }
128     }
129
130     protected ResultSet JavaDoc executeQuery(String JavaDoc query) throws SQLException JavaDoc {
131         try {
132             loggerSql.debug(query);
133             return db.createStatement().executeQuery(query);
134         } catch (SQLException JavaDoc e) {
135             logger.error("executeQuery query failed: "+query);
136             throw e;
137         }
138     }
139
140     protected boolean execute(String JavaDoc query) throws SQLException JavaDoc {
141         try {
142             loggerSql.debug(query);
143             return db.createStatement().execute(query);
144         } catch (SQLException JavaDoc e) {
145             logger.error("execute query failed: "+query);
146             throw e;
147         }
148     }
149
150     protected boolean executeSilent(String JavaDoc query) throws SQLException JavaDoc {
151         return db.createStatement().execute(query);
152     }
153
154     public void deleteObject(OID oid) throws Exception JavaDoc
155     {
156         logger.debug("deleteObject("+oid+")");
157         executeUpdate("delete from objects where id="+oid.localId());
158         executeUpdate("delete from roots where id="+oid.localId());
159         // WE SHOULD ALSO REMOVE COLLECTIONS
160
}
161
162     public void setField(OID oid, FieldItem field, Object JavaDoc object)
163         throws Exception JavaDoc
164     {
165         logger.debug("setField("+oid+","+field.getName()+","+object+")");
166         String JavaDoc value = ValueConverter.objectToString(this,object);
167         String JavaDoc fieldID = field.getName();
168         String JavaDoc query = "insert into objects (id,fieldID,value) values "+
169             "("+oid.localId()+",'"+fieldID+"','"+addSlashes(value)+"')";
170         if (executeUpdate(query)==0) {
171             logger.error("setField failed : "+oid+","+fieldID+","+value);
172         }
173     }
174
175     public void updateField(OID oid, FieldItem field, Object JavaDoc object)
176         throws Exception JavaDoc
177     {
178         logger.debug("updateField("+oid+","+field+","+object+")");
179         String JavaDoc fieldID = field.getName();
180         String JavaDoc value = ValueConverter.objectToString(this,object);
181         String JavaDoc query = "update objects set value='"+addSlashes(value)+"' where "+
182             "id="+oid.localId()+" and fieldID='"+fieldID+"'";
183         if (executeUpdate(query)==0) {
184             setField(oid,field,object);
185         }
186     }
187
188     public Object JavaDoc getField(OID oid, FieldItem field)
189         throws Exception JavaDoc
190     {
191         logger.debug("getField("+oid+","+field.getName()+")");
192         checkStorage();
193         String JavaDoc fieldID = field.getName();
194         ResultSet JavaDoc rs = executeQuery("select value from objects where id="+oid.localId()+
195                                     " and fieldID='"+fieldID+"'");
196         if (rs.next()) {
197             return ValueConverter.stringToObject(this,rs.getString("value"));
198         } else {
199             if (field.isPrimitive()) {
200                 logger.warn("no such field in storage "+oid+","+fieldID);
201             }
202             return null;
203         }
204     }
205
206     public StorageField[] getFields(OID oid, ClassItem cl, FieldItem[] fields)
207         throws Exception JavaDoc
208     {
209         logger.debug("getFields "+oid+","+cl+","+Arrays.asList(fields));
210         // compute stringified list of fields for SQL query
211
if (fields.length == 0) {
212             return new StorageField[0];
213         }
214         String JavaDoc fieldlist = "(";
215         boolean first = true;
216         for (int i=0; i<fields.length; i++) {
217             if (!fields[i].isCalculated() && !fields[i].isTransient()) {
218                 if (!first)
219                     fieldlist += " or ";
220                 fieldlist += "fieldID='"+fields[i].getName()+"'";
221                 first = false;
222             }
223         }
224         fieldlist += ")";
225       
226         StorageField fieldValues[] = new StorageField[fields.length];
227         String JavaDoc query = "select * from objects where id="+oid.localId()+" and "+fieldlist;
228         ResultSet JavaDoc rs = executeQuery(query);
229
230         int i=0;
231         while (rs.next()) {
232             FieldItem field = cl.getField(rs.getString("fieldID"));
233             fieldValues[i] = new StorageField(
234                 cl,field,
235                 ValueConverter.stringToObject(this,rs.getString("value")));
236             i++;
237         }
238         return fieldValues;
239     }
240
241     public void removeField(OID oid, FieldItem field, Object JavaDoc value)
242         throws Exception JavaDoc
243     {
244         logger.debug("removeField("+oid+","+field+","+value+")");
245         String JavaDoc fieldID = field.getName();
246         executeUpdate("delete from objects where id="+oid.localId()+
247                       " and fieldID='"+fieldID+"'");
248     }
249
250     public Collection JavaDoc getRootObjects() throws Exception JavaDoc {
251         logger.debug("getRootObjects");
252         String JavaDoc sql = "select id from roots";
253         ResultSet JavaDoc rs = executeQuery(sql);
254         Vector JavaDoc result = new Vector JavaDoc();
255         while (rs.next()) {
256             result.add(new LongOID(this,rs.getLong("value")));
257         }
258         logger.debug("getRootObjects returns " + result);
259         return result;
260     }
261
262     // Collection methods
263

264     public OID getCollectionID(OID oid, CollectionItem collection)
265         throws Exception JavaDoc
266     {
267         return getOID("select value from objects where "+
268                       "id="+oid.localId()+" and fieldID='"+collection.getName()+"'");
269     }
270
271     public List JavaDoc getCollectionValues(OID oid, CollectionItem collection,
272                                     String JavaDoc table, String JavaDoc orderBy)
273         throws Exception JavaDoc
274     {
275         logger.debug("getCollectionValues("+oid+","+collection+")");
276         String JavaDoc fieldID = collection.getName();
277
278         String JavaDoc sql = "select "+table+".value from "+table+",objects where "+
279             "objects.id="+oid.localId()+
280             " and objects.fieldID='"+fieldID+"'"+
281             " and objects.value="+table+".id";
282         if (orderBy!=null) {
283             sql += " order by " + orderBy;
284         }
285       
286         ResultSet JavaDoc rs = executeQuery(sql);
287         Vector JavaDoc result = new Vector JavaDoc();
288         while (rs.next()) {
289             result.add(ValueConverter.stringToObject(this,rs.getString("value")));
290         }
291         logger.debug("getCollectionValues returns " + result);
292         return result;
293     }
294
295     public boolean collectionContains(String JavaDoc table, OID cid, Object JavaDoc value)
296         throws Exception JavaDoc
297     {
298         ResultSet JavaDoc res = executeQuery(
299             "select id from "+table+" where "+
300             "id="+cid.localId()+" and value='"+
301             addSlashes(ValueConverter.objectToString(this,value))+"'");
302         return res.next();
303     }
304
305     // List methods
306

307     public void clearList(OID cid)
308         throws Exception JavaDoc
309     {
310         logger.debug("clearList("+cid+")");
311         executeUpdate("delete from lists where id="+cid.localId());
312     }
313
314     public List JavaDoc getList(OID oid, CollectionItem collection)
315         throws Exception JavaDoc
316     {
317         return getList(getCollectionID(oid,collection));
318     }
319
320     public List JavaDoc getList(OID cid)
321         throws Exception JavaDoc
322     {
323         logger.debug("getList("+cid+")");
324         ResultSet JavaDoc rs = executeQuery("select value from lists "+
325                                     "where id="+cid.localId()+" order by index");
326         Vector JavaDoc result = new Vector JavaDoc();
327         while (rs.next()) {
328             result.add(
329                 ValueConverter.stringToObject(this,rs.getString("value")));
330         }
331         logger.debug("getList returns " + result);
332         return result;
333     }
334
335     public long getListSize(OID cid)
336         throws Exception JavaDoc
337     {
338         return getLong("select count(*) from lists where id="+cid.localId());
339     }
340
341     public boolean listContains(OID cid, Object JavaDoc value)
342         throws Exception JavaDoc
343     {
344         return collectionContains("lists",cid,value);
345     }
346
347     public Object JavaDoc getListItem(OID cid, long index)
348         throws Exception JavaDoc
349     {
350         ResultSet JavaDoc rs =
351             executeQuery("select value from lists where "+
352                          "id="+cid.localId()+" order by index limit 1 offset "+index);
353         if (rs.next()) {
354             return ValueConverter.stringToObject(this,rs.getString("value"));
355         } else {
356             return null;
357         }
358     }
359
360     public long getIndexInList(OID cid, Object JavaDoc value)
361         throws Exception JavaDoc
362     {
363         ResultSet JavaDoc rs = executeQuery(
364             "select min(index) as index from lists where "+
365             "id="+cid.localId()+" and value='"+
366             addSlashes(ValueConverter.objectToString(this,value))+"'");
367         if (rs.next()) {
368             long index = rs.getLong(1);
369             return getLong("select count(*) from lists where id="+cid.localId()+
370                            " and index<="+index)-1;
371         } else {
372             return -1;
373         }
374     }
375
376     protected long getInternalIndexInList(OID cid, Object JavaDoc value)
377         throws Exception JavaDoc
378     {
379         ResultSet JavaDoc rs = executeQuery(
380             "select min(index) as index from lists where "+
381             "id="+cid.localId()+" and value='"+
382             addSlashes(ValueConverter.objectToString(this,value))+"'");
383         if (rs.next()) {
384             long result = rs.getLong(1);
385             if (rs.wasNull())
386                 return -1;
387             else
388                 return result;
389         } else {
390             return -1;
391         }
392     }
393
394     public long getLastIndexInList(OID cid, Object JavaDoc value)
395         throws Exception JavaDoc
396     {
397         ResultSet JavaDoc rs = executeQuery(
398             "select max(index) from lists where "+
399             "id="+cid.localId()+" and value='"+
400             addSlashes(ValueConverter.objectToString(this,value))+"'");
401         if (rs.next()) {
402             long index = rs.getLong(1);
403             return getLong("select count(*) from lists where id="+cid.localId()+
404                            " and index<="+index)-1;
405         } else {
406             return -1;
407         }
408     }
409
410     public void addToList(OID cid, long position, Object JavaDoc value)
411         throws Exception JavaDoc
412     {
413         logger.debug("addToList("+cid+","+position+","+value+")");
414         executeUpdate("update lists set index=index+1 where id="+cid.localId()+
415                       " and index>="+position);
416         executeUpdate(
417             "insert into lists (id,index,value) values "+
418             "("+cid.localId()+","+position+",'"+
419             addSlashes(ValueConverter.objectToString(this,value))+"')");
420     }
421
422     public void addToList(OID cid, Object JavaDoc value)
423         throws Exception JavaDoc
424     {
425         logger.debug("addToList("+cid+","+value+")");
426         long size = getListSize(cid);
427         String JavaDoc indexExpr;
428         if (size==0)
429             indexExpr = "0";
430         else
431             indexExpr = "select max(index)+1 from lists where id="+cid.localId();
432         executeUpdate(
433             "insert into lists (id,index,value) values "+
434             "("+cid.localId()+",("+indexExpr+"),'"+
435             addSlashes(ValueConverter.objectToString(this,value))+"')");
436     }
437
438     public void setListItem(OID cid, long index, Object JavaDoc value)
439         throws Exception JavaDoc
440     {
441         logger.debug("setListItem("+cid+","+index+","+value+")");
442         executeUpdate("update lists set value='"+
443                       addSlashes(ValueConverter.objectToString(this,value))+
444                       " where id="+cid.localId()+" and index="+index);
445     }
446
447     public void removeFromList(OID cid, long position)
448         throws Exception JavaDoc
449     {
450         logger.debug("removeFromList("+cid+","+position+")");
451         // First, get the index for the position
452
ResultSet JavaDoc rs = executeQuery("select index from lists where "+
453                                     "id="+cid.localId()+" order by index limit 1,"+position);
454         long index = rs.getLong("index");
455         executeUpdate("delete from lists where "+"id="+cid.localId()+" and index="+index);
456     }
457
458     public void removeFromList(OID cid, Object JavaDoc value)
459         throws Exception JavaDoc
460     {
461         logger.debug("removeFromList("+cid+","+value+")");
462         long index = getInternalIndexInList(cid,value);
463         executeUpdate("delete from lists where "+"id="+cid.localId()+" and index="+index);
464     }
465
466     // Set methods
467

468     public void clearSet(OID cid)
469         throws Exception JavaDoc
470     {
471         logger.debug("clearSet("+cid+")");
472         executeUpdate("delete from sets where id="+cid.localId());
473     }
474
475     public List JavaDoc getSet(OID oid, CollectionItem collection) throws Exception JavaDoc {
476         return getSet(getCollectionID(oid,collection));
477     }
478
479     public List JavaDoc getSet(OID cid) throws Exception JavaDoc {
480         logger.debug("getSet("+cid+")");
481         ResultSet JavaDoc rs = executeQuery("select value from sets "+
482                                     "where id="+cid.localId());
483         Vector JavaDoc result = new Vector JavaDoc();
484         while (rs.next()) {
485             result.add(
486                 ValueConverter.stringToObject(this,rs.getString("value")));
487         }
488         logger.debug("getSet returns " + result);
489         return result;
490     }
491
492     public long getSetSize(OID cid)
493         throws Exception JavaDoc
494     {
495         return getLong("select count(*) from sets where id="+cid.localId());
496     }
497
498     public boolean setContains(OID cid, Object JavaDoc value)
499         throws Exception JavaDoc
500     {
501         return collectionContains("sets",cid,value);
502     }
503
504     public boolean addToSet(OID cid, Object JavaDoc value)
505         throws Exception JavaDoc
506     {
507         logger.debug("addToSet("+cid+","+value+")");
508         if (!collectionContains("sets",cid,value)) {
509             executeUpdate(
510                 "insert into sets (id,value) values "+"("+cid.localId()+",'"+
511                 addSlashes(ValueConverter.objectToString(this,value))+"')");
512             return true;
513         } else {
514             return false;
515         }
516     }
517
518     public boolean removeFromSet(OID cid, Object JavaDoc value)
519         throws Exception JavaDoc
520     {
521         logger.debug("removeFromSet("+cid+","+value+")");
522         boolean result = collectionContains("set",cid,value);
523         if (result)
524             executeUpdate(
525                 "delete from sets where "+
526                 "id="+cid.localId()+" and value='"+
527                 addSlashes(ValueConverter.objectToString(this,value))+"'");
528         return result;
529     }
530
531     // Map functions
532

533     public void clearMap(OID cid)
534         throws Exception JavaDoc
535     {
536         logger.debug("clearMap("+cid+")");
537         executeUpdate("delete from maps where id="+cid.localId());
538     }
539
540     public Map JavaDoc getMap(OID oid, CollectionItem collection)
541         throws Exception JavaDoc
542     {
543         return getMap(getCollectionID(oid,collection));
544     }
545
546     public Map JavaDoc getMap(OID cid) throws Exception JavaDoc
547     {
548         logger.debug("getMap("+cid+")");
549         ResultSet JavaDoc rs =
550             executeQuery("select value,key from maps where id="+cid.localId());
551         Map JavaDoc result = new HashMap JavaDoc();
552         while (rs.next()) {
553             result.put(
554                 ValueConverter.stringToObject(this,rs.getString("key")),
555                 ValueConverter.stringToObject(this,rs.getString("value")));
556         }
557         logger.debug("getMap returns " + result);
558         return result;
559     }
560
561     public long getMapSize(OID cid)
562         throws Exception JavaDoc
563     {
564         return getLong("select count(*) from maps where id="+cid.localId());
565     }
566
567     public Object JavaDoc putInMap(OID cid, Object JavaDoc key, Object JavaDoc value)
568         throws Exception JavaDoc
569     {
570         logger.debug("putInMap("+cid+","+key+"->"+value+")");
571         if (mapContainsKey(cid,key)) {
572             Object JavaDoc old = getFromMap(cid,key);
573             executeUpdate(
574                 "update maps set "+
575                 "key='"+addSlashes(ValueConverter.objectToString(this,key))+"',"+
576                 "value='"+addSlashes(ValueConverter.objectToString(this,value))+"' "+
577                 "where id="+cid.localId()+
578                 " and key='"+addSlashes(ValueConverter.objectToString(this,key))+"'");
579             return old;
580         } else {
581             executeUpdate(
582                 "insert into maps (id,key,value) values "+
583                 "("+cid.localId()+",'"+
584                 addSlashes(ValueConverter.objectToString(this,key))+
585                 "','"+addSlashes(ValueConverter.objectToString(this,value))+"')");
586             return null;
587         }
588     }
589
590     public Object JavaDoc getFromMap(OID cid, Object JavaDoc key)
591         throws Exception JavaDoc
592     {
593         logger.debug("getFromMap("+cid+","+key+")");
594         ResultSet JavaDoc res = executeQuery(
595             "select value from maps where "+
596             "id="+cid.localId()+" and key='"+
597             addSlashes(ValueConverter.objectToString(this,key))+"'");
598         if (res.next()) {
599             return ValueConverter.stringToObject(this,res.getString("value"));
600         } else {
601             return null;
602         }
603     }
604
605     public boolean mapContainsKey(OID cid, Object JavaDoc key)
606         throws Exception JavaDoc
607     {
608         logger.debug("mapContainsKey("+cid+","+key+")");
609         ResultSet JavaDoc res = executeQuery(
610             "select value from maps where "+
611             "id="+cid.localId()+" and key='"+
612             addSlashes(ValueConverter.objectToString(this,key))+"'");
613         return res.next();
614     }
615
616     public boolean mapContainsValue(OID cid, Object JavaDoc value)
617         throws Exception JavaDoc
618     {
619         return collectionContains("maps",cid,value);
620     }
621
622     public Object JavaDoc removeFromMap(OID cid, Object JavaDoc key)
623         throws Exception JavaDoc
624
625     {
626         logger.debug("removeFromMap("+cid+","+key+")");
627         if (!mapContainsKey(cid,key)) {
628             return null;
629         } else {
630             Object JavaDoc result = getFromMap(cid,key);
631             executeUpdate(
632                 "delete from maps where "+
633                 "id="+cid.localId()+" and key='"+
634                 addSlashes(ValueConverter.objectToString(this,key))+"'");
635             return result;
636         }
637     }
638
639     public abstract String JavaDoc newName(String JavaDoc className) throws Exception JavaDoc;
640
641     public abstract Map JavaDoc getNameCounters() throws Exception JavaDoc;
642
643     public abstract void updateNameCounters(Map JavaDoc counters) throws Exception JavaDoc;
644
645     public OID getOIDFromName(String JavaDoc name)
646         throws Exception JavaDoc
647     {
648         ResultSet JavaDoc rs = executeQuery(
649             "select id from roots where name='"+name+"'");
650         if (rs.next()) {
651             return new LongOID(this,rs.getLong("id"));
652         } else {
653             return null;
654         }
655     }
656    
657     public String JavaDoc getNameFromOID(OID oid) throws Exception JavaDoc
658     {
659         ResultSet JavaDoc rs = executeQuery(
660             "select name from roots where id="+oid.localId());
661         if (rs.next()) {
662             return rs.getString("name");
663         } else {
664             return null;
665         }
666     }
667
668     public void bindOIDToName(OID oid, String JavaDoc name) throws Exception JavaDoc
669     {
670         logger.debug("bindOIDToName "+oid+" -> "+name);
671         executeUpdate("insert into roots (id,name) values ("+
672                       oid.localId()+",'"+name+"')");
673     }
674
675     public void deleteName(String JavaDoc name) throws Exception JavaDoc
676     {
677         logger.debug("deleteName("+name+")");
678         executeUpdate("delete from roots where name='"+name+"'");
679     }
680
681     public String JavaDoc getClassID(OID oid) throws Exception JavaDoc
682     {
683         ResultSet JavaDoc rs = executeQuery("select classid from classes where id="+oid.localId());
684         if (rs.next()) {
685             String JavaDoc classID = rs.getString("classid");
686             logger.debug("getClassID("+oid+") -> "+classID);
687             return classID;
688         } else {
689             throw new NoSuchOIDError(oid);
690         }
691     }
692
693     public Collection JavaDoc getObjects(ClassItem cl) throws Exception JavaDoc
694     {
695         logger.debug("getObjects("+cl.getName()+")");
696         Vector JavaDoc result = new Vector JavaDoc();
697         getObjects(cl,result);
698         return result;
699     }
700
701     protected void getObjects(ClassItem cl, Vector JavaDoc objects) throws SQLException JavaDoc {
702         String JavaDoc query = "select id from classes";
703         if (cl != null) {
704             query += " where classes.classid='"+cl.getName()+"'";
705         }
706                
707         ResultSet JavaDoc rs = executeQuery(query);
708         while (rs.next()) {
709             objects.add(new LongOID(this,rs.getLong("id")));
710         }
711
712         Iterator JavaDoc i = cl.getChildren().iterator();
713         while(i.hasNext()) {
714             ClassItem subclass = (ClassItem)i.next();
715             getObjects(subclass,objects);
716         }
717     }
718
719     public void startTransaction() throws SQLException JavaDoc {
720         execute("BEGIN TRANSACTION");
721     }
722
723     public void commit() throws SQLException JavaDoc {
724         execute("COMMIT");
725     }
726
727     public void rollback() throws SQLException JavaDoc {
728         execute("ROLLBACK");
729     }
730
731     /**
732      * Throw an exception if storage is null or invalid
733      *
734      */

735     protected void checkStorage() {
736         if (db==null) {
737             logger.error("connection is NULL");
738             throw new InvalidStorageException("connection is NULL");
739         }
740     }
741
742     /**
743      * Creates a new object using a PostgreSQL sequance.<p>
744      *
745      * @return the new object OID
746      */

747     public OID createObject(String JavaDoc className) throws Exception JavaDoc {
748         LongOID res = new LongOID(this,getNextVal("object_id"));
749         executeUpdate("insert into classes (id,classid) values ("+
750                       res.localId()+",'"+className+"')");
751         return res;
752     }
753
754     /**
755      * Returns the next value of a sequence
756      */

757     public abstract long getNextVal(String JavaDoc sequence) throws Exception JavaDoc;
758
759     public long getLong(String JavaDoc query) throws Exception JavaDoc {
760         ResultSet JavaDoc rs = executeQuery(query);
761         rs.next();
762         return rs.getLong(1);
763     }
764
765     public int getInt(String JavaDoc query, int defaultValue) throws Exception JavaDoc {
766         ResultSet JavaDoc rs = executeQuery(query);
767         if (rs.next())
768             return rs.getInt(1);
769         else
770             return defaultValue;
771     }
772
773     public OID getOID(String JavaDoc query) throws Exception JavaDoc {
774         return new LongOID(this,getLong(query));
775     }
776
777     public static class InvalidStorageException extends RuntimeException JavaDoc {
778         public InvalidStorageException(String JavaDoc msg) {
779             super(msg);
780         }
781     }
782
783     /* add '\' before ''' and '\' */
784     public static String JavaDoc addSlashes(String JavaDoc str) {
785         StringBuffer JavaDoc res = new StringBuffer JavaDoc(str.length());
786         for (int i=0; i<str.length();i++) {
787             if (str.charAt(i)=='\'' || str.charAt(i)=='\\') {
788                 res.append('\\');
789             }
790             res.append(str.charAt(i));
791         }
792         return res.toString();
793     }
794
795     PersistenceAC ac;
796 }
797
Popular Tags