JDBCStateRepository implements StateRepository { @Override public void setFeatureState(FeatureState featureState) { try { Connection connection = dataSource.getConnection(); try { int updatedRows = 0; String updateSql = "UPDATE %TABLE% SET FEATURE_ENABLED = ?, STRATEGY_ID = ?, STRATEGY_PARAMS = ? WHERE FEATURE_NAME = ?"; PreparedStatement updateStatement = connection.prepareStatement(insertTableName(updateSql)); try { String paramData = serializer.serialize(featureState.getParameterMap()); updateStatement.setInt(1, featureState.isEnabled() ? 1 : 0); updateStatement.setString(2, Strings.trimToNull(featureState.getStrategyId())); updateStatement.setString(3, Strings.trimToNull(paramData)); updateStatement.setString(4, featureState.getFeature().name()); updatedRows = updateStatement.executeUpdate(); } finally { DbUtils.closeQuietly(updateStatement); } if (updatedRows == 0) { String insertSql = "INSERT INTO %TABLE% (FEATURE_NAME, FEATURE_ENABLED, STRATEGY_ID, STRATEGY_PARAMS) VALUES (?,?,?,?)"; PreparedStatement insertStatement = connection.prepareStatement(insertTableName(insertSql)); try { String paramsAsString = serializer.serialize(featureState.getParameterMap()); insertStatement.setString(1, featureState.getFeature().name()); insertStatement.setInt(2, featureState.isEnabled() ? 1 : 0); insertStatement.setString(3, Strings.trimToNull(featureState.getStrategyId())); insertStatement.setString(4, Strings.trimToNull(paramsAsString)); insertStatement.executeUpdate(); } finally { DbUtils.closeQuietly(insertStatement); } } if (!connection.getAutoCommit() && !noCommit) { connection.commit(); } } finally { DbUtils.closeQuietly(connection); } } catch (SQLException e) { throw new IllegalStateException("Failed to set the feature's state in the database", e); } } JDBCStateRepository(DataSource dataSource);  JDBCStateRepository(DataSource dataSource, String tableName); @Deprecated  JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable); @Deprecated  JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer);  JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer,
        boolean noCommit); private  JDBCStateRepository(Builder builder); @Override FeatureState getFeatureState(Feature feature); @Override void setFeatureState(FeatureState featureState); static Builder newBuilder(DataSource dataSource);  }
@Test void testShouldSaveStateWithoutStrategyOrParameters() { FeatureState state = new FeatureState(TestFeature.F1).disable(); repository.setFeatureState(state); assertEquals(1L, query(dataSource, "SELECT COUNT(*) FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals(0, query(dataSource, "SELECT FEATURE_ENABLED FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertNull(query(dataSource, "SELECT STRATEGY_ID FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertNull(query(dataSource, "SELECT STRATEGY_PARAMS FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); }
@Test void testShouldSaveStateStrategyAndParameters() { FeatureState state = new FeatureState(TestFeature.F1) .enable() .setStrategyId("someId") .setParameter("param", "foo"); repository.setFeatureState(state); assertEquals(1L, query(dataSource, "SELECT COUNT(*) FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals(1, query(dataSource, "SELECT FEATURE_ENABLED FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals("someId", query(dataSource, "SELECT STRATEGY_ID FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals("param=foo", query(dataSource, "SELECT STRATEGY_PARAMS FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); }
@Test void testShouldUpdateExistingDatabaseEntry() { update(dataSource, "INSERT INTO TOGGLZ VALUES ('F1', 1, 'myStrategy', 'param23=foobar')"); assertEquals(1L, query(dataSource, "SELECT COUNT(*) FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals(1, query(dataSource, "SELECT FEATURE_ENABLED FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals("myStrategy", query(dataSource, "SELECT STRATEGY_ID FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals("param23=foobar", query(dataSource, "SELECT STRATEGY_PARAMS FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); FeatureState state = new FeatureState(TestFeature.F1) .disable() .setStrategyId("someId") .setParameter("param", "foo"); repository.setFeatureState(state); assertEquals(1L, query(dataSource, "SELECT COUNT(*) FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals(0, query(dataSource, "SELECT FEATURE_ENABLED FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals("someId", query(dataSource, "SELECT STRATEGY_ID FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); assertEquals("param=foo", query(dataSource, "SELECT STRATEGY_PARAMS FROM TOGGLZ WHERE FEATURE_NAME = 'F1'")); }