Search in sources :

Example 66 with CouldntSaveDataException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.

the class PostgreSQLViewFunctions method untagView.

/**
   * Removes a tag from a view.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param view The view from which the tag is removed.
   * @param tag The tag to be removed from the view.
   * 
   * @throws CouldntSaveDataException Thrown if the tag could not be removed from the view.
   */
public static void untagView(final AbstractSQLProvider provider, final INaviView view, final CTag tag) throws CouldntSaveDataException {
    checkArguments(provider, view);
    Preconditions.checkNotNull(tag, "IE00617: Tag argument can not be null");
    Preconditions.checkArgument(tag.inSameDatabase(provider), "IE00618: Tag is not part of this database");
    final String query = String.format("delete from %s where view_id = %d and tag_id = %d", CTableNames.TAGGED_VIEWS_TABLE, view.getConfiguration().getId(), tag.getId());
    try {
        provider.getConnection().executeUpdate(query, true);
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
}
Also used : SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)

Example 67 with CouldntSaveDataException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.

the class PostgreSQLViewFunctions method tagView.

/**
   * Tags a view.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param view The view to tag.
   * @param tag The tag to tag the view.
   * 
   * @throws CouldntSaveDataException Thrown if the view could not be tagged.
   */
public static void tagView(final AbstractSQLProvider provider, final INaviView view, final CTag tag) throws CouldntSaveDataException {
    checkArguments(provider, view);
    Preconditions.checkNotNull(tag, "IE00615: Tag argument can not be null");
    Preconditions.checkArgument(tag.inSameDatabase(provider), "IE00616: Tag is not part of this database");
    final String query = String.format("insert into %s(view_id, tag_id) values(%d, %d)", CTableNames.TAGGED_VIEWS_TABLE, view.getConfiguration().getId(), tag.getId());
    final CConnection connection = provider.getConnection();
    try {
        connection.executeUpdate(query, true);
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)

Example 68 with CouldntSaveDataException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.

the class PostgreSQLNodeSaver method saveTextNodes.

/**
   * Saves the text 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 textNodeIndices Index into the nodes list that identifies the text nodes.
   *
   * @throws SQLException Thrown if saving the text nodes failed.
   */
protected static void saveTextNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> textNodeIndices) throws SQLException {
    Preconditions.checkNotNull(provider, "IE02527: provider argument can not be null");
    Preconditions.checkNotNull(nodes, "IE02528: nodes argument can not be null");
    Preconditions.checkNotNull(textNodeIndices, "IE02529: textNodeIndices argument can not be null");
    if (!textNodeIndices.isEmpty()) {
        final String query = "INSERT INTO " + CTableNames.TEXT_NODES_TABLE + "(node_id, comment_id) VALUES (?, ?)";
        final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
        final List<INaviTextNode> textNodesWithUnsavedComments = new ArrayList<INaviTextNode>();
        try {
            for (final Integer index : textNodeIndices) {
                final INaviTextNode node = (INaviTextNode) nodes.get(index);
                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)) {
                    textNodesWithUnsavedComments.add(node);
                }
                preparedStatement.setInt(1, firstNode + index);
                if (commentId == null) {
                    preparedStatement.setNull(2, Types.INTEGER);
                } else {
                    preparedStatement.setInt(2, commentId);
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        } finally {
            preparedStatement.close();
        }
        // unsaved comments to be stored. Possibly one can handle all of those in one query.
        for (final INaviTextNode textNode : textNodesWithUnsavedComments) {
            final ArrayList<IComment> textNodeComments = new ArrayList<IComment>();
            for (final IComment comment : textNode.getComments()) {
                try {
                    final Integer commentId = provider.appendTextNodeComment(textNode, comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    textNodeComments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            textNode.initializeComment(textNodeComments);
        }
    }
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) INaviTextNode(com.google.security.zynamics.binnavi.disassembly.INaviTextNode) PreparedStatement(java.sql.PreparedStatement)

Example 69 with CouldntSaveDataException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.

the class PostgreSQLHelpers method setName.

/**
   * Sets the 'name' column of a row identified by an ID in a given table.
   *
   * @param connection Connection to a SQL database.
   * @param id ID of the row whose 'name' column is changed.
   * @param name The new value of the name column.
   * @param tableName Name of the table where the row is found.
   *
   * @throws CouldntSaveDataException Thrown if the 'name' column could not be updated.
   */
public static void setName(final CConnection connection, final int id, final String name, final String tableName) throws CouldntSaveDataException {
    Preconditions.checkNotNull(tableName, "IE02210: Table name argument can not be null");
    Preconditions.checkNotNull(name, "IE02243: Name argument can not be null");
    Preconditions.checkNotNull(connection, "IE02252: Connection argument can not be null");
    Preconditions.checkArgument(id >= 0, "Error: Id argument can not less then zero");
    final String query = "UPDATE " + tableName + " SET name = ?, modification_date = NOW() WHERE id = ?";
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
        statement.setString(1, name);
        statement.setInt(2, id);
        statement.executeUpdate();
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
}
Also used : SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) PreparedStatement(java.sql.PreparedStatement)

Example 70 with CouldntSaveDataException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.

the class CTagFunctions method editTag.

/**
   * Used to edit the name and description of a tag.
   *
   * @param parent Parent window used for dialogs.
   * @param tag The tag to delete.
   */
public static void editTag(final JFrame parent, final CTag tag) {
    final CViewCommentDialog dlg = new CViewCommentDialog(parent, "Edit Tag", tag.getName(), tag.getDescription());
    dlg.setVisible(true);
    if (!dlg.wasCancelled()) {
        try {
            tag.setName(dlg.getName());
        } catch (final CouldntSaveDataException e) {
            CUtilityFunctions.logException(e);
            final String innerMessage = "E00126: " + "Could not change tag name";
            final String innerDescription = CUtilityFunctions.createDescription(String.format("The name of the tag '%s' could not be changed.", tag.getName()), new String[] { "There was a problem with the database connection." }, new String[] { "The tag name could not be changed." });
            NaviErrorDialog.show(parent, innerMessage, innerDescription, e);
        }
        try {
            tag.setDescription(dlg.getComment());
        } catch (final CouldntSaveDataException e) {
            CUtilityFunctions.logException(e);
            final String innerMessage = "E00127: " + "Could not change tag description";
            final String innerDescription = CUtilityFunctions.createDescription(String.format("The description of the tag '%s' could not be changed.", tag.getName()), new String[] { "There was a problem with the database connection." }, new String[] { "The tag description could not be changed." });
            NaviErrorDialog.show(parent, innerMessage, innerDescription, e);
        }
    }
}
Also used : CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CViewCommentDialog(com.google.security.zynamics.binnavi.Gui.GraphWindows.CViewCommentDialog)

Aggregations

CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)133 SQLException (java.sql.SQLException)71 PreparedStatement (java.sql.PreparedStatement)38 CConnection (com.google.security.zynamics.binnavi.Database.CConnection)35 CallableStatement (java.sql.CallableStatement)20 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)17 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)13 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)10 CDefaultProgressOperation (com.google.security.zynamics.binnavi.Gui.Progress.CDefaultProgressOperation)10 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)9 ArrayList (java.util.ArrayList)9 MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)8 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)8 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)8 BigInteger (java.math.BigInteger)8 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 TraceList (com.google.security.zynamics.binnavi.debug.models.trace.TraceList)7 LoadCancelledException (com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException)6 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)5