public void removeSpecificFileVersions(Map<FileHistoryId, List<FileVersion>> purgeFileVersions) throws SQLException { if (purgeFileVersions.size() > 0) { try (PreparedStatement preparedStatement = getStatement(connection, "fileversion.delete.all.removeSpecificFileVersionsByIds.sql")) { for (FileHistoryId purgeFileHistoryId : purgeFileVersions.keySet()) { for (FileVersion purgeFileVersion : purgeFileVersions.get(purgeFileHistoryId)) { preparedStatement.setString(1, purgeFileHistoryId.toString()); preparedStatement.setLong(2, purgeFileVersion.getVersion()); preparedStatement.addBatch(); } } preparedStatement.executeBatch(); } } }
/** * Removes all file versions with versions <b>lower or equal</b> than the given file version. * * <p>Note that this method does not just delete the given file version, but also all of its * previous versions. */ public void removeFileVersions(Map<FileHistoryId, FileVersion> purgeFileVersions) throws SQLException { if (purgeFileVersions.size() > 0) { try (PreparedStatement preparedStatement = getStatement(connection, "fileversion.delete.all.removeFileVersionsByIds.sql")) { for (Map.Entry<FileHistoryId, FileVersion> purgeFileVersionEntry : purgeFileVersions.entrySet()) { FileHistoryId purgeFileHistoryId = purgeFileVersionEntry.getKey(); FileVersion purgeFileVersion = purgeFileVersionEntry.getValue(); preparedStatement.setString(1, purgeFileHistoryId.toString()); preparedStatement.setLong(2, purgeFileVersion.getVersion()); preparedStatement.addBatch(); } preparedStatement.executeBatch(); } } }
public List<FileVersion> getFileHistory(FileHistoryId fileHistoryId) { try (PreparedStatement preparedStatement = getStatement("fileversion.select.master.getFileHistoryById.sql")) { preparedStatement.setString(1, fileHistoryId.toString()); List<FileVersion> fileTree = new ArrayList<FileVersion>(); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { FileVersion fileVersion = createFileVersionFromRow(resultSet); fileTree.add(fileVersion); } return fileTree; } catch (SQLException e) { throw new RuntimeException(e); } } catch (SQLException e) { throw new RuntimeException(e); } }
xmlOut.writeAttribute("id", fileHistory.getFileHistoryId().toString());
String fileContentChecksumStr = (fileVersion.getChecksum() != null) ? fileVersion.getChecksum().toString() : null; preparedStatement.setString(1, fileHistoryId.toString()); preparedStatement.setInt(2, Integer.parseInt("" + fileVersion.getVersion())); preparedStatement.setLong(3, databaseVersionId);
private String parsePathExpression(String pathExpression, boolean isFileHistoryId) { if (pathExpression != null) { if (isFileHistoryId) { String randomFileHistoryId = FileHistoryId.secureRandomFileId().toString(); boolean isFullLength = pathExpression.length() == randomFileHistoryId.length(); if (isFullLength) { return pathExpression; } else { return pathExpression + "%"; } } else { if (pathExpression.contains("^") || pathExpression.contains("*")) { return pathExpression.replace('^', '%').replace('*', '%'); } else { return pathExpression + "%"; } } } else { return null; } } }
/** * Writes a list of {@link PartialFileHistory}s to the database table <i>filehistory</i> using <tt>INSERT</tt>s * and the given connection. In addition, this method also writes the corresponding {@link FileVersion}s of * each file history to the database using * {@link FileVersionSqlDao#writeFileVersions(Connection, FileHistoryId, long, Collection) FileVersionSqlDao#writeFileVersions}. * * <p><b>Note:</b> This method executes, but <b>does not commit</b> the queries. * * @param connection The connection used to execute the statements * @param databaseVersionId References the {@link PartialFileHistory} to which the list of file versions belongs * @param fileHistories List of {@link PartialFileHistory}s to be written to the database * @throws SQLException If the SQL statement fails */ public void writeFileHistories(Connection connection, long databaseVersionId, Collection<PartialFileHistory> fileHistories) throws SQLException { for (PartialFileHistory fileHistory : fileHistories) { PreparedStatement preparedStatement = getStatement(connection, "filehistory.insert.all.writeFileHistories.sql"); preparedStatement.setString(1, fileHistory.getFileHistoryId().toString()); preparedStatement.setLong(2, databaseVersionId); int affectedRows = preparedStatement.executeUpdate(); if (affectedRows == 0) { throw new SQLException("Cannot add database version header. Affected rows is zero."); } preparedStatement.close(); fileVersionDao.writeFileVersions(connection, fileHistory.getFileHistoryId(), databaseVersionId, fileHistory.getFileVersions().values()); } }
public FileHistoryId expandFileHistoryId(FileHistoryId fileHistoryIdPrefix) { String fileHistoryIdPrefixLikeQuery = fileHistoryIdPrefix.toString() + "%"; try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.expandFileHistoryId.sql")) { preparedStatement.setString(1, fileHistoryIdPrefixLikeQuery); try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { FileHistoryId fullFileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id")); boolean nonUniqueResult = resultSet.next(); if (nonUniqueResult) { return null; } else { return fullFileHistoryId; } } else { return null; } } } catch (SQLException e) { throw new RuntimeException(e); } }
public FileVersion getFileVersion(FileHistoryId fileHistoryId, long version) { try (PreparedStatement preparedStatement = getStatement("fileversion.select.master.getFileVersionByHistoryAndVersion.sql")) { preparedStatement.setString(1, fileHistoryId.toString()); preparedStatement.setLong(2, version); return executeAndCreateFileVersion(preparedStatement); } catch (SQLException e) { throw new RuntimeException(e); } }
@Override public String apply(FileHistoryId fileHistoryId) { return fileHistoryId.toString(); } }).toArray(new String[0]);
@Override public void write(OutputNode node, PartialFileHistory.FileHistoryId value) throws Exception { node.setValue(value.toString()); } }