/** {@inheritDoc} */ public String retrieve(long id, Connection conn) throws EntityDoesNotExistException, SQLException { if (cache != null && id < cache.length) { String value = cache[(int) id]; if (value != null) { return value; } } Statement stmt = conn.createStatement(); ResultSet rs = null; try { rs = stmt.executeQuery("SELECT data FROM " + getTable() + " WHERE data_key = " + id); if (!rs.next()) { throw new EntityDoesNotExistException( "Item #" + id + " does not exist in table " + getTable()); } String value = unescape(rs.getString(1)); if (cache != null && id < cache.length) { cache[(int) id] = value; } return value; } finally { DatabaseUtils.close(rs); DatabaseUtils.close(stmt); } }
/** {@inheritDoc} */ public long create(String value, Connection conn) throws SQLException { String str = value; if (str.length() > 255) { throw new IllegalArgumentException( "maximum lenght of string attributes " + "is 255 characters. Use text attributes " + "wherever greater capacity is desired"); } long id = getNextId(); Statement stmt = conn.createStatement(); try { stmt.execute( "INSERT INTO " + getTable() + "(data_key, data) VALUES (" + id + ", '" + escape(str) + "')"); return id; } finally { DatabaseUtils.close(stmt); } }
/** {@inheritDoc} */ public void preload(Connection conn) throws SQLException { Statement stmt = conn.createStatement(); ResultSet rs = null; try { rs = stmt.executeQuery("SELECT max(data_key) from " + getTable()); rs.next(); int count = rs.getInt(1); cache = new String[count + 1]; rs.close(); rs = stmt.executeQuery("SELECT data_key, data from " + getTable()); while (rs.next()) { cache[rs.getInt(1)] = unescape(rs.getString(2)); } } finally { DatabaseUtils.close(rs); DatabaseUtils.close(stmt); } }
/** {@inheritDoc} */ public void update(long id, String value, Connection conn) throws EntityDoesNotExistException, SQLException { String str = value; if (str.length() > 255) { throw new IllegalArgumentException( "maximum lenght of string attributes " + "is 255 characters. Use text attributes " + "wherever greater capacity is desired"); } if (cache != null && id < cache.length) { cache[(int) id] = str; } Statement stmt = conn.createStatement(); try { checkExists(id, stmt); stmt.execute( "UPDATE " + getTable() + " SET data = '" + escape(str) + "' WHERE data_key = " + id); } finally { DatabaseUtils.close(stmt); } }