Details
-
Improvement
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
None
-
None
Description
/**
- Determines whether the given dyna bean is stored in the database.
- @param dynaBean The bean
- @param connection The connection
- @return <code>true</code> if this dyna bean has a primary key
*/
protected boolean exists(Connection connection, Database model, DynaBean dynaBean)
{
SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
if (primaryKeys.length == 0)
{ return false; } String tableName = _builder.getDelimitedIdentifier(dynaClass.getTable().getName());
StringBuilder sql = new StringBuilder("SELECT * FROM " + tableName + " WHERE ");
for (int i = 0; i < primaryKeys.length; i++)
{
if (i > 0)
String key = primaryKeys[i].getColumn().getName();
sql.append(_builder.getDelimitedIdentifier(key));
sql.append('=');
sql.append('\'');
sql.append(dynaBean.get(key));
sql.append('\'');
}
Statement statement = null;
ResultSet resultSet = null;
boolean exists = false;
try
catch (SQLException ex)
{ throw new DatabaseOperationException("Error while reading from the database", ex); }finally
{ closeStatement(statement); } return exists;
}
/**
- {@inheritDoc}
*/
public void store(Database model, DynaBean dynaBean) throws DatabaseOperationException
{
Connection connection = borrowConnection();
try
{
if (exists(connection, model, dynaBean))
else
{ insert(connection, model, dynaBean); } }
finally
}