String getSetting(String key) { Connection con = null; String value = ""; try { con = pool.getConnection(timeout); PreparedStatement s = con.prepareStatement("SELECT `value` FROM `settings` WHERE `key` = ?"); s.setString(1, key); s.executeQuery(); ResultSet rs = s.getResultSet(); while (rs.next()) { value = rs.getString("value"); } 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 value; }
String getRandomChannelWithVelociraptors(String exclude) { Connection con = null; String value = ""; try { con = pool.getConnection(timeout); PreparedStatement s = con.prepareStatement( "SELECT `channel` FROM `channels` WHERE `channel` != ? AND `active_velociraptors` > 0 ORDER BY (RAND() * active_velociraptors) DESC LIMIT 1;"); s.setString(1, exclude); s.executeQuery(); ResultSet rs = s.getResultSet(); while (rs.next()) { value = rs.getString("channel"); } 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 value; }
public void deleteTSecGroupByTenantVisIdx( CFSecurityAuthorization Authorization, long TenantId, boolean IsVisible) { final String S_ProcName = "deleteTSecGroupByTenantVisIdx"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory() .newUsageException(getClass(), S_ProcName, "Transaction not open"); } ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "exec sp_delete_tsecgrp_by_tenantvisidx ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?"; if (stmtDeleteByTenantVisIdx == null) { stmtDeleteByTenantVisIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByTenantVisIdx.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByTenantVisIdx.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByTenantVisIdx.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByTenantVisIdx.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByTenantVisIdx.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByTenantVisIdx.setLong(argIdx++, TenantId); if (IsVisible) { stmtDeleteByTenantVisIdx.setString(argIdx++, "Y"); } else { stmtDeleteByTenantVisIdx.setString(argIdx++, "N"); } Object stuff = null; boolean moreResults = stmtDeleteByTenantVisIdx.execute(); while (stuff == null) { try { moreResults = stmtDeleteByTenantVisIdx.getMoreResults(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } if (moreResults) { try { stuff = stmtDeleteByTenantVisIdx.getResultSet(); } catch (SQLException e) { } } else if (-1 == stmtDeleteByTenantVisIdx.getUpdateCount()) { break; } } } 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 deleteContactTag(CFSecurityAuthorization Authorization, CFCrmContactTagBuff Buff) { final String S_ProcName = "deleteContactTag"; try { Connection cnx = schema.getCnx(); long TenantId = Buff.getRequiredTenantId(); long ContactId = Buff.getRequiredContactId(); long TagId = Buff.getRequiredTagId(); String sql = "exec sp_delete_ctc_tag ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", " + "?" + ", " + "?"; if (stmtDeleteByPKey == null) { stmtDeleteByPKey = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByPKey.setLong(argIdx++, TenantId); stmtDeleteByPKey.setLong(argIdx++, ContactId); stmtDeleteByPKey.setLong(argIdx++, TagId); stmtDeleteByPKey.setInt(argIdx++, Buff.getRequiredRevision()); ; Object stuff = null; boolean moreResults = stmtDeleteByPKey.execute(); while (stuff == null) { try { moreResults = stmtDeleteByPKey.getMoreResults(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } if (moreResults) { try { stuff = stmtDeleteByPKey.getResultSet(); } catch (SQLException e) { } } else if (-1 == stmtDeleteByPKey.getUpdateCount()) { break; } } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } }
public void deleteISOTimezoneByOffsetIdx( CFSecurityAuthorization Authorization, short argTZHourOffset, short argTZMinOffset) { final String S_ProcName = "deleteISOTimezoneByOffsetIdx"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory() .newUsageException(getClass(), S_ProcName, "Transaction not open"); } ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "exec sp_delete_isotz_by_offsetidx ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?"; if (stmtDeleteByOffsetIdx == null) { stmtDeleteByOffsetIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByOffsetIdx.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByOffsetIdx.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByOffsetIdx.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByOffsetIdx.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByOffsetIdx.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByOffsetIdx.setShort(argIdx++, argTZHourOffset); stmtDeleteByOffsetIdx.setShort(argIdx++, argTZMinOffset); Object stuff = null; boolean moreResults = stmtDeleteByOffsetIdx.execute(); while (stuff == null) { try { moreResults = stmtDeleteByOffsetIdx.getMoreResults(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } if (moreResults) { try { stuff = stmtDeleteByOffsetIdx.getResultSet(); } catch (SQLException e) { } } else if (-1 == stmtDeleteByOffsetIdx.getUpdateCount()) { break; } } } 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 deleteFeeDetailByIdIdx( CFSecurityAuthorization Authorization, long TenantId, long FeeDetailId) { final String S_ProcName = "deleteFeeDetailByIdIdx"; try { Connection cnx = schema.getCnx(); String sql = "exec sp_delete_feedtl ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?"; if (stmtDeleteByPKey == null) { stmtDeleteByPKey = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByPKey.setLong(argIdx++, TenantId); stmtDeleteByPKey.setLong(argIdx++, FeeDetailId); Object stuff = null; boolean moreResults = stmtDeleteByPKey.execute(); while (stuff == null) { try { moreResults = stmtDeleteByPKey.getMoreResults(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } if (moreResults) { try { stuff = stmtDeleteByPKey.getResultSet(); } catch (SQLException e) { } } else if (-1 == stmtDeleteByPKey.getUpdateCount()) { break; } } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } }
public void updateISOCountry( CFSecurityAuthorization Authorization, CFSecurityISOCountryBuff Buff) { final String S_ProcName = "updateISOCountry"; ResultSet resultSet = null; try { short Id = Buff.getRequiredId(); String ISOCode = Buff.getRequiredISOCode(); String Name = Buff.getRequiredName(); int Revision = Buff.getRequiredRevision(); Connection cnx = schema.getCnx(); String sql = "exec sp_update_iso_cntry ?, ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", " + "?" + ", " + "?"; 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++, "ISOC"); stmtUpdateByPKey.setShort(argIdx++, Id); stmtUpdateByPKey.setString(argIdx++, ISOCode); stmtUpdateByPKey.setString(argIdx++, Name); stmtUpdateByPKey.setInt(argIdx++, Revision); stmtUpdateByPKey.execute(); boolean moreResults = true; resultSet = null; while (resultSet == null) { try { moreResults = stmtUpdateByPKey.getMoreResults(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } if (moreResults) { try { resultSet = stmtUpdateByPKey.getResultSet(); } catch (SQLException e) { } } else if (-1 == stmtUpdateByPKey.getUpdateCount()) { break; } } if (resultSet == null) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException(getClass(), S_ProcName, 0, "resultSet"); } if (resultSet.next()) { CFSecurityISOCountryBuff updatedBuff = unpackISOCountryResultSetToBuff(resultSet); if (resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Did not expect multi-record response, " + resultSet.getRow() + " rows selected"); } Buff.setRequiredISOCode(updatedBuff.getRequiredISOCode()); Buff.setRequiredName(updatedBuff.getRequiredName()); 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 CFSecurityISOCountryBuff lockBuff( CFSecurityAuthorization Authorization, CFSecurityISOCountryPKey PKey) { final String S_ProcName = "lockBuff"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory() .newUsageException(getClass(), S_ProcName, "Transaction not open"); } ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); short Id = PKey.getRequiredId(); String sql = "{ call sp_lock_iso_cntry( ?, ?, ?, ?, ?" + ", " + "?" + " ) }"; if (stmtLockBuffByPKey == null) { stmtLockBuffByPKey = cnx.prepareStatement(sql); } int argIdx = 1; stmtLockBuffByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtLockBuffByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtLockBuffByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtLockBuffByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtLockBuffByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtLockBuffByPKey.setShort(argIdx++, Id); stmtLockBuffByPKey.execute(); boolean moreResults = true; resultSet = null; while (resultSet == null) { try { moreResults = stmtLockBuffByPKey.getMoreResults(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } if (moreResults) { try { resultSet = stmtLockBuffByPKey.getResultSet(); } catch (SQLException e) { } } else if (-1 == stmtLockBuffByPKey.getUpdateCount()) { break; } } if ((resultSet != null) && resultSet.next()) { CFSecurityISOCountryBuff buff = unpackISOCountryResultSetToBuff(resultSet); if (resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Did not expect multi-record response, " + resultSet.getRow() + " rows selected"); } return (buff); } else { return (null); } } 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 createISOCurrency( CFSecurityAuthorization Authorization, CFSecurityISOCurrencyBuff Buff) { final String S_ProcName = "createISOCurrency"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory() .newUsageException(getClass(), S_ProcName, "Transaction not open"); } ResultSet resultSet = null; try { String ISOCode = Buff.getRequiredISOCode(); String Name = Buff.getRequiredName(); String UnitSymbol = Buff.getOptionalUnitSymbol(); short Precis = Buff.getRequiredPrecis(); Connection cnx = schema.getCnx(); String sql = "exec sp_create_iso_ccy ?, ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", " + "?" + ", " + "?"; if (stmtCreateByPKey == null) { stmtCreateByPKey = cnx.prepareStatement(sql); } int argIdx = 1; stmtCreateByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtCreateByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtCreateByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtCreateByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtCreateByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtCreateByPKey.setString(argIdx++, "ISCY"); stmtCreateByPKey.setString(argIdx++, ISOCode); stmtCreateByPKey.setString(argIdx++, Name); if (UnitSymbol != null) { stmtCreateByPKey.setString(argIdx++, UnitSymbol); } else { stmtCreateByPKey.setNull(argIdx++, java.sql.Types.VARCHAR); } stmtCreateByPKey.setShort(argIdx++, Precis); stmtCreateByPKey.execute(); boolean moreResults = true; resultSet = null; while (resultSet == null) { try { moreResults = stmtCreateByPKey.getMoreResults(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } if (moreResults) { try { resultSet = stmtCreateByPKey.getResultSet(); } catch (SQLException e) { } } else if (-1 == stmtCreateByPKey.getUpdateCount()) { break; } } if (resultSet == null) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException(getClass(), S_ProcName, 0, "resultSet"); } if (resultSet.next()) { CFSecurityISOCurrencyBuff createdBuff = unpackISOCurrencyResultSetToBuff(resultSet); if (resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Did not expect multi-record response, " + resultSet.getRow() + " rows selected"); } Buff.setRequiredISOCurrencyId(createdBuff.getRequiredISOCurrencyId()); Buff.setRequiredISOCode(createdBuff.getRequiredISOCode()); Buff.setRequiredName(createdBuff.getRequiredName()); Buff.setOptionalUnitSymbol(createdBuff.getOptionalUnitSymbol()); Buff.setRequiredPrecis(createdBuff.getRequiredPrecis()); Buff.setRequiredRevision(createdBuff.getRequiredRevision()); Buff.setCreatedByUserId(createdBuff.getCreatedByUserId()); Buff.setCreatedAt(createdBuff.getCreatedAt()); Buff.setUpdatedByUserId(createdBuff.getUpdatedByUserId()); Buff.setUpdatedAt(createdBuff.getUpdatedAt()); } 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; } } }
private void getChannelList() { Connection con = null; ChannelInfo ci; this.channel_data.clear(); try { con = pool.getConnection(timeout); ResultSet rs; try (Statement s = con.createStatement()) { rs = s.executeQuery("SELECT * FROM `channels`"); PreparedStatement s2; ResultSet rs2; while (rs.next()) { ci = new ChannelInfo(rs.getString("channel")); ci.setDefaultOptions(); ci.setReactiveChatter( rs.getInt("reactive_chatter_level"), rs.getInt("chatter_name_multiplier")); ci.setRandomChatter(rs.getInt("random_chatter_level")); ci.setTwitterTimers( rs.getFloat("tweet_bucket_max"), rs.getFloat("tweet_bucket_charge_rate")); ci.setWarAutoMuzzle(rs.getBoolean("auto_muzzle_wars")); ci.velociraptorSightings = rs.getInt("velociraptor_sightings"); ci.activeVelociraptors = rs.getInt("active_velociraptors"); ci.deadVelociraptors = rs.getInt("dead_velociraptors"); ci.killedVelociraptors = rs.getInt("killed_velociraptors"); if (rs.getDate("last_sighting_date") != null) { long time = rs.getDate("last_sighting_date").getTime(); ci.lastSighting = new java.util.Date(time); } s2 = con.prepareStatement( "SELECT `setting`, `value` FROM `channel_chatter_settings` WHERE `channel` = ?"); s2.setString(1, rs.getString("channel")); s2.executeQuery(); rs2 = s2.getResultSet(); while (rs2.next()) { ci.addChatterSetting(rs2.getString("setting"), rs2.getBoolean("value")); } s2.close(); rs2.close(); s2 = con.prepareStatement( "SELECT `setting`, `value` FROM `channel_command_settings` WHERE `channel` = ?"); s2.setString(1, rs.getString("channel")); s2.executeQuery(); rs2 = s2.getResultSet(); while (rs2.next()) { ci.addCommandSetting(rs2.getString("setting"), rs2.getBoolean("value")); } s2.close(); rs2.close(); s2 = con.prepareStatement( "SELECT `account` FROM `channel_twitter_feeds` WHERE `channel` = ?"); s2.setString(1, rs.getString("channel")); s2.executeQuery(); rs2 = s2.getResultSet(); while (rs2.next()) { ci.addTwitterAccount(rs2.getString("account")); } s2.close(); rs2.close(); this.channel_data.put(ci.channel, ci); this.saveChannelSettings(ci); } } rs.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 createHostNode(CFSecurityAuthorization Authorization, CFSecurityHostNodeBuff Buff) { final String S_ProcName = "createHostNode"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory() .newUsageException(getClass(), S_ProcName, "Transaction not open"); } ResultSet resultSet = null; try { long ClusterId = Buff.getRequiredClusterId(); String Description = Buff.getRequiredDescription(); String HostName = Buff.getRequiredHostName(); Connection cnx = schema.getCnx(); String sql = "exec sp_create_hostnode ?, ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", " + "?"; if (stmtCreateByPKey == null) { stmtCreateByPKey = cnx.prepareStatement(sql); } int argIdx = 1; stmtCreateByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtCreateByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtCreateByPKey.setString( argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtCreateByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtCreateByPKey.setLong( argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtCreateByPKey.setString(argIdx++, "HSND"); stmtCreateByPKey.setLong(argIdx++, ClusterId); stmtCreateByPKey.setString(argIdx++, Description); stmtCreateByPKey.setString(argIdx++, HostName); stmtCreateByPKey.execute(); boolean moreResults = true; resultSet = null; while (resultSet == null) { try { moreResults = stmtCreateByPKey.getMoreResults(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } if (moreResults) { try { resultSet = stmtCreateByPKey.getResultSet(); } catch (SQLException e) { } } else if (-1 == stmtCreateByPKey.getUpdateCount()) { break; } } if (resultSet == null) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException(getClass(), S_ProcName, 0, "resultSet"); } if (resultSet.next()) { CFSecurityHostNodeBuff createdBuff = unpackHostNodeResultSetToBuff(resultSet); if (resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Did not expect multi-record response, " + resultSet.getRow() + " rows selected"); } Buff.setRequiredClusterId(createdBuff.getRequiredClusterId()); Buff.setRequiredHostNodeId(createdBuff.getRequiredHostNodeId()); Buff.setRequiredDescription(createdBuff.getRequiredDescription()); Buff.setRequiredHostName(createdBuff.getRequiredHostName()); Buff.setRequiredRevision(createdBuff.getRequiredRevision()); Buff.setCreatedByUserId(createdBuff.getCreatedByUserId()); Buff.setCreatedAt(createdBuff.getCreatedAt()); Buff.setUpdatedByUserId(createdBuff.getUpdatedByUserId()); Buff.setUpdatedAt(createdBuff.getUpdatedAt()); } 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 runReplicate() throws InterruptedException { if (verbose) log.append("Started at " + Calendar.getInstance().getTime() + "\r\n"); Connection connectionFrom = null; Connection connectionTo = null; try { connectionFrom = providerI.getConnection(dbFrom); connectionTo = providerI.getConnection(dbTo); PreparedStatement preparedStatementFrom = null; PreparedStatement preparedStatementTo = null; ResultSet resultSet = null; for (Iterator<HashMap<String, String>> HashMapIterator = tables.iterator(); HashMapIterator.hasNext(); ) { try { HashMap<String, String> h = HashMapIterator.next(); String name = h.get("name"); String name2 = h.get("name2"); String where1 = h.get("where1"); String where2 = h.get("where2"); if (verbose) log.append("Replicating " + dbFrom + "/" + name + " to " + dbTo + "/" + name2 + "\r\n"); preparedStatementFrom = connectionFrom.prepareStatement("select * from " + name + " WHERE 1=1 " + where1); boolean b = preparedStatementFrom.execute(); HashMap meta = new HashMap(); // 1. find all ids from DB1 ArrayList db1 = new ArrayList(); String nullMark = "____NULL"; if (b) { resultSet = preparedStatementFrom.getResultSet(); ResultSetMetaData setMetaData = resultSet.getMetaData(); for (int i = 1; i <= setMetaData.getColumnCount(); i++) { meta.put( setMetaData.getColumnLabel(i).toLowerCase(), setMetaData.getColumnClassName(i)); } while (resultSet.next()) { HashMap results = new HashMap(); for (int i = 1; i <= setMetaData.getColumnCount(); i++) { String columnName = setMetaData.getColumnLabel(i).toLowerCase(); Object o = resultSet.getObject(i); if (o != null) { results.put(columnName, o); } else { results.put(columnName + nullMark, nullMark); } } db1.add(results); } resultSet.close(); } else { log.append("Couldn't execute select from " + dbFrom + "/" + name + " /r/n"); return; } StringBuffer allIds = new StringBuffer(); HashMap records1 = new HashMap(); HashMap toInsert = new HashMap(); HashMap toUpdate = new HashMap(); for (Iterator iterator = db1.iterator(); iterator.hasNext(); ) { HashMap record = (HashMap) iterator.next(); if (allIds.length() != 0) { allIds.append(","); } allIds.append(record.get("id")); records1.put(record.get("id"), record); } toInsert.putAll(records1); // 2. find all ids to delete in DB2; if (allIds.length() > 0) { preparedStatementTo = connectionTo.prepareStatement( "delete from " + name2 + " where id not in (" + allIds.toString() + ")" + where2); if (verbose) log.append( "deleted from " + dbTo + "/" + name2 + " " + preparedStatementTo.executeUpdate() + " records;\r\n"); } else { if (verbose) log.append( "No records in " + dbFrom + "/" + name + ", nothing to delete in " + dbTo + "/" + name2 + " ;\r\n"); } // 3. find all ids from DB2; preparedStatementTo = connectionTo.prepareStatement("select * from " + name2 + " WHERE 1=1 " + where2); b = preparedStatementTo.execute(); HashMap meta2 = new HashMap(); ArrayList db2 = new ArrayList(); if (b) { resultSet = preparedStatementTo.getResultSet(); ResultSetMetaData setMetaData = resultSet.getMetaData(); for (int i = 1; i <= setMetaData.getColumnCount(); i++) { meta2.put( setMetaData.getColumnLabel(i).toLowerCase(), setMetaData.getColumnClassName(i)); } while (resultSet.next()) { HashMap results = new HashMap(); for (int i = 1; i <= setMetaData.getColumnCount(); i++) { String columnName = setMetaData.getColumnLabel(i).toLowerCase(); Object o = resultSet.getObject(i); if (o != null) { results.put(columnName, o); } else { results.put(columnName + nullMark, nullMark); } } db2.add(results); } } else { log.append("Couldn't execute select from " + dbTo + "/" + name2 + " /r/n"); return; } // compare meta-data; { HashMap temp = new HashMap(); { temp.putAll(meta); Set set = meta2.keySet(); for (Iterator iteratorSet = set.iterator(); iteratorSet.hasNext(); ) { Object o = iteratorSet.next(); if (meta.containsKey(o)) { if (meta.get(o).equals(meta2.get(o))) { temp.remove(o); } } else { log.append("ERROR: Meta data not equals! \r\n"); log.append(o + "\t" + temp.get(o) + " not present in table " + name + "\r\n"); } } } if (!temp.isEmpty()) { log.append("ERROR: Meta data not equals! \r\n"); Set set = temp.keySet(); for (Iterator iteratorSet = set.iterator(); iteratorSet.hasNext(); ) { Object o = iteratorSet.next(); log.append(o + "\t" + temp.get(o) + " != " + meta2.get(o) + "\r\n"); } return; } } for (Iterator iterator = db2.iterator(); iterator.hasNext(); ) { HashMap db2Record = (HashMap) iterator.next(); if (toInsert.containsKey(db2Record.get("id"))) { HashMap db1Record = (HashMap) toInsert.get(db2Record.get("id")); boolean equal = true; Set set = meta2.keySet(); for (Iterator iteratorSet = set.iterator(); equal && iteratorSet.hasNext(); ) { String columnName2 = (String) iteratorSet.next(); if (db2Record.containsKey(columnName2 + nullMark) || db1Record.containsKey(columnName2 + nullMark)) { equal = db2Record.containsKey(columnName2 + nullMark) && db1Record.containsKey(columnName2 + nullMark); } else { // checking not-null; equal = equalRecords(db1Record.get(columnName2), db2Record.get(columnName2)); } } if (!equal) { toUpdate.put(db2Record.get("id"), toInsert.get(db2Record.get("id"))); } toInsert.remove(db2Record.get("id")); } else { // this case shouldn't happen at all, since we've deleted all such records } } log.append( "Found " + toUpdate.size() + " to update, and " + toInsert.size() + " to insert.\r\n"); int totalUpdated = 0; // 4. calculate all to update in DB2 if (!toUpdate.isEmpty()) { Set set = toUpdate.keySet(); for (Iterator iteratorSet = set.iterator(); iteratorSet.hasNext(); ) { StringBuffer sql = new StringBuffer(); Object id = iteratorSet.next(); HashMap r = (HashMap) toUpdate.get(id); sql.append("UPDATE " + name2 + " SET "); StringBuffer values = new StringBuffer(); Set en = meta2.keySet(); for (Iterator iteratorSetEn = en.iterator(); iteratorSetEn.hasNext(); ) { Object o = iteratorSetEn.next(); if (!o.equals("id")) { if (values.length() != 0) { values.append(","); } Object quote = dbQuotes.get(dbToType); if (quote == null) { quote = ""; } values.append(quote).append(o).append(quote); values.append(" = ? "); } } values.append(" WHERE id = '" + r.get("id") + "';"); PreparedStatement statement = connectionTo.prepareStatement(sql.toString() + values.toString()); en = meta2.keySet(); int i = 0; for (Iterator iteratorSetEn = en.iterator(); iteratorSetEn.hasNext(); ) { Object o = iteratorSetEn.next(); if (!o.equals("id")) { i++; statement.setObject(i, r.get(o)); } } try { totalUpdated += statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); log.append("Error occured: " + e + "\r\n"); } } } if (verbose) log.append("Updated " + totalUpdated + " records.\r\n"); // 4. calculate all to insert to DB2 if (!toInsert.isEmpty()) { StringBuffer header = new StringBuffer(); if (header.length() == 0) { header.append(" INSERT INTO " + name2 + " ("); StringBuffer columns = new StringBuffer(); Set en = meta2.keySet(); for (Iterator iteratorSetEn = en.iterator(); iteratorSetEn.hasNext(); ) { Object o = iteratorSetEn.next(); if (columns.length() != 0) { columns.append(","); } Object quote = dbQuotes.get(dbToType); if (quote == null) { quote = ""; } columns.append(quote).append(o).append(quote); } header.append(columns.toString()); header.append(") VALUES "); } Set enumeration = toInsert.keySet(); for (Iterator iteratorSetX = enumeration.iterator(); iteratorSetX.hasNext(); ) { Object id = iteratorSetX.next(); HashMap r = (HashMap) toInsert.get(id); StringBuffer values = new StringBuffer(); if (values.length() != 0) { values.append(","); } values.append("("); StringBuffer columns = new StringBuffer(); Set en = meta2.keySet(); for (Iterator iteratorSetEn = en.iterator(); iteratorSetEn.hasNext(); ) { Object o = iteratorSetEn.next(); if (columns.length() != 0) { columns.append(","); } columns.append(" ? "); } values.append(columns.toString()); values.append(");"); PreparedStatement statement = connectionTo.prepareStatement(header.toString() + values.toString()); en = meta2.keySet(); int i = 0; for (Iterator iteratorSetEn = en.iterator(); iteratorSetEn.hasNext(); ) { Object o = iteratorSetEn.next(); i++; statement.setObject(i, r.get(o)); } statement.execute(); } } if (verbose) log.append("Replication finished OK.\r\n"); } catch (Exception e) { log.append("Some error occured: " + e + "\r\n"); log.append(e.getMessage() + "\r\n"); e.printStackTrace(); } } } catch (Exception e) { log.append("Error with query = " + e); log.append(e.getMessage()); return; } finally { try { if (connectionFrom != null && !connectionFrom.isClosed()) { connectionFrom.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (connectionTo != null && !connectionTo.isClosed()) { connectionTo.close(); } } catch (SQLException e) { e.printStackTrace(); } } if (verbose) log.append("Ended at " + Calendar.getInstance().getTime()); }