public class GcDbAccess extends Object
Get access to the global database connections, create a new connection, and execute sql against them.
Modifier and Type | Class and Description |
---|---|
static class |
GcDbAccess.ConnectionBean
returned from connection call
|
Constructor and Description |
---|
GcDbAccess() |
Modifier and Type | Method and Description |
---|---|
static void |
accumulateQueryMillis(boolean _accumulateQueryMillis)
If true, the map queryAndTime will be populated with the time spent in each unique query (unique by query string, not considering bind variable values)
- BE SURE TO TURN THIS OFF when done debugging, this is ONLY for debugging on the desktop!Turning it off CLEARS the stats, so write it off first!
Example:
1.
|
GcDbAccess |
addBindVar(Object _bindVar)
Add to the list of bind variable objects, leaving any that exist there - if you use this in a transaction callback
you will have to clear bindvars between calls or they will accumulate.
|
GcDbAccess |
batchBindVars(List<List<Object>> _batchBindVars)
If you are executing sql as a batch statement, set the batch bind variables here.
|
GcDbAccess |
bindVars(Object... _bindVars)
/**
Set the list of bind variable objects, always replacing any that exist.
|
GcDbAccess |
cacheMinutes(Integer _cacheMinutes)
Cache the results of a SELECT query for the allotted minutes.
|
<T> T |
callbackCallableStatement(GcCallableStatementCallback<T> callableStatementCallback)
Callback to get a callableStatement - commit is called if there is no exception thrown, otherwise rollback is called.
|
<T> T |
callbackConnection(GcConnectionCallback<T> connectionCallback)
Callback to get a connection - commit is called if there is no exception thrown, otherwise rollback is called.
|
<T> void |
callbackEntity(Class<T> clazz,
GcEntityCallback<T> entityCallback)
For each row of a given resultset, hydrate an object and pass it to the callback.
|
<T> T |
callbackPreparedStatement(GcPreparedStatementCallback<T> preparedStatementCallback)
Callback to get a preparedStatement - commit is called if there is no exception thrown, otherwise rollback is called.
|
<T> T |
callbackResultSet(GcResultSetCallback<T> resultSetCallback)
Callback a resultSet.
|
<T> T |
callbackTransaction(GcTransactionCallback<T> transactionCallback)
Use a transaction for all calls that happen within this callback.
|
GcDbAccess |
connectionName(String theConnectionName)
connection name from the config file, or null for default
|
static String |
createInString(int numberOfBindVariables)
Create an in statement with the given number of bind variables: createInString(2) returns " (?,?) "
|
void |
deleteFromDatabase(Object o)
Delete the object from the database if it has already been stored - the object should have appropriate annotations from the PersistableX annotations.
|
GcDbAccess |
example(Object _example)
If selecting by example, set this and all column values will be used to create a where clause.
|
int[] |
executeBatchSql()
Execute some sql as a batch.
|
int |
executeSql()
Execute some sql.
|
static Map<MultiKey,GcDbQueryCache> |
getGcDbQueryCacheMap()
Cached queries, exposed mostly for testing, you should not need direct access to this.
|
static Map<String,GcQueryReport> |
getQueriesAndMillis()
The map containing reports if they have been turned on.
|
boolean |
isPreviouslyPersisted(Object o)
Whether this class has already been saved to the database, looks for a field(s) with annotation @Persistable(primaryKeyField=true),
assumes that it is a number, and returns true if it is null or larger than 0.
|
static void |
loadBoundDataConversion(GcBoundDataConversion _boundDataConversion)
This is the helper to convert data to and from Oracle, which has a default of BoundDataConversionImpl.
|
GcDbAccess |
omitNullValuesForExample()
If selecting by example, set this and all column values of the given example object except null values will be used to create a where clause.
|
GcDbAccess |
primaryKey(Object... _primaryKey)
Set the primary key to select by.
|
GcDbAccess |
queryTimeoutSeconds(Integer _queryTimeoutSeconds)
The amount of seconds that the query can run before being rolled back.
|
static void |
reportQueriesAndMillisAndTurnOffAccumulation(String fileLocation)
Write the stats of queries and time spent in them to a file at the given location, then stop collection stats.
|
<T> T |
select(Class<T> clazz)
Select something from the database - either set sql() before calling or primaryKey()
|
<T> List<T> |
selectList(Class<T> clazz)
Select something from the database - either set sql() before calling or primaryKey(...)
|
List<GcCaseIgnoreHashMap> |
selectListMap()
Select a map of rows from the database with column name as key and valueClass as value (should be Object if types differ) from the database - set sql() before calling
Example: select first_name, last_name, middle_name from person where rownum < 3:
List(0)
Map key Map value
first_name Fred
last_name Jones
middle_name Percival
List(1)
Map key Map value
first_name Jeanette
last_name Shawna
middle_name Percival
|
<K,V> Map<K,V> |
selectMap(Class<K> keyClass,
Class<V> valueClass)
Select a map of something from the database - set sql() before calling - this will return a map with column name and column value - this should only select one row from the database.
|
GcCaseIgnoreHashMap |
selectMapMultipleColumnsOneRow()
Select a map of key : column name and value : column value from the database - set sql() before calling.
|
<K,V> Map<K,V> |
selectMapMultipleRows(Class<K> keyClass,
Class<V> valueClass)
Select a map of two column values from the database - set sql() before calling - the first column in the sql will be used for the map keys and the second will be used for the map values.
|
GcDbAccess |
sql(String _sql)
Set the sql to use.
|
<T> void |
storeBatchToDatabase(List<T> objects)
Store the given objects to the database in a batch -
the objects should have appropriate annotations from the PersistableX annotations.
|
<T> void |
storeBatchToDatabase(List<T> objects,
boolean omitPrimaryKeyPopulation)
Store the given objects to the database in a batch -
the objects should have appropriate annotations from the PersistableX annotations.
|
<T> void |
storeBatchToDatabase(List<T> objects,
int batchSize)
Store the given objects to the database in a batch -
the objects should have appropriate annotations from the PersistableX annotations.
|
<T> void |
storeBatchToDatabase(List<T> objects,
int batchSize,
boolean omitPrimaryKeyPopulation)
Store the given objects to the database in a batch -
the objects should have appropriate annotations from the PersistableX annotations.
|
<T> void |
storeListToDatabase(List<T> objects)
Store the given objects to the database in one transaction - the object should have appropriate annotations from the PersistableX annotations.
|
<T> void |
storeToDatabase(T t)
Store the given object to the database - the object should have appropriate annotations from the PersistableX annotations.
|
static void |
transactionEnd(GcTransactionEnd transactionEnd,
boolean endOnlyIfStarted)
end a transaction
|
static void |
transactionEnd(GcTransactionEnd transactionEnd,
boolean endOnlyIfStarted,
String connectionName)
end a transaction
|
public GcDbAccess connectionName(String theConnectionName)
theConnectionName
- public static void transactionEnd(GcTransactionEnd transactionEnd, boolean endOnlyIfStarted)
transactionEnd
- endOnlyIfStarted
- public static void transactionEnd(GcTransactionEnd transactionEnd, boolean endOnlyIfStarted, String connectionName)
transactionEnd
- endOnlyIfStarted
- connectionName
- public static void loadBoundDataConversion(GcBoundDataConversion _boundDataConversion)
_boundDataConversion
- the boundDataConversion to set.public static String createInString(int numberOfBindVariables)
numberOfBindVariables
- is the number of bind variables to use.public GcDbAccess bindVars(Object... _bindVars)
_bindVars
- are the variables to add to the list.public GcDbAccess addBindVar(Object _bindVar)
_bindVar
- is the variable to add to the list.public GcDbAccess batchBindVars(List<List<Object>> _batchBindVars)
_batchBindVars
- are the variables to set.public GcDbAccess cacheMinutes(Integer _cacheMinutes)
Cache the results of a SELECT query for the allotted minutes. Note that cached objects are not immutable; if you modify them you are modifying them in the cache as well.
_cacheMinutes
- is how long to persist the object(s) in cache for after the initial selection.public GcDbAccess sql(String _sql)
_sql
- is the sql to use.public GcDbAccess omitNullValuesForExample()
public GcDbAccess example(Object _example)
_example
- is the example to use.public GcDbAccess queryTimeoutSeconds(Integer _queryTimeoutSeconds)
_queryTimeoutSeconds
- is the amount of seconds to set.public static void accumulateQueryMillis(boolean _accumulateQueryMillis)
If true, the map queryAndTime will be populated with the time spent in each unique query (unique by query string, not considering bind variable values) - BE SURE TO TURN THIS OFF when done debugging, this is ONLY for debugging on the desktop!Turning it off CLEARS the stats, so write it off first! Example: 1. DbAccess.accumulateQueryMillis(true); 2. use application normally 3. Get the results: MaptimeSpentInQueries =
_accumulateQueryMillis
- is whether to accumulate them or not.public static void reportQueriesAndMillisAndTurnOffAccumulation(String fileLocation)
Write the stats of queries and time spent in them to a file at the given location, then stop collection stats. accumulateQueryMillis(true) must be called first to turn on debugging.
fileLocation
- is the location of the file to write.public GcDbAccess primaryKey(Object... _primaryKey)
_primaryKey
- is the _primaryKey to use.public boolean isPreviouslyPersisted(Object o)
Whether this class has already been saved to the database, looks for a field(s) with annotation @Persistable(primaryKeyField=true), assumes that it is a number, and returns true if it is null or larger than 0.
o
- is the object to store to the database.public void deleteFromDatabase(Object o)
o
- is the object to delete from the database.- this annotation must be placed at the class level.
,
these annotations may be placed at the method level depending on your needs.
public <T> void storeListToDatabase(List<T> objects)
T
- is the type to store.objects
- are the object to store to the database.- this annotation must be placed at the class level.
,
these annotations may be placed at the method level depending on your needs.
public <T> void storeToDatabase(T t)
T
- is the type to store.t
- is the object to store to the database.- this annotation must be placed at the class level.
,
these annotations may be placed at the method level depending on your needs.
public <T> void storeBatchToDatabase(List<T> objects, int batchSize)
Store the given objects to the database in a batch - the objects should have appropriate annotations from the PersistableX annotations. You cannot have both inserts and updates in the list of objects to store; they MUST all have the same action (insert or update) being taken against them as jdbc statements supoprt mutliple sqls in a batch but do not support bind variables when using this capability.
T
- is the type to store.objects
- is the list of objects to store to the database.batchSize
- is the size of the batch to insert or update in.- this annotation must be placed at the class level.
,
these annotations may be placed at the method level depending on your needs.
public <T> void storeBatchToDatabase(List<T> objects, int batchSize, boolean omitPrimaryKeyPopulation)
Store the given objects to the database in a batch - the objects should have appropriate annotations from the PersistableX annotations. You cannot have both inserts and updates in the list of objects to store; they MUST all have the same action (insert or update) being taken against them as jdbc statements supoprt mutliple sqls in a batch but do not support bind variables when using this capability.
T
- is the type being stored.objects
- is the list of objects to store to the database.batchSize
- is the size of the batch to insert or update in.omitPrimaryKeyPopulation
- if you DON'T need primary keys populated into your objects, you can set this and save some query time since
we will just set the primary key population as "some_sequence.nextval" instead of selecting it manually before storing the object.- this annotation must be placed at the class level.
,
these annotations may be placed at the method level depending on your needs.
public <T> void storeBatchToDatabase(List<T> objects)
Store the given objects to the database in a batch - the objects should have appropriate annotations from the PersistableX annotations. You cannot have both inserts and updates in the list of objects to store; they MUST all have the same action (insert or update) being taken against them as jdbc statements supoprt mutliple sqls in a batch but do not support bind variables when using this capability.
T
- is the type being stored.objects
- is the list of objects to store to the database.- this annotation must be placed at the class level.
,
these annotations may be placed at the method level depending on your needs.
public <T> void storeBatchToDatabase(List<T> objects, boolean omitPrimaryKeyPopulation)
Store the given objects to the database in a batch - the objects should have appropriate annotations from the PersistableX annotations. You cannot have both inserts and updates in the list of objects to store; they MUST all have the same action (insert or update) being taken against them as jdbc statements supoprt mutliple sqls in a batch but do not support bind variables when using this capability.
T
- is the type being stored.objects
- is the list of objects to store to the database.omitPrimaryKeyPopulation
- if you DON'T need primary keys populated into your objects, you can set this and save some query time since
we will just set the primary key population as "some_sequence.nextval" instead of selecting it manually before storing the object.- this annotation must be placed at the class level.
,
these annotations may be placed at the method level depending on your needs.
public <T> void callbackEntity(Class<T> clazz, GcEntityCallback<T> entityCallback)
For each row of a given resultset, hydrate an object and pass it to the callback.
T
- clazz
- is the type of thing passed to the entity callback.entityCallback
- is the callback object that receives this dbAccess with a session set up.public <T> T callbackTransaction(GcTransactionCallback<T> transactionCallback)
Use a transaction for all calls that happen within this callback. Upon success with no exceptions thrown, commit is called automatically. Upon failure, rollback it called. You may also call dbAccess.setTransactionEnd() within the callback block.
T
- is the type of thing being returned.transactionCallback
- is the callback object that receives this dbAccess with a session set up.public <K,V> Map<K,V> selectMap(Class<K> keyClass, Class<V> valueClass)
K
- V
- keyClass
- is the class of the key.valueClass
- is the class of the value.public <K,V> Map<K,V> selectMapMultipleRows(Class<K> keyClass, Class<V> valueClass)
K
- V
- keyClass
- is the class of the key.valueClass
- is the class of the value.public List<GcCaseIgnoreHashMap> selectListMap()
public GcCaseIgnoreHashMap selectMapMultipleColumnsOneRow()
Select a map of key : column name and value : column value from the database - set sql() before calling. Example: select first_name, last_name, middle_name from person: Map key Map value first_name Fred last_name Jones middle_name Percival
public <T> T select(Class<T> clazz)
T
- is the type of object that will be returned.clazz
- is the type of object that will be returned.public <T> List<T> selectList(Class<T> clazz)
T
- is the type of object that will be returned.clazz
- is the type of object that will be returned.public <T> T callbackCallableStatement(GcCallableStatementCallback<T> callableStatementCallback)
T
- is what you are returning, must be a type but you can return null.callableStatementCallback
- is the callback object.public <T> T callbackPreparedStatement(GcPreparedStatementCallback<T> preparedStatementCallback)
T
- is what you are returning, must be a type but you can return null.preparedStatementCallback
- is the callback object.public <T> T callbackConnection(GcConnectionCallback<T> connectionCallback)
T
- is what you are returning, must be a type but you can return null.connectionCallback
- is the callback object.public <T> T callbackResultSet(GcResultSetCallback<T> resultSetCallback)
T
- is the type of object that will be returned.resultSetCallback
- is the object to callback.public int executeSql()
public int[] executeBatchSql()
public static Map<MultiKey,GcDbQueryCache> getGcDbQueryCacheMap()
public static Map<String,GcQueryReport> getQueriesAndMillis()
Copyright © 2016 Internet2. All rights reserved.