@Override public Connection getConnection() { return delegate.getConnection(); }
private static PreparedStatement newScrollingSelectStatement(DbSession session, String sql, int fetchSize) { try { PreparedStatement stmt = session.getConnection().prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(fetchSize); return stmt; } catch (SQLException e) { throw new IllegalStateException("Fail to create SQL statement: " + sql, e); } } }
private static PreparedStatement createMeasuresStatement(DbSession session) { try { String metricNameQuestionMarks = METRIC_KEYS.stream().map(x -> "?").collect(Collectors.joining(",")); String sql = StringUtils.replace(SQL_MEASURES, "{metricNames}", metricNameQuestionMarks); return session.getConnection().prepareStatement(sql); } catch (SQLException e) { throw new IllegalStateException("Fail to prepare SQL request to select measures", e); } }
private static Database loadDatabaseMetadata(DbSession dbSession) { try { DatabaseMetaData metadata = dbSession.getConnection().getMetaData(); return new Database(metadata.getDatabaseProductName(), metadata.getDatabaseProductVersion()); } catch (SQLException e) { throw new IllegalStateException("Fail to get DB metadata", e); } } }
@Override public Connection get() { return dbSession.getConnection(); }
/** * The scanner context is very likely to contain lines, which are forcefully separated by {@code \n} characters, * whichever the platform SQ is running on ({@see LogsIteratorInputStream}). */ public Optional<String> selectScannerContext(DbSession dbSession, String taskUuid) { try (PreparedStatement stmt = dbSession.getConnection().prepareStatement("select context_data from ce_scanner_context where task_uuid=?")) { stmt.setString(1, taskUuid); try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { return Optional.of(IOUtils.toString(rs.getBinaryStream(1), UTF_8)); } return Optional.empty(); } } catch (SQLException | IOException e) { throw new IllegalStateException("Fail to retrieve scanner context of task " + taskUuid, e); } }
public void insert(DbSession dbSession, String taskUuid, InputStream data) { long now = system.now(); Connection connection = dbSession.getConnection(); try (PreparedStatement stmt = connection.prepareStatement( "INSERT INTO ce_task_input (task_uuid, created_at, updated_at, input_data) VALUES (?, ?, ?, ?)")) { stmt.setString(1, taskUuid); stmt.setLong(2, now); stmt.setLong(3, now); stmt.setBinaryStream(4, data); stmt.executeUpdate(); connection.commit(); } catch (SQLException e) { throw new IllegalStateException("Fail to insert data of CE task " + taskUuid, e); } }
private static PreparedStatement createProjectsStatement(DbSession session, @Nullable String projectUuid) { try { StringBuilder sql = new StringBuilder(SQL_PROJECTS); if (projectUuid != null) { sql.append(PROJECT_FILTER); } PreparedStatement stmt = session.getConnection().prepareStatement(sql.toString()); stmt.setBoolean(1, true); stmt.setBoolean(2, true); stmt.setString(3, Scopes.PROJECT); stmt.setString(4, Qualifiers.PROJECT); if (projectUuid != null) { stmt.setString(5, projectUuid); } return stmt; } catch (SQLException e) { throw new IllegalStateException("Fail to prepare SQL request to select all project measures", e); } }
public boolean hasProjectNotificationSubscribersForDispatchers(String projectUuid, Collection<String> dispatcherKeys) { try (DbSession session = mybatis.openSession(false); Connection connection = session.getConnection(); PreparedStatement pstmt = createStatement(projectUuid, dispatcherKeys, connection); ResultSet rs = pstmt.executeQuery()) { return rs.next() && rs.getInt(1) > 0; } catch (SQLException e) { throw new IllegalStateException("Fail to execute SQL for hasProjectNotificationSubscribersForDispatchers", e); } }
public void clearDb() { try (DbSession dbSession = dbClient.openSession(false); Connection connection = dbSession.getConnection(); Statement ddlStatement = connection.createStatement()) { for (String tableName : SqTables.TABLES) { Optional.ofNullable(TABLE_CLEANERS.get(tableName)) .orElse(BackendCleanup::truncateDefault) .clean(tableName, ddlStatement, connection); } } catch (Exception e) { throw new IllegalStateException("Fail to clear db", e); } }
public void readLineHashesStream(DbSession dbSession, String fileUuid, Consumer<Reader> consumer) { Connection connection = dbSession.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; Reader reader = null; try { pstmt = connection.prepareStatement("SELECT line_hashes FROM file_sources WHERE file_uuid=? AND data_type='SOURCE'"); pstmt.setString(1, fileUuid); rs = pstmt.executeQuery(); if (rs.next()) { reader = rs.getCharacterStream(1); if (reader != null) { consumer.accept(reader); } } } catch (SQLException e) { throw new IllegalStateException("Fail to read FILE_SOURCES.LINE_HASHES of file " + fileUuid, e); } finally { IOUtils.closeQuietly(reader); DbUtils.closeQuietly(connection, pstmt, rs); } }
/** * @throws IllegalArgumentException if {@code scannerContextLines} is empty or fully read. */ public void insert(DbSession dbSession, String taskUuid, CloseableIterator<String> scannerContextLines) { checkArgument(scannerContextLines.hasNext(), "Scanner context can not be empty"); long now = system.now(); Connection connection = dbSession.getConnection(); try (PreparedStatement stmt = connection.prepareStatement( "INSERT INTO ce_scanner_context (task_uuid, created_at, updated_at, context_data) VALUES (?, ?, ?, ?)"); InputStream inputStream = new LogsIteratorInputStream(scannerContextLines, UTF_8)) { stmt.setString(1, taskUuid); stmt.setLong(2, now); stmt.setLong(3, now); stmt.setBinaryStream(4, inputStream); stmt.executeUpdate(); connection.commit(); } catch (SQLException | IOException e) { throw new IllegalStateException("Fail to insert scanner context for task " + taskUuid, e); } }
public Optional<DataStream> selectData(DbSession dbSession, String taskUuid) { PreparedStatement stmt = null; ResultSet rs = null; DataStream result = null; try { stmt = dbSession.getConnection().prepareStatement("SELECT input_data FROM ce_task_input WHERE task_uuid=? AND input_data IS NOT NULL"); stmt.setString(1, taskUuid); rs = stmt.executeQuery(); if (rs.next()) { result = new DataStream(stmt, rs, rs.getBinaryStream(1)); return Optional.of(result); } return Optional.empty(); } catch (SQLException e) { throw new IllegalStateException("Fail to select data of CE task " + taskUuid, e); } finally { if (result == null) { DatabaseUtils.closeQuietly(rs); DatabaseUtils.closeQuietly(stmt); } } }
private void mockDbClientOpenSession() { Connection connection = mock(Connection.class); DbSession session = mock(DbSession.class); when(session.getConnection()).thenReturn(connection); when(dbClient.openSession(false)).thenReturn(session); }
@After public void tearDown() throws Exception { // schema_migration is not cleared by DbTester try(Statement statement = dbTester.getSession().getConnection().createStatement()) { statement.execute("truncate table schema_migrations"); } }
@Override public Section toProtobuf() { Section.Builder protobuf = Section.newBuilder(); protobuf.setName("Database"); try (DbSession dbSession = dbClient.openSession(false)) { DatabaseMetaData metadata = dbSession.getConnection().getMetaData(); setAttribute(protobuf, "Database", metadata.getDatabaseProductName()); setAttribute(protobuf, "Database Version", metadata.getDatabaseProductVersion()); setAttribute(protobuf, "Username", metadata.getUserName()); setAttribute(protobuf, "URL", metadata.getURL()); setAttribute(protobuf, "Driver", metadata.getDriverName()); setAttribute(protobuf, "Driver Version", metadata.getDriverVersion()); } catch (SQLException e) { throw new IllegalStateException("Fail to get DB metadata", e); } return protobuf.build(); } }
@VisibleForTesting void installH2() { Connection connection = null; try (DbSession session = dbClient.openSession(false)) { connection = session.getConnection(); createH2Schema(connection, dbClient.getDatabase().getDialect().getId()); } finally { DbUtils.closeQuietly(connection); } }
private void assertDatabaseMetadata(String json) { try (DbSession dbSession = db.getDbClient().openSession(false)) { DatabaseMetaData metadata = dbSession.getConnection().getMetaData(); assertJson(json).isSimilarTo("{\n" + " \"database\": {\n" + " \"name\": \"H2\",\n" + " \"version\": \"" + metadata.getDatabaseProductVersion() + "\"\n" + " }\n" + "}"); } catch (SQLException e) { throw new RuntimeException(e); } }
@Test public void shouldOpenBatchSession() { underTest.start(); try (DbSession session = underTest.openSession(false)) { assertThat(session.getConnection(), notNullValue()); assertThat(session.getMapper(RuleMapper.class), notNullValue()); } } }
/** * Reset data in order to to be in same state as a fresh installation (but without having to drop db and restart the server). * * Please be careful when updating this method as it's called by Orchestrator. */ public void resetData() { try (DbSession dbSession = dbClient.openSession(false); Connection connection = dbSession.getConnection()) { truncateAnalysisTables(connection); deleteManualRules(connection); truncateInternalProperties(null, null, connection); truncateUsers(null, null, connection); truncateOrganizations(null, null, connection); } catch (SQLException e) { throw new IllegalStateException("Fail to reset data", e); } clearIndex(IssueIndexDefinition.INDEX_TYPE_ISSUE); clearIndex(ViewIndexDefinition.INDEX_TYPE_VIEW); clearIndex(ProjectMeasuresIndexDefinition.INDEX_TYPE_PROJECT_MEASURES); clearIndex(ComponentIndexDefinition.INDEX_TYPE_COMPONENT); }