db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rijk van Haaften (JIRA)" <j...@apache.org>
Subject [jira] Updated: (DDLUTILS-197) Implementation for store() and exists()
Date Mon, 03 Mar 2008 09:54:50 GMT

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

Rijk van Haaften updated DDLUTILS-197:
--------------------------------------

    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)
            {
                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);
        }
    }

  was:
    /**
     * 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);
        }
    }


> 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
>
>     /**
>      * 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.


Mime
View raw message