|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sourceforge.pbeans.data.AbstractDatabase
public abstract class AbstractDatabase
Base implementation of Database. This class implements common functionalilty in order to allow fairly straight-forward Database implementations by simply extending this class and overriding some methods.
Nested Class Summary | |
---|---|
protected class |
AbstractDatabase.TypeInfo
|
Field Summary | |
---|---|
protected int |
sqlStateType
|
Constructor Summary | |
---|---|
AbstractDatabase()
Constructs an AbstractDatabase. |
Method Summary | |
---|---|
protected int |
autoIncrementColumnNumber()
Returns the column number of the auto-increment value found in the result set of the query provided by getAutoIncrementRetrievalStatement . |
protected java.lang.String |
autoIncrementDeclaration()
Override to provide field declaration qualifier for auto-increment fields. |
void |
beginTransaction(int transactionIsolationLevel)
Begins a transaction. |
protected boolean |
canSetNullableOnTableAlter()
Determines if the database is able to alter existing fields such that their nullable status changes (and the change is reflected in database metadata.) This implementation returns true
by default. |
void |
endTransaction(boolean rollback)
Ends a transaction. |
void |
ensureTableExists(java.lang.String tableName,
FieldDescriptor mandatoryField,
java.util.Collection fieldDescriptors,
java.util.Collection indexDescriptors)
Makes sure that a table exists. |
void |
ensureTableExists(java.lang.String tableName,
FieldDescriptor mandatoryField,
java.util.Collection fieldDescriptors,
java.util.Collection indexDescriptors,
boolean userManaged,
boolean autoIncrementRequested,
boolean deleteFields)
Creates a table if necessary. |
protected abstract java.sql.PreparedStatement |
getAutoIncrementRetrievalStatement(java.sql.Connection c)
Gets a PreparedStatement instance that can be
used to retrieve the auto-increment value of a record just
inserted. |
java.lang.String |
getAutomaticPrimaryKeyIndexName()
Gets the name of an implicit index created when there's a primary, as reported in database metadata. |
protected ResultsIterator<BeanWrapper> |
getBeanIterator(net.sourceforge.pbeans.data.ConnectionPool.ConnectionWrapper conn,
java.sql.ResultSet resultSet,
BeanUnmarshaller unmarshaller)
Given a ResultSet, this method returns an iterator of BeanWrapper's. |
protected BeanWrapper |
getBeanWrapper(java.sql.ResultSet resultSet,
BeanUnmarshaller unmarshaller,
java.lang.String idField)
Converts current ResultSet data into a BeanWrapper. |
protected abstract java.sql.PreparedStatement[] |
getChangeFieldStatements(java.sql.Connection c,
java.lang.String tableName,
FieldDescriptor fd,
boolean autoIncrement)
Gets a sequence of Statements that can modify a field's type and whether it's nullable. |
protected net.sourceforge.pbeans.data.ConnectionPool.ConnectionWrapper |
getConnectionWrapper()
Gets a connection wrapper from the internal connection pool. |
protected java.sql.PreparedStatement[] |
getCreateFieldStatements(java.sql.Connection c,
java.lang.String normalTableName,
FieldDescriptor fd,
boolean autoIncrement)
Gets a Statement that adds a field to a table. |
protected java.sql.PreparedStatement[] |
getCreateIndexStatements(java.sql.Connection c,
java.lang.String tableName,
IndexDescriptor id,
java.util.Map<java.lang.String,FieldDescriptor> normalFieldMap)
Gets a sequence of Statements that create one index. |
protected java.sql.PreparedStatement |
getCreateTableStatement(java.sql.Connection c,
java.lang.String tableName,
FieldDescriptor fd,
boolean autoIncrementRequested)
Gets a Statement that creates a table with a single field. |
protected java.sql.PreparedStatement[] |
getDropIndexStatements(java.sql.Connection c,
java.lang.String tableName,
java.lang.String name)
Gets a sequence of Statements that remove one index. |
int |
getMaxColumnNameLength()
Gets the maximum length of a column name in this database. |
int |
getMaxTableNameLength()
Gets the maximum length of a table name. |
protected java.sql.PreparedStatement |
getRemoveFieldStatement(java.sql.Connection c,
java.lang.String tableName,
java.lang.String columnName)
Gets a Statement that removes a field. |
protected abstract java.sql.PreparedStatement[] |
getRenameFieldStatements(java.sql.Connection c,
java.lang.String tableName,
java.lang.String oldName,
FieldDescriptor fd,
boolean autoIncrement)
Gets a sequence of Statements that can rename a field. |
protected java.lang.Object |
getResultSetValue(int i,
java.lang.String name,
int type,
java.sql.ResultSet resultSet)
Gets an object from the ResultSet. |
protected java.lang.String |
getTypeDeclaration(int dataType)
Gets the type declaration for a type constant as defined in java.sql.Types. |
void |
init(javax.sql.DataSource dataSource)
Override this method to obtain metadata information, such as the JDBC URL. |
protected void |
initDatabaseInfo()
Initializes some meta-information about the database, particularly about supported types. |
java.lang.Long |
insert(java.lang.String tableName,
java.lang.String idField,
java.lang.String[] normalFieldNames,
java.lang.Object[] values,
boolean autoIncrementRequested)
Inserts a record in a table. |
boolean |
inTransaction()
Determines if the current thread is executing within a transaction. |
protected boolean |
isBlob(int type)
|
protected boolean |
isDecimal(int type)
|
abstract boolean |
isDuplicateEntryError(java.sql.SQLException se)
Determines if the SQLException given corresponds to a duplicate record error (such as one given when you try to insert a record with a unique index that already exists.) |
protected boolean |
isLong(int type)
|
protected boolean |
isText(int type)
|
protected boolean |
isTiny(int type)
|
protected IndexDescriptor |
normalizeIndex(java.lang.String normalTableName,
IndexDescriptor id)
Override this method if the database has special requirements on index names, such as global uniqueness or lowercase strings. |
java.lang.String |
normalizeName(java.lang.String s)
Takes a field name or table name as it would be requested by a caller and coverts it to a string as it would be reported by database metadata (for example, as lowercase strings, in some databases.) Override this method if the database has special naming requirements for field or table names. |
ResultsIterator<BeanWrapper> |
query(java.lang.String sql,
java.lang.Object[] parameters,
BeanUnmarshaller unmarshaller)
Queries the database. |
protected void |
releaseConnectionWrapper(net.sourceforge.pbeans.data.ConnectionPool.ConnectionWrapper wrapper)
|
void |
relinquishLock(boolean rollback)
If the matching call to requestLock requested a transaction, this method ends it, and also relinquishes the Java lock. |
void |
requestLock(java.lang.String lockStr,
int transactionIsolationLevel)
Requests a Java thread lock, in addition to starting a transaction, if not already in one. |
protected boolean |
requiresKeyLength()
Determines whether the database requires a key length qualifier for indexes on text fields. |
void |
rollback(java.sql.Savepoint savepoint)
|
void |
setConnectionTimeout(int timeout)
Sets the amount of time, in milliseconds, that a pooled connection may be idle before it is discarded. |
void |
setMaxConnections(int maxConnections)
Sets the maximum number of simultaneous open connections. |
protected void |
setParameter(java.sql.PreparedStatement ps,
int index,
java.lang.Object objVal)
Sets a PrepartedStatement parameter. |
java.sql.Savepoint |
setSavepoint(java.lang.String savepointName)
|
boolean |
shouldCloseConnectionAfterTransaction()
Determines if the database connection should be closed after a commit. |
protected boolean |
supportsAutoIncrement()
Determines if the database supports auto-increment primary keys. |
protected boolean |
typesMatch(int type1,
int type2)
Checks whether two SQL types match when trying to determine if a field declaration should be changed. |
int |
update(java.lang.String sql,
java.lang.Object[] parameters)
Executes an insert or update operation in the database. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected int sqlStateType
Constructor Detail |
---|
public AbstractDatabase() throws java.sql.SQLException
java.sql.SQLException
GenericDataSource
Method Detail |
---|
public void init(javax.sql.DataSource dataSource) throws java.sql.SQLException
super
implementation.
init
in interface Database
dataSource
- A DataSource instance.
java.sql.SQLException
public final void setMaxConnections(int maxConnections)
public final void setConnectionTimeout(int timeout)
public int getMaxTableNameLength() throws java.sql.SQLException
getMaxTableNameLength
in interface Database
java.sql.SQLException
public final int getMaxColumnNameLength() throws java.sql.SQLException
getMaxColumnNameLength
in interface Database
java.sql.SQLException
protected void initDatabaseInfo() throws java.sql.SQLException
java.sql.SQLException
protected java.lang.String getTypeDeclaration(int dataType) throws java.sql.SQLException
java.sql.SQLException
public abstract boolean isDuplicateEntryError(java.sql.SQLException se)
isDuplicateEntryError
in interface Database
public void ensureTableExists(java.lang.String tableName, FieldDescriptor mandatoryField, java.util.Collection fieldDescriptors, java.util.Collection indexDescriptors) throws java.sql.SQLException
tableName
- The name of the table.mandatoryField
- Information about the mandatory (primary key) field.fieldDescriptors
- Information about other fields.indexDescriptors
- Information about indexes.
java.sql.SQLException
public java.lang.String getAutomaticPrimaryKeyIndexName()
null
.
Override if the create table statement is sufficient to create
a primary key which need not be created explicitly.
public void ensureTableExists(java.lang.String tableName, FieldDescriptor mandatoryField, java.util.Collection fieldDescriptors, java.util.Collection indexDescriptors, boolean userManaged, boolean autoIncrementRequested, boolean deleteFields) throws java.sql.SQLException
fieldDescriptors
. It adds any new fields, and changes types
of existing fields if needed. Finally, it modifies and creates requested indexes.
There should be no need to override this method.
ensureTableExists
in interface Database
tableName
- Name of the table.mandatoryField
- A FieldDescriptor which cannot be removed later (typically the table's primary key.)fieldDescriptors
- A collection of FieldDescriptor instances.indexDescriptors
- A collection of IndexDescriptor instances.autoIncrementRequested
- Whether auto-increment is requested for mandatory field.deleteFields
- Whether fields not requested that were found in the table should be deleted.
java.sql.SQLException
public java.lang.String normalizeName(java.lang.String s)
normalizeName
in interface Database
protected boolean canSetNullableOnTableAlter()
true
by default. Override if the database is unable to
alter fields in this manner.
protected IndexDescriptor normalizeIndex(java.lang.String normalTableName, IndexDescriptor id)
normalTableName
- A table name, already normalized.id
- An IndexDescriptor instance.protected boolean typesMatch(int type1, int type2)
protected boolean isLong(int type)
protected boolean isDecimal(int type)
protected boolean isBlob(int type)
protected boolean isText(int type)
protected boolean isTiny(int type)
protected java.sql.PreparedStatement getCreateTableStatement(java.sql.Connection c, java.lang.String tableName, FieldDescriptor fd, boolean autoIncrementRequested) throws java.sql.SQLException
java.sql.SQLException
protected java.sql.PreparedStatement getRemoveFieldStatement(java.sql.Connection c, java.lang.String tableName, java.lang.String columnName) throws java.sql.SQLException
java.sql.SQLException
protected java.sql.PreparedStatement[] getCreateFieldStatements(java.sql.Connection c, java.lang.String normalTableName, FieldDescriptor fd, boolean autoIncrement) throws java.sql.SQLException
java.sql.SQLException
protected abstract java.sql.PreparedStatement[] getChangeFieldStatements(java.sql.Connection c, java.lang.String tableName, FieldDescriptor fd, boolean autoIncrement) throws java.sql.SQLException
java.sql.SQLException
protected abstract java.sql.PreparedStatement[] getRenameFieldStatements(java.sql.Connection c, java.lang.String tableName, java.lang.String oldName, FieldDescriptor fd, boolean autoIncrement) throws java.sql.SQLException
java.sql.SQLException
protected boolean requiresKeyLength()
false
.
Override if database requires key lenghts in the text fields
of an index.
protected java.sql.PreparedStatement[] getCreateIndexStatements(java.sql.Connection c, java.lang.String tableName, IndexDescriptor id, java.util.Map<java.lang.String,FieldDescriptor> normalFieldMap) throws java.sql.SQLException
normalFieldMap
- A map from normalized field names to FieldDescriptors.
java.sql.SQLException
protected java.sql.PreparedStatement[] getDropIndexStatements(java.sql.Connection c, java.lang.String tableName, java.lang.String name) throws java.sql.SQLException
name
- The name of the index.
java.sql.SQLException
public final void requestLock(java.lang.String lockStr, int transactionIsolationLevel) throws java.lang.InterruptedException, java.sql.SQLException
requestLock
in interface Database
java.lang.InterruptedException
java.sql.SQLException
public final void relinquishLock(boolean rollback) throws java.sql.SQLException
relinquishLock
in interface Database
java.sql.SQLException
public ResultsIterator<BeanWrapper> query(java.lang.String sql, java.lang.Object[] parameters, BeanUnmarshaller unmarshaller) throws java.sql.SQLException
query
in interface Database
sql
- A SQL SELECT statement with question marks (?) as placeholders.parameters
- An array of parameters whose length is equal to the number of placeholders in the SQL statement.unmarshaller
- A BeanUnmarshaller implementation instance.
java.sql.SQLException
Database.update(java.lang.String, java.lang.Object[])
public int update(java.lang.String sql, java.lang.Object[] parameters) throws java.sql.SQLException
update
in interface Database
sql
- A SQL modification statement with question marks (?) as placeholders.parameters
- An array of parameters whose length is equal to the number of placeholders in the SQL statement.
java.sql.SQLException
Database.query(java.lang.String, java.lang.Object[], net.sourceforge.pbeans.data.BeanUnmarshaller)
protected boolean supportsAutoIncrement()
protected java.lang.String autoIncrementDeclaration()
protected abstract java.sql.PreparedStatement getAutoIncrementRetrievalStatement(java.sql.Connection c) throws java.sql.SQLException
PreparedStatement
instance that can be
used to retrieve the auto-increment value of a record just
inserted.
c
- A Connection instance.
java.sql.SQLException
protected int autoIncrementColumnNumber()
getAutoIncrementRetrievalStatement
. Override
if the column number is anything other than 1.
public java.lang.Long insert(java.lang.String tableName, java.lang.String idField, java.lang.String[] normalFieldNames, java.lang.Object[] values, boolean autoIncrementRequested) throws java.sql.SQLException
insert
in interface Database
idField
- Name of ID field.autoIncrementRequested
- Whether auto-increment is requested for the ID field.
java.sql.SQLException
protected void setParameter(java.sql.PreparedStatement ps, int index, java.lang.Object objVal) throws java.sql.SQLException
PrepartedStatement
parameter. Override if this method
or the setObject
method of PrepartedStatement
are not setting parameter values as expected.
- Throws:
java.sql.SQLException
protected ResultsIterator<BeanWrapper> getBeanIterator(net.sourceforge.pbeans.data.ConnectionPool.ConnectionWrapper conn, java.sql.ResultSet resultSet, BeanUnmarshaller unmarshaller)
protected BeanWrapper getBeanWrapper(java.sql.ResultSet resultSet, BeanUnmarshaller unmarshaller, java.lang.String idField) throws java.sql.SQLException
java.sql.SQLException
protected java.lang.Object getResultSetValue(int i, java.lang.String name, int type, java.sql.ResultSet resultSet) throws java.sql.SQLException
java.sql.SQLException
protected net.sourceforge.pbeans.data.ConnectionPool.ConnectionWrapper getConnectionWrapper() throws java.sql.SQLException
java.sql.SQLException
protected void releaseConnectionWrapper(net.sourceforge.pbeans.data.ConnectionPool.ConnectionWrapper wrapper) throws java.sql.SQLException
java.sql.SQLException
public final void beginTransaction(int transactionIsolationLevel) throws java.sql.SQLException
beginTransaction
in interface Database
java.sql.SQLException
public final void endTransaction(boolean rollback) throws java.sql.SQLException
endTransaction
in interface Database
java.sql.SQLException
public final boolean inTransaction()
inTransaction
in interface Database
public boolean shouldCloseConnectionAfterTransaction()
true
.
Override if a connection where a transaction just occurred
can still be used.
public final java.sql.Savepoint setSavepoint(java.lang.String savepointName) throws java.sql.SQLException
setSavepoint
in interface Database
java.sql.SQLException
public final void rollback(java.sql.Savepoint savepoint) throws java.sql.SQLException
rollback
in interface Database
java.sql.SQLException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |