private void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable)
      throws SQLException {
    try {
      PreparedStatement s =
          conn.get()
              .prepareStatement(
                  "INSERT INTO headers(hash, chainWork, height, header, wasUndoable)"
                      + " VALUES(?, ?, ?, ?, ?)");
      // We skip the first 4 bytes because (on prodnet) the minimum target has 4 0-bytes
      byte[] hashBytes = new byte[28];
      System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 3, hashBytes, 0, 28);
      s.setBytes(1, hashBytes);
      s.setBytes(2, storedBlock.getChainWork().toByteArray());
      s.setInt(3, storedBlock.getHeight());
      s.setBytes(4, storedBlock.getHeader().unsafeRimbitSerialize());
      s.setBoolean(5, wasUndoable);
      s.executeUpdate();
      s.close();
    } catch (SQLException e) {
      // It is possible we try to add a duplicate StoredBlock if we upgraded
      // In that case, we just update the entry to mark it wasUndoable
      if (!(e.getSQLState().equals(POSTGRES_DUPLICATE_KEY_ERROR_CODE)) || !wasUndoable) throw e;

      PreparedStatement s =
          conn.get().prepareStatement("UPDATE headers SET wasUndoable=? WHERE hash=?");
      s.setBoolean(1, true);
      // We skip the first 4 bytes because (on prodnet) the minimum target has 4 0-bytes
      byte[] hashBytes = new byte[28];
      System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 3, hashBytes, 0, 28);
      s.setBytes(2, hashBytes);
      s.executeUpdate();
      s.close();
    }
  }
  int create_war(
      Channel channel,
      String starter,
      String name,
      long base_duration,
      long duration,
      long remaining,
      long time_to_start,
      int total_chains,
      int current_chain,
      int break_duration,
      boolean do_randomness) {
    int id = 0;
    Connection con = null;
    try {
      con = pool.getConnection(timeout);

      PreparedStatement s =
          con.prepareStatement(
              "INSERT INTO `wars` (`channel`, `starter`, `name`, `base_duration`, `randomness`, `delay`, `duration`, `remaining`, `time_to_start`, `total_chains`, `current_chain`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
              Statement.RETURN_GENERATED_KEYS);
      s.setString(1, channel.getName().toLowerCase());
      s.setString(2, starter);
      s.setString(3, name);
      s.setLong(4, base_duration);
      s.setBoolean(5, do_randomness);
      s.setLong(6, break_duration);
      s.setLong(7, duration);
      s.setLong(8, remaining);
      s.setLong(9, time_to_start);
      s.setInt(10, total_chains);
      s.setInt(11, current_chain);
      s.executeUpdate();

      ResultSet rs = s.getGeneratedKeys();

      if (rs.next()) {
        id = rs.getInt(1);
      }

      rs.close();
      s.close();
    } catch (SQLException ex) {
      Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        if (con != null) {
          con.close();
        }
      } catch (SQLException ex) {
        Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    return id;
  }
 public void deleteURLProtocolByIsSecureIdx(
     CFSecurityAuthorization Authorization, boolean argIsSecure) {
   final String S_ProcName = "deleteURLProtocolByIsSecureIdx";
   ResultSet resultSet = null;
   try {
     Connection cnx = schema.getCnx();
     String sql =
         "SELECT "
             + schema.getLowerDbSchemaName()
             + ".sp_delete_urlproto_by_issecureidx( ?, ?, ?, ?, ?"
             + ", "
             + "?"
             + " ) as DeletedFlag";
     if (stmtDeleteByIsSecureIdx == null) {
       stmtDeleteByIsSecureIdx = cnx.prepareStatement(sql);
     }
     int argIdx = 1;
     stmtDeleteByIsSecureIdx.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
     stmtDeleteByIsSecureIdx.setString(
         argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString());
     stmtDeleteByIsSecureIdx.setString(
         argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
     stmtDeleteByIsSecureIdx.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
     stmtDeleteByIsSecureIdx.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
     stmtDeleteByIsSecureIdx.setBoolean(argIdx++, argIsSecure);
     resultSet = stmtDeleteByIsSecureIdx.executeQuery();
     if (resultSet.next()) {
       boolean deleteFlag = resultSet.getBoolean(1);
       if (resultSet.next()) {
         throw CFLib.getDefaultExceptionFactory()
             .newRuntimeException(getClass(), S_ProcName, "Did not expect multi-record response");
       }
     } else {
       throw CFLib.getDefaultExceptionFactory()
           .newRuntimeException(
               getClass(),
               S_ProcName,
               "Expected 1 record result set to be returned by delete, not 0 rows");
     }
   } catch (SQLException e) {
     throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
   } finally {
     if (resultSet != null) {
       try {
         resultSet.close();
       } catch (SQLException e) {
       }
       resultSet = null;
     }
   }
 }
 public CFInternetURLProtocolBuff[] readBuffByIsSecureIdx(
     CFSecurityAuthorization Authorization, boolean IsSecure) {
   final String S_ProcName = "readBuffByIsSecureIdx";
   ResultSet resultSet = null;
   try {
     Connection cnx = schema.getCnx();
     String sql =
         "SELECT * FROM "
             + schema.getLowerDbSchemaName()
             + ".sp_read_urlproto_by_issecureidx( ?, ?, ?, ?, ?"
             + ", "
             + "?"
             + " )";
     if (stmtReadBuffByIsSecureIdx == null) {
       stmtReadBuffByIsSecureIdx = cnx.prepareStatement(sql);
     }
     int argIdx = 1;
     stmtReadBuffByIsSecureIdx.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
     stmtReadBuffByIsSecureIdx.setString(
         argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString());
     stmtReadBuffByIsSecureIdx.setString(
         argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
     stmtReadBuffByIsSecureIdx.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
     stmtReadBuffByIsSecureIdx.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
     stmtReadBuffByIsSecureIdx.setBoolean(argIdx++, IsSecure);
     resultSet = stmtReadBuffByIsSecureIdx.executeQuery();
     List<CFInternetURLProtocolBuff> buffList = new LinkedList<CFInternetURLProtocolBuff>();
     while (resultSet.next()) {
       CFInternetURLProtocolBuff buff = unpackURLProtocolResultSetToBuff(resultSet);
       buffList.add(buff);
     }
     int idx = 0;
     CFInternetURLProtocolBuff[] retBuff = new CFInternetURLProtocolBuff[buffList.size()];
     Iterator<CFInternetURLProtocolBuff> iter = buffList.iterator();
     while (iter.hasNext()) {
       retBuff[idx++] = iter.next();
     }
     return (retBuff);
   } catch (SQLException e) {
     throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
   } finally {
     if (resultSet != null) {
       try {
         resultSet.close();
       } catch (SQLException e) {
       }
       resultSet = null;
     }
   }
 }
  public void saveChannelSettings(ChannelInfo channel) {
    Connection con = null;
    try {
      con = pool.getConnection(timeout);

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      PreparedStatement s =
          con.prepareStatement(
              "UPDATE `channels` SET reactive_chatter_level = ?, chatter_name_multiplier = ?, random_chatter_level = ?, tweet_bucket_max = ?, "
                  + "tweet_bucket_charge_rate = ?, auto_muzzle_wars = ?, velociraptor_sightings = ?, active_velociraptors = ?, dead_velociraptors = ?, killed_velociraptors = ?, last_sighting_date = ? WHERE channel = ?;");
      s.setInt(1, channel.reactiveChatterLevel);
      s.setInt(2, channel.chatterNameMultiplier);
      s.setInt(3, channel.randomChatterLevel);
      s.setFloat(4, channel.tweetBucketMax);
      s.setFloat(5, channel.tweetBucketChargeRate);
      s.setBoolean(6, channel.auto_muzzle_wars);
      s.setInt(7, channel.velociraptorSightings);
      s.setInt(8, channel.activeVelociraptors);
      s.setInt(9, channel.deadVelociraptors);
      s.setInt(10, channel.killedVelociraptors);
      if (channel.lastSighting != null) {
        s.setString(11, sdf.format(channel.lastSighting));
      } else {
        s.setString(11, "1999-01-01");
      }
      s.setString(12, channel.channel);
      s.executeUpdate();

      s = con.prepareStatement("DELETE FROM `channel_chatter_settings` WHERE `channel` = ?;");
      s.setString(1, channel.channel);
      s.executeUpdate();

      s = con.prepareStatement("DELETE FROM `channel_command_settings` WHERE `channel` = ?;");
      s.setString(1, channel.channel);
      s.executeUpdate();

      s = con.prepareStatement("DELETE FROM `channel_twitter_feeds` WHERE `channel` = ?;");
      s.setString(1, channel.channel);
      s.executeUpdate();

      s =
          con.prepareStatement(
              "INSERT INTO `channel_chatter_settings` SET `channel` = ?, `setting` = ?, `value` = ?;");
      s.setString(1, channel.channel);

      for (Map.Entry<String, Boolean> setting : channel.chatter_enabled.entrySet()) {
        s.setString(2, setting.getKey());
        s.setBoolean(3, setting.getValue());
        s.executeUpdate();
      }

      s =
          con.prepareStatement(
              "INSERT INTO `channel_command_settings` SET `channel` = ?, `setting` = ?, `value` = ?;");
      s.setString(1, channel.channel);

      for (Map.Entry<String, Boolean> setting : channel.commands_enabled.entrySet()) {
        s.setString(2, setting.getKey());
        s.setBoolean(3, setting.getValue());
        s.executeUpdate();
      }

      s =
          con.prepareStatement(
              "INSERT INTO `channel_twitter_feeds` SET `channel` = ?, `account` = ?;");
      s.setString(1, channel.channel);

      for (String account : channel.twitter_accounts) {
        s.setString(2, account);
        s.executeUpdate();
      }

      s.close();
    } catch (SQLException ex) {
      Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        if (con != null) {
          con.close();
        }
      } catch (SQLException ex) {
        Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }
 public void updateISOTimezone(
     CFSecurityAuthorization Authorization, CFSecurityISOTimezoneBuff Buff) {
   final String S_ProcName = "updateISOTimezone";
   ResultSet resultSet = null;
   try {
     short ISOTimezoneId = Buff.getRequiredISOTimezoneId();
     String Iso8601 = Buff.getRequiredIso8601();
     String TZName = Buff.getRequiredTZName();
     short TZHourOffset = Buff.getRequiredTZHourOffset();
     short TZMinOffset = Buff.getRequiredTZMinOffset();
     String Description = Buff.getRequiredDescription();
     boolean Visible = Buff.getRequiredVisible();
     int Revision = Buff.getRequiredRevision();
     Connection cnx = schema.getCnx();
     String sql =
         "select * from "
             + schema.getLowerDbSchemaName()
             + ".sp_update_isotz( ?, ?, ?, ?, ?, ?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + " )";
     if (stmtUpdateByPKey == null) {
       stmtUpdateByPKey = cnx.prepareStatement(sql);
     }
     int argIdx = 1;
     stmtUpdateByPKey.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
     stmtUpdateByPKey.setString(
         argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString());
     stmtUpdateByPKey.setString(
         argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
     stmtUpdateByPKey.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
     stmtUpdateByPKey.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
     stmtUpdateByPKey.setString(argIdx++, "ITZN");
     stmtUpdateByPKey.setShort(argIdx++, ISOTimezoneId);
     stmtUpdateByPKey.setString(argIdx++, Iso8601);
     stmtUpdateByPKey.setString(argIdx++, TZName);
     stmtUpdateByPKey.setShort(argIdx++, TZHourOffset);
     stmtUpdateByPKey.setShort(argIdx++, TZMinOffset);
     stmtUpdateByPKey.setString(argIdx++, Description);
     stmtUpdateByPKey.setBoolean(argIdx++, Visible);
     stmtUpdateByPKey.setInt(argIdx++, Revision);
     resultSet = stmtUpdateByPKey.executeQuery();
     if (resultSet.next()) {
       CFSecurityISOTimezoneBuff updatedBuff = unpackISOTimezoneResultSetToBuff(resultSet);
       if (resultSet.next()) {
         throw CFLib.getDefaultExceptionFactory()
             .newRuntimeException(getClass(), S_ProcName, "Did not expect multi-record response");
       }
       Buff.setRequiredIso8601(updatedBuff.getRequiredIso8601());
       Buff.setRequiredTZName(updatedBuff.getRequiredTZName());
       Buff.setRequiredTZHourOffset(updatedBuff.getRequiredTZHourOffset());
       Buff.setRequiredTZMinOffset(updatedBuff.getRequiredTZMinOffset());
       Buff.setRequiredDescription(updatedBuff.getRequiredDescription());
       Buff.setRequiredVisible(updatedBuff.getRequiredVisible());
       Buff.setRequiredRevision(updatedBuff.getRequiredRevision());
     } else {
       throw CFLib.getDefaultExceptionFactory()
           .newRuntimeException(
               getClass(),
               S_ProcName,
               "Expected a single-record response, " + resultSet.getRow() + " rows selected");
     }
   } catch (SQLException e) {
     throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
   } finally {
     if (resultSet != null) {
       try {
         resultSet.close();
       } catch (SQLException e) {
       }
       resultSet = null;
     }
   }
 }
 public void updateURLProtocol(
     CFSecurityAuthorization Authorization, CFInternetURLProtocolBuff Buff) {
   final String S_ProcName = "updateURLProtocol";
   ResultSet resultSet = null;
   try {
     int URLProtocolId = Buff.getRequiredURLProtocolId();
     String Name = Buff.getRequiredName();
     String Description = Buff.getRequiredDescription();
     boolean IsSecure = Buff.getRequiredIsSecure();
     int Revision = Buff.getRequiredRevision();
     Connection cnx = schema.getCnx();
     String sql =
         "select * from "
             + schema.getLowerDbSchemaName()
             + ".sp_update_urlproto( ?, ?, ?, ?, ?, ?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + ", "
             + "?"
             + " )";
     if (stmtUpdateByPKey == null) {
       stmtUpdateByPKey = cnx.prepareStatement(sql);
     }
     int argIdx = 1;
     stmtUpdateByPKey.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
     stmtUpdateByPKey.setString(
         argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString());
     stmtUpdateByPKey.setString(
         argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
     stmtUpdateByPKey.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
     stmtUpdateByPKey.setLong(
         argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
     stmtUpdateByPKey.setString(argIdx++, "UPRT");
     stmtUpdateByPKey.setInt(argIdx++, URLProtocolId);
     stmtUpdateByPKey.setString(argIdx++, Name);
     stmtUpdateByPKey.setString(argIdx++, Description);
     stmtUpdateByPKey.setBoolean(argIdx++, IsSecure);
     stmtUpdateByPKey.setInt(argIdx++, Revision);
     resultSet = stmtUpdateByPKey.executeQuery();
     if (resultSet.next()) {
       CFInternetURLProtocolBuff updatedBuff = unpackURLProtocolResultSetToBuff(resultSet);
       if (resultSet.next()) {
         throw CFLib.getDefaultExceptionFactory()
             .newRuntimeException(getClass(), S_ProcName, "Did not expect multi-record response");
       }
       Buff.setRequiredName(updatedBuff.getRequiredName());
       Buff.setRequiredDescription(updatedBuff.getRequiredDescription());
       Buff.setRequiredIsSecure(updatedBuff.getRequiredIsSecure());
       Buff.setRequiredRevision(updatedBuff.getRequiredRevision());
     } else {
       throw CFLib.getDefaultExceptionFactory()
           .newRuntimeException(
               getClass(),
               S_ProcName,
               "Expected a single-record response, " + resultSet.getRow() + " rows selected");
     }
   } catch (SQLException e) {
     throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
   } finally {
     if (resultSet != null) {
       try {
         resultSet.close();
       } catch (SQLException e) {
       }
       resultSet = null;
     }
   }
 }