Package marauroa.server.db.adapter
Class AbstractDatabaseAdapter
java.lang.Object
marauroa.server.db.adapter.AbstractDatabaseAdapter
- All Implemented Interfaces:
DatabaseAdapter
- Direct Known Subclasses:
H2DatabaseAdapter,MySQLDatabaseAdapter,PostgresDatabaseAdapter
abstract database adapter
- Author:
- hendrik
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Connectionconnection to the databaseprotected LinkedList<ResultSet>list of open result setsprotected LinkedList<Statement>list of open statements -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedcreates a new AbstractDatabaseAdapter for test purpose without connection to the DBAbstractDatabaseAdapter(Properties connInfo)creates a new AbstractDatabaseAdapter -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()closes the database connectionvoidcommit()commits the current transactionprotected ConnectioncreateConnection(Properties connInfo)This method creates the real connection to database.booleandoesColumnExist(String table, String column)checks whether the specified column existsbooleandoesTableExist(String table)checks whether the specified table existsintexecutes an SQL statementintexecute(String sql, InputStream... inputStreams)executes an SQL statement with streamed parametersvoidexecuteBatch(String sql, InputStream... inputStreams)executes a batch of sql-statementsintgetColumnLength(String table, String column)Gets the length of the specified columnintgetLastInsertId(String table, String idcolumn)gets the id of the last insert.booleandetermine if the Exception indicates a connection error, so a retry of the transaction makes sense.prepareStatement(String sql)Prepares a statement for a batch operation.queries the database for informationintquerySingleCellInt(String sql)queries for a single row, single column integer response like a count-selectprotected StringrewriteSql(String sql)rewrites an SQL statement so that it is accepted by the database server softwarevoidrollback()rolls the current transaction back, undoing all the changes.booleanverifies that the connection i still working
-
Field Details
-
connection
connection to the database -
statements
list of open statements -
resultSets
list of open result sets
-
-
Constructor Details
-
AbstractDatabaseAdapter
creates a new AbstractDatabaseAdapter- Parameters:
connInfo- parameters specifying the- Throws:
DatabaseConnectionException- if the connection cannot be established.
-
AbstractDatabaseAdapter
creates a new AbstractDatabaseAdapter for test purpose without connection to the DB- Throws:
DatabaseConnectionException- if the connection cannot be established.
-
-
Method Details
-
createConnection
protected Connection createConnection(Properties connInfo) throws SQLException, DatabaseConnectionExceptionThis method creates the real connection to database.- Parameters:
connInfo- connection information like url, username and password- Returns:
- a connection to the database
- Throws:
SQLException- if the connection cannot be established.DatabaseConnectionException- if the connection cannot be established.
-
commit
Description copied from interface:DatabaseAdaptercommits the current transaction- Specified by:
commitin interfaceDatabaseAdapter- Throws:
SQLException- in case of an database error
-
rollback
Description copied from interface:DatabaseAdapterrolls the current transaction back, undoing all the changes.- Specified by:
rollbackin interfaceDatabaseAdapter- Throws:
SQLException- in case of an database error
-
execute
Description copied from interface:DatabaseAdapterexecutes an SQL statement- Specified by:
executein interfaceDatabaseAdapter- Parameters:
sql- sql-statement to execute- Returns:
- number of affected rows
- Throws:
SQLException- in case of an database error
-
execute
Description copied from interface:DatabaseAdapterexecutes an SQL statement with streamed parameters- Specified by:
executein interfaceDatabaseAdapter- Parameters:
sql- sql-statement to executeinputStreams- parameters- Returns:
- number of affected rows
- Throws:
SQLException- in case of an database errorIOException- in case the stream cannot be read to the end
-
executeBatch
Description copied from interface:DatabaseAdapterexecutes a batch of sql-statements- Specified by:
executeBatchin interfaceDatabaseAdapter- Parameters:
sql- sql-statement to executeinputStreams- a list of input stream. For each of them the statement is executed- Throws:
SQLException- in case of an database errorIOException- in case one of the streams cannot be read to the end
-
query
Description copied from interface:DatabaseAdapterqueries the database for information- Specified by:
queryin interfaceDatabaseAdapter- Parameters:
sql- sql-statement to execute- Returns:
- ResultSet
- Throws:
SQLException- in case of an database error
-
querySingleCellInt
Description copied from interface:DatabaseAdapterqueries for a single row, single column integer response like a count-select- Specified by:
querySingleCellIntin interfaceDatabaseAdapter- Parameters:
sql- sql-statement to execute- Returns:
- integer of the first column in the first row
- Throws:
SQLException- in case of an database error
-
getLastInsertId
Description copied from interface:DatabaseAdaptergets the id of the last insert. Note: The table and idcolumn parameters must match the last insert statement. This is because on some database systems a SELECT IDENTITY is performed and on other database systems a SELECT curval(table_idcolumn_seq).- Specified by:
getLastInsertIdin interfaceDatabaseAdapter- Parameters:
table- name of table on which the last insert was doneidcolumn- name autoincrement serial column of that table- Returns:
- generated id
- Throws:
SQLException- in case of an database error
-
close
Description copied from interface:DatabaseAdaptercloses the database connection- Specified by:
closein interfaceDatabaseAdapter- Throws:
SQLException- in case of an database error
-
prepareStatement
Description copied from interface:DatabaseAdapterPrepares a statement for a batch operation.- Specified by:
prepareStatementin interfaceDatabaseAdapter- Parameters:
sql- SQL statement- Returns:
- PreparedStatement
- Throws:
SQLException- in case of an database error
-
doesTableExist
Description copied from interface:DatabaseAdapterchecks whether the specified table exists- Specified by:
doesTableExistin interfaceDatabaseAdapter- Parameters:
table- name of table- Returns:
- true, if the table exists, false otherwise
- Throws:
SQLException- in case of an database error
-
doesColumnExist
Description copied from interface:DatabaseAdapterchecks whether the specified column exists- Specified by:
doesColumnExistin interfaceDatabaseAdapter- Parameters:
table- name of tablecolumn- name of column- Returns:
- true, if the column exists, false otherwise
- Throws:
SQLException- in case of an database error
-
getColumnLength
Description copied from interface:DatabaseAdapterGets the length of the specified column- Specified by:
getColumnLengthin interfaceDatabaseAdapter- Parameters:
table- name of tablecolumn- name of column- Returns:
- the length of the column or -1 if no column with the given name in the given table exists
- Throws:
SQLException- in case of an database error
-
rewriteSql
rewrites an SQL statement so that it is accepted by the database server software- Parameters:
sql- original SQL statement- Returns:
- modified SQL statement
- Throws:
SQLException- in case of a database error
-
verifyConnection
public boolean verifyConnection()verifies that the connection i still working- Specified by:
verifyConnectionin interfaceDatabaseAdapter- Returns:
- true, if the connection is valid; false otherwise
-
isConnectionError
determine if the Exception indicates a connection error, so a retry of the transaction makes sense.- Specified by:
isConnectionErrorin interfaceDatabaseAdapter- Parameters:
e- Exception- Returns:
- true, if the exception indicates a connection error; false otherwise
-