org.mentabean.jdbc
Class JdbcBeanSession

java.lang.Object
  extended by org.mentabean.jdbc.JdbcBeanSession
All Implemented Interfaces:
BeanSession
Direct Known Subclasses:
H2BeanSession, MySQLBeanSession, OracleBeanSession

public class JdbcBeanSession
extends Object
implements BeanSession

The bean session implementation based on JDBC and SQL.

Author:
soliveira

Field Summary
protected  BeanManager beanManager
           
protected  Connection conn
           
protected static boolean DEBUG
           
protected  IdentityHashMap<Object,Map<String,org.mentabean.jdbc.JdbcBeanSession.Value>> loaded
           
 
Constructor Summary
JdbcBeanSession(BeanManager beanManager, Connection conn)
          Creates a JdbcBeanSession with a BeanManager and a Connection.
 
Method Summary
 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, String tablePrefix)
          Build a column/field list for a SQL SELECT statement based on the bean configuration.
 String buildSelectMinus(Class<? extends Object> beanClass, String[] minus)
          Like buildSelect but you can exclude some properties from the resulting list.
 String buildSelectMinus(Class<? extends Object> beanClass, String tablePrefix, String[] minus)
          Same as buildSelectMinus with support for a database table prefix that will be applied on each field.
 int countList(Object bean)
          Count the number of beans that would be returned by a loadList method.
static void debugSql(boolean b)
          Turn SQL debugging on and off.
 boolean delete(Object bean)
          Delete the bean from the database.
 Connection getConnection()
          Get the connection associated with this JdbcBeanSession.
protected  String getNow()
          Get the command representing 'now' in this database.
protected static Object getValueFromBean(Object bean, String fieldName)
          Get a value from a bean through reflection.
protected static Object getValueFromBean(Object bean, String fieldName, Method m)
          Get a value from a bean through reflection.
protected  StringBuilder handleLimit(StringBuilder sb, String orderBy, int limit)
          Some databases will sort before applying the limit (MySql), others will not (Oracle).
protected static 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.
 boolean load(Object bean)
          Load the bean from the database, injecting all its properties through reflection.
<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, int limit)
          Same as loadList(bean) except that you can limit the number of beans returned in the list.
<E> List<E>
loadList(E bean, String 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, String orderBy, int 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>
loadListMinus(E bean, String[] minus)
          Load a list of beans, but exclude some fields.
<E> List<E>
loadListMinus(E bean, String[] minus, int limit)
          Load a list of beans, but exclude some fields.
<E> List<E>
loadListMinus(E bean, String[] minus, String orderBy)
          Load a list of beans, but exclude some fields.
<E> List<E>
loadListMinus(E bean, String[] minus, String orderBy, int limit)
          Load a list of beans, but exclude some fields.
<E> E
loadUnique(E bean)
          Same as loadList(bean) but it attempts to load a single bean only.
 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, String tablePrefix)
          Same as populateBean, but use a table prefix before fetching the values from the result set.
 void populateBeanMinus(ResultSet rset, Object bean, String[] minus)
          Same as populateBean, but exclude some fields when populating.
 void populateBeanMinus(ResultSet rset, Object bean, String tablePrefix, String[] minus)
          Same as populateBean, but exclude some fields when populating and use a table prefix in front of the field names.
 int update(Object bean)
          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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

protected static boolean DEBUG

loaded

protected IdentityHashMap<Object,Map<String,org.mentabean.jdbc.JdbcBeanSession.Value>> loaded

conn

protected Connection conn

beanManager

protected final BeanManager beanManager
Constructor Detail

JdbcBeanSession

public JdbcBeanSession(BeanManager beanManager,
                       Connection conn)
Creates a JdbcBeanSession with a BeanManager and a Connection.

Parameters:
beanManager - The bean manager
conn - The database connection
Method Detail

debugSql

public static void debugSql(boolean b)
Turn SQL debugging on and off.

Parameters:
b - true if it should be debugged

getConnection

public Connection getConnection()
Get the connection associated with this JdbcBeanSession.

Returns:
the database connection

getNow

protected String getNow()
Get the command representing 'now' in this database. This base implementation returns null, in other words, no now command will be used.

Returns:
the command for now in this database (now(), sysdate, etc)

getValueFromBean

protected static Object getValueFromBean(Object bean,
                                         String fieldName)
Get a value from a bean through reflection.

Parameters:
bean -
fieldName -
Returns:
The value of a bean property

getValueFromBean

protected static Object getValueFromBean(Object bean,
                                         String fieldName,
                                         Method m)
Get a value from a bean through reflection.

Parameters:
bean -
fieldName -
m -
Returns:
The value of a bean property

load

public boolean load(Object bean)
Description copied from interface: BeanSession
Load the bean from the database, injecting all its properties through reflection. Note that the bean passed MUST have its primary key set otherwise there is no way we can load it from the database.

Specified by:
load in interface BeanSession
Parameters:
bean - The bean we want to load from the DB.
Returns:
true if the bean was found in the database, false otherwise

injectValue

protected static void injectValue(Object bean,
                                  String fieldName,
                                  Object value,
                                  Class<? extends Object> valueType)
Inject a value in a bean through reflection.

Parameters:
bean -
fieldName -
value -
valueType -

handleLimit

protected StringBuilder handleLimit(StringBuilder sb,
                                    String orderBy,
                                    int limit)
Some databases will sort before applying the limit (MySql), others will not (Oracle). Handle each one accordingly. Note: This base implementation does nothing.

Parameters:
sb -
orderBy -
limit -
Returns:
A string builder with the the SQL modified for the limit operation

buildSelect

public String buildSelect(Class<? extends Object> beanClass)
Build the column/field list for a SQL SELECT statement based on the bean configuration. Very useful to create select statements.

Parameters:
beanClass - the bean class
Returns:
the column/field list for a select

buildSelect

public String buildSelect(Class<? extends Object> beanClass,
                          String tablePrefix)
Build a column/field list for a SQL SELECT statement based on the bean configuration. A table prefix will be used on each field. Very useful to create select statements on multiple tables (joins).

Parameters:
beanClass - the bean class
tablePrefix - the table prefix to use before each field
Returns:
the column/field list for a select

buildSelectMinus

public String buildSelectMinus(Class<? extends Object> beanClass,
                               String[] minus)
Like buildSelect but you can exclude some properties from the resulting list. Useful when you have a bean with too many properties and you just want to fetch a few. Note: The list of properties to exclude contains 'property names' and NOT database column names.

Parameters:
beanClass - the bean class
minus - a list for property names to exclude
Returns:
the column/field list for a select

buildSelectMinus

public String buildSelectMinus(Class<? extends Object> beanClass,
                               String tablePrefix,
                               String[] minus)
Same as buildSelectMinus with support for a database table prefix that will be applied on each field.

Parameters:
beanClass - the bean class
tablePrefix - the database table prefix
minus - a list of property names to exclude
Returns:
the column/field list for a select

populateBean

public 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.

Parameters:
rset - the result set from where to get the property values
bean - the bean to be populated
Throws:
Exception

populateBean

public void populateBean(ResultSet rset,
                         Object bean,
                         String tablePrefix)
Same as populateBean, but use a table prefix before fetching the values from the result set. Useful when there are multiple tables involved and you want to avoid field name clashing.

Parameters:
rset - the result set
bean - the bean to be populated
tablePrefix - the table prefix

populateBeanMinus

public void populateBeanMinus(ResultSet rset,
                              Object bean,
                              String[] minus)
Same as populateBean, but exclude some fields when populating.

Parameters:
rset -
bean -
minus -

populateBeanMinus

public void populateBeanMinus(ResultSet rset,
                              Object bean,
                              String tablePrefix,
                              String[] minus)
Same as populateBean, but exclude some fields when populating and use a table prefix in front of the field names.

Parameters:
rset -
bean -
tablePrefix -
minus -

loadListMinus

public <E> List<E> loadListMinus(E bean,
                                 String[] minus,
                                 String orderBy,
                                 int limit)
Load a list of beans, but exclude some fields.

Type Parameters:
E -
Parameters:
bean -
minus -
orderBy -
limit -
Returns:
A list of beans

loadList

public <E> List<E> loadList(E bean,
                            String orderBy,
                            int limit)
Description copied from interface: BeanSession
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.

Specified by:
loadList in interface BeanSession
Parameters:
bean - 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.
Returns:
A list of beans the match the properties in the given bean. Or an empty list if nothing was found.

countList

public int countList(Object bean)
Description copied from interface: BeanSession
Count the number of beans that would be returned by a loadList method.

Specified by:
countList in interface BeanSession
Parameters:
bean - The bean holding the properties used by the list query.
Returns:
the number of beans found in the database

isSet

protected boolean isSet(Object value,
                        Class<? extends Object> returnType)
if Boolean consider TRUE to be set and FALSE to be not set. if Character, cast to integer and assume it is set if different than 0 if Number consider everything different than zero to be set. Otherwise returns TRUE for anything different than null and FALSE for null.

Parameters:
value -
returnType -
Returns:
true if is set

update

public int update(Object bean)
Description copied from interface: BeanSession
Update the bean in the database. Only the bean fields that have been modified (dirty) will be updated. It will return 1 if an update did happen or 0 if the bean could not be found in the database or if there was nothing modified in bean. The bean MUST have its primary key set, otherwise it is impossible to update the bean in the database, and an exception will be thrown.

Specified by:
update in interface BeanSession
Parameters:
bean - The bean to be updated
Returns:
1 if update was successful, 0 if the update did not happen or was not necessary

updateAll

public int updateAll(Object bean)
Description copied from interface: BeanSession
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.

Specified by:
updateAll in interface BeanSession
Returns:
the number of rows that were updated

insert

public void insert(Object bean)
Description copied from interface: BeanSession
Insert the bean in the database. Depending on the type of PK, the generation of the PK can and should be taken care by the DB itself. The generated PK should be inserted in the bean by reflection, before the method returns. The default, database-independent implementation of this method, insert all fields in the database not worrying about PK generation strategies.

Specified by:
insert in interface BeanSession
Parameters:
bean - The bean to insert

delete

public boolean delete(Object bean)
Description copied from interface: BeanSession
Delete the bean from the database. The PK of the bean MUST be set. The bean can only be deleted by its PK.

Specified by:
delete in interface BeanSession
Returns:
true if it was deleted or false if it did not exist

loadList

public <E> List<E> loadList(E bean)
Description copied from interface: BeanSession
Load a list of beans based on the properties present in the bean passed. For example, if you want to load all users with lastName equal to 'saoj' you would instantiate a bean and set its lastName property to 'saoj' before passing it as an argument to this method.

Specified by:
loadList in interface BeanSession
Parameters:
bean - The bean holding the properties used by the list query.
Returns:
A list of beans the match the properties in the given bean. Or an empty list if nothing was found.

loadUnique

public <E> E loadUnique(E bean)
Description copied from interface: BeanSession
Same as loadList(bean) but it attempts to load a single bean only. If more than one bean is found it throws an exception.

Specified by:
loadUnique in interface BeanSession
Parameters:
bean - The bean holding the properties used by the load query.
Returns:
A unique bean that match the properties in the given bean. Or null if nothing was found.

loadList

public <E> List<E> loadList(E bean,
                            String orderBy)
Description copied from interface: BeanSession
Same as loadList(bean) except that you can order the list returned by passing an SQL orderBy clause.

Specified by:
loadList in interface BeanSession
Parameters:
bean - The bean holding the properties used by the list query.
orderBy - The orderBy SQL clause.
Returns:
A list of beans the match the properties in the given bean. Or an empty list if nothing was found.

loadList

public <E> List<E> loadList(E bean,
                            int limit)
Description copied from interface: BeanSession
Same as loadList(bean) except that you can limit the number of beans returned in the list.

Specified by:
loadList in interface BeanSession
Parameters:
bean - The bean holding the properties used by the list query.
limit - The max number of beans returned in the list.
Returns:
A list of beans the match the properties in the given bean. Or an empty list if nothing was found.

loadListMinus

public <E> List<E> loadListMinus(E bean,
                                 String[] minus)
Load a list of beans, but exclude some fields. Useful when the bean has too many properties and you don't want to fetch everything from the database.

Type Parameters:
E -
Parameters:
bean -
minus -
Returns:
A list of beans

loadListMinus

public <E> List<E> loadListMinus(E bean,
                                 String[] minus,
                                 String orderBy)
Load a list of beans, but exclude some fields. Useful when the bean has too many properties and you don't want to fetch everything from the database.

Type Parameters:
E -
Parameters:
bean -
minus -
orderBy -
Returns:
A list of beans

loadListMinus

public <E> List<E> loadListMinus(E bean,
                                 String[] minus,
                                 int limit)
Load a list of beans, but exclude some fields. Useful when the bean has too many properties and you don't want to fetch everything from the database.

Type Parameters:
E -
Parameters:
bean -
minus -
limit -
Returns:
A list of beans


Copyright © 2011. All Rights Reserved.