JAC project
AOPSYS
CEDRIC & LIP6 labs

org.objectweb.jac.aspects.persistence
Class SQLStorage

java.lang.Object
  |
  +--org.objectweb.jac.aspects.persistence.SQLStorage
All Implemented Interfaces:
Serializable, Storage
Direct Known Subclasses:
PostgresStorage

public abstract class SQLStorage
extends Object
implements Storage, Serializable

Implements the storage to store within an SQL compliant database system.

See Also:
LongOID, Serialized Form

Nested Class Summary
static class SQLStorage.InvalidStorageException
           
 
Field Summary
protected  Connection db
          The SQL connection to the database that is use by this storage.
 
Constructor Summary
protected SQLStorage()
          Default constructor.
  SQLStorage(Connection db)
          Creates a new SQL storage.
 
Method Summary
static String addSlashes(String str)
           
 void addToList(OID cid, long position, Object value)
          Insert a value into an existing List.
 void addToList(OID cid, Object value)
          Add a value at the end of a list.
 boolean addToSet(OID cid, Object value)
          Add an object to a Set.
 void bindOIDToName(OID oid, String name)
          Bind an existing object to a logical name to that it can be found later on.
protected  void checkStorage()
          Throw an exception if storage is null or invalid
 void clearList(OID cid)
          Remove all objects from a list.
 void clearMap(OID cid)
          Remove all objects from a set.
 void clearSet(OID cid)
          Remove all objects from a set.
 void close()
          Closes the storage.
 boolean collectionContains(String table, OID cid, Object value)
           
 void commit()
          Commit started transaction
 OID createObject(String className)
          Creates a new object using a PostgreSQL sequance.
 void deleteName(String name)
          Delete a name from the storage.
 void deleteObject(OID oid)
          Delete an object from the storage.
protected  boolean execute(String query)
           
protected  ResultSet executeQuery(String query)
           
protected  int executeUpdate(String query)
           
 String getClassID(OID oid)
          Get the class ID of a given object.
 OID getCollectionID(OID oid, CollectionItem collection)
          Get the ID of a collection
 List getCollectionValues(OID oid, CollectionItem collection, String table, String orderBy)
           
 Object getField(OID oid, FieldItem field)
          Get the value of a field.
 StorageField[] getFields(OID oid, ClassItem cl, FieldItem[] fields)
          Get the values of several fields.
 Object getFromMap(OID cid, Object key)
          Get the value associated to a key from a Map.
 long getIndexInList(OID cid, Object value)
          Get the smallest index of an element in a List.
protected  long getInternalIndexInList(OID cid, Object value)
           
 long getLastIndexInList(OID cid, Object value)
          Get the highest index of an element in a List.
 List getList(OID cid)
          Get a List from the storage.
 List getList(OID oid, CollectionItem collection)
          Get a List from the storage.
 Object getListItem(OID cid, long index)
          Get an element from a list
 long getListSize(OID cid)
          Get the number of objects contained in a list
 long getLong(String query)
           
 Map getMap(OID cid)
          Get the elements of a Map.
 Map getMap(OID oid, CollectionItem collection)
          Describe getMap method here.
 long getMapSize(OID cid)
          Get the number of objects contained in a map
 String getNameFromOID(OID oid)
          Get the name of an object from its oid.
abstract  long getNextVal(String sequence)
          Returns the next value of a sequence
 Collection getObjects(ClassItem cl)
          Get all instances of a class, or all objects if cl == null.
protected  void getObjects(ClassItem cl, Vector objects)
           
 OID getOID(String query)
           
 OID getOIDFromName(String name)
          Get the ID of an object from its name.
 Collection getRootObjects()
          Get OIDs of all root objects.
 List getSet(OID cid)
          Get the elements of a Set.
 List getSet(OID oid, CollectionItem collection)
          Get the elements of a Set.
 long getSetSize(OID cid)
          Get the number of objects contained in a set
 boolean listContains(OID cid, Object value)
          Say if a List contains an object.
 boolean mapContainsKey(OID cid, Object key)
          Says if a Map contains a key.
 boolean mapContainsValue(OID cid, Object value)
          Says if a Map contains a value.
 String newName(String className)
          Generate a new name for an instance.
 Object putInMap(OID cid, Object key, Object value)
          Put an element in a Map.
 void removeField(OID oid, FieldItem field, Object value)
          Remove a field from an existing object.
 void removeFromList(OID cid, long position)
          Remove a value from an existing list.
 void removeFromList(OID cid, Object value)
          Remove the first value from an existing list.
 Object removeFromMap(OID cid, Object key)
          Remove a key from a Map.
 boolean removeFromSet(OID cid, Object value)
          Remove an element from a Set.
 void rollback()
          Rollback started transaction
protected  void setConnection(Connection db)
          Resets the connection to the database with the given connection.
 boolean setContains(OID cid, Object value)
          Say if a set contains an object.
 void setField(OID oid, FieldItem field, Object object)
          Initialize the value of an object's field.
 void setListItem(OID cid, long index, Object value)
          Set the value of a list element.
 void startTransaction()
          Starts a transaction
 void updateField(OID oid, FieldItem field, Object object)
          Update a field value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

db

protected Connection db
The SQL connection to the database that is use by this storage.

Constructor Detail

SQLStorage

protected SQLStorage()
              throws SQLException
Default constructor.


SQLStorage

public SQLStorage(Connection db)
           throws SQLException
Creates a new SQL storage.

Parameters:
db - the connection to the database
Method Detail

close

public void close()
Description copied from interface: Storage
Closes the storage.

Specified by:
close in interface Storage

setConnection

protected void setConnection(Connection db)
Resets the connection to the database with the given connection.

Parameters:
db - the new connection

executeUpdate

protected int executeUpdate(String query)
                     throws SQLException
SQLException

executeQuery

protected ResultSet executeQuery(String query)
                          throws SQLException
SQLException

execute

protected boolean execute(String query)
                   throws SQLException
SQLException

deleteObject

public void deleteObject(OID oid)
                  throws Exception
Description copied from interface: Storage
Delete an object from the storage.

Specified by:
deleteObject in interface Storage
Parameters:
oid - the ID of the object to delete
Throws:
Exception - if an error occurs

setField

public void setField(OID oid,
                     FieldItem field,
                     Object object)
              throws Exception
Description copied from interface: Storage
Initialize the value of an object's field. The field must not have already been set.

Specified by:
setField in interface Storage
Parameters:
oid - the ID of the object that will contain the field
field - the field to set
object - the value of the field
Throws:
Exception - if an error occurs

updateField

public void updateField(OID oid,
                        FieldItem field,
                        Object object)
                 throws Exception
Description copied from interface: Storage
Update a field value.

Specified by:
updateField in interface Storage
Parameters:
oid - the ID of the object that already contains the field
field - the field to update
object - the new value of the field
Throws:
Exception - if an error occurs

getField

public Object getField(OID oid,
                       FieldItem field)
                throws Exception
Description copied from interface: Storage
Get the value of a field.

Specified by:
getField in interface Storage
Parameters:
oid - the ID of the object that contains the field
field - the field to retrieve
Returns:
the stored value of the field, or null if there's no stored value for that field
Throws:
Exception - if an error occurs

getFields

public StorageField[] getFields(OID oid,
                                ClassItem cl,
                                FieldItem[] fields)
                         throws Exception
Description copied from interface: Storage
Get the values of several fields.

Specified by:
getFields in interface Storage
Parameters:
oid - the ID of object to retrieve
cl - the class of the object
fields - the fields to retrieve
Returns:
the value of the fields
Throws:
Exception - if an error occurs

removeField

public void removeField(OID oid,
                        FieldItem field,
                        Object value)
                 throws Exception
Description copied from interface: Storage
Remove a field from an existing object.

Specified by:
removeField in interface Storage
Parameters:
oid - the ID of the object that contains the field
field - the ID of the field to remove
value - Deprecated
Exception

getRootObjects

public Collection getRootObjects()
                          throws Exception
Description copied from interface: Storage
Get OIDs of all root objects.

Specified by:
getRootObjects in interface Storage
Returns:
the root objects
Throws:
Exception - if an error occurs

getCollectionID

public OID getCollectionID(OID oid,
                           CollectionItem collection)
                    throws Exception
Description copied from interface: Storage
Get the ID of a collection

Specified by:
getCollectionID in interface Storage
Parameters:
oid - the oid of he object that owns the collection
collection - the collection
Returns:
the id of the collection
Throws:
Exception - if an error occurs

getCollectionValues

public List getCollectionValues(OID oid,
                                CollectionItem collection,
                                String table,
                                String orderBy)
                         throws Exception
Exception

collectionContains

public boolean collectionContains(String table,
                                  OID cid,
                                  Object value)
                           throws Exception
Exception

clearList

public void clearList(OID cid)
               throws Exception
Description copied from interface: Storage
Remove all objects from a list.

Specified by:
clearList in interface Storage
Parameters:
cid - the ID of the list to clear
Throws:
Exception - if an error occurs

getList

public List getList(OID oid,
                    CollectionItem collection)
             throws Exception
Description copied from interface: Storage
Get a List from the storage.

Specified by:
getList in interface Storage
Parameters:
oid - the ID of the object that contains the vector
collection - the collection to retrieve
Returns:
the List
Throws:
Exception - if an error occurs

getList

public List getList(OID cid)
             throws Exception
Description copied from interface: Storage
Get a List from the storage.

Specified by:
getList in interface Storage
Parameters:
cid - the ID of the List
Returns:
the List
Throws:
Exception - if an error occurs

getListSize

public long getListSize(OID cid)
                 throws Exception
Description copied from interface: Storage
Get the number of objects contained in a list

Specified by:
getListSize in interface Storage
Parameters:
cid - the ID of the list
Returns:
the number of objects contained in the list
Throws:
Exception - if an error occurs

listContains

public boolean listContains(OID cid,
                            Object value)
                     throws Exception
Description copied from interface: Storage
Say if a List contains an object.

Specified by:
listContains in interface Storage
Parameters:
cid - the ID of the List
value - the object to look for in the list
Returns:
wether the List contains the value
Throws:
Exception - if an error occurs

getListItem

public Object getListItem(OID cid,
                          long index)
                   throws Exception
Description copied from interface: Storage
Get an element from a list

Specified by:
getListItem in interface Storage
Parameters:
cid - the ID of the List
index - the index of the element
Returns:
the element
Throws:
Exception - if an error occurs

getIndexInList

public long getIndexInList(OID cid,
                           Object value)
                    throws Exception
Description copied from interface: Storage
Get the smallest index of an element in a List.

Specified by:
getIndexInList in interface Storage
Parameters:
cid - the ID of the List
value - the value
Returns:
the index of value
Throws:
Exception - if an error occurs

getInternalIndexInList

protected long getInternalIndexInList(OID cid,
                                      Object value)
                               throws Exception
Exception

getLastIndexInList

public long getLastIndexInList(OID cid,
                               Object value)
                        throws Exception
Description copied from interface: Storage
Get the highest index of an element in a List.

Specified by:
getLastIndexInList in interface Storage
Parameters:
cid - the ID of the List
value - the value
Returns:
the index of value
Throws:
Exception - if an error occurs

addToList

public void addToList(OID cid,
                      long position,
                      Object value)
               throws Exception
Description copied from interface: Storage
Insert a value into an existing List.

Specified by:
addToList in interface Storage
Parameters:
cid - the ID of the List
position - the position where to insert the value
value - the value to insert
Throws:
Exception - if an error occurs

addToList

public void addToList(OID cid,
                      Object value)
               throws Exception
Description copied from interface: Storage
Add a value at the end of a list.

Specified by:
addToList in interface Storage
Parameters:
cid - the ID of the List
value - the value to add
Throws:
Exception - if an error occurs

setListItem

public void setListItem(OID cid,
                        long index,
                        Object value)
                 throws Exception
Description copied from interface: Storage
Set the value of a list element.

Specified by:
setListItem in interface Storage
Parameters:
cid - the ID of the List
index - the index of the element to set
value - the value
Throws:
Exception - if an error occurs

removeFromList

public void removeFromList(OID cid,
                           long position)
                    throws Exception
Description copied from interface: Storage
Remove a value from an existing list.

Specified by:
removeFromList in interface Storage
Parameters:
cid - the ID the List
position - the position of the element to remove
Throws:
Exception - if an error occurs

removeFromList

public void removeFromList(OID cid,
                           Object value)
                    throws Exception
Description copied from interface: Storage
Remove the first value from an existing list.

Specified by:
removeFromList in interface Storage
Parameters:
cid - the ID the List
value - the value to remove
Throws:
Exception - if an error occurs

clearSet

public void clearSet(OID cid)
              throws Exception
Description copied from interface: Storage
Remove all objects from a set.

Specified by:
clearSet in interface Storage
Parameters:
cid - the ID of the set to clear
Throws:
Exception - if an error occurs

getSet

public List getSet(OID oid,
                   CollectionItem collection)
            throws Exception
Description copied from interface: Storage
Get the elements of a Set.

Specified by:
getSet in interface Storage
Parameters:
oid - the ID of the object that contains the Set
collection - the collection
Returns:
the elements of the Set
Throws:
Exception - if an error occurs

getSet

public List getSet(OID cid)
            throws Exception
Description copied from interface: Storage
Get the elements of a Set.

Specified by:
getSet in interface Storage
Parameters:
cid - the ID of the Set
Returns:
the elements of the Set
Throws:
Exception - if an error occurs

getSetSize

public long getSetSize(OID cid)
                throws Exception
Description copied from interface: Storage
Get the number of objects contained in a set

Specified by:
getSetSize in interface Storage
Parameters:
cid - the ID of the list
Returns:
the number of objects contained in the set
Throws:
Exception - if an error occurs

setContains

public boolean setContains(OID cid,
                           Object value)
                    throws Exception
Description copied from interface: Storage
Say if a set contains an object.

Specified by:
setContains in interface Storage
Parameters:
cid - the ID of the Set
value - the value
Returns:
wether the Set contains the value
Throws:
Exception - if an error occurs

addToSet

public boolean addToSet(OID cid,
                        Object value)
                 throws Exception
Description copied from interface: Storage
Add an object to a Set.

Specified by:
addToSet in interface Storage
Parameters:
cid - the ID of the Set
value - the value to add
Returns:
true if the set did not already contain the object, false otherwise.
Throws:
Exception - if an error occurs

removeFromSet

public boolean removeFromSet(OID cid,
                             Object value)
                      throws Exception
Description copied from interface: Storage
Remove an element from a Set.

Specified by:
removeFromSet in interface Storage
Parameters:
cid - the ID of the Set
value - the value to add
Returns:
wether the set did contain the object
Throws:
Exception - if an error occurs

clearMap

public void clearMap(OID cid)
              throws Exception
Description copied from interface: Storage
Remove all objects from a set.

Specified by:
clearMap in interface Storage
Parameters:
cid - the ID of the set to clear
Throws:
Exception - if an error occurs

getMap

public Map getMap(OID oid,
                  CollectionItem collection)
           throws Exception
Description copied from interface: Storage
Describe getMap method here.

Specified by:
getMap in interface Storage
Parameters:
oid - an OID value
collection - a CollectionItem value
Returns:
a Map value
Throws:
Exception - if an error occurs

getMap

public Map getMap(OID cid)
           throws Exception
Description copied from interface: Storage
Get the elements of a Map.

Specified by:
getMap in interface Storage
Parameters:
cid - the ID of the Set
Returns:
the Map
Throws:
Exception - if an error occurs

getMapSize

public long getMapSize(OID cid)
                throws Exception
Description copied from interface: Storage
Get the number of objects contained in a map

Specified by:
getMapSize in interface Storage
Parameters:
cid - the ID of the list
Returns:
the number of objects contained in the map
Throws:
Exception - if an error occurs

putInMap

public Object putInMap(OID cid,
                       Object key,
                       Object value)
                throws Exception
Description copied from interface: Storage
Put an element in a Map.

Specified by:
putInMap in interface Storage
Parameters:
cid - the ID of the Map
key - the key
value - the value
Returns:
the previous value associated with the key
Throws:
Exception - if an error occurs

getFromMap

public Object getFromMap(OID cid,
                         Object key)
                  throws Exception
Description copied from interface: Storage
Get the value associated to a key from a Map.

Specified by:
getFromMap in interface Storage
Parameters:
cid - the ID of the Map
key - the key
Returns:
the value associated with the key
Throws:
Exception - if an error occurs

mapContainsKey

public boolean mapContainsKey(OID cid,
                              Object key)
                       throws Exception
Description copied from interface: Storage
Says if a Map contains a key.

Specified by:
mapContainsKey in interface Storage
Parameters:
cid - the ID of the Map
key - the key to search
Returns:
wether the Map contains the key
Throws:
Exception - if an error occurs

mapContainsValue

public boolean mapContainsValue(OID cid,
                                Object value)
                         throws Exception
Description copied from interface: Storage
Says if a Map contains a value.

Specified by:
mapContainsValue in interface Storage
Parameters:
cid - the ID of the Map
value - the value to search
Returns:
wether the Map contains the value
Throws:
Exception - if an error occurs

removeFromMap

public Object removeFromMap(OID cid,
                            Object key)
                     throws Exception
Description copied from interface: Storage
Remove a key from a Map.

Specified by:
removeFromMap in interface Storage
Parameters:
cid - the ID the Map
key - the key to remove
Returns:
the previous value associated to the key, or null
Throws:
Exception - if an error occurs

newName

public String newName(String className)
               throws Exception
Description copied from interface: Storage
Generate a new name for an instance.

Specified by:
newName in interface Storage
Parameters:
className - the className of the instance for which to generate a name
Returns:
the generated name, null if failure
Exception

getOIDFromName

public OID getOIDFromName(String name)
                   throws Exception
Description copied from interface: Storage
Get the ID of an object from its name.

Specified by:
getOIDFromName in interface Storage
Parameters:
name - the candidate object name
Returns:
null if not found
Exception

getNameFromOID

public String getNameFromOID(OID oid)
                      throws Exception
Description copied from interface: Storage
Get the name of an object from its oid.

Specified by:
getNameFromOID in interface Storage
Parameters:
oid - the candidate object oid
Exception

bindOIDToName

public void bindOIDToName(OID oid,
                          String name)
                   throws Exception
Description copied from interface: Storage
Bind an existing object to a logical name to that it can be found later on.

This method allows the user to create persistence roots.

Specified by:
bindOIDToName in interface Storage
Parameters:
oid - an existing object ID
name - the name that is given to it
Exception

deleteName

public void deleteName(String name)
                throws Exception
Description copied from interface: Storage
Delete a name from the storage.

Specified by:
deleteName in interface Storage
Parameters:
name - the name to remove
Exception

getClassID

public String getClassID(OID oid)
                  throws Exception
Description copied from interface: Storage
Get the class ID of a given object.

Specified by:
getClassID in interface Storage
Parameters:
oid - the object's class ID
Throws:
Exception - if an error occurs

getObjects

public Collection getObjects(ClassItem cl)
                      throws Exception
Description copied from interface: Storage
Get all instances of a class, or all objects if cl == null.

Specified by:
getObjects in interface Storage
Parameters:
cl - the class
Returns:
the instances
Exception

getObjects

protected void getObjects(ClassItem cl,
                          Vector objects)
                   throws SQLException
SQLException

startTransaction

public void startTransaction()
                      throws SQLException
Description copied from interface: Storage
Starts a transaction

Specified by:
startTransaction in interface Storage
SQLException

commit

public void commit()
            throws SQLException
Description copied from interface: Storage
Commit started transaction

Specified by:
commit in interface Storage
SQLException

rollback

public void rollback()
              throws SQLException
Description copied from interface: Storage
Rollback started transaction

Specified by:
rollback in interface Storage
SQLException

checkStorage

protected void checkStorage()
Throw an exception if storage is null or invalid


createObject

public OID createObject(String className)
                 throws Exception
Creates a new object using a PostgreSQL sequance.

Specified by:
createObject in interface Storage
Parameters:
className - a String value
Returns:
the new object OID
Throws:
Exception - if an error occurs

getNextVal

public abstract long getNextVal(String sequence)
                         throws Exception
Returns the next value of a sequence

Exception

getLong

public long getLong(String query)
             throws Exception
Exception

getOID

public OID getOID(String query)
           throws Exception
Exception

addSlashes

public static String addSlashes(String str)

Contact JAC development team:
Renaud Pawlak
Lionel Seinturier
Laurent Martelli