com.sibvisions.rad.persist.jdbc
Class DBStorage

java.lang.Object
  extended by com.sibvisions.rad.persist.jdbc.DBStorage
All Implemented Interfaces:
IStorage

public class DBStorage
extends Object
implements IStorage

The DBStorage is a IStorage for SQL database specific features.

The DBStorage allows to influence the creation of the SELECT statement to get the data.
The following template shows how the SELECT will be constructed out of the specified properties.

 
 SELECT getBeforeQueryColumns() getQueryColumns()
 FROM   getFromClause()
 WHERE  getFilter()
 AND    getMasterReference()
 AND    getWhereClause()
 getAfterWhereClause()
 ORDER BY getSort()
 
 
Example:
  
 SELECT DISTINCT // ++ comment, optimizer hints 
        a.COL1 C, b.COL2 D, a.FK_ID FK_ID, ...
 FROM   TABLE1 a,
        TABLE2 b,
        ...
 WHERE  C LIKE 'a%' AND D IS NOT NULL ... // getFilter().getSQL
 AND    FK_ID = 23                        // getMasterReference() get all detail rows to a 
                                             specific master row in an other DataBook
 AND    a.ID = b.FK_ID ...                // getLastWhereCondition() * GROUP BY C, D                            // getAfterWhereClause()
 ORDER BY C DESC                                                        // getSort()
 
 

See Also:
IStorage, ColumnMetaData, RemoteDataBook, DBAccess

Constructor Summary
DBStorage()
          Constructs a DBStorage.
 
Method Summary
 void close()
          It close the DBStorage.
 int countRows(ICondition pFilter)
          Returns the number of rows in this AbstractStorage from the storage.
 IFileHandle createCSVFile(ColumnMetaData[] pClientColumnMetaData, ICondition pFilter, SortDefinition pSort)
          Write the current DBStorage with the specified filter and sort to the export.csv file in CSV format and returns the file handle.
 IFileHandle createCSVFile(String pFilename, ColumnMetaData[] pClientColumnMetaData, ICondition pFilter, SortDefinition pSort)
          Write the current DBStorage with the specified filter and sort to the export.csv file in CSV format and returns the file handle.
 IBean createEmptyBean()
          Creates a bean with the column names as allowed properties.
 void delete(Object[] pDeleteDataRow)
          Deletes the specified row from the storage.
 void deleteBean(IBean pDeleteDataRow)
          Deletes the specified row from the Storage.
 List<Object[]> fetch(ICondition pFilter, SortDefinition pSort, int pFromRow, int pMinimumRowCount)
          Returns the requested rows as List[Object[]].
 IBean fetchBean(ICondition pFilter)
          Returns the requested row as single IBean.
 List<IBean> fetchBean(ICondition pFilter, SortDefinition pSort, int pFromRow, int pMinimumRowCount)
          Returns the requested rows as list of IBean.
 String getAfterWhereClause()
          Returns the string to place in the SELECT statement after the WHERE clause and before the ORDER BY clause.
 String getBeforeQueryColumns()
          Returns the string to place in the SELECT statement between the SELECT and the first query column.
 String[] getColumnNames()
          Gets an array of column names as defined in the metadata.
 IDBAccess getDBAccess()
          Returns the IDBAccess of this DBStorage .
 SortDefinition getDefaultSort()
          Returns the default sort.
 String getFromClause()
          Returns the query tables to use in the SELECT statement to get the data from the storage.
 MetaData getMetaData()
          Returns the meta data for this AbstractStorage from the storage as MetaData.
static Hashtable<String,MetaData> getMetaData(String pLifeCycleName)
          Gets the cached column meta data for a specific life-cycle object.
 String[] getQueryColumns()
          Returns the query columns of the SELECT statement.
 ICondition getRestrictCondition()
          Returns the restrict condition.
 Map<String,IStorage> getSubStorages()
          Gets all known sub storages as key / value pair.
 String getWhereClause()
          Returns the string to place in the SELECT statement after the last WHERE condition from the Filter or MasterReference (Master-Detail Condition).
 String[] getWritebackColumns()
          Returns the list of write back columns to use in the INSERT, UPDATE statements.
 String getWritebackTable()
          Returns the WritebackTable, which is used for the INSERT, UPDATE, DELETE calls.
 Object[] insert(Object[] pDataRow)
          Returns the newly inserted row for this IStorage.
 IBean insertBean(IBean pDataRow)
          Returns the newly inserted row for this Storage.
 boolean isAutoLinkReference()
          Returns if the automatic LinkReference mode is on or off.
static boolean isDefaultAutoLinkReference()
          Returns the default automatic LinkReference mode.
 boolean isOpen()
          Returns if the DBStorage is open.
 boolean isRefetch()
          Returns if this DBStorage refetchs after insert and update.
 void open()
          Opens the DBStorage and checks if the StorageDataSource is !
protected  void openInternal(boolean pUseRepresentationColumnsAsQueryColumns)
          Opens the DBStorage and checks if the StorageDataSource is !
 Object[] refetchRow(Object[] pDataRow)
          It refetchs the specified row and returns it as Object[] from the storage.
protected  Object[] refetchRow(Object[] pDataRow, boolean pLock)
          Refetch and optional locks the specified DataRow via PK.
 IBean refetchRowBean(IBean pDataRow)
          It refetchs the specified row and returns it as IBean from the Storage.
 void setAfterWhereClause(String pAfterWhereClause)
          Sets the string to place in the SELECT statement after the WHERE clause and before the ORDER BY clause.
 void setAutoLinkReference(boolean pAutoLinkReference)
          Sets if the automatic LinkReference mode is on/off or default.
 void setBeforeQueryColumns(String pBeforeQueryColumns)
          Sets the string to place in the SELECT statement between the SELECT and the first query column.
 void setDBAccess(IDBAccess pDBAccess)
          Set the IDBAccess of this DBStorage .
static void setDefaultAutoLinkReference(boolean pDefaultAutoLinkReference)
          Sets the default automatic LinkReference mode.
 void setDefaultSort(SortDefinition pDefaultSort)
          Sets the default sort.
 void setFromClause(String pFromClause)
          Sets the query tables to use in the SELECT statement to get the data from the storage.
 void setQueryColumns(String[] pQueryColumns)
          Sets the query columns of the SELECT statement.
 void setRefetch(boolean pRefetch)
          Sets if this DBStorage refetchs after insert and update.
 void setRestrictCondition(ICondition pRestrictCondition)
          Sets the restrict condition.
 void setWhereClause(String pWhereClause)
          Sets the string to place in the SELECT statement after the last WHERE condition from the Filter or MasterReference (Master-Detail Condition).
 void setWritebackColumns(String[] pWritebackColumns)
          Sets the list of write back columns to use in the INSERT, UPDATE statements.
 void setWritebackTable(String pWritebackTable)
          Sets the WritebackTable, which is used for the INSERT, UPDATE, DELETE calls.
 String toString()
          
 Object[] update(Object[] pOldDataRow, Object[] pNewDataRow)
          Return the updated row as Object[].
 IBean updateBean(IBean pNewDataRow)
          Updates a row with provided values and returns the updated row as IBean.
 void writeCSV(OutputStream pOutputStream, ColumnMetaData[] pClientColumnMetaData, ICondition pFilter, SortDefinition pSort)
          Write the current DBStorage with the specified filter and sort into the given output stream as CSV Format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DBStorage

public DBStorage()
Constructs a DBStorage.

Method Detail

isDefaultAutoLinkReference

public static boolean isDefaultAutoLinkReference()
Returns the default automatic LinkReference mode.

Returns:
the default automatic LinkReference mode.

setDefaultAutoLinkReference

public static void setDefaultAutoLinkReference(boolean pDefaultAutoLinkReference)
Sets the default automatic LinkReference mode.

Parameters:
pDefaultAutoLinkReference - the default automatic LinkReference mode to use.

getMetaData

public MetaData getMetaData()
                     throws DataSourceException
Returns the meta data for this AbstractStorage from the storage as MetaData.

Specified by:
getMetaData in interface IStorage
Returns:
the meta data for this AbstractStorage from the storage as MetaData.
Throws:
DataSourceException - if an Exception occur during getting the meta data from the storage

countRows

public int countRows(ICondition pFilter)
              throws DataSourceException
Returns the number of rows in this AbstractStorage from the storage.
It consider the specified ICondition to count the rows.

Specified by:
countRows in interface IStorage
Parameters:
pFilter - the ICondition to use.
Returns:
the number of rows in this AbstractStorage from the storage.
Throws:
DataSourceException - if an Exception occur during interacting with the storage.

fetch

public List<Object[]> fetch(ICondition pFilter,
                            SortDefinition pSort,
                            int pFromRow,
                            int pMinimumRowCount)
                     throws DataSourceException
Returns the requested rows as List[Object[]]. Optimization can also return more then the minimum row count. If possible all in a certain time.

Specified by:
fetch in interface IStorage
Parameters:
pFilter - the ICondition to use
pSort - the SortDefinition to use
pFromRow - the from row index to request from storage
pMinimumRowCount - the minimum row count to request, beginning from the pFromRow.
Returns:
the requested rows as List[Object[]].
Throws:
DataSourceException - if an Exception occur during interacting with the storage.

refetchRow

public Object[] refetchRow(Object[] pDataRow)
                    throws DataSourceException
It refetchs the specified row and returns it as Object[] from the storage.
Before the user starts editing in the GUI, the IDataRow in the storage should be locked for updates.

Specified by:
refetchRow in interface IStorage
Parameters:
pDataRow - the specified row as Object[] to lock
Returns:
It refetchs the specified row and returns it as Object[] from the storage.
Throws:
DataSourceException - if an Exception occur during interacting with the storage.

insert

public Object[] insert(Object[] pDataRow)
                throws DataSourceException
Returns the newly inserted row for this IStorage.

Specified by:
insert in interface IStorage
Parameters:
pDataRow - the new row as Object[] to insert.
Returns:
the newly inserted row from this IStorage.
Throws:
DataSourceException - if an Exception occur during insert the row to the storage

update

public Object[] update(Object[] pOldDataRow,
                       Object[] pNewDataRow)
                throws DataSourceException
Return the updated row as Object[].

Specified by:
update in interface IStorage
Parameters:
pOldDataRow - the old row as Object[]
pNewDataRow - the new row as Object[] to update
Returns:
the updated row as Object[].
Throws:
DataSourceException - if an Exception occur during updating the row.

delete

public void delete(Object[] pDeleteDataRow)
            throws DataSourceException
Deletes the specified row from the storage.

Specified by:
delete in interface IStorage
Parameters:
pDeleteDataRow - the row as Object[] to delete
Throws:
DataSourceException - if an Exception occur during deleting the row or if the storage isn't opened or the PrimaryKey is wrong and more/less then one row is deleted

toString

public String toString()

Overrides:
toString in class Object

open

public void open()
          throws DataSourceException
Opens the DBStorage and checks if the StorageDataSource is != null. If automatic link celleditors are used, then the implicit link DBStorge objects are put with "detailtablename" as sub storage.

Throws:
DataSourceException - if the StorageDataSource is null.

openInternal

protected void openInternal(boolean pUseRepresentationColumnsAsQueryColumns)
                     throws DataSourceException
Opens the DBStorage and checks if the StorageDataSource is != null. It doesn't cache the MetaData!

Parameters:
pUseRepresentationColumnsAsQueryColumns - yes if the QueryColumns are set with all representation columns including the Primary Key columns.
Throws:
DataSourceException - if the StorageDataSource is null.

isOpen

public boolean isOpen()
Returns if the DBStorage is open.

Returns:
true if the DBStorage is open.

close

public void close()
It close the DBStorage.


setDBAccess

public void setDBAccess(IDBAccess pDBAccess)
Set the IDBAccess of this DBStorage .

Parameters:
pDBAccess - the IDBAccess of this DBStorage .

getDBAccess

public IDBAccess getDBAccess()
Returns the IDBAccess of this DBStorage .

Returns:
the IDBAccess of this DBStorage .

getFromClause

public String getFromClause()
Returns the query tables to use in the SELECT statement to get the data from the storage.

Returns:
the query tables to use in the SELECT statement to get the data from the storage.

setFromClause

public void setFromClause(String pFromClause)
Sets the query tables to use in the SELECT statement to get the data from the storage.

Parameters:
pFromClause - the queryTables to set.

getQueryColumns

public String[] getQueryColumns()
Returns the query columns of the SELECT statement.

Returns:
the query columns of the SELECT statement.

setQueryColumns

public void setQueryColumns(String[] pQueryColumns)
Sets the query columns of the SELECT statement.

Parameters:
pQueryColumns - the queryColumns to set

getBeforeQueryColumns

public String getBeforeQueryColumns()
Returns the string to place in the SELECT statement between the SELECT and the first query column.

Returns:
the string to place in the SELECT statement between the SELECT and the first query column.

setBeforeQueryColumns

public void setBeforeQueryColumns(String pBeforeQueryColumns)
Sets the string to place in the SELECT statement between the SELECT and the first query column.

Parameters:
pBeforeQueryColumns - the beforeQueryColumns to set

getWhereClause

public String getWhereClause()
Returns the string to place in the SELECT statement after the last WHERE condition from the Filter or MasterReference (Master-Detail Condition).

Returns:
the string to place in the SELECT statement after the last WHERE condition from the Filter or MasterReference (Master-Detail Condition).

setWhereClause

public void setWhereClause(String pWhereClause)
Sets the string to place in the SELECT statement after the last WHERE condition from the Filter or MasterReference (Master-Detail Condition).

Parameters:
pWhereClause - the last Where Condition to set

getAfterWhereClause

public String getAfterWhereClause()
Returns the string to place in the SELECT statement after the WHERE clause and before the ORDER BY clause.

Returns:
the string to place in the SELECT statement after the WHERE clause and before the ORDER BY clause.

setAfterWhereClause

public void setAfterWhereClause(String pAfterWhereClause)
Sets the string to place in the SELECT statement after the WHERE clause and before the ORDER BY clause.

Parameters:
pAfterWhereClause - the afterWhereClause to set

getWritebackColumns

public String[] getWritebackColumns()
Returns the list of write back columns to use in the INSERT, UPDATE statements.

Returns:
the saWritebackColumns.

setWritebackColumns

public void setWritebackColumns(String[] pWritebackColumns)
Sets the list of write back columns to use in the INSERT, UPDATE statements.

Parameters:
pWritebackColumns - the pWritebackColumns to set

getWritebackTable

public String getWritebackTable()
Returns the WritebackTable, which is used for the INSERT, UPDATE, DELETE calls.

Returns:
the sWritebackTable.

setWritebackTable

public void setWritebackTable(String pWritebackTable)
Sets the WritebackTable, which is used for the INSERT, UPDATE, DELETE calls.

Parameters:
pWritebackTable - the pWritebackTable to set

getDefaultSort

public SortDefinition getDefaultSort()
Returns the default sort. It is used, if no other sort is defined.

Returns:
the default sort.

setDefaultSort

public void setDefaultSort(SortDefinition pDefaultSort)
Sets the default sort. It is used, if no other sort is defined.

Parameters:
pDefaultSort - the default sort.

getRestrictCondition

public ICondition getRestrictCondition()
Returns the restrict condition. It is always added with And to any given condition.

Returns:
the restrict condition.

setRestrictCondition

public void setRestrictCondition(ICondition pRestrictCondition)
Sets the restrict condition. It is always added with And to any given condition.

Parameters:
pRestrictCondition - the restrict condition.

isRefetch

public boolean isRefetch()
Returns if this DBStorage refetchs after insert and update.

Returns:
if this DBStorage refetchs after insert and update.

setRefetch

public void setRefetch(boolean pRefetch)
Sets if this DBStorage refetchs after insert and update.

Parameters:
pRefetch - true if this DBStorage refetchs after insert and update.

isAutoLinkReference

public boolean isAutoLinkReference()
Returns if the automatic LinkReference mode is on or off.

Returns:
trueif the automatic LinkReference mode is on, otherwise false

setAutoLinkReference

public void setAutoLinkReference(boolean pAutoLinkReference)
Sets if the automatic LinkReference mode is on/off or default.

Parameters:
pAutoLinkReference - true if the automatic LinkReference mode is on, false to disable auto link reference mode

createCSVFile

public IFileHandle createCSVFile(ColumnMetaData[] pClientColumnMetaData,
                                 ICondition pFilter,
                                 SortDefinition pSort)
                          throws Exception
Write the current DBStorage with the specified filter and sort to the export.csv file in CSV format and returns the file handle. The filename will be built depending of the object name from the SessionContext.

Parameters:
pClientColumnMetaData - the the client column meta data for the column headers of the CSV file.
pFilter - the filter to use on the DBStorage
pSort - the sort to use
Returns:
the file handle for the new generated CSV file.
Throws:
Exception - if the CSV output couldn't written to stream.

createCSVFile

public IFileHandle createCSVFile(String pFilename,
                                 ColumnMetaData[] pClientColumnMetaData,
                                 ICondition pFilter,
                                 SortDefinition pSort)
                          throws Exception
Write the current DBStorage with the specified filter and sort to the export.csv file in CSV format and returns the file handle.

Parameters:
pFilename - the filename to use.
pClientColumnMetaData - the the client column meta data for the column headers of the CSV file.
pFilter - the filter to use on the DBStorage
pSort - the sort to use
Returns:
the file handle for the new generated CSV file.
Throws:
Exception - if the CSV output couldn't written to stream.

writeCSV

public void writeCSV(OutputStream pOutputStream,
                     ColumnMetaData[] pClientColumnMetaData,
                     ICondition pFilter,
                     SortDefinition pSort)
              throws Exception
Write the current DBStorage with the specified filter and sort into the given output stream as CSV Format.

Parameters:
pOutputStream - the output stream to use for the CSV stream.
pClientColumnMetaData - the column headers for the CSV file.
pFilter - the filter to use on the DBStorage
pSort - the sort to use
Throws:
Exception - if the CSV output couldn't written to stream.

refetchRow

protected Object[] refetchRow(Object[] pDataRow,
                              boolean pLock)
                       throws DataSourceException
Refetch and optional locks the specified DataRow via PK.

Parameters:
pDataRow - the DataRow to refetch
pLock - true if the row is locked before.
Returns:
the refetched DataRow
Throws:
DataSourceException - if the refetch fails

getColumnNames

public String[] getColumnNames()
Gets an array of column names as defined in the metadata.

Returns:
the column names as array
See Also:
getMetaData()

fetchBean

public List<IBean> fetchBean(ICondition pFilter,
                             SortDefinition pSort,
                             int pFromRow,
                             int pMinimumRowCount)
                      throws DataSourceException
Returns the requested rows as list of IBean.

Parameters:
pFilter - the ICondition to use
pSort - the SortDefinition to use
pFromRow - the from row index to request from storage
pMinimumRowCount - the minimum row count to request, beginning from the pFromRow.
Returns:
the requested rows as list of IBeans.
Throws:
DataSourceException - if an Exception occur during interacting with the storage.

fetchBean

public IBean fetchBean(ICondition pFilter)
                throws DataSourceException
Returns the requested row as single IBean.

Parameters:
pFilter - the ICondition to use
Returns:
the requested rows as IBeans, or null if no row was found
Throws:
DataSourceException - if an Exception occur during interacting with the storage or more than one row were found

refetchRowBean

public IBean refetchRowBean(IBean pDataRow)
                     throws DataSourceException
It refetchs the specified row and returns it as IBean from the Storage. Before the user starts editing in the GUI, the IDataRow in the storage should be locked for updates.

Parameters:
pDataRow - the specified row as IBean to lock
Returns:
It refetchs the specified row and returns it as IBean from the Storage.
Throws:
DataSourceException - if an Exception occur during interacting with the Storage.

insertBean

public IBean insertBean(IBean pDataRow)
                 throws DataSourceException
Returns the newly inserted row for this Storage.

Parameters:
pDataRow - the new row as IBean to insert.
Returns:
the newly inserted row from this Storage.
Throws:
DataSourceException - if an Exception occur during insert the row to the storage

updateBean

public IBean updateBean(IBean pNewDataRow)
                 throws DataSourceException
Updates a row with provided values and returns the updated row as IBean.

Parameters:
pNewDataRow - the new row as IBean to update
Returns:
the updated row as IBean
Throws:
DataSourceException - if an Exception occur during updating the row

deleteBean

public void deleteBean(IBean pDeleteDataRow)
                throws DataSourceException
Deletes the specified row from the Storage.

Parameters:
pDeleteDataRow - the row as IBean to delete
Throws:
DataSourceException - if an Exception occur during deleting the row or if the storage isn't opened or the PrimaryKey is wrong and more/less then one row is deleted

createEmptyBean

public IBean createEmptyBean()
Creates a bean with the column names as allowed properties. Only properties with the column names are allowed to use.

Returns:
a new instance of an IBean implementation

getMetaData

public static Hashtable<String,MetaData> getMetaData(String pLifeCycleName)
Gets the cached column meta data for a specific life-cycle object.

Parameters:
pLifeCycleName - the name of the life-cycle object
Returns:
a Hashtable with column meta data, mapped with a specific name or null if there are no cached column meta data for the pLifeCycleName

getSubStorages

public Map<String,IStorage> getSubStorages()
Gets all known sub storages as key / value pair.

Returns:
the key / value pair with subtablename and substorage or null if no substorages are known


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.