You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ddlutils-dev@db.apache.org by "Thomas Dudziak (JIRA)" <ji...@apache.org> on 2008/04/21 02:10:21 UTC

[jira] Resolved: (DDLUTILS-197) Implementation for store() and exists()

     [ https://issues.apache.org/jira/browse/DDLUTILS-197?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thomas Dudziak resolved DDLUTILS-197.
-------------------------------------

    Resolution: Fixed

> Implementation for store() and exists()
> ---------------------------------------
>
>                 Key: DDLUTILS-197
>                 URL: https://issues.apache.org/jira/browse/DDLUTILS-197
>             Project: DdlUtils
>          Issue Type: Improvement
>          Components: Core (No specific database)
>            Reporter: Rijk van Haaften
>            Assignee: Thomas Dudziak
>            Priority: Minor
>             Fix For: 1.1
>
>
>     /**
>      * 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)
>             {
>                 sql.append(" AND ");
>             }
>             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
>         {
>             statement = connection.createStatement();
>             resultSet = statement.executeQuery(sql.toString());
>             exists = resultSet.next();
>             resultSet.close();
>         }
>         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))
>             {
>                 update(connection, model, dynaBean);
>             }
>             else
>             {
>                 insert(connection, model, dynaBean);
>             }
>         }
>         finally
>         {
>             returnConnection(connection);
>         }
>     }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.