@Override public PluginResourceTypeView getResourceTypeView(ResourceType type) { return new SQLPluginResourceTypeView(dbConnectionPool, dbQueryExecutor, account, type); }
private ResourceMeta getMeta(Connection conn, String name, int version) throws SQLException { PreparedStatement statement = conn.prepareStatement( "SELECT name, version, slated, live FROM pluginMeta WHERE tenant_id=? " + "AND plugin_type=? AND plugin_name=? AND resource_type=? AND name=? AND version=? AND deleted=false"); try { setConstantFields(statement); statement.setString(5, name); statement.setInt(6, version); return getResourceMeta(statement); } finally { statement.close(); } }
private ResourceMeta getResourceMeta(PreparedStatement statement) throws SQLException { ResultSet results = statement.executeQuery(); try { if (results.next()) { return metaFromResult(results); } return null; } finally { results.close(); } }
@Override public Set<ResourceMeta> getResourcesToSync() throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name, version, slated, live FROM pluginMeta WHERE tenant_id=? AND plugin_type=? AND " + "plugin_name=? AND resource_type=? AND slated=true AND deleted=false"); try { setConstantFields(statement); return getResourceMetaSet(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting metadata of all resources slated to be active.", e); throw new IOException(e); } }
private void setConstantFields(PreparedStatement statement) throws SQLException { setConstantFields(statement, 1); }
@Override public Map<String, Set<ResourceMeta>> getAll() throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name, version, slated, live FROM pluginMeta " + "WHERE tenant_id=? AND plugin_type=? AND plugin_name=? AND resource_type=? AND deleted=false"); try { setConstantFields(statement); return getResourceMetaMap(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting resources.", e); throw new IOException(e); } }
@Override public void stage(String name, int version) throws IOException { Connection conn = null; try { conn = dbConnectionPool.getConnection(false); try { // set the slated flag for this version to true // if it returns 0, no rows were updated, which means the resource does not exist if (slateVersion(conn, name, version) > 0) { // set slated to false for any other version except this one unslateResource(conn, name, version); } conn.commit(); } finally { conn.close(); } } catch (SQLException e) { if (conn != null) { LOG.error("Exception activating version {} of {}. Rolling back...", version, name, e); try { conn.rollback(); } catch (SQLException se) { LOG.error("Exception rolling back.", se); } } throw new IOException(e); } }
@Override public ResourceMeta get(String name, int version) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { return getMeta(conn, name, version); } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting metadata of version {} of resource {} for tenant {}.", version, name, tenant, e); throw new IOException(e); } }
@Override public Set<ResourceMeta> getLiveResources() throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name, version, slated, live FROM pluginMeta WHERE tenant_id=? AND plugin_type=? AND " + "plugin_name=? AND resource_type=? AND live=true AND deleted=false"); try { setConstantFields(statement); return getResourceMetaSet(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting metadata of all resources slated to be active.", e); throw new IOException(e); } }
private int unslateResource(Connection conn, String name, int exceptVersion) throws SQLException { PreparedStatement statement = conn.prepareStatement( "UPDATE pluginMeta SET slated=false WHERE tenant_id=? AND plugin_type=? " + "AND plugin_name=? AND resource_type=? AND name=? AND slated=true AND version<>?"); try { setConstantFields(statement); statement.setString(5, name); statement.setInt(6, exceptVersion); return statement.executeUpdate(); } finally { statement.close(); } }
@Override public Map<String, Set<ResourceMeta>> getAll(ResourceStatus status) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name, version, slated, live FROM pluginMeta WHERE tenant_id=? AND plugin_type=? " + "AND plugin_name=? AND resource_type=? AND slated=? AND live=? AND deleted=false"); try { setConstantFields(statement); statement.setBoolean(5, status.isLiveAfterSync()); statement.setBoolean(6, status.isLive()); return getResourceMetaMap(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting resources.", e); throw new IOException(e); } }
@Override public Set<ResourceMeta> getAll(String name) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name, version, slated, live FROM pluginMeta " + "WHERE tenant_id=? AND plugin_type=? AND plugin_name=? AND resource_type=? AND name=? AND deleted=false"); try { setConstantFields(statement); statement.setString(5, name); return getResourceMetaSet(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting all metadata for resource {}.", name, e); throw new IOException(e); } }
private int slateVersion(Connection conn, String name, int version) throws SQLException { PreparedStatement statement = conn.prepareStatement( "UPDATE pluginMeta SET slated=true WHERE tenant_id=? AND plugin_type=? " + "AND plugin_name=? AND resource_type=? AND name=? AND version=?"); try { setConstantFields(statement); statement.setString(5, name); statement.setInt(6, version); return statement.executeUpdate(); } finally { statement.close(); } }
private Set<ResourceMeta> getResourceMetaSet(PreparedStatement statement) throws SQLException { ResultSet results = statement.executeQuery(); try { Set<ResourceMeta> output = Sets.newHashSet(); while (results.next()) { output.add(metaFromResult(results)); } return ImmutableSet.copyOf(output); } finally { results.close(); } }
@Override public Set<ResourceMeta> getAll(String name, ResourceStatus status) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name, version, slated, live FROM pluginMeta " + "WHERE tenant_id=? AND plugin_type=? AND plugin_name=? AND " + "resource_type=? AND name=? AND slated=? AND live=? AND deleted=false"); try { setConstantFields(statement); statement.setString(5, name); statement.setBoolean(6, status.isLiveAfterSync()); statement.setBoolean(7, status.isLive()); return getResourceMetaSet(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting all metadata for resource {}.", name, e); throw new IOException(e); } }
@Override public void recall(String name, int version) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "UPDATE pluginMeta SET slated=false WHERE tenant_id=? AND plugin_type=? AND " + "plugin_name=? AND resource_type=? AND name=? AND version=?"); try { setConstantFields(statement); statement.setString(5, name); statement.setInt(6, version); statement.executeUpdate(); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception deactivating plugin resource {}.", name, e); throw new IOException(e); } }
private Map<String, Set<ResourceMeta>> getResourceMetaMap(PreparedStatement statement) throws SQLException { ResultSet results = statement.executeQuery(); try { Map<String, Set<ResourceMeta>> output = Maps.newHashMap(); while (results.next()) { ResourceMeta meta = metaFromResult(results); String name = meta.getName(); if (output.containsKey(name)) { output.get(name).add(meta); } else { output.put(name, Sets.newHashSet(meta)); } } return ImmutableMap.copyOf(output); } finally { results.close(); } }
@Override public void delete(String name, int version) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "UPDATE pluginMeta SET deleted=true, delete_time=? WHERE " + "tenant_id=? AND plugin_type=? AND plugin_name=? AND resource_type=? AND name=? AND version=?"); try { statement.setTimestamp(1, DBHelper.getTimestamp(System.currentTimeMillis())); setConstantFields(statement, 2); statement.setString(6, name); statement.setInt(7, version); statement.executeUpdate(); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception deleting version {} of resource {} for tenant {}.", version, name, tenant); throw new IOException(e); } }
@Override public boolean exists(String name, int version) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name FROM pluginMeta WHERE tenant_id=? AND plugin_type=? AND plugin_name=? " + "AND resource_type=? AND name=? AND version=? AND deleted=false"); try { setConstantFields(statement); statement.setString(5, name); statement.setInt(6, version); return dbQueryExecutor.hasResults(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception checking existance of version {} of resource {} for tenant {}.", version, name, tenant); throw new IOException(e); } }
@Override public boolean exists(String name) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name FROM pluginMeta WHERE " + "tenant_id=? AND plugin_type=? AND plugin_name=? AND resource_type=? AND name=? AND deleted=false"); try { setConstantFields(statement); statement.setString(5, name); return dbQueryExecutor.hasResults(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception checking existance of resource {} for tenant {}.", name, tenant); throw new IOException(e); } }