public class ObjectsDB extends Object implements Serializable
Constructor and Description |
---|
ObjectsDB(String folder,
String dbName,
boolean deleteOldDatabase,
ObjectsCache objectsCache)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addTable(String tableName)
Adds the desired table in the database.
|
void |
close()
Closes the db connection.
|
String |
correctTableName(String tableName)
Surrounds the table name with quotation marks such that spaces etc are
allowed.
|
void |
deleteObject(String tableName,
String objectKey)
Deletes an object from the desired table.
|
void |
establishConnection(String aDbFolder,
boolean deleteOldDatabase,
ObjectsCache objectsCache)
Establishes connection to the database.
|
String |
getName()
Returns the database name.
|
ObjectsCache |
getObjectsCache()
Returns the cache used by this database.
|
boolean |
inDB(String tableName,
String objectKey,
boolean cache)
Indicates whether an object is loaded in the given table.
|
void |
insertObject(String tableName,
String objectKey,
Object object,
boolean inCache)
Stores an object in the desired table.
|
void |
insertObjects(String tableName,
HashMap<String,Object> objects,
WaitingHandler waitingHandler)
Inserts a set of objects in the given table.
|
void |
loadObjects(String tableName,
ArrayList<String> keys,
WaitingHandler waitingHandler)
Loads some objects from a table in the cache.
|
void |
loadObjects(String tableName,
WaitingHandler waitingHandler)
Loads all objects from a table in the cache.
|
Object |
retrieveObject(String tableName,
String objectKey,
boolean useDB)
Retrieves an object from the desired table.
|
void |
setObjectCache(ObjectsCache objectCache)
Sets the object cache to be used by this database.
|
ArrayList<String> |
tableContent(String tableName)
Returns an arraylist with the content in the given table.
|
void |
updateObject(String tableName,
String objectKey,
Object object)
Updates an object in the cache or in the tables if not in cache.
|
void |
updateObject(String tableName,
String objectKey,
Object object,
boolean cache)
Updates an object in the cache or in the tables if not in cache or if
cache is wrong.
|
public ObjectsDB(String folder, String dbName, boolean deleteOldDatabase, ObjectsCache objectsCache) throws SQLException
folder
- absolute path of the folder where to establish the databasedbName
- name of the databasedeleteOldDatabase
- if true, tries to delete the old databaseobjectsCache
- SQLException
public String getName()
public ObjectsCache getObjectsCache()
public void setObjectCache(ObjectsCache objectCache)
objectCache
- the object cache to be used by this databasepublic void addTable(String tableName) throws SQLException
tableName
- the name of the tableSQLException
- exception thrown whenever a problem occurred while
working with the databasepublic void insertObject(String tableName, String objectKey, Object object, boolean inCache) throws SQLException, IOException
tableName
- the name of the tableobjectKey
- the key of the objectobject
- the object to storeinCache
- boolean indicating whether the method shall try to put the
object in cache or notSQLException
- exception thrown whenever an error occurred while
storing the objectIOException
- exception thrown whenever an error occurred while
writing in the databasepublic void insertObjects(String tableName, HashMap<String,Object> objects, WaitingHandler waitingHandler) throws SQLException, IOException
tableName
- the name of the tableobjects
- map of the objects (object key -> object)waitingHandler
- a waiting handler displaying the progress (can be
null). The progress will be displayed on the secondary progress bar.SQLException
IOException
public void loadObjects(String tableName, WaitingHandler waitingHandler) throws SQLException, IOException, ClassNotFoundException, InterruptedException
tableName
- the table namewaitingHandler
- the waiting handlerSQLException
- exception thrown whenever an error occurred while
interrogating the databaseIOException
- exception thrown whenever an error occurred while
reading the databaseClassNotFoundException
- exception thrown whenever the class of the
object is not found when deserializing it.InterruptedException
public void loadObjects(String tableName, ArrayList<String> keys, WaitingHandler waitingHandler) throws SQLException, IOException, ClassNotFoundException, InterruptedException
tableName
- the table namekeys
- the keys of the objects to loadwaitingHandler
- the waiting handler, will only be increasedSQLException
- exception thrown whenever an error occurred while
interrogating the databaseIOException
- exception thrown whenever an error occurred while
reading the databaseClassNotFoundException
- exception thrown whenever the class of the
object is not found when deserializing it.InterruptedException
public Object retrieveObject(String tableName, String objectKey, boolean useDB) throws SQLException, IOException, ClassNotFoundException
tableName
- the name of the tableobjectKey
- the object keyuseDB
- if useDB is false, null will be returned if the object is
not in the cacheSQLException
- exception thrown whenever an error occurred while
interrogating the databaseIOException
- exception thrown whenever an error occurred while
reading the databaseClassNotFoundException
- exception thrown whenever the class of the
object is not found when deserializing it.public boolean inDB(String tableName, String objectKey, boolean cache) throws SQLException
tableName
- the table nameobjectKey
- the object keycache
- a boolean indicating whether the cache should be searched as
wellSQLException
- exception thrown whenever an exception occurred
while interrogating the databasepublic ArrayList<String> tableContent(String tableName) throws SQLException
tableName
- the table to get the content forSQLException
public void deleteObject(String tableName, String objectKey) throws SQLException, IOException
tableName
- the name of the tableobjectKey
- the object keySQLException
- exception thrown whenever an error occurred while
interrogating the databaseIOException
- exception thrown whenever an error occurred while
interrogating the databasepublic void updateObject(String tableName, String objectKey, Object object) throws SQLException, IOException
tableName
- the name of the tableobjectKey
- the key of the objectobject
- the object to storeSQLException
- exception thrown whenever an error occurred while
storing the objectIOException
- exception thrown whenever an error occurred while
writing in the databasepublic void updateObject(String tableName, String objectKey, Object object, boolean cache) throws SQLException, IOException
tableName
- the name of the tableobjectKey
- the key of the objectobject
- the object to storecache
- a boolean indicating whether the method should look in the
cacheSQLException
- exception thrown whenever an error occurred while
storing the objectIOException
- exception thrown whenever an error occurred while
writing in the databasepublic void close() throws SQLException
SQLException
- exception thrown whenever an error occurred while
closing the database connectionpublic void establishConnection(String aDbFolder, boolean deleteOldDatabase, ObjectsCache objectsCache) throws SQLException
aDbFolder
- the folder where the database is locateddeleteOldDatabase
- if true, tries to delete the old databaseobjectsCache
- SQLException
- exception thrown whenever an error occurred while
establishing the connectionCopyright © 2013. All Rights Reserved.