Search in sources :

Example 21 with CConnection

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

the class PostgreSQLTagFunctions method setName.

/**
   * Changes the name of a tag.
   * 
   * @param provider The connection to the database.
   * @param tag The tag whose name is changed.
   * @param name The new name of the tag.
   * 
   * @throws CouldntSaveDataException Thrown if changing the tag name failed.
   */
public static void setName(final AbstractSQLProvider provider, final CTag tag, final String name) throws CouldntSaveDataException {
    checkArguments(provider, tag);
    Preconditions.checkNotNull(name, "IE00565: Name argument can not be null");
    final CConnection connection = provider.getConnection();
    final String query = "update " + CTableNames.TAGS_TABLE + " set name = ? where id = ?";
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
        statement.setString(1, name);
        statement.setInt(2, tag.getId());
        statement.executeUpdate();
    } 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) PreparedStatement(java.sql.PreparedStatement)

Example 22 with CConnection

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

the class PostgreSQLTagFunctions method createTag.

/**
   * Creates a new tag in the database.
   * 
   * @param provider The connection to the database.
   * @param parent The parent tag of the tag.
   * @param name The name of the new tag.
   * @param description The description of the new tag.
   * @param type The type of the new tag.
   * 
   * @return The new tag.
   * 
   * @throws CouldntSaveDataException Thrown if creating the tag failed.
   */
public static CTag createTag(final AbstractSQLProvider provider, final CTag parent, final String name, final String description, final TagType type) throws CouldntSaveDataException {
    checkArguments(provider, parent, type);
    Preconditions.checkNotNull(name, "IE00556: Name argument can not be null");
    Preconditions.checkNotNull(description, "IE00557: Description argument can not be null");
    final CConnection connection = provider.getConnection();
    final String query = "insert into " + CTableNames.TAGS_TABLE + "(parent_id, name, description, type) values(?, ?, ?, ?::tag_type) returning id";
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
        if (parent.getId() == 0) {
            statement.setNull(1, Types.INTEGER);
        } else {
            statement.setInt(1, parent.getId());
        }
        statement.setString(2, name);
        statement.setString(3, description);
        statement.setString(4, tagToString(type));
        Integer id = null;
        try (ResultSet resultSet = statement.executeQuery()) {
            while (resultSet.next()) {
                if (resultSet.isFirst()) {
                    id = resultSet.getInt(1);
                }
            }
        }
        if (id != null) {
            return new CTag(id, name, description, type, provider);
        } else {
            throw new IllegalStateException("IE02141: Error id can not be null");
        }
    } 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) ResultSet(java.sql.ResultSet) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) PreparedStatement(java.sql.PreparedStatement)

Example 23 with CConnection

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

the class PostgreSQLTagFunctions method deleteTag.

/**
   * Deletes a tag from the database.
   * 
   * @param provider The connection to the database.
   * @param tag The tag to delete.
   * 
   * @throws CouldntDeleteException Thrown if the tag could not be deleted.
   */
public static void deleteTag(final AbstractSQLProvider provider, final ITreeNode<CTag> tag) throws CouldntDeleteException {
    checkArguments(provider, tag);
    Preconditions.checkNotNull(tag.getParent(), "IE00558: Can not delete the root tag");
    final CConnection connection = provider.getConnection();
    try {
        final ITreeNode<CTag> parent = tag.getParent();
        final String parentId = parent.getObject().getId() == 0 ? "null" : String.valueOf(parent.getObject().getId());
        final String query_1 = String.format("UPDATE %s SET parent_id = %s WHERE parent_id = ?", CTableNames.TAGS_TABLE, parentId);
        try (PreparedStatement statement_1 = connection.getConnection().prepareStatement(query_1)) {
            statement_1.setInt(1, tag.getObject().getId());
            statement_1.executeUpdate();
        }
        final String query_2 = String.format("DELETE FROM %s WHERE id = ?", CTableNames.TAGS_TABLE);
        try (PreparedStatement statement_2 = connection.getConnection().prepareStatement(query_2)) {
            statement_2.setInt(1, tag.getObject().getId());
            statement_2.executeUpdate();
        }
    } catch (final SQLException e) {
        throw new CouldntDeleteException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException) SQLException(java.sql.SQLException) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) PreparedStatement(java.sql.PreparedStatement)

Example 24 with CConnection

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

the class PostgreSQLTagFunctions method deleteTagSubtree.

/**
   * Deletes a tag and all of its subtrees.
   * 
   * @param provider The connection to the database.
   * @param tag The root tag of the subtree to delete.
   * 
   * @throws CouldntDeleteException Thrown if the subtree could not be deleted.
   */
public static void deleteTagSubtree(final AbstractSQLProvider provider, final ITreeNode<CTag> tag) throws CouldntDeleteException {
    checkArguments(provider, tag);
    Preconditions.checkNotNull(tag.getParent(), "IE00559: Can not delete the root tag");
    final CConnection connection = provider.getConnection();
    try {
        final StringBuilder query = new StringBuilder(String.format("delete from %s where id = %d", CTableNames.TAGS_TABLE, tag.getObject().getId()));
        final List<Integer> idsToDelete = new ArrayList<>();
        for (final ITreeNode<CTag> child : DepthFirstSorter.getSortedList(tag)) {
            idsToDelete.add(child.getObject().getId());
            query.append(" or id = " + tag.getObject().getId());
        }
        connection.executeUpdate(query.toString(), true);
    } catch (final SQLException e) {
        throw new CouldntDeleteException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) CTag(com.google.security.zynamics.binnavi.Tagging.CTag)

Example 25 with CConnection

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

the class PostgreSQLTraceFunctions method createTrace.

/**
   * Creates a new trace list object in the database.
   *
   * @param provider The connection to the database.
   * @param tracesTable The traces table where the trace is added.
   * @param tracesColumn Identifies the view container column for which the trace is created.
   * @param containerTable Identifies the view container table.
   * @param containerId ID of the view container for which the trace is created.
   * @param name The name of the new trace.
   * @param description The description of the new trace.
   *
   * @return The created trace list.
   *
   * @throws CouldntSaveDataException Thrown if the trace list could not be created.
   */
// ESCA-JAVA0138:
private static TraceList createTrace(final AbstractSQLProvider provider, final String tracesTable, final String tracesColumn, final String containerTable, final int containerId, final String name, final String description) throws CouldntSaveDataException {
    Preconditions.checkNotNull(name, "IE00568: Name argument can not be null");
    Preconditions.checkNotNull(description, "IE00569: Description argument can not be null");
    final CConnection connection = provider.getConnection();
    final String query = "INSERT INTO " + CTableNames.TRACES_TABLE + "(view_id, name, description) VALUES(?, ?, ?) RETURNING id";
    try {
        final PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        Integer listId = null;
        try {
            statement.setInt(1, 0);
            statement.setString(2, name);
            statement.setString(3, description);
            final ResultSet resultSet = statement.executeQuery();
            try {
                while (resultSet.next()) {
                    if (resultSet.isFirst()) {
                        listId = resultSet.getInt(1);
                    }
                }
            } finally {
                resultSet.close();
            }
        } finally {
            statement.close();
        }
        connection.executeUpdate("INSERT INTO " + tracesTable + "(" + tracesColumn + ", trace_id) " + " VALUES(" + containerId + ", " + listId + ")", true);
        PostgreSQLHelpers.updateModificationDate(connection, containerTable, containerId);
        return new TraceList(listId, name, description, provider);
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ResultSet(java.sql.ResultSet) TraceList(com.google.security.zynamics.binnavi.debug.models.trace.TraceList) PreparedStatement(java.sql.PreparedStatement)

Aggregations

CConnection (com.google.security.zynamics.binnavi.Database.CConnection)70 SQLException (java.sql.SQLException)59 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)31 PreparedStatement (java.sql.PreparedStatement)25 ResultSet (java.sql.ResultSet)25 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)20 ArrayList (java.util.ArrayList)15 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)7 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)6 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)6 Timestamp (java.sql.Timestamp)6 HashMap (java.util.HashMap)6 Set (java.util.Set)6 DebuggerTemplate (com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate)5 AbstractSQLProvider (com.google.security.zynamics.binnavi.Database.AbstractSQLProvider)4 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)4 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)4 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)4 CallableStatement (java.sql.CallableStatement)4 Test (org.junit.Test)4