Uploaded image for project: 'DdlUtils'
  1. DdlUtils
  2. DDLUTILS-197

Implementation for store() and exists()

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 1.1
    • 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)

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

      }

      Attachments

        Activity

          People

            tomdz Thomas Dudziak
            cordeo Cordeo
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: