@Override public void close() { IOUtils.closeQuietly(stream); DatabaseUtils.closeQuietly(rs); DatabaseUtils.closeQuietly(stmt); } }
/** * Partition by 1000 elements a list of input and execute a function on each part. * * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query */ public static <OUTPUT, INPUT extends Comparable<INPUT>> List<OUTPUT> executeLargeInputs(Collection<INPUT> input, Function<List<INPUT>, List<OUTPUT>> function) { return executeLargeInputs(input, function, i -> i); }
private static List<Project> selectProjects(DbSession session, @Nullable String projectUuid) { List<Project> projects = new ArrayList<>(); try (PreparedStatement stmt = createProjectsStatement(session, projectUuid); ResultSet rs = stmt.executeQuery()) { while (rs.next()) { String orgUuid = rs.getString(1); String uuid = rs.getString(2); String key = rs.getString(3); String name = rs.getString(4); Long analysisDate = DatabaseUtils.getLong(rs, 5); List<String> tags = readDbTags(DatabaseUtils.getString(rs, 6)); Project project = new Project(orgUuid, uuid, key, name, tags, analysisDate); projects.add(project); } return projects; } catch (SQLException e) { throw new IllegalStateException("Fail to execute request to select all projects", e); } }
private Measures selectMeasures(String projectUuid) { Measures measures = new Measures(); ResultSet rs = null; try { AtomicInteger index = new AtomicInteger(1); measuresStatement.setString(index.getAndIncrement(), projectUuid); METRIC_KEYS.forEach(DatabaseUtils.setStrings(measuresStatement, index::getAndIncrement)); measuresStatement.setBoolean(index.getAndIncrement(), ENABLED); rs = measuresStatement.executeQuery(); while (rs.next()) { readMeasure(rs, measures); } return measures; } catch (Exception e) { throw new IllegalStateException(String.format("Fail to execute request to select measures of project %s", projectUuid), e); } finally { DatabaseUtils.closeQuietly(rs); } }
/** * Ensure values {@code inputs} are unique (which avoids useless arguments) and sorted before creating the partition. */ public static <INPUT extends Comparable<INPUT>> Iterable<List<INPUT>> toUniqueAndSortedPartitions(Collection<INPUT> inputs) { return toUniqueAndSortedPartitions(inputs, i -> i); }
/** * Partition by 1000 elements a list of input and execute a consumer on each part. * * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query */ public static <INPUT extends Comparable<INPUT>> void executeLargeUpdates(Collection<INPUT> inputs, Consumer<List<INPUT>> consumer) { executeLargeUpdates(inputs, consumer, i -> i); }
doc.setLine(DatabaseUtils.getInt(rs, 3)); doc.setResolution(rs.getString(4)); doc.setSeverity(rs.getString(5)); doc.setStatus(rs.getString(6)); doc.setEffort(getLong(rs, 7)); doc.setAuthorLogin(rs.getString(8)); doc.setFuncCloseDate(longToDate(getLong(rs, 9))); doc.setFuncCreationDate(longToDate(getLong(rs, 10))); doc.setFuncUpdateDate(longToDate(getLong(rs, 11))); Integer ruleId = rs.getInt(12); doc.setRuleId(ruleId); doc.setOrganizationUuid(rs.getString(18)); String branchUuid = rs.getString(19); String mainBranchProjectUuid = DatabaseUtils.getString(rs, 20); doc.setBranchUuid(branchUuid); if (mainBranchProjectUuid == null) {
@Override public void start() { try (Connection connection = database.getDataSource().getConnection()) { checkState(DatabaseUtils.tableExists(MigrationHistoryTable.NAME, connection), "Migration history table is missing"); } catch (SQLException e) { Throwables.propagate(e); } }
/** * Ensure values {@code inputs} are unique (which avoids useless arguments) and sorted before creating the partition. */ public static <INPUT extends Comparable<INPUT>> Iterable<List<INPUT>> toUniqueAndSortedPartitions(Collection<INPUT> inputs, IntFunction<Integer> partitionSizeManipulations) { int partitionSize = partitionSizeManipulations.apply(PARTITION_SIZE_FOR_ORACLE); return Iterables.partition(toUniqueAndSortedList(inputs), partitionSize); }
@Override public String[] convert(ResultSet rs) throws SQLException { String[] row = new String[nbColumns]; for (int i = 0; i < nbColumns; i++) { row[i] = DatabaseUtils.getString(rs, i + 1); } return row; } }
private static boolean doTableExists(String table, Connection connection) { String schema = null; try { // Using H2 with a JDBC TCP connection is throwing an exception // See org.h2.engine.SessionRemote#getCurrentSchemaName() if (!"H2 JDBC Driver".equals(connection.getMetaData().getDriverName())) { schema = connection.getSchema(); } } catch (SQLException e) { Loggers.get(DatabaseUtils.class).warn("Fail to determine schema. Keeping it null for searching tables", e); } // table type is used to speed-up Oracle by removing introspection of system tables and aliases. try (ResultSet rs = connection.getMetaData().getTables(connection.getCatalog(), schema, table, TABLE_TYPE)) { while (rs.next()) { String name = rs.getString("TABLE_NAME"); if (table.equalsIgnoreCase(name)) { return true; } } return false; } catch (SQLException e) { throw wrapSqlException(e, "Can not check that table %s exists", table); } }
public IssueIteratorForMultipleChunks(DbClient dbClient, Collection<String> issueKeys) { this.dbClient = dbClient; iteratorOverChunks = DatabaseUtils.toUniqueAndSortedPartitions(issueKeys).iterator(); }
public void deleteDeprecatedRuleKeys(DbSession dbSession, Collection<String> uuids) { if (uuids.isEmpty()) { return; } executeLargeUpdates(uuids, mapper(dbSession)::deleteDeprecatedRuleKeys); }
doc.setLine(DatabaseUtils.getInt(rs, 3)); doc.setResolution(rs.getString(4)); doc.setSeverity(rs.getString(5)); doc.setStatus(rs.getString(6)); doc.setEffort(getLong(rs, 7)); doc.setAuthorLogin(rs.getString(8)); doc.setFuncCloseDate(longToDate(getLong(rs, 9))); doc.setFuncCreationDate(longToDate(getLong(rs, 10))); doc.setFuncUpdateDate(longToDate(getLong(rs, 11))); Integer ruleId = rs.getInt(12); doc.setRuleId(ruleId); doc.setOrganizationUuid(rs.getString(18)); String branchUuid = rs.getString(19); String mainBranchProjectUuid = DatabaseUtils.getString(rs, 20); doc.setBranchUuid(branchUuid); if (mainBranchProjectUuid == null) {
public boolean tableExists(Database database) throws SQLException { try (Connection connection = database.getDataSource().getConnection()) { return DatabaseUtils.tableExists(tableName, connection); } }
@Test public void toUniqueAndSortedList_removes_duplicates() { assertThat(toUniqueAndSortedList(asList("A", "A", "A"))).containsExactly("A"); assertThat(toUniqueAndSortedList(asList("A", "C", "A"))).containsExactly("A", "C"); assertThat(toUniqueAndSortedList(asList("C", "C", "B", "B", "A", "N", "C", "A"))).containsExactly("A", "B", "C", "N"); }
@Override public String[] convert(ResultSet rs) throws SQLException { String[] row = new String[nbColumns]; for (int i = 0; i < nbColumns; i++) { row[i] = DatabaseUtils.getString(rs, i + 1); } return row; } }
/** * @param table case-insensitive name of table * @return true if a table exists with this name, otherwise false * @throws SQLException */ public static boolean tableExists(String table, Connection connection) { // table type is used to speed-up Oracle by removing introspection of system tables and aliases. try (ResultSet rs = connection.getMetaData().getTables(null, null, null, TABLE_TYPE)) { while (rs.next()) { String name = rs.getString("TABLE_NAME"); if (table.equalsIgnoreCase(name)) { return true; } } return false; } catch (SQLException e) { throw wrapSqlException(e, "Can not check that table %s exists", table); } }
/** * Partition by 1000 elements a list of input and execute a function on each part. * * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query */ public static <OUTPUT, INPUT extends Comparable<INPUT>> List<OUTPUT> executeLargeInputs(Collection<INPUT> input, Function<List<INPUT>, List<OUTPUT>> function, IntFunction<Integer> partitionSizeManipulations) { return executeLargeInputs(input, function, size -> size == 0 ? Collections.emptyList() : new ArrayList<>(size), partitionSizeManipulations); }
public <T> List<T> select(Connection connection, String sql, RowConverter<T> rowConverter) throws SQLException { PreparedStatement stmt = null; ResultSet rs = null; try { stmt = connection.prepareStatement(sql); rs = stmt.executeQuery(); List<T> result = new ArrayList<>(); while (rs.next()) { result.add(rowConverter.convert(rs)); } return result; } finally { DatabaseUtils.closeQuietly(rs); DatabaseUtils.closeQuietly(stmt); } }