use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLNodeSaver method saveFunctionNodes.
/**
* Saves the function nodes to the database.
*
* @param provider The connection to the database.
* @param nodes The nodes to save.
* @param firstNode The database index of the first node.
* @param functionNodeIndices Index into the nodes list that identifies the function nodes.
*
* @throws SQLException Thrown if saving the function nodes failed.
*/
protected static void saveFunctionNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> functionNodeIndices) throws SQLException {
if (functionNodeIndices.isEmpty()) {
return;
}
final String query = "INSERT INTO " + CTableNames.FUNCTION_NODES_TABLE + "(module_id, node_id, function, comment_id) VALUES (?, ?, ?, ?)";
final ArrayList<INaviFunctionNode> functionNodesWithUnsavedComments = new ArrayList<INaviFunctionNode>();
final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
try {
for (final int index : functionNodeIndices) {
final CFunctionNode node = (CFunctionNode) nodes.get(index);
final INaviFunction function = node.getFunction();
final List<IComment> comments = node.getLocalFunctionComment();
final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
functionNodesWithUnsavedComments.add(node);
}
preparedStatement.setInt(1, function.getModule().getConfiguration().getId());
preparedStatement.setInt(2, firstNode + index);
preparedStatement.setObject(3, function.getAddress().toBigInteger(), Types.BIGINT);
if (commentId == null) {
preparedStatement.setNull(4, Types.INTEGER);
} else {
preparedStatement.setInt(4, commentId);
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
for (final INaviFunctionNode functionNode : functionNodesWithUnsavedComments) {
final ArrayList<IComment> functionNodeComments = new ArrayList<IComment>();
for (final IComment comment : functionNode.getLocalFunctionComment()) {
try {
final Integer commentId = provider.appendFunctionNodeComment(functionNode, comment.getComment(), comment.getUser().getUserId());
final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
functionNodeComments.add(newComment);
} catch (final CouldntSaveDataException exception) {
CUtilityFunctions.logException(exception);
}
}
functionNode.initializeLocalFunctionComment(functionNodeComments);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLNodeSaver method saveCodeNodes.
/**
* Saves the code nodes to the database.
*
* @param provider The connection to the database.
* @param nodes The nodes to save.
* @param firstNode The database index of the first node.
* @param codeNodeIndices Index into the nodes list that identifies the code nodes.
*
* @throws SQLException Thrown if saving the code node instructions failed.
*/
protected static void saveCodeNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> codeNodeIndices) throws SQLException {
if (!codeNodeIndices.isEmpty()) {
final List<Pair<INaviCodeNode, INaviInstruction>> instructionsWithUnsavedLocalComments = PostgreSQLNodeSaver.saveCodeNodeInstructions(provider, nodes, firstNode, codeNodeIndices);
final String query = "INSERT INTO " + CTableNames.CODE_NODES_TABLE + "(module_id, node_id, parent_function, comment_id) VALUES (?, ?, ?, ?)";
final ArrayList<INaviCodeNode> codeNodesWithUnsavedComments = new ArrayList<INaviCodeNode>();
final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
try {
for (final int index : codeNodeIndices) {
final INaviCodeNode codeNode = (INaviCodeNode) nodes.get(index);
codeNode.setId(firstNode + index);
INaviFunction function = null;
try {
function = codeNode.getParentFunction();
} catch (final MaybeNullException e) {
}
final int moduleId = Iterables.getLast(codeNode.getInstructions()).getModule().getConfiguration().getId();
final List<IComment> comment = codeNode.getComments().getLocalCodeNodeComment();
final Integer commentId = comment == null ? null : comment.size() == 0 ? null : Iterables.getLast(comment).getId();
if ((comment != null) && (comment.size() != 0) && (commentId == null)) {
codeNodesWithUnsavedComments.add(codeNode);
}
preparedStatement.setInt(1, moduleId);
preparedStatement.setInt(2, firstNode + index);
if (function == null) {
preparedStatement.setNull(3, Types.BIGINT);
} else {
preparedStatement.setObject(3, function.getAddress().toBigInteger(), Types.BIGINT);
}
if (commentId == null) {
preparedStatement.setNull(4, Types.INTEGER);
} else {
preparedStatement.setInt(4, commentId);
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
// implementation.
for (final INaviCodeNode codeNode : codeNodesWithUnsavedComments) {
final ArrayList<IComment> codeNodecomments = new ArrayList<IComment>();
for (final IComment comment : codeNode.getComments().getLocalCodeNodeComment()) {
try {
final Integer commentId = PostgreSQLNodeFunctions.appendLocalCodeNodeComment(provider, codeNode, comment.getComment(), comment.getUser().getUserId());
final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
codeNodecomments.add(newComment);
} catch (final CouldntSaveDataException exception) {
CUtilityFunctions.logException(exception);
}
}
codeNode.getComments().initializeLocalCodeNodeComment(codeNodecomments);
}
// implementation.
for (final Pair<INaviCodeNode, INaviInstruction> pair : instructionsWithUnsavedLocalComments) {
final ArrayList<IComment> localInstructionComments = new ArrayList<IComment>();
for (final IComment comment : pair.first().getComments().getLocalInstructionComment(pair.second())) {
try {
final int commentId = PostgreSQLInstructionFunctions.appendLocalInstructionComment(provider, pair.first(), pair.second(), comment.getComment(), comment.getUser().getUserId());
final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
localInstructionComments.add(newComment);
} catch (final CouldntSaveDataException exception) {
CUtilityFunctions.logException(exception);
}
}
pair.first().getComments().initializeLocalInstructionComment(pair.second(), localInstructionComments);
}
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLNodeSaver method saveGroupNodes.
/**
* Saves the group nodes to the database.
*
* @param provider The connection to the database.
* @param nodes The nodes to save.
* @param firstNode The database index of the first node.
* @param groupNodeIndices Index into the nodes list that identifies the group nodes.
*
* @throws SQLException Thrown if saving the group nodes failed.
*/
protected static void saveGroupNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> groupNodeIndices) throws SQLException {
Preconditions.checkNotNull(provider, "IE02525: connection argument can not be null");
Preconditions.checkNotNull(nodes, "IE02526: nodes argument can not be null");
Preconditions.checkNotNull(groupNodeIndices, "Error: groupNodeIndices argument can not be null");
if (!groupNodeIndices.isEmpty()) {
final String query = "INSERT INTO " + CTableNames.GROUP_NODES_TABLE + "(node_id, collapsed, comment_id) VALUES (?, ?, ?)";
final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
final List<INaviGroupNode> groupNodesWithUnsavedComments = new ArrayList<INaviGroupNode>();
try {
for (final Integer index : groupNodeIndices) {
final INaviGroupNode node = (INaviGroupNode) nodes.get(index);
preparedStatement.setInt(1, firstNode + index);
preparedStatement.setBoolean(2, node.isCollapsed());
final List<IComment> comment = node.getComments();
final Integer commentId = comment == null ? null : comment.size() == 0 ? null : Iterables.getLast(comment).getId();
if ((comment != null) && (comment.size() != 0) && (commentId == null)) {
groupNodesWithUnsavedComments.add(node);
}
if (commentId == null) {
preparedStatement.setNull(3, Types.INTEGER);
} else {
preparedStatement.setInt(3, commentId);
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
// TODO (timkornau): this can work better.
for (final INaviGroupNode groupNode : groupNodesWithUnsavedComments) {
final ArrayList<IComment> groupNodeComments = new ArrayList<IComment>();
for (final IComment comment : groupNode.getComments()) {
try {
final Integer commentId = provider.appendGroupNodeComment(groupNode, comment.getComment(), comment.getUser().getUserId());
final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
groupNodeComments.add(newComment);
} catch (final CouldntSaveDataException exception) {
CUtilityFunctions.logException(exception);
}
}
groupNode.initializeComment(groupNodeComments);
}
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLHelpers method updateModificationDate.
/**
* Updates the modification date column of a row identified by an ID in a given table.
*
* @param connection Connection to a SQL database.
* @param tableName table name of the table that contains the row to update.
* @param id ID of the row to update.
* @throws CouldntSaveDataException
*/
public static void updateModificationDate(final CConnection connection, final String tableName, final int id) throws CouldntSaveDataException {
Preconditions.checkNotNull(tableName, "IE01486: Table name argument can not be null");
Preconditions.checkNotNull(connection, "IE01858: Connection argument can not be null");
final String query = "UPDATE " + tableName + " SET modification_date = NOW() WHERE id = ?";
try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
statement.setInt(1, id);
statement.executeUpdate();
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class RenameTypeInstanceAction method actionPerformed.
@Override
public void actionPerformed(final ActionEvent e) {
final EditVariableDialog dialog = EditVariableDialog.CreateEditVariableDialog(parent, typeInstance.getName());
dialog.setVisible(true);
if (dialog.wasOkClicked()) {
try {
instanceContainer.setInstanceName(typeInstance, dialog.getVariableName());
} catch (CouldntSaveDataException | CouldntLoadDataException exception) {
CUtilityFunctions.logException(exception);
}
}
}
Aggregations