info.fingo.db
Class DatabaseOperations

java.lang.Object
  extended by info.fingo.db.DatabaseOperations
Direct Known Subclasses:
DatabaseOperationsOracle, DatabaseOperationsSQLServer

public abstract class DatabaseOperations
extends java.lang.Object

Abstract class for different database operations

Author:
FINGO - Jan Wielgus

Field Summary
protected  java.lang.String MSSQL_SUFFIX
           
protected  java.lang.String ORACLE_SUFFIX
           
 
Constructor Summary
DatabaseOperations()
           
 
Method Summary
protected abstract  void bindInputParameter(java.sql.PreparedStatement ps, int type, int index, java.lang.Object value)
          Binds a single parameter to its value in the given PreparedStatement
protected abstract  void bindParameters(BaseCollection peer, BaseEntity entity, java.util.ArrayList<java.lang.String> names, java.sql.PreparedStatement ps, int initialIndex)
          Binds parameters for the given PreparedStatement objects
abstract  void bindResourceCallParams(java.sql.CallableStatement cs, java.util.ArrayList<Parameter> params, java.sql.Connection conn)
          Binds parameters for a callable statement for a SQL statement taken from an external resource
abstract  void bindResourceParams(java.sql.PreparedStatement ps, java.util.ArrayList<Parameter> params, java.sql.Connection conn)
          Binds parameters for a prepared statement for a SQL statement taken from an external resource
protected  void delete(BaseCollection peer, BaseEntity where, java.sql.Connection conn)
          Performs the given SQL delete statement on the given collection represented by a BaseCollection object
protected  void deleteByPrimaryKey(BaseCollection collection, java.lang.Object primaryKey, java.sql.Connection conn)
          Deletes the instance with the given primary key from the database
 void execute(java.lang.String query, java.sql.Connection conn)
          Executes the given SQL statement
protected static Column getColumn(BaseCollection instance, java.lang.String columnName)
          Retrieves Column-objects for the current table
protected  Column getColumnForName(Column[] cols, java.lang.String name)
          Retrieves a column with the given name from the given array of Column objects
abstract  void mapAndSetCallOutParam(java.sql.CallableStatement cs, Parameter p)
          Maps an OUT parameter for the given CallableStatement
 java.util.ArrayList<BaseEntity> mapQuery(java.sql.PreparedStatement ps, BaseCollection peer, java.sql.Connection conn)
          Maps the given prepared statement to a list of BaseEntity objects
 java.util.ArrayList<BaseEntity[]> mapQuery(java.lang.String q, BaseCollection[] peers, java.sql.Connection conn)
          Maps the given SQL query to a list of BaseEntity arrays
 java.util.ArrayList<BaseEntity> mapQuery(java.lang.String q, BaseCollection peer, java.sql.Connection conn)
          Maps the given SQL query to a list of BaseEntity objects
abstract  BaseEntity mapResultSet(java.sql.ResultSet rs, BaseCollection collection)
          Maps the given result set to a BaseEntity object
protected abstract  int mapToSpecificTypeNumber(Column.GenericTypes genericType)
          Maps the given generic data type to its JDBC numeric value
abstract  java.lang.Object save(BaseCollection peer, BaseEntity bo, java.sql.Connection conn)
          Saves or updates the given BaseEntity object in the database
protected  java.util.ArrayList<BaseEntity> select(BaseCollection peer, BaseEntity where, java.sql.Connection conn)
          Performs a select operation on the given BaseCollection object with the given where criteria
protected  java.util.ArrayList<BaseEntity> select(BaseCollection peer, BaseEntity where, java.lang.String orderBy, java.sql.Connection conn)
          Performs a select operation on the given BaseCollection object with the given where and orderBy-criteria
 java.util.ArrayList<BaseEntity[]> select(java.lang.String q, BaseCollection[] peers, java.sql.Connection conn)
          Performs the given SQL select statement on the given collections represented by an array of BaseCollection objects
protected  java.util.ArrayList<BaseEntity> select(java.lang.String q, BaseCollection peer, java.sql.Connection conn)
          Performs the given SQL select statement on the given collection represented by a BaseCollection object
protected  java.util.ArrayList<BaseEntity> selectAll(BaseCollection p, java.sql.Connection conn)
          Selects all objects from the given collection
protected  java.util.ArrayList<BaseEntity> selectAll(BaseCollection p, java.lang.String orderBy, java.sql.Connection conn)
          Selects all objects from the given collection using an ORDER BY clause
protected  BaseEntity selectByPrimaryKey(BaseCollection peer, java.lang.Object pk, java.sql.Connection conn)
          Selects an instance of BaseEntity for the given primary key
protected  void update(BaseCollection peer, BaseEntity update, BaseEntity where, java.sql.Connection conn)
          Performs the given SQL update statement on the given collection represented by a BaseCollection object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ORACLE_SUFFIX

protected final java.lang.String ORACLE_SUFFIX
See Also:
Constant Field Values

MSSQL_SUFFIX

protected final java.lang.String MSSQL_SUFFIX
See Also:
Constant Field Values
Constructor Detail

DatabaseOperations

public DatabaseOperations()
Method Detail

bindInputParameter

protected abstract void bindInputParameter(java.sql.PreparedStatement ps,
                                           int type,
                                           int index,
                                           java.lang.Object value)
                                    throws java.sql.SQLException
Binds a single parameter to its value in the given PreparedStatement

Parameters:
ps - The PreparedStatement object used
type - Numeric representation of the parameter data type
index - Index of the parameter in the prepared statement
value - Value of the parameter
Throws:
java.sql.SQLException

bindParameters

protected abstract void bindParameters(BaseCollection peer,
                                       BaseEntity entity,
                                       java.util.ArrayList<java.lang.String> names,
                                       java.sql.PreparedStatement ps,
                                       int initialIndex)
                                throws java.sql.SQLException
Binds parameters for the given PreparedStatement objects

Parameters:
peer - The used BaseCollection object
entity - The used BaseEntity object from which parameter values are taken
names - An ArrayList of parameters' names
ps - The used PreparedStatement object
initialIndex - The index with the parameters' binding shall start
Throws:
java.sql.SQLException

mapResultSet

public abstract BaseEntity mapResultSet(java.sql.ResultSet rs,
                                        BaseCollection collection)
                                 throws java.sql.SQLException
Maps the given result set to a BaseEntity object

Parameters:
rs - The used ResultSet object
collection - The used BaseCollection object
Returns:
The mapped BaseEntity object
Throws:
java.sql.SQLException

save

public abstract java.lang.Object save(BaseCollection peer,
                                      BaseEntity bo,
                                      java.sql.Connection conn)
                               throws java.sql.SQLException,
                                      java.io.IOException
Saves or updates the given BaseEntity object in the database

Parameters:
peer - The collection the BaseEntity object belongs to
bo - The BaseEntity object to be saved
conn - The used Connection object
Returns:
The saved/updated instance
Throws:
java.sql.SQLException
java.io.IOException

bindResourceParams

public abstract void bindResourceParams(java.sql.PreparedStatement ps,
                                        java.util.ArrayList<Parameter> params,
                                        java.sql.Connection conn)
                                 throws java.sql.SQLException,
                                        java.io.IOException
Binds parameters for a prepared statement for a SQL statement taken from an external resource

Parameters:
ps - The used PreparedStatement object
params - An ArrayList of the parameters to be bound
conn - The used Connection object
Throws:
java.sql.SQLException
java.io.IOException

bindResourceCallParams

public abstract void bindResourceCallParams(java.sql.CallableStatement cs,
                                            java.util.ArrayList<Parameter> params,
                                            java.sql.Connection conn)
                                     throws java.sql.SQLException,
                                            java.io.IOException
Binds parameters for a callable statement for a SQL statement taken from an external resource

Parameters:
cs - The used CallableStatement object
params - An ArrayList of the parameters to be bound
conn - The used Connection object
Throws:
java.sql.SQLException
java.io.IOException

mapToSpecificTypeNumber

protected abstract int mapToSpecificTypeNumber(Column.GenericTypes genericType)
Maps the given generic data type to its JDBC numeric value

Parameters:
genericType -
Returns:
The JDBC numeric value of the data type

mapAndSetCallOutParam

public abstract void mapAndSetCallOutParam(java.sql.CallableStatement cs,
                                           Parameter p)
                                    throws java.sql.SQLException
Maps an OUT parameter for the given CallableStatement

Parameters:
cs - The used CallableStatement object
p - The parameter to be set
Throws:
java.sql.SQLException

mapQuery

public java.util.ArrayList<BaseEntity> mapQuery(java.lang.String q,
                                                BaseCollection peer,
                                                java.sql.Connection conn)
                                         throws java.sql.SQLException
Maps the given SQL query to a list of BaseEntity objects

Parameters:
q - The query to be mapped
peer - The BaseCollection object the entities to be returned belong to
conn - The used Connection object
Returns:
ArrayList of returned BaseEntity objects
Throws:
java.sql.SQLException

mapQuery

public java.util.ArrayList<BaseEntity> mapQuery(java.sql.PreparedStatement ps,
                                                BaseCollection peer,
                                                java.sql.Connection conn)
                                         throws java.sql.SQLException
Maps the given prepared statement to a list of BaseEntity objects

Parameters:
ps - The used PreparedStatement object
peer - The BaseCollection object the entities to be returned belong to
conn - The used Connection object
Returns:
ArrayList of returned BaseEntity objects
Throws:
java.sql.SQLException

mapQuery

public java.util.ArrayList<BaseEntity[]> mapQuery(java.lang.String q,
                                                  BaseCollection[] peers,
                                                  java.sql.Connection conn)
                                           throws java.sql.SQLException
Maps the given SQL query to a list of BaseEntity arrays

Parameters:
q - The SQL query to be mapped
peers - Array of BaseCollection objects
conn - The used Connection object
Returns:
ArrayList of BaseEntity arrays
Throws:
java.sql.SQLException

select

protected java.util.ArrayList<BaseEntity> select(java.lang.String q,
                                                 BaseCollection peer,
                                                 java.sql.Connection conn)
                                          throws java.sql.SQLException
Performs the given SQL select statement on the given collection represented by a BaseCollection object

Parameters:
q - The SQL select statement to be performed
peer - The BaseCollection object the select to be performed on
conn - The used Connection object
Returns:
ArrayList of returned BaseEntity objects
Throws:
java.sql.SQLException

select

public java.util.ArrayList<BaseEntity[]> select(java.lang.String q,
                                                BaseCollection[] peers,
                                                java.sql.Connection conn)
                                         throws java.sql.SQLException
Performs the given SQL select statement on the given collections represented by an array of BaseCollection objects

Parameters:
q - The SQL select statement to be performed
peers - The BaseCollection objects the select to be performed on
conn - The used Connection object
Returns:
ArrayList of returned BaseEntity objects
Throws:
java.sql.SQLException

update

protected void update(BaseCollection peer,
                      BaseEntity update,
                      BaseEntity where,
                      java.sql.Connection conn)
               throws java.sql.SQLException
Performs the given SQL update statement on the given collection represented by a BaseCollection object

Parameters:
peer - The BaseCollection object the object to be updated belongs to
update - BaseEntity object with update values
where - BaseEntity object with where criteria
conn - The used Connection object
Throws:
java.sql.SQLException

delete

protected void delete(BaseCollection peer,
                      BaseEntity where,
                      java.sql.Connection conn)
               throws java.sql.SQLException
Performs the given SQL delete statement on the given collection represented by a BaseCollection object

Parameters:
peer - The BaseCollection object the objects to be deleted belongs to
where - BaseEntity object with where criteria
conn - The used Connection object
Throws:
java.sql.SQLException

select

protected java.util.ArrayList<BaseEntity> select(BaseCollection peer,
                                                 BaseEntity where,
                                                 java.lang.String orderBy,
                                                 java.sql.Connection conn)
                                          throws java.sql.SQLException
Performs a select operation on the given BaseCollection object with the given where and orderBy-criteria

Parameters:
peer - The used BaseCollection object
where - The BaseEntity object with select criteria set
orderBy - The SQL 'ORDER BY' clause
conn - The used Connection object
Returns:
An ArrayList of returned BaseEntity array objects
Throws:
java.sql.SQLException

select

protected java.util.ArrayList<BaseEntity> select(BaseCollection peer,
                                                 BaseEntity where,
                                                 java.sql.Connection conn)
                                          throws java.sql.SQLException
Performs a select operation on the given BaseCollection object with the given where criteria

Parameters:
peer - The used BaseCollection object
where - The BaseEntity object with select criteria set
conn - The used Connection object
Returns:
An ArrayList of returned BaseEntity objects
Throws:
java.sql.SQLException

selectAll

protected java.util.ArrayList<BaseEntity> selectAll(BaseCollection p,
                                                    java.sql.Connection conn)
                                             throws java.sql.SQLException
Selects all objects from the given collection

Parameters:
p - The used BaseCollection object
conn - The used Connection object
Returns:
An ArrayList of returned BaseEntity objects
Throws:
java.sql.SQLException

selectAll

protected java.util.ArrayList<BaseEntity> selectAll(BaseCollection p,
                                                    java.lang.String orderBy,
                                                    java.sql.Connection conn)
                                             throws java.sql.SQLException
Selects all objects from the given collection using an ORDER BY clause

Parameters:
p - The used BaseCollection object
orderBy - The used ORDER BY clause
conn - The used Connection object
Returns:
An ArrayList of returned BaseEntity objects
Throws:
java.sql.SQLException

selectByPrimaryKey

protected BaseEntity selectByPrimaryKey(BaseCollection peer,
                                        java.lang.Object pk,
                                        java.sql.Connection conn)
                                 throws java.sql.SQLException
Selects an instance of BaseEntity for the given primary key

Parameters:
peer - The used BaseCollection collection
pk - The primary key of the searched object
conn - The used Connection object
Returns:
The returned BaseEntity object
Throws:
java.sql.SQLException

execute

public void execute(java.lang.String query,
                    java.sql.Connection conn)
             throws java.sql.SQLException
Executes the given SQL statement

Parameters:
query - The SQL statement to be executed
conn - The used Connection object
Throws:
java.sql.SQLException

deleteByPrimaryKey

protected void deleteByPrimaryKey(BaseCollection collection,
                                  java.lang.Object primaryKey,
                                  java.sql.Connection conn)
                           throws java.sql.SQLException
Deletes the instance with the given primary key from the database

Parameters:
collection - The BaseCollection collection the instance belongs to
primaryKey - The primary key of the instance to be deleted
conn - The used Connection object
Throws:
java.sql.SQLException

getColumn

protected static Column getColumn(BaseCollection instance,
                                  java.lang.String columnName)
Retrieves Column-objects for the current table

Parameters:
instance - The BaseCollection object the column belongs to
columnName - The column name
Returns:
Array of Column objects

getColumnForName

protected Column getColumnForName(Column[] cols,
                                  java.lang.String name)
Retrieves a column with the given name from the given array of Column objects

Parameters:
cols - The array with Column objects
name - The name of the column to be retrieved
Returns:
Column object of the retrieved column