|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sibvisions.rad.persist.jdbc.DBAccess
public class DBAccess
The DBAccess is the implementation for most used SQL databases.
Standard ANSI SQL Databases are anyways supported.
Its used to read/write data between the storage and the DataBook/DataPage.
It has database type specific implementations.
Example:
DBAccess dba = new DBAccess();
// set connect properties
dba.setDriver("org.hsqldb.jdbcDriver");
dba.setUrl("jdbc:hsqldb:file:testdbs/test/testdb");
dba.setUsername("sa");
dba.setPassword("");
Properties pDBProperties = new Properties();
pDBProperties.put("shutdown", true);
dba.setDBProperties(pDBProperties);
// open
dba.open();
To get Database independent DBAccess, it is better to use:
DBAccess dba = DBAccess.getDBAccess("jdbc:hsqldb:file:testdbs/test/testdb");
// insert data into test table
PreparedStatement psPreparedStatement = dba.getPreparedStatement(
"insert into test (id, name) values(?,?)", false);
psPreparedStatement.setInt(1, 1);
psPreparedStatement.setString(2, "projectX");
dba.executeUpdate(psPreparedStatement);
// select data from test table
psPreparedStatement = dba.getPreparedStatement("select * from test", false);
ResultSet rs = dba.executeQuery(psPreparedStatement);
while (rs.next())
{
System.out.println(rs.getInt("id") + "," + rs.getString("name"));
}
IDBAccess,
RemoteDataBook| Field Summary | |
|---|---|
protected static ILogger |
logger
The logger for protocol the performance. |
static int |
LONGNVARCHAR
A jdbc VARCHAR DB column data type constant. |
static int |
NCHAR
A jdbc VARCHAR DB column data type constant. |
static int |
NCLOB
A jdbc BLOB DB column data type constant. |
static int |
NVARCHAR
A jdbc VARCHAR DB column data type constant. |
static int |
SQLXML
A jdbc XML DB column data type constant. |
| Constructor Summary | |
|---|---|
DBAccess()
Constructs a new DBAccess Object. |
|
| Method Summary | |
|---|---|
void |
close()
Closes the database Connection and releases all memory. |
void |
commit()
Commits the DB transaction. |
void |
delete(String pWritebackTable,
MetaData pMetaData,
Object[] pDelete)
Deletes the specified row. |
Object |
executeFunction(String pStatement,
Object... pParameters)
Executes a DB function with the specified parameters and return the result. |
void |
executeProcedure(String pStatement,
Object... pParameters)
Executes a DB procedure with the specified parameters. |
int |
executeUpdate(PreparedStatement pSqlStatement)
Calls executesUpdate() for specified PreparedStatement
and returns the number of updated rows. |
List<Object[]> |
fetch(MetaData pMetaData,
String pBeforeQueryColumns,
String[] pQueryColumns,
String pFromClause,
ICondition pFilter,
String pLastWhereCondition,
String pAfterWhereClause,
SortDefinition pSort,
int pFromRow,
int pMinimumRowCount)
Returns the List of fetched rows (as List of Object[]) for the specified query tables and parameters. |
protected String |
getAutomaticLinkColName(ForeignKey pForeignKey)
Returns the automatic link column name for the specified ForeignKey and FK Column in the master table. |
ColumnMetaData[] |
getColumnMetaData(String pFromClause,
String[] pQueryColumns,
String pBeforeQueryColumns,
String pWhereClause,
String pAfterWhereClause,
String pWritebackTable,
String[] pWritebackColumns,
String[] pMetaDataContext)
Returns the meta data for the specified query, and initials all columns. |
Connection |
getConnection()
Returns the SQL Conncetion to the database. |
String |
getDatabaseSpecificLockStatement(String pWritebackTable,
MetaData pMetaData,
ICondition pPKFilter)
Returns the database specific statement to lock the specified row in the database. |
static DBAccess |
getDBAccess(String pJdbcUrl)
Gets the suitable DBAcces for the given JdbcUrl. |
Properties |
getDBProperties()
Returns the DB specific initial parameters for the Connection creation. |
String |
getDBProperty(String pName)
Gets the value for a specific database property. |
String |
getDriver()
Gets the database driver name as String. |
List<ForeignKey> |
getFKs(String pCatalog,
String pSchema,
String pTable)
Returns all Foreign Keys for the specified table. |
protected String |
getFromClause(String pWritebackTable,
List<ForeignKey> auForeignKeys)
It creates and returns a from clause with joins over all foreign tables. |
int |
getMaxTime()
Returns the maximum time in miliseconds to use, to try to fetch all rows. reduce open cursors, and increase performance. |
String |
getPassword()
Sets the password to use for the connection to the database. |
String[] |
getPK(String pCatalog,
String pSchema,
String pTable)
It's gets all Primary Key columns and return it as String[]. |
PreparedStatement |
getPreparedStatement(String pSqlStatement,
boolean pReturnKey)
Return a PreparedStatement for the given SQL statement. |
protected String[] |
getQueryColumns(String pWritebackTable,
List<ForeignKey> auForeignKeys,
String[] auWritebackColumns)
It creates and returns the query columns, with all write back columns and includs the columns for the LinkReferences (automatic link celleditors). |
String |
getSelectStatement(ICondition pFilter,
String sFromClause,
String[] saQueryColumns,
String sBeforeQueryColumns,
String sWhereClause,
String sAfterWhereClause,
MetaData pMetaData)
It initialize the select for a specified storage unit and return the SELECT statement. |
List<String[]> |
getUKs(String pCatalog,
String pSchema,
String pTable)
It's gets all columns for each Unique Key and return it. |
String |
getUrl()
Gets the jdbc url String for this database. |
String |
getUsername()
Gets the user name to connect with. |
String |
getWHEREClause(ICondition pFilter,
MetaData pMetaData,
boolean pUsePrefix)
Returns the WHERE clause for a UPDATE, DELETE or SELECT statement specified with a Filter. |
Object[] |
insert(String pWritebackTable,
MetaData pMetaData,
Object[] pNewDataRow)
Returns the newly inserted row from the write back table. |
Object[] |
insertAnsiSQL(String pWritebackTable,
String pInsertStatement,
MetaData pMetaData,
Object[] pNewDataRow,
String pDummyColumn)
Returns the newly inserted row from a Ansi SQL Database. |
Object[] |
insertDatabaseSpecific(String pWritebackTable,
String pInsertStatement,
MetaData pMetaData,
Object[] pNewDataRow,
String pDummyColumn)
Returns the newly inserted row from an Database specific insert statement. |
boolean |
isOpen()
Returns true, if the database is still open. |
void |
lockRow(String pWritebackTable,
MetaData pMetaData,
ICondition pPKFilter)
It locks the specified row in the storage. |
void |
open()
It opens the database and stores the Connection object. |
String |
quote(Object pObject)
Quotes an Object. |
static void |
registerDBAccessClass(String pJdbcUrlPrefix,
Class<? extends DBAccess> pClass)
Registers the Class for the jdbc url prefix. |
void |
rollback()
Rollback the DB transaction. |
protected int |
setColumnsToStore(PreparedStatement pInsert,
ColumnMetaData[] pColumnMetaData,
int[] iaWriteables,
Object[] pNew,
Object[] pOld)
Sets the values of all changed columns to store from the value Object[]s into the PreparedStatement and returns the last used parameter index. |
void |
setDBProperties(Properties pProperties)
Sets DB specific initial parameters for the Connection creation. |
void |
setDBProperty(String pName,
String pValue)
Sets a specific database property. |
void |
setDriver(String pDriver)
Sets the database driver name as String. |
protected void |
setFilterParameter(int iStartParameterIndex,
PreparedStatement pStatement,
ICondition pFilter)
Sets all Filter parameter values into the PreparedStatement. |
void |
setMaxTime(int pMaxTime)
Sets the maximum time in miliseconds to use, to try to fetch all rows. reduce open cursors, and increase performance. |
void |
setPassword(String pPassword)
Sets the password to use for the connection to the database. |
void |
setQueryTimeOut(int pQueryTimeOut)
Sets the query time out. |
void |
setUrl(String pUrl)
Sets the url String for this database. |
void |
setUsername(String pUsername)
Sets the user name to connect with. |
boolean |
supportsGetGeneratedKeys()
Returns if this Database specific supports generated keys. |
String |
toString()
|
Object[] |
update(String pWritebackTable,
MetaData pMetaData,
Object[] pOld,
Object[] pNew)
Return the updated row. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int NCHAR
public static final int NVARCHAR
public static final int LONGNVARCHAR
public static final int SQLXML
public static final int NCLOB
protected static ILogger logger
| Constructor Detail |
|---|
public DBAccess()
| Method Detail |
|---|
public static DBAccess getDBAccess(String pJdbcUrl)
throws InstantiationException,
IllegalAccessException
pJdbcUrl - the JdbcUrl
IllegalAccessException - if the DBAccess instance is not allowed.
InstantiationException - if the DBAccess instance cannot be created.
public static void registerDBAccessClass(String pJdbcUrlPrefix,
Class<? extends DBAccess> pClass)
pJdbcUrlPrefix - the jdbc url prefix.pClass - the Class.
protected String getFromClause(String pWritebackTable,
List<ForeignKey> auForeignKeys)
throws DataSourceException
pWritebackTable - the write back table to useauForeignKeys - the ForeignKey array to use
DataSourceException - if an error occur in determining the from clause and query columns.protected String getAutomaticLinkColName(ForeignKey pForeignKey)
pForeignKey - the ForeignKEy to use.
protected String[] getQueryColumns(String pWritebackTable,
List<ForeignKey> auForeignKeys,
String[] auWritebackColumns)
throws DataSourceException
pWritebackTable - the write back table to useauForeignKeys - the ForeignKey array to useauWritebackColumns - the write back table columns to use.
DataSourceException - if an error occur in determining the from clause and query columns.
public List<String[]> getUKs(String pCatalog,
String pSchema,
String pTable)
throws DataSourceException
pCatalog - the catalog to usepSchema - the schema to usepTable - the table to use
DataSourceException - if an error occur during UK search process.
public String[] getPK(String pCatalog,
String pSchema,
String pTable)
throws DataSourceException
pCatalog - the catalog to usepSchema - the schema to usepTable - the table to use
DataSourceException - if an error occur during PK search process.
public List<ForeignKey> getFKs(String pCatalog,
String pSchema,
String pTable)
throws DataSourceException
pCatalog - the catalog to usepSchema - the schema to usepTable - the table to use as base table.
DataSourceException - if an error occur in determining the ForeignKeys.
public List<Object[]> fetch(MetaData pMetaData,
String pBeforeQueryColumns,
String[] pQueryColumns,
String pFromClause,
ICondition pFilter,
String pLastWhereCondition,
String pAfterWhereClause,
SortDefinition pSort,
int pFromRow,
int pMinimumRowCount)
throws DataSourceException
fetch in interface IDBAccesspMetaData - the MetaDataColumn array to use.pBeforeQueryColumns - the before query columnspQueryColumns - the query columnspFromClause - the from clause with query tables and join definitionspFilter - the filter to usepLastWhereCondition - the last where condition in querypAfterWhereClause - the after where clause in querypSort - the sort order to usepFromRow - the row index from to fetchpMinimumRowCount - the minimum count row to fetch
DataSourceException - if the fetch fails.
public void lockRow(String pWritebackTable,
MetaData pMetaData,
ICondition pPKFilter)
throws DataSourceException
lockRow in interface IDBAccesspWritebackTable - the storage unit to usepMetaData - the MetaDataColumn array to use.pPKFilter - the PrimaryKey in as an ICondition to identify the row to lock
DataSourceException - if an Exception occur during interacting with the storage
public Object[] insert(String pWritebackTable,
MetaData pMetaData,
Object[] pNewDataRow)
throws DataSourceException
insert in interface IDBAccesspWritebackTable - the write back table to use.pMetaData - the meta data to use.pNewDataRow - the new values Object[] to insert.
DataSourceException - if an Exception occur during insert the row to the table
public Object[] update(String pWritebackTable,
MetaData pMetaData,
Object[] pOld,
Object[] pNew)
throws DataSourceException
update in interface IDBAccesspWritebackTable - the write back table to use.pMetaData - the meta data to use.pOld - the old values of the rowpNew - the new values of the row
DataSourceException - if an Exception occur during update.
public void delete(String pWritebackTable,
MetaData pMetaData,
Object[] pDelete)
throws DataSourceException
delete in interface IDBAccesspWritebackTable - the write back table to use.pMetaData - the meta data to use.pDelete - the row to delete.
DataSourceException - if an Exception occur during delete.public String toString()
toString in class Object
public void open()
throws DataSourceException
Connection object.
DataSourceException - if the database couldn't opened
public boolean isOpen()
throws DataSourceException
DataSourceException - if isClosed() on the DB Connection throws an Exception
public void close()
throws DataSourceException
Connection and releases all memory.
DataSourceException - if database couldn't closed.
public void rollback()
throws DataSourceException
DataSourceException - if the transaction couldn't rollback
public void commit()
throws DataSourceException
DataSourceException - if the transaction couldn't commitpublic void setQueryTimeOut(int pQueryTimeOut)
pQueryTimeOut - the timeout in milisecondspublic Connection getConnection()
Conncetion to the database.
Conncetion to the database.public String getDriver()
String.
public void setDriver(String pDriver)
String.
pDriver - the database driver namepublic String getUrl()
String for this database.
String.public void setUrl(String pUrl)
String for this database.
pUrl - the jdbc url String.public String getUsername()
public void setUsername(String pUsername)
pUsername - the user namepublic String getPassword()
public void setPassword(String pPassword)
pPassword - the password to use for the database
public void setDBProperty(String pName,
String pValue)
pName - the property namepValue - th valuepublic String getDBProperty(String pName)
pName - the property name
null if the property was not foundpublic void setDBProperties(Properties pProperties)
Connection creation.
pProperties - DB specific initial parameterspublic Properties getDBProperties()
Connection creation.
Connection creation.public int getMaxTime()
public void setMaxTime(int pMaxTime)
pMaxTime - the iMaxTime to setpublic String quote(Object pObject)
pObject - the object to quote.
public void executeProcedure(String pStatement,
Object... pParameters)
throws DataSourceException
pStatement - the procedure (option with package) name.pParameters - the parameters to use with the correct and corresponding java type.
DataSourceException - if the call failed.
public Object executeFunction(String pStatement,
Object... pParameters)
throws DataSourceException
pStatement - the procedure (option with package) name.pParameters - the parameters to use with the correct and corresponding java type.
DataSourceException - if the call failed.
public PreparedStatement getPreparedStatement(String pSqlStatement,
boolean pReturnKey)
throws DataSourceException
PreparedStatement for the given SQL statement.
pSqlStatement - the SQL statement to preparepReturnKey - if the generated key in insert statements should returned
PreparedStatement for the given SQL statement.
DataSourceException - if the statement couldn't prepared.
public int executeUpdate(PreparedStatement pSqlStatement)
throws DataSourceException
executesUpdate() for specified PreparedStatement
and returns the number of updated rows.
pSqlStatement - the PreparedStatement to use
DataSourceException - if the statement couldn't updated.
public String getSelectStatement(ICondition pFilter,
String sFromClause,
String[] saQueryColumns,
String sBeforeQueryColumns,
String sWhereClause,
String sAfterWhereClause,
MetaData pMetaData)
throws DataSourceException
pFilter - the Filter to usesFromClause - the list of query tables to use in the SELECT statement.saQueryColumns - the list of query columns to use in the SELECT statement.sBeforeQueryColumns - the string to place in the SELECT statement between the SELECT and the first query column.sWhereClause - the string to place in the SELECT statement after the last WHERE condition from the
Filter or MasterReference (Master-Detail Condition).sAfterWhereClause - the string to place in the SELECT statement after the WHERE clause and before the ORDER BY clause.pMetaData - the MetaDataColumn array to use.
DataSourceException - if it's not possible to build the select statement in fact of missing elements
public String getDatabaseSpecificLockStatement(String pWritebackTable,
MetaData pMetaData,
ICondition pPKFilter)
throws DataSourceException
pWritebackTable - the table to use.pPKFilter - the PK filter with the values to use.pMetaData - the MetaDataColumn array to use.
DataSourceException - if some parts are missing for the statement
public Object[] insertDatabaseSpecific(String pWritebackTable,
String pInsertStatement,
MetaData pMetaData,
Object[] pNewDataRow,
String pDummyColumn)
throws DataSourceException
pWritebackTable - the table to use for the insertpInsertStatement - the SQL Statement to use for the insertpMetaData - the meta data to use.pNewDataRow - the new row (Object[]) with the values to insertpDummyColumn - null, if all writeable columns are null, but for a correct INSERT it have
to be minimum one column to use in the syntax.
DataSourceException - if an Exception occur during insert to the storagepublic boolean supportsGetGeneratedKeys()
public Object[] insertAnsiSQL(String pWritebackTable,
String pInsertStatement,
MetaData pMetaData,
Object[] pNewDataRow,
String pDummyColumn)
throws DataSourceException
pWritebackTable - the table to use for the insertpInsertStatement - the SQL Statement to use for the insertpMetaData - the meta data to use.pNewDataRow - the new row (Object[]) with the values to insertpDummyColumn - true, if all writeable columns are null, but for a correct INSERT it have
to be minimum one column to use in the syntax.
DataSourceException - if an Exception occur during insert to the storage
public ColumnMetaData[] getColumnMetaData(String pFromClause,
String[] pQueryColumns,
String pBeforeQueryColumns,
String pWhereClause,
String pAfterWhereClause,
String pWritebackTable,
String[] pWritebackColumns,
String[] pMetaDataContext)
throws DataSourceException
pBeforeQueryColumns - the before query columnspQueryColumns - the query columnspFromClause - the from clause with query tables and join definitionspWhereClause - the last where condition in querypAfterWhereClause - the after where clause in querypWritebackTable - the write back table to use for the isWriteable() state (Optional)pWritebackColumns - the write back columns to use for the isWriteable() state (Optional)pMetaDataContext - if an String[] with length == 2 is used, the methods returns the MetaData Context back.
That's in [0] the Catalog and in [1] the Schema of the pMetaDataQuery, if possible.
DataSourceException - if an Exception occur during getting the meta data or
if the storage isn't opened or
if one columns SQL type isn't supported
public String getWHEREClause(ICondition pFilter,
MetaData pMetaData,
boolean pUsePrefix)
Filter.
pFilter - the Filter to usepMetaData - the MetaData to usepUsePrefix - true to use the prefixed column (real column name) and false to
ignore prefixes (the simple name of the column)
Filter.
protected void setFilterParameter(int iStartParameterIndex,
PreparedStatement pStatement,
ICondition pFilter)
throws DataSourceException
Filter parameter values into the PreparedStatement.
iStartParameterIndex - the start index for the parameters to set.pStatement - the PreparedStatement to initializepFilter - the Filter to get the values
DataSourceException - if the values can't set into the PreparedStatement
protected int setColumnsToStore(PreparedStatement pInsert,
ColumnMetaData[] pColumnMetaData,
int[] iaWriteables,
Object[] pNew,
Object[] pOld)
throws DataSourceException
PreparedStatement and returns the last used parameter index.
pInsert - the PreparedStatement to initializepColumnMetaData - the column meta data to use.iaWriteables - the writable columns as int index arraypNew - the new values Object[]pOld - the old values Object[]
PreparedStatement.
DataSourceException - if the values can't set into the PreparedStatement
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||