@Override public List<Set<String>> load(String project) throws Exception { try (Connection conn = connectionPool.getConnection()) { return getKeys(conn, project); } } });
@Override public Connection getConnection() throws SQLException { return getConnection(disablePool); }
public Connection getConnection() throws SQLException { return connectionPool.getConnection(); }
@Override public List<SchemaField> load(ProjectCollection key) throws Exception { try (Connection conn = connectionPool.getConnection()) { List<SchemaField> schema = getSchema(conn, key.project, key.collection); if (schema == null) { return ImmutableList.of(); } return schema; } } });
@Override public void revokeAllKeys(String project) { try (Connection conn = connectionPool.getConnection()) { PreparedStatement ps = conn.prepareStatement("DELETE FROM api_key WHERE project = ?"); ps.setString(1, project); ps.execute(); } catch (SQLException e) { throw Throwables.propagate(e); } }
@Override public void revokeApiKeys(String project, String masterKey) { try (Connection conn = connectionPool.getConnection()) { PreparedStatement ps = conn.prepareStatement("DELETE FROM api_key WHERE project = ? AND master_key = ?"); ps.setString(1, project); ps.setString(2, masterKey); ps.execute(); } catch (SQLException e) { throw Throwables.propagate(e); } }
@Override public void setup() { try (Connection connection = connectionPool.getConnection()) { Statement statement = connection.createStatement(); statement.execute("CREATE TABLE IF NOT EXISTS api_key (" + " id SERIAL NOT NULL,\n" + " project VARCHAR(255) NOT NULL,\n" + " read_key VARCHAR(255) NOT NULL,\n" + " write_key VARCHAR(255) NOT NULL,\n" + " master_key VARCHAR(255) NOT NULL,\n" + " created_at TIMESTAMP default current_timestamp NOT NULL," + "PRIMARY KEY (id)\n" + " )"); } catch (SQLException e) { throw new RuntimeException(e); } } }
public HashSet<String> getViews(String project) { try (Connection conn = connectionPool.getConnection()) { HashSet<String> tables = new HashSet<>(); ResultSet tableRs = conn.getMetaData().getTables("", project, null, new String[]{"VIEW"}); while (tableRs.next()) { String tableName = tableRs.getString("table_name"); if (!tableName.startsWith("_")) { tables.add(tableName); } } return tables; } catch (SQLException e) { throw Throwables.propagate(e); } }
public <T> T runRawQuery(String query, CheckedFunction<ResultSet, T> mapper) { try (Connection connection = connectionPool.getConnection()) { java.sql.Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query); return mapper.apply(resultSet); } catch (SQLException e) { throw new RuntimeException(e); } }
@Override public Map<String, List<SchemaField>> getCollections(String project) { try (Connection connection = connectionPool.getConnection()) { return getAllSchema(connection, project); } catch (SQLException e) { throw Throwables.propagate(e); } }
@Override public Set<String> getProjects() { ImmutableSet.Builder<String> builder = ImmutableSet.builder(); try (Connection connection = connectionPool.getConnection()) { ResultSet schemas = connection.getMetaData().getSchemas(); while (schemas.next()) { String tableSchem = schemas.getString("table_schem"); if (!tableSchem.equals("information_schema") && !tableSchem.startsWith("pg_") && !tableSchem.equals("public")) { builder.add(tableSchem); } } } catch (SQLException e) { throw Throwables.propagate(e); } return builder.build(); }
@Inject public PostgresqlVersion(@Named("store.adapter.postgresql") JDBCPoolDataSource dataSource) { try (Connection conn = dataSource.getConnection()) { Statement statement = conn.createStatement(); ResultSet resultsSet = statement.executeQuery("SHOW server_version"); resultsSet.next(); String version = resultsSet.getString(1); String[] split = version.split("\\.", 2); if (Integer.parseInt(split[0]) > 9) { this.version = Version.PG10; } else if (Integer.parseInt(split[0]) == 9 && Double.parseDouble(split[1]) >= 5) { this.version = Version.PG_MIN_9_5; } else { this.version = Version.OLD; } } catch (Exception e) { this.version = Version.OLD; } }
@Override public void deleteProject(String project) { try (Connection conn = connectionPool.getConnection()) { conn.createStatement().execute(format("DROP SCHEMA %s CASCADE", checkProject(project, '"'))); } catch (SQLException e) { throw new RuntimeException(e); } super.onDeleteProject(project); } }
@Override public Key getProjectKey(int apiId, AccessKeyType type) { try (Connection conn = connectionPool.getConnection()) { PreparedStatement ps = conn.prepareStatement(String.format("SELECT lower(project), %s FROM api_key WHERE id = ?", type.getKey())); ps.setInt(1, apiId); ResultSet resultSet = ps.executeQuery(); if (!resultSet.next()) { throw new RakamException("api key is invalid", FORBIDDEN); } return new Key(resultSet.getString(1), resultSet.getString(2)); } catch (SQLException e) { throw Throwables.propagate(e); } }
public PostgresqlLockService(JDBCPoolDataSource poolDataSource) { this.dbi = new DBI(() -> { return poolDataSource.getConnection(true); }); this.currentHandle = dbi.open(); locks = new ConcurrentSkipListSet<>(); }
public MysqlLockService(JDBCPoolDataSource poolDataSource) { this.dbi = new DBI(() -> { return poolDataSource.getConnection(true); }); this.currentHandle = dbi.open(); locks = new ConcurrentSkipListSet<>(); }
@Override public void createProject(String project) { if (ImmutableList.of("public", "information_schema", "pg_catalog").contains(project)) { throw new IllegalArgumentException("The name is a reserved name for Postgresql backend."); } try (Connection connection = connectionPool.getConnection()) { final Statement statement = connection.createStatement(); statement.executeUpdate(format("CREATE SCHEMA %s", checkProject(project, '"'))); statement.executeUpdate(format("CREATE FUNCTION %s.to_unixtime(timestamp) RETURNS double precision AS 'select extract(epoch from $1)' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT", checkProject(project, '"'))); } catch (SQLException e) { throw Throwables.propagate(e); } super.onCreateProject(project); }
@Override public ProjectApiKeys createApiKeys(String project) { String masterKey = CryptUtil.generateRandomKey(64); String readKey = CryptUtil.generateRandomKey(64); String writeKey = CryptUtil.generateRandomKey(64); try (Connection connection = connectionPool.getConnection()) { PreparedStatement ps = connection.prepareStatement("INSERT INTO api_key " + "(master_key, read_key, write_key, project) VALUES (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); ps.setString(1, masterKey); ps.setString(2, readKey); ps.setString(3, writeKey); ps.setString(4, project); ps.executeUpdate(); final ResultSet generatedKeys = ps.getGeneratedKeys(); generatedKeys.next(); } catch (SQLException e) { throw Throwables.propagate(e); } return ProjectApiKeys.create(masterKey, readKey, writeKey); }
@Override public String getProjectOfApiKey(String apiKey, AccessKeyType type) { if (type == null) { throw new IllegalStateException(); } if (apiKey == null) { throw new RakamException(type.getKey() + " is missing", FORBIDDEN); } try (Connection conn = connectionPool.getConnection()) { PreparedStatement ps = conn.prepareStatement(format("SELECT lower(project) FROM api_key WHERE %s = ?", type.name())); ps.setString(1, apiKey); ResultSet resultSet = ps.executeQuery(); if (!resultSet.next()) { throw new RakamException(type.getKey() + " is invalid", FORBIDDEN); } return resultSet.getString(1); } catch (SQLException e) { throw Throwables.propagate(e); } }
public void store(Event event, boolean partitionCheckDone) { GenericRecord record = event.properties(); try (Connection connection = connectionPool.getConnection()) { Schema schema = event.properties().getSchema(); PreparedStatement ps = connection.prepareStatement(getQuery(event.project(), event.collection(), schema)); bindParam(connection, ps, event.schema(), record); ps.executeUpdate(); } catch (SQLException e) { // check_violation -> https://www.postgresql.org/docs/8.2/static/errcodes-appendix.html if (version.getVersion() == PG10 && !partitionCheckDone && "23514".equals(e.getSQLState())) { generateMissingPartitions(event.project(), event.collection(), ImmutableList.of(event), 0); store(event, true); } else { throw new RuntimeException(e); } } catch (Exception e) { throw new RuntimeException(e); } }