public class AnsiSQLBeanSession extends Object implements BeanSession
| Modifier and Type | Class and Description |
|---|---|
protected class |
AnsiSQLBeanSession.QueryAndValues |
protected class |
AnsiSQLBeanSession.Value |
| Modifier and Type | Field and Description |
|---|---|
protected BeanManager |
beanManager |
protected Connection |
conn |
protected static boolean |
DEBUG |
protected static boolean |
DEBUG_NATIVE |
protected TriggerDispatcher |
dispatcher |
protected IdentityHashMap<Object,Map<String,AnsiSQLBeanSession.Value>> |
loaded |
INSERT, UPDATE| Constructor and Description |
|---|
AnsiSQLBeanSession(BeanManager beanManager,
Connection conn)
Creates a JdbcBeanSession with a BeanManager and a Connection.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addTrigger(TriggerListener trigger)
Add a TriggerListener in this session.
|
protected Map<String,AnsiSQLBeanSession.Value> |
bindToInsertStatement(PreparedStatement stmt,
List<AnsiSQLBeanSession.Value> values) |
QueryBuilder |
buildQuery()
Builds a new QueryBuilder so it's possible to create fluent custom SQL queries
|
String |
buildSelect(Class<? extends Object> beanClass)
Build the column/field list for a SQL SELECT statement based on the bean configuration.
|
String |
buildSelect(Class<? extends Object> beanClass,
Object... properties) |
String |
buildSelect(Class<? extends Object> beanClass,
String tablePrefix)
Build a column/field list for a SQL SELECT statement based on the bean configuration.
|
String |
buildSelect(Class<? extends Object> beanClass,
String tablePrefix,
Object... properties) |
protected String |
buildSelectImpl(Class<? extends Object> beanClass,
String tablePrefix,
String[] properties,
String[] minus,
boolean includePK,
boolean addSuffix) |
String |
buildSelectMinus(Class<? extends Object> beanClass,
Object... minus)
Like buildSelect but you can exclude some properties from the resulting list.
|
String |
buildSelectMinus(Class<? extends Object> beanClass,
String tablePrefix,
Object... minus)
Same as buildSelectMinus with support for a database table prefix that will be applied on each field.
|
String |
buildTableName(Class<? extends Object> clazz)
Returns the table name configured for the given class
|
<E> E |
compareDifferences(E bean,
E another,
List<String> nullProps)
Compare differences between two beans holding the properties of first bean passed and
adding null properties in nullProps list.
|
int |
countList(Object bean)
Count the number of beans that would be returned by a loadList method.
|
<E> E |
createBasicInstance(E bean)
Creates an single instance only with primary keys according the given object
|
protected String |
createPKConstraintQuery(String table,
Iterator<DBField> pks)
Create a SQL query to add the primary key constraint
|
void |
createTable(Class<? extends Object> beanKlass) |
<E> TableAlias<E> |
createTableAlias(Class<? extends E> beanClass)
Return a table alias for this bean class
|
<E> TableAlias<E> |
createTableAlias(Class<? extends E> beanClass,
String prefix)
Return a table alias for this bean class with a prefix.
|
void |
createTables() |
static void |
debugNativeSql(boolean b)
Turn SQL native queries debugging on and off.
|
static void |
debugSql(boolean b)
Turn SQL debugging on and off.
|
boolean |
delete(Object bean)
Delete the bean from the database.
|
int |
deleteAll(Object bean)
Delete all data based on the properties present in the bean passed
The PK doesn't need to be set
|
protected void |
dispatchAfterDelete(Object bean) |
protected void |
dispatchAfterInsert(Object bean) |
protected void |
dispatchAfterUpdate(Object bean) |
protected void |
dispatchBeforeDelete(Object bean) |
protected void |
dispatchBeforeInsert(Object bean) |
protected void |
dispatchBeforeUpdate(Object bean) |
protected void |
dispatchTrigger(TriggerDispatcher.Type type,
Object bean)
Dispatch all triggers from actual
BeanSession and respective BeanConfig. |
void |
dropTable(Class<? extends Object> beanKlass) |
BeanConfig |
getConfigFor(Class<? extends Object> clazz)
Returns the BeanConfig object from the given bean class
|
Connection |
getConnection()
Get the connection associated with this JdbcBeanSession.
|
protected String |
getCurrentTimestampCommand()
Get the command representing 'now' in this database.
|
protected String |
getDatabaseType(DBType<?> dbType)
Each dialect can override this to return the database column type it supports other than the ANSI standard.
|
static String[] |
getProperties(Object[] names) |
protected Object |
getPropertyBean(Object target,
String name,
boolean create)
Get a value from target through reflection and tries to create a new instance if create parameter is true
|
protected Object |
getValueFromBean(Object bean,
String fieldName)
Get a value from a bean through reflection.
|
protected Object |
getValueFromBean(Object bean,
String fieldName,
Method m)
Get a value from a bean through reflection.
|
protected StringBuilder |
handleLimit(StringBuilder sb,
OrderBy orderBy,
Limit limit)
Some databases will sort before applying the limit (MySql), others will not (Oracle).
|
protected void |
injectValue(Object bean,
String fieldName,
Object value,
Class<? extends Object> valueType)
Inject a value in a bean through reflection.
|
void |
insert(Object bean)
Insert the bean in the database.
|
protected boolean |
isSet(Object value,
Class<? extends Object> returnType)
if Boolean consider TRUE to be set and FALSE to be not set.
|
protected boolean |
isVarcharUnlimitedSupported()
Each dialect can override this to return true if the VARCHAR type supports unlimited size
|
boolean |
load(Object bean)
Load the bean from the database, injecting all its properties through reflection.
|
boolean |
load(Object bean,
Object... properties) |
protected boolean |
loadImpl(Object bean,
String[] properties,
String[] minus) |
<E> List<E> |
loadList(E bean)
Load a list of beans based on the properties present in the bean passed.
|
<E> List<E> |
loadList(E bean,
Limit limit)
Same as loadList(bean) except that you can limit the number of beans returned in the list.
|
<E> List<E> |
loadList(E bean,
Limit limit,
Object... properties) |
<E> List<E> |
loadList(E bean,
Object... properties) |
<E> List<E> |
loadList(E bean,
OrderBy orderBy)
Same as loadList(bean) except that you can order the list returned by passing an SQL orderBy clause.
|
<E> List<E> |
loadList(E bean,
OrderBy orderBy,
Limit limit)
Same as loadList(bean) except that you can limit the number of beans returned in the list as well as sort them by passing a orderBy SQL clause.
|
<E> List<E> |
loadList(E bean,
OrderBy orderBy,
Limit limit,
Object... properties) |
<E> List<E> |
loadList(E bean,
OrderBy orderBy,
Object... properties) |
<E> List<E> |
loadListMinus(E bean,
Limit limit,
Object... minus)
Load a list of beans, but exclude some fields.
|
<E> List<E> |
loadListMinus(E bean,
Object... minus)
Load a list of beans, but exclude some fields.
|
<E> List<E> |
loadListMinus(E bean,
OrderBy orderBy,
Limit limit,
Object... minus)
Load a list of beans, but exclude some fields.
|
<E> List<E> |
loadListMinus(E bean,
OrderBy orderBy,
Object... minus)
Load a list of beans, but exclude some fields.
|
boolean |
loadMinus(Object bean,
Object... minus) |
<E> E |
loadUnique(E bean)
Same as loadList(bean) but it attempts to load a single bean only.
|
<E> E |
loadUnique(E bean,
Object... properties) |
protected <E> E |
loadUniqueImpl(E bean,
String[] properties,
String[] minus) |
<E> E |
loadUniqueMinus(E bean,
Object... minus) |
void |
populateBean(ResultSet rset,
Object bean)
Populate a bean (insert all its properties) from the results in a result set, based on the bean configuration.
|
void |
populateBean(ResultSet rset,
Object bean,
Object... properties) |
void |
populateBean(ResultSet rset,
Object bean,
String tablePrefix)
Same as populateBean, but use a table prefix before fetching the values from the result set.
|
void |
populateBean(ResultSet rset,
Object bean,
String tablePrefix,
Object... properties) |
protected void |
populateBeanImpl(ResultSet rset,
Object bean,
String tablePrefix,
String[] properties,
String[] minus,
boolean includePK) |
void |
populateBeanMinus(ResultSet rset,
Object bean,
Object... minus)
Same as populateBean, but exclude some fields when populating.
|
void |
populateBeanMinus(ResultSet rset,
Object bean,
String tablePrefix,
Object... minus)
Same as populateBean, but exclude some fields when populating and use a table prefix in front of the field names.
|
protected AnsiSQLBeanSession.QueryAndValues |
prepareInsertQuery(Object bean) |
String |
propertyToColumn(BeanConfig bc,
Object property)
Returns a database column name for a bean attribute.
|
String |
propertyToColumn(Class<? extends Object> clazz,
Object property) |
String |
propertyToColumn(Class<? extends Object> clazz,
Object property,
String alias) |
void |
removeTrigger(TriggerListener trigger)
Removes the TriggerListener from session
|
protected int |
save(Object bean,
boolean dynUpdate,
String[] nullProps)
Update or insert a bean into database.
|
int |
save(Object bean,
Object... forceNull)
Tries to update or insert a bean using the
update method. |
int |
saveAll(Object bean)
Tries to update or insert a bean object into database.
|
protected Object |
secureLoadUnique(Object bean) |
int |
update(Object bean,
Object... forceNull)
Update the bean in the database.
|
int |
updateAll(Object bean)
Same as update(bean) but here you can turn off the default dynamic update behavior and force all bean properties to be updated regardless whether they have been modified or not.
|
<E> int |
updateDiff(E newBean,
E oldBean)
Updates an object using only the differences between newBean and oldBean instances.
|
protected static boolean DEBUG
protected static boolean DEBUG_NATIVE
protected IdentityHashMap<Object,Map<String,AnsiSQLBeanSession.Value>> loaded
protected Connection conn
protected final BeanManager beanManager
protected final TriggerDispatcher dispatcher
public AnsiSQLBeanSession(BeanManager beanManager, Connection conn)
beanManager - The bean managerconn - The database connectionpublic static void debugSql(boolean b)
b - true if it should be debuggedpublic static void debugNativeSql(boolean b)
b - true if it should be debuggedpublic Connection getConnection()
getConnection in interface BeanSessionprotected String getCurrentTimestampCommand()
protected Object getValueFromBean(Object bean, String fieldName)
bean - fieldName - protected Object getValueFromBean(Object bean, String fieldName, Method m)
bean - fieldName - m - public boolean load(Object bean)
BeanSessionload in interface BeanSessionbean - The bean we want to load from the DB.public boolean load(Object bean, Object... properties)
load in interface BeanSessionpublic boolean loadMinus(Object bean, Object... minus)
loadMinus in interface BeanSessionprotected Object getPropertyBean(Object target, String name, boolean create)
target - name - create - protected void injectValue(Object bean, String fieldName, Object value, Class<? extends Object> valueType)
bean - fieldName - value - valueType - protected StringBuilder handleLimit(StringBuilder sb, OrderBy orderBy, Limit limit)
sb - orderBy - limit - public String buildSelect(Class<? extends Object> beanClass)
buildSelect in interface BeanSessionbeanClass - the bean classpublic String buildSelect(Class<? extends Object> beanClass, Object... properties)
buildSelect in interface BeanSessionpublic String buildSelect(Class<? extends Object> beanClass, String tablePrefix)
buildSelect in interface BeanSessionbeanClass - the bean classtablePrefix - the table prefix to use before each fieldpublic String buildSelect(Class<? extends Object> beanClass, String tablePrefix, Object... properties)
buildSelect in interface BeanSessionpublic String buildSelectMinus(Class<? extends Object> beanClass, Object... minus)
buildSelectMinus in interface BeanSessionbeanClass - the bean classminus - a list for property names to excludepublic String buildSelectMinus(Class<? extends Object> beanClass, String tablePrefix, Object... minus)
buildSelectMinus in interface BeanSessionbeanClass - the bean classtablePrefix - the database table prefixminus - a list of property names to excludeprotected String buildSelectImpl(Class<? extends Object> beanClass, String tablePrefix, String[] properties, String[] minus, boolean includePK, boolean addSuffix)
public void populateBean(ResultSet rset, Object bean)
populateBean in interface BeanSessionrset - the result set from where to get the property valuesbean - the bean to be populatedExceptionpublic void populateBean(ResultSet rset, Object bean, Object... properties)
populateBean in interface BeanSessionpublic void populateBean(ResultSet rset, Object bean, String tablePrefix)
populateBean in interface BeanSessionrset - the result setbean - the bean to be populatedtablePrefix - the table prefixpublic void populateBean(ResultSet rset, Object bean, String tablePrefix, Object... properties)
populateBean in interface BeanSessionpublic void populateBeanMinus(ResultSet rset, Object bean, Object... minus)
populateBeanMinus in interface BeanSessionrset - bean - minus - public void populateBeanMinus(ResultSet rset, Object bean, String tablePrefix, Object... minus)
populateBeanMinus in interface BeanSessionrset - bean - tablePrefix - minus - protected void populateBeanImpl(ResultSet rset, Object bean, String tablePrefix, String[] properties, String[] minus, boolean includePK)
public <E> List<E> loadListMinus(E bean, OrderBy orderBy, Limit limit, Object... minus)
loadListMinus in interface BeanSessionE - bean - minus - orderBy - limit - public <E> List<E> loadList(E bean, OrderBy orderBy, Limit limit)
BeanSessionloadList in interface BeanSessionbean - The bean holding the properties used by the list query.orderBy - The orderBy SQL clause.limit - The max number of beans returned in the list.public <E> List<E> loadList(E bean, OrderBy orderBy, Limit limit, Object... properties)
loadList in interface BeanSessionpublic String propertyToColumn(BeanConfig bc, Object property)
bc - - The BeanConfig objectproperty - - A bean propertypropertypublic String propertyToColumn(Class<? extends Object> clazz, Object property)
propertyToColumn in interface BeanSessionpublic String propertyToColumn(Class<? extends Object> clazz, Object property, String alias)
propertyToColumn in interface BeanSessionpublic String buildTableName(Class<? extends Object> clazz)
BeanSessionbuildTableName in interface BeanSessionclazz - - The bean classpublic QueryBuilder buildQuery()
BeanSessionbuildQuery in interface BeanSessionpublic int countList(Object bean)
BeanSessioncountList in interface BeanSessionbean - The bean holding the properties used by the list query.protected boolean isSet(Object value, Class<? extends Object> returnType)
value - returnType - public int update(Object bean, Object... forceNull)
BeanSessionupdate in interface BeanSessionbean - The bean to be updatedforceNull - Database columns that will be forced to null if the bean property is not setpublic int updateAll(Object bean)
BeanSessionupdateAll in interface BeanSessionpublic <E> E createBasicInstance(E bean)
BeanSessioncreateBasicInstance in interface BeanSessionbean - - The bean with all primary key setpublic <E> int updateDiff(E newBean,
E oldBean)
BeanSessionupdateDiff in interface BeanSessionpublic <E> E compareDifferences(E bean,
E another,
List<String> nullProps)
BeanSessioncompareDifferences in interface BeanSessionnullProps - An empty listpublic int save(Object bean, Object... forceNull)
BeanSessionupdate method.save in interface BeanSessionbean - The bean to update or insertforceNull - Database columns that will be forced to null (or zero) if the bean property is not setupdate and 1 (one) if insert method was executedBeanSession.saveAll(Object),
BeanSession.update(Object, Object...)public int saveAll(Object bean)
BeanSessionupdateAll method.saveAll in interface BeanSessionbean - The bean to update or insertupdate and 1 (one) if insert method was executedBeanSession.updateAll(Object)protected int save(Object bean, boolean dynUpdate, String[] nullProps)
bean - Object to update or insertdynUpdate - flag indicating a dynamic updateupdate,
1 (one) if insert method was executedsaveAll(Object),
save(Object, Object...)protected AnsiSQLBeanSession.QueryAndValues prepareInsertQuery(Object bean)
protected Map<String,AnsiSQLBeanSession.Value> bindToInsertStatement(PreparedStatement stmt, List<AnsiSQLBeanSession.Value> values)
public void insert(Object bean)
BeanSessioninsert in interface BeanSessionbean - The bean to insertpublic int deleteAll(Object bean)
BeanSessiondeleteAll in interface BeanSessionpublic boolean delete(Object bean)
BeanSessiondelete in interface BeanSessionpublic <E> List<E> loadList(E bean)
BeanSessionloadList in interface BeanSessionbean - The bean holding the properties used by the list query.public <E> List<E> loadList(E bean, Object... properties)
loadList in interface BeanSessionpublic <E> E loadUnique(E bean)
BeanSessionloadUnique in interface BeanSessionbean - The bean holding the properties used by the load query.public <E> E loadUnique(E bean,
Object... properties)
loadUnique in interface BeanSessionpublic <E> E loadUniqueMinus(E bean,
Object... minus)
loadUniqueMinus in interface BeanSessionpublic <E> List<E> loadList(E bean, OrderBy orderBy)
BeanSessionloadList in interface BeanSessionbean - The bean holding the properties used by the list query.orderBy - The orderBy SQL clause.public <E> List<E> loadList(E bean, OrderBy orderBy, Object... properties)
loadList in interface BeanSessionpublic <E> List<E> loadList(E bean, Limit limit)
BeanSessionloadList in interface BeanSessionbean - The bean holding the properties used by the list query.limit - The max number of beans returned in the list.public <E> List<E> loadList(E bean, Limit limit, Object... properties)
loadList in interface BeanSessionpublic <E> List<E> loadListMinus(E bean, Object... minus)
loadListMinus in interface BeanSessionE - bean - minus - public <E> List<E> loadListMinus(E bean, OrderBy orderBy, Object... minus)
loadListMinus in interface BeanSessionE - bean - minus - orderBy - public <E> List<E> loadListMinus(E bean, Limit limit, Object... minus)
loadListMinus in interface BeanSessionE - bean - minus - limit - protected String getDatabaseType(DBType<?> dbType)
dbType - protected boolean isVarcharUnlimitedSupported()
public void createTable(Class<? extends Object> beanKlass)
createTable in interface BeanSessionpublic void createTables()
createTables in interface BeanSessionpublic void dropTable(Class<? extends Object> beanKlass)
dropTable in interface BeanSessionprotected String createPKConstraintQuery(String table, Iterator<DBField> pks)
table - - The table namepks - - An iterator of all primary key fields that will be added to the tablepublic BeanConfig getConfigFor(Class<? extends Object> clazz)
BeanSessiongetConfigFor in interface BeanSessionpublic void addTrigger(TriggerListener trigger)
BeanSessionaddTrigger in interface BeanSessionpublic void removeTrigger(TriggerListener trigger)
BeanSessionremoveTrigger in interface BeanSessionprotected void dispatchBeforeInsert(Object bean)
protected void dispatchAfterInsert(Object bean)
protected void dispatchBeforeUpdate(Object bean)
protected void dispatchAfterUpdate(Object bean)
protected void dispatchBeforeDelete(Object bean)
protected void dispatchAfterDelete(Object bean)
protected void dispatchTrigger(TriggerDispatcher.Type type, Object bean)
BeanSession and respective BeanConfig.
This method is called when insert, update or delete operation occurs.type - - TriggerType indicating what trigger event will be dispatchedbean - - Bean that will be set into TriggerEvent objectpublic <E> TableAlias<E> createTableAlias(Class<? extends E> beanClass)
BeanSessioncreateTableAlias in interface BeanSessionpublic <E> TableAlias<E> createTableAlias(Class<? extends E> beanClass, String prefix)
BeanSessioncreateTableAlias in interface BeanSessionCopyright © 2016. All Rights Reserved.