SRJRCFrames
v0.1.296

de.schwarzrot.data.access.support
Class AbstractDbUtils

java.lang.Object
  extended by de.schwarzrot.data.access.support.AbstractDbUtils
All Implemented Interfaces:
DbUtil
Direct Known Subclasses:
MyUtils, PgUtils

public abstract class AbstractDbUtils
extends Object
implements DbUtil

common implementation of DbUtil and base class for database specific utility classes. This class is used to generate SQL used at JDBC-layer.

Author:
Reinhard Mantey

Field Summary
protected static Map<String,Integer> skipList
           
 
Fields inherited from interface de.schwarzrot.data.access.support.DbUtil
EMPTY_STRING, ONE_SPACE, PK_NAME
 
Constructor Summary
AbstractDbUtils()
           
 
Method Summary
 Collection<ColumnMetaData> columnDefinitions(Connection conn, TableDefinition td)
           
protected  String createColumnDef(ColumnDefinition col)
           
protected  void dumpRS(ResultSet rs)
           
 String genAccessRightsForPK(TableDefinition tbInfo, String defaultUser)
          generates a SQL-string to grant access to an external sequence
protected abstract  String genAutoIncrement(ColumnDefinition cd)
           
 String genColumnCreation(ColumnDefinition cd)
          generates a SQL string to add a column to an existing table
 String genColumnModification(ColumnDefinition cd)
          generates a SQL string to modify a column of an existing table
 String genCondition(EntityDescriptor ed, Collection<ConditionElement> args)
          generates a condition string for SQL statements
<E extends Entity>
String
genCondition(EntityDescriptor ed, E instance, Collection<String> fields)
          generates a condition string for SQL statements
 String genCondition(EntityDescriptor ed, Map<String,Object> args)
          generates a condition string for SQL statements
 String genDBCreation(DatabaseModel dbInfo)
          generates a SQL string to create a database
 String genIndexCreation(IndexDefinition ii)
          generates a SQL string to create an index
 String genIndexRemoval(IndexDefinition ii)
          generates a SQL string to remove an index
 String genInsert(EntityDescriptor ed, Entity instance)
          generates a SQL-insert statement for given instance
 String genInsertIdQuery(EntityDescriptor ed, Date dtCreated, String userName)
          generates a query for the generated autoincrement key.
 String genOrder(EntityDescriptor ed, Collection<String> properties)
          generates the sorting part of a SQL query
 String genOrder(EntityDescriptor ed, Collection<String> properties, Collection<SortInfo> orderSequence)
          generates the sorting part of an SQL-query.
protected abstract  String genPrimary(TableDefinition ti)
           
 String genRemove(EntityDescriptor ed, Collection<ConditionElement> args)
          generates a SQL delete statement
 String genSchemaAccess(SchemaDefinition schemInfo, String defaultUser)
          generates a SQL-string to grant access rights to schema
 String genSchemaCreation(SchemaDefinition schemInfo)
          generates a SQL string to create a schema
 String genSequenceReset(TableDefinition tbInfo)
          generates a SQL string to reset an external sequence
 String genTableCreation(TableDefinition tbInfo)
          generates a SQL string to create a database table
 String genTableMetaAccess(TableDefinition tbInfo)
          generates a SQL query that returns no data to access tables meta data information
 String genTablePKCreation(TableDefinition tbInfo)
          generates a statement to create an external sequence for autoincrement-column
 String genTablePKMod(TableDefinition tbInfo)
          generates a statement to modify the external sequence after table creation.
 String genTableRemoval(TableDefinition tbInfo)
          generates a SQL string that removes a table from database
 String genTableRemoval(TableDefinition tbInfo, boolean withExist)
          generates a SQL string that removes a table from database.
 String genTruncate(EntityDescriptor ed)
          generates a SQL statement that truncates a database table, which means, that all entries will be removed.
 String genUpdate(EntityDescriptor ed, Entity instance)
          generates a SQL statement to change the persistent view of an entity
 String genUpdate(EntityDescriptor ed, Entity instance, Collection<String> fields, boolean withCondition)
          generates a SQL statement to partially change the persistent view of an entity.
 String genUserCreation(String user, String password)
          generates a SQL statement to create a user/role/principal
protected abstract  String getDBCharset()
           
protected abstract  String getIndexName(IndexDefinition ii)
           
protected  org.apache.commons.logging.Log getLogger()
           
 String getMapped(EntityDescriptor ed, String attributeName)
          get the physical name for the given property, already quoted with the right syntax for the involved database
protected abstract  String getTableAttributes(TableDefinition tbInfo)
           
protected  String getTypeFor(ColumnDefinition ci)
           
 ColumnType getTypeFrom(int sqlType)
          maps SQL types to logical ColumnTypes used by application
protected abstract  String getUsernameFor(String user, String host)
           
 Collection<IndexMetaData> indexDefinitions(Connection conn, TableDefinition td)
           
protected abstract  boolean isExistsSupported()
          signals whether current database supports exists on drop tables
 boolean needsUpdate(ColumnDefinition master, ColumnDefinition other)
           
 boolean needsUpdate(IndexDefinition master, IndexDefinition other)
           
 Collection<SchemaDefinition> schemaDefinitions(Connection conn)
           
protected abstract  boolean supportsNativeSchemata()
           
 Collection<TableDefinition> tableDefinitions(Connection conn, SchemaDefinition sd)
           
protected  String xpandInsert(EntityDescriptor ed)
          some databases need special keywords on insert statements to return the values of autoincrement keys.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.schwarzrot.data.access.support.DbUtil
genAccessRightsFor, genDBAccess, genUserCreation, getFieldSeparator, getMapped, getSchemaSeparator, getTableNameFor, getTableNameFor, getTerminator, needDropIndexAfterDropTable, needExternalSequenceForSerial, needSequenceResetAfterTruncate, quoteAndCutValue, supportsGeneratedKeyQuery
 

Field Detail

skipList

protected static final Map<String,Integer> skipList
Constructor Detail

AbstractDbUtils

public AbstractDbUtils()
Method Detail

columnDefinitions

public Collection<ColumnMetaData> columnDefinitions(Connection conn,
                                                    TableDefinition td)
Specified by:
columnDefinitions in interface DbUtil

genAccessRightsForPK

public String genAccessRightsForPK(TableDefinition tbInfo,
                                   String defaultUser)
Description copied from interface: DbUtil
generates a SQL-string to grant access to an external sequence

Specified by:
genAccessRightsForPK in interface DbUtil
Parameters:
tbInfo - - table model information
defaultUser - - the user that gets the rights granted
Returns:
- the ready to use SQL statement

genColumnCreation

public String genColumnCreation(ColumnDefinition cd)
Description copied from interface: DbUtil
generates a SQL string to add a column to an existing table

Specified by:
genColumnCreation in interface DbUtil
Parameters:
cd - - column model information
Returns:
- the ready to use SQL statement

genColumnModification

public String genColumnModification(ColumnDefinition cd)
Description copied from interface: DbUtil
generates a SQL string to modify a column of an existing table

Specified by:
genColumnModification in interface DbUtil
Parameters:
cd - - column model information
Returns:
-the ready to use SQL statement

genCondition

public String genCondition(EntityDescriptor ed,
                           Collection<ConditionElement> args)
Description copied from interface: DbUtil
generates a condition string for SQL statements

Specified by:
genCondition in interface DbUtil
Parameters:
ed - - descriptor of the entity
args - - list of elements that form the condition
Returns:
- the generated condition string

genCondition

public <E extends Entity> String genCondition(EntityDescriptor ed,
                                              E instance,
                                              Collection<String> fields)
Description copied from interface: DbUtil
generates a condition string for SQL statements

Specified by:
genCondition in interface DbUtil
Type Parameters:
E - - the entity to use
Parameters:
ed - - descriptor of the entity
instance - - instance of that entity, containing the values for the condition
fields - - list of attributes used in the condition
Returns:
- the generated condition string

genCondition

public String genCondition(EntityDescriptor ed,
                           Map<String,Object> args)
Description copied from interface: DbUtil
generates a condition string for SQL statements

Specified by:
genCondition in interface DbUtil
Parameters:
ed - - descriptor of the entity
args - - map of attribute/value pairs to build the condition from
Returns:
- the generated condition string

genDBCreation

public String genDBCreation(DatabaseModel dbInfo)
Description copied from interface: DbUtil
generates a SQL string to create a database

Specified by:
genDBCreation in interface DbUtil
Parameters:
dbInfo - - database model information
Returns:
- the ready to use SQL statement

genIndexCreation

public String genIndexCreation(IndexDefinition ii)
Description copied from interface: DbUtil
generates a SQL string to create an index

Specified by:
genIndexCreation in interface DbUtil
Parameters:
ii - - index model information
Returns:
- the ready to use SQL statement

genIndexRemoval

public String genIndexRemoval(IndexDefinition ii)
Description copied from interface: DbUtil
generates a SQL string to remove an index

Specified by:
genIndexRemoval in interface DbUtil
Parameters:
ii - - index model information
Returns:
- the ready to use SQL statement

genInsert

public String genInsert(EntityDescriptor ed,
                        Entity instance)
Description copied from interface: DbUtil
generates a SQL-insert statement for given instance

Specified by:
genInsert in interface DbUtil
Parameters:
ed - - descriptor of the entity
instance - - the instance that should be copied to persistence
Returns:
- the ready to use SQL statement

genInsertIdQuery

public String genInsertIdQuery(EntityDescriptor ed,
                               Date dtCreated,
                               String userName)
Description copied from interface: DbUtil
generates a query for the generated autoincrement key. This method is a fallback for JDBC implementations, that don't support the Statement.getGeneratedKeys(). So they have to support at least dirty reads.

Specified by:
genInsertIdQuery in interface DbUtil
Parameters:
ed - - descriptor of the entity
dtCreated - - timestamp of the insert
userName - - name of the user that triggered the insert
Returns:
- the ready to use SQL statement

genOrder

public String genOrder(EntityDescriptor ed,
                       Collection<String> properties)
Description copied from interface: DbUtil
generates the sorting part of a SQL query

Specified by:
genOrder in interface DbUtil
Parameters:
ed - - descriptor of the entity
properties - - list of attributes to sort by
Returns:
- the generated sort part of the SQL query

genOrder

public String genOrder(EntityDescriptor ed,
                       Collection<String> properties,
                       Collection<SortInfo> orderSequence)
Description copied from interface: DbUtil
generates the sorting part of an SQL-query. This method validates that only attributes that get read form part of the sort order

Specified by:
genOrder in interface DbUtil
Parameters:
ed - - descriptor of the entity
properties - - list of attributes to read
orderSequence - - list of sorting segment info
Returns:
- the sorting part of an SQL query

genRemove

public String genRemove(EntityDescriptor ed,
                        Collection<ConditionElement> args)
Description copied from interface: DbUtil
generates a SQL delete statement

Specified by:
genRemove in interface DbUtil
Parameters:
ed - - descriptor of the entity to remove
args - - list of elements that form the condition
Returns:
- the ready to use SQL statement

genSchemaAccess

public String genSchemaAccess(SchemaDefinition schemInfo,
                              String defaultUser)
Description copied from interface: DbUtil
generates a SQL-string to grant access rights to schema

Specified by:
genSchemaAccess in interface DbUtil
Parameters:
schemInfo - - schema model information
defaultUser - - the user that gets the rights granted
Returns:
- the ready to use SQL statement

genSchemaCreation

public String genSchemaCreation(SchemaDefinition schemInfo)
Description copied from interface: DbUtil
generates a SQL string to create a schema

Specified by:
genSchemaCreation in interface DbUtil
Parameters:
schemInfo - - schema model information
Returns:
- the ready to use SQL statement

genSequenceReset

public String genSequenceReset(TableDefinition tbInfo)
Description copied from interface: DbUtil
generates a SQL string to reset an external sequence

Specified by:
genSequenceReset in interface DbUtil
Parameters:
tbInfo - - table model information
Returns:
- the ready to use SQL statement

genTableCreation

public String genTableCreation(TableDefinition tbInfo)
Description copied from interface: DbUtil
generates a SQL string to create a database table

Specified by:
genTableCreation in interface DbUtil
Parameters:
tbInfo - - table model information
Returns:
- the ready to use SQL statement

genTableMetaAccess

public String genTableMetaAccess(TableDefinition tbInfo)
Description copied from interface: DbUtil
generates a SQL query that returns no data to access tables meta data information

Specified by:
genTableMetaAccess in interface DbUtil
Parameters:
tbInfo - - table model information
Returns:
- the ready to use SQL statement

genTablePKCreation

public String genTablePKCreation(TableDefinition tbInfo)
Description copied from interface: DbUtil
generates a statement to create an external sequence for autoincrement-column

Specified by:
genTablePKCreation in interface DbUtil
Parameters:
tbInfo - - table model information
Returns:
- the ready to use SQL statement

genTablePKMod

public String genTablePKMod(TableDefinition tbInfo)
Description copied from interface: DbUtil
generates a statement to modify the external sequence after table creation.

Specified by:
genTablePKMod in interface DbUtil
Parameters:
tbInfo - - table model information
Returns:
- the ready to use SQL statement

genTableRemoval

public String genTableRemoval(TableDefinition tbInfo)
Description copied from interface: DbUtil
generates a SQL string that removes a table from database

Specified by:
genTableRemoval in interface DbUtil
Parameters:
tbInfo - - table model information
Returns:
- the ready to use SQL statement

genTableRemoval

public String genTableRemoval(TableDefinition tbInfo,
                              boolean withExist)
Description copied from interface: DbUtil
generates a SQL string that removes a table from database. SQL statement is extended with table existence check

Specified by:
genTableRemoval in interface DbUtil
Parameters:
tbInfo - - table model information
withExist - - flag to indicate, whether existence check should be added
Returns:
- the ready to use SQL statement

genTruncate

public String genTruncate(EntityDescriptor ed)
Description copied from interface: DbUtil
generates a SQL statement that truncates a database table, which means, that all entries will be removed. The truncation of a db table is vendor specific, so for database systems, that support truncation, that statement should be created. For database systems, that don't support truncation, a delete without where clause will be used.

Truncation of a table may require additional permissions beside the permission to delete records.

Specified by:
genTruncate in interface DbUtil
Parameters:
ed - - descriptor of the Entity
Returns:
- the ready to use truncate expression

genUpdate

public String genUpdate(EntityDescriptor ed,
                        Entity instance)
Description copied from interface: DbUtil
generates a SQL statement to change the persistent view of an entity

Specified by:
genUpdate in interface DbUtil
Parameters:
ed - - descriptor of the involved entity
instance - - the instance to save
Returns:
- the field modification part of the SQL - needs to get extended by a condition

genUpdate

public String genUpdate(EntityDescriptor ed,
                        Entity instance,
                        Collection<String> fields,
                        boolean withCondition)
Description copied from interface: DbUtil
generates a SQL statement to partially change the persistent view of an entity. Only those attributes contained in the list of fields will be modified.

Specified by:
genUpdate in interface DbUtil
Parameters:
ed - - descriptor of the involved entity
instance - - instance containing the modified values to save
fields - the attribute names to modify
withCondition -
  • true - the condition to update the given instance is included
  • false - no condition is included (usable to update more than one instance)
Returns:
- the generated SQL statement

genUserCreation

public String genUserCreation(String user,
                              String password)
Description copied from interface: DbUtil
generates a SQL statement to create a user/role/principal

Specified by:
genUserCreation in interface DbUtil
Parameters:
user - - name of the user
password - - password for that user
Returns:
- the generated SQL statement

getMapped

public String getMapped(EntityDescriptor ed,
                        String attributeName)
Description copied from interface: DbUtil
get the physical name for the given property, already quoted with the right syntax for the involved database

Specified by:
getMapped in interface DbUtil
Parameters:
ed - - descriptor of the involved entity
attributeName - - name of attribute to map
Returns:
- the physical name of the attribute with id-quotes

getTypeFrom

public ColumnType getTypeFrom(int sqlType)
Description copied from interface: DbUtil
maps SQL types to logical ColumnTypes used by application

Specified by:
getTypeFrom in interface DbUtil
Parameters:
sqlType - - a SQL type to map
Returns:
- the mapped ColumnType

indexDefinitions

public Collection<IndexMetaData> indexDefinitions(Connection conn,
                                                  TableDefinition td)
Specified by:
indexDefinitions in interface DbUtil

needsUpdate

public boolean needsUpdate(ColumnDefinition master,
                           ColumnDefinition other)
Specified by:
needsUpdate in interface DbUtil

needsUpdate

public boolean needsUpdate(IndexDefinition master,
                           IndexDefinition other)
Specified by:
needsUpdate in interface DbUtil

schemaDefinitions

public Collection<SchemaDefinition> schemaDefinitions(Connection conn)
Specified by:
schemaDefinitions in interface DbUtil

tableDefinitions

public Collection<TableDefinition> tableDefinitions(Connection conn,
                                                    SchemaDefinition sd)
Specified by:
tableDefinitions in interface DbUtil

createColumnDef

protected String createColumnDef(ColumnDefinition col)

dumpRS

protected void dumpRS(ResultSet rs)

genAutoIncrement

protected abstract String genAutoIncrement(ColumnDefinition cd)

genPrimary

protected abstract String genPrimary(TableDefinition ti)

getDBCharset

protected abstract String getDBCharset()

getIndexName

protected abstract String getIndexName(IndexDefinition ii)

getLogger

protected final org.apache.commons.logging.Log getLogger()

getTableAttributes

protected abstract String getTableAttributes(TableDefinition tbInfo)

getTypeFor

protected String getTypeFor(ColumnDefinition ci)

getUsernameFor

protected abstract String getUsernameFor(String user,
                                         String host)

isExistsSupported

protected abstract boolean isExistsSupported()
signals whether current database supports exists on drop tables

Returns:
true if conditional drop tables are supported, otherwise false

supportsNativeSchemata

protected abstract boolean supportsNativeSchemata()

xpandInsert

protected String xpandInsert(EntityDescriptor ed)
some databases need special keywords on insert statements to return the values of autoincrement keys.


SRJRCFrames
v0.1.296

hosted at
Find SRJRCFrames at SourceForge.net. Fast, secure and free:
           Open Source Software download
Submit a bug or request a feature

SRJRCFrames is published according to the GNU General Public License
Copyright 2005-2012 Reinhard Mantey - some rights reserved.