org.mentabean.jdbc
Class AnsiSQLBeanSession

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

public class AnsiSQLBeanSession
extends Object
implements BeanSession

The bean session implementation based on JDBC and SQL.

Author:
soliveira

Nested Class Summary
protected  class AnsiSQLBeanSession.QueryAndValues
           
protected  class AnsiSQLBeanSession.Value
           
 
Field Summary
protected  BeanManager beanManager
           
protected  Connection conn
           
protected static boolean DEBUG
           
protected  IdentityHashMap<Object,Map<String,AnsiSQLBeanSession.Value>> loaded
           
 
Constructor Summary
AnsiSQLBeanSession(BeanManager beanManager, Connection conn)
          Creates a JdbcBeanSession with a BeanManager and a Connection.
 
Method Summary
protected  Map<String,AnsiSQLBeanSession.Value> bindToInsertStatement(PreparedStatement stmt, List<AnsiSQLBeanSession.Value> values)
           
 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)
           
 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.
 int countList(Object bean)
          Count the number of beans that would be returned by a loadList method.
protected  String createPKConstraintQuery(String table, Iterator<DBField> pks)
          Create a SQL query to add the primary key constraint
 void createTable(Class<? extends Object> beanKlass)
           
 void createTables()
           
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 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 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, OrderBy orderBy, Limit 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.
 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)
           
 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)
           
 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,AnsiSQLBeanSession.Value>> loaded

conn

protected Connection conn

beanManager

protected final BeanManager beanManager
Constructor Detail

AnsiSQLBeanSession

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

Specified by:
getConnection in interface BeanSession
Returns:
the database connection

getCurrentTimestampCommand

protected String getCurrentTimestampCommand()
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

getProperties

public static String[] getProperties(Object[] names)

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

load

public boolean load(Object bean,
                    Object... properties)
Specified by:
load in interface BeanSession

loadMinus

public boolean loadMinus(Object bean,
                         Object... minus)
Specified by:
loadMinus in interface BeanSession

loadImpl

protected boolean loadImpl(Object bean,
                           String[] properties,
                           String[] minus)

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,
                                    OrderBy orderBy,
                                    Limit 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.

Specified by:
buildSelect in interface BeanSession
Parameters:
beanClass - the bean class
Returns:
the column/field list for a select

buildSelect

public String buildSelect(Class<? extends Object> beanClass,
                          Object... properties)
Specified by:
buildSelect in interface BeanSession

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

Specified by:
buildSelect in interface BeanSession
Parameters:
beanClass - the bean class
tablePrefix - the table prefix to use before each field
Returns:
the column/field list for a select

buildSelect

public String buildSelect(Class<? extends Object> beanClass,
                          String tablePrefix,
                          Object... properties)
Specified by:
buildSelect in interface BeanSession

buildSelectMinus

public String buildSelectMinus(Class<? extends Object> beanClass,
                               Object... 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.

Specified by:
buildSelectMinus in interface BeanSession
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,
                               Object... minus)
Same as buildSelectMinus with support for a database table prefix that will be applied on each field.

Specified by:
buildSelectMinus in interface BeanSession
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.

Specified by:
populateBean in interface BeanSession
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,
                         Object... properties)
Specified by:
populateBean in interface BeanSession

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.

Specified by:
populateBean in interface BeanSession
Parameters:
rset - the result set
bean - the bean to be populated
tablePrefix - the table prefix

populateBean

public void populateBean(ResultSet rset,
                         Object bean,
                         String tablePrefix,
                         Object... properties)
Specified by:
populateBean in interface BeanSession

populateBeanMinus

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

Specified by:
populateBeanMinus in interface BeanSession
Parameters:
rset -
bean -
minus -

populateBeanMinus

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

Specified by:
populateBeanMinus in interface BeanSession
Parameters:
rset -
bean -
tablePrefix -
minus -

loadListMinus

public <E> List<E> loadListMinus(E bean,
                                 OrderBy orderBy,
                                 Limit limit,
                                 Object... minus)
Load a list of beans, but exclude some fields.

Specified by:
loadListMinus in interface BeanSession
Type Parameters:
E -
Parameters:
bean -
minus -
orderBy -
limit -
Returns:
A list of beans

loadList

public <E> List<E> loadList(E bean,
                            OrderBy orderBy,
                            Limit 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.

loadList

public <E> List<E> loadList(E bean,
                            OrderBy orderBy,
                            Limit limit,
                            Object... properties)
Specified by:
loadList in interface BeanSession

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

prepareInsertQuery

protected AnsiSQLBeanSession.QueryAndValues prepareInsertQuery(Object bean)

bindToInsertStatement

protected Map<String,AnsiSQLBeanSession.Value> bindToInsertStatement(PreparedStatement stmt,
                                                                     List<AnsiSQLBeanSession.Value> values)

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.

loadList

public <E> List<E> loadList(E bean,
                            Object... properties)
Specified by:
loadList in interface BeanSession

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. NOTE: The returned bean will be attached by the session so only the modified properties will be updated in case update() is called.

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.

loadUnique

public <E> E loadUnique(E bean,
                        Object... properties)
Specified by:
loadUnique in interface BeanSession

loadUniqueMinus

public <E> E loadUniqueMinus(E bean,
                             Object... minus)
Specified by:
loadUniqueMinus in interface BeanSession

loadUniqueImpl

protected <E> E loadUniqueImpl(E bean,
                               String[] properties,
                               String[] minus)

loadList

public <E> List<E> loadList(E bean,
                            OrderBy 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,
                            OrderBy orderBy,
                            Object... properties)
Specified by:
loadList in interface BeanSession

loadList

public <E> List<E> loadList(E bean,
                            Limit 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.

loadList

public <E> List<E> loadList(E bean,
                            Limit limit,
                            Object... properties)
Specified by:
loadList in interface BeanSession

loadListMinus

public <E> List<E> loadListMinus(E bean,
                                 Object... 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.

Specified by:
loadListMinus in interface BeanSession
Type Parameters:
E -
Parameters:
bean -
minus -
Returns:
A list of beans

loadListMinus

public <E> List<E> loadListMinus(E bean,
                                 OrderBy orderBy,
                                 Object... 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.

Specified by:
loadListMinus in interface BeanSession
Type Parameters:
E -
Parameters:
bean -
minus -
orderBy -
Returns:
A list of beans

loadListMinus

public <E> List<E> loadListMinus(E bean,
                                 Limit limit,
                                 Object... 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.

Specified by:
loadListMinus in interface BeanSession
Type Parameters:
E -
Parameters:
bean -
minus -
limit -
Returns:
A list of beans

getDatabaseType

protected String getDatabaseType(DBType<?> dbType)
Each dialect can override this to return the database column type it supports other than the ANSI standard.

Parameters:
dbType -
Returns:
The string representation of this database type to be used with create table statement

createTable

public void createTable(Class<? extends Object> beanKlass)
Specified by:
createTable in interface BeanSession

createTables

public void createTables()
Specified by:
createTables in interface BeanSession

createPKConstraintQuery

protected String createPKConstraintQuery(String table,
                                         Iterator<DBField> pks)
Create a SQL query to add the primary key constraint

Parameters:
table - - The table name
pks - - An iterator of all primary key fields that will be added to the table
Returns:
A String containing the resulting alter table constraint query


Copyright © 2012. All Rights Reserved.