Search in sources :

Example 61 with CConnection

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

the class PostgreSQLNodeFunctions method untagNode.

/**
   * Removes a tag from a node.
   *
   * The node must be stored in the database connected to by the provider argument.
   *
   * @param provider The SQL provider that provides the connection.
   * @param node The node to be untagged.
   * @param tagId The ID of the tag to be removed from the node.
   *
   * @throws CouldntSaveDataException Thrown if the node could not be untagged.
   */
public static void untagNode(final SQLProvider provider, final INaviViewNode node, final int tagId) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE01990: Provider argument can not be null");
    Preconditions.checkNotNull(node, "IE01991: Node argument can not be null");
    final CConnection connection = provider.getConnection();
    try {
        connection.executeUpdate(String.format("DELETE FROM %s " + " WHERE node_id = %d AND tag_id = %d", CTableNames.TAGGED_NODES_TABLE, node.getId(), tagId), true);
    } 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)

Example 62 with CConnection

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

the class PostgreSQLProjectFunctions method addDebugger.

/**
   * Adds a debugger to a project.
   *
   * The project and the debugger must be stored in the database connected to by the provider
   * argument.
   *
   * @param provider The SQL provider that provides the connection.
   * @param project The project the debugger is added to.
   * @param debugger The debugger to add to the project.
   *
   * @throws CouldntSaveDataException Thrown if the debugger could not be added to the project.
   */
public static void addDebugger(final AbstractSQLProvider provider, final INaviProject project, final DebuggerTemplate debugger) throws CouldntSaveDataException {
    checkArguments(provider, project);
    Preconditions.checkNotNull(debugger, "IE00519: Debugger argument can't be null");
    Preconditions.checkArgument(debugger.inSameDatabase(provider), "IE00520: The given debugger template is not part of this database");
    final CConnection connection = provider.getConnection();
    try {
        final String insertQuery = String.format("INSERT INTO %s values(?, ?)", CTableNames.PROJECT_DEBUGGERS_TABLE);
        final PreparedStatement insertStatement = connection.getConnection().prepareStatement(insertQuery);
        final String deleteQuery = String.format("DELETE FROM %s WHERE project_id = ? AND debugger_id = ?", CTableNames.PROJECT_DEBUGGERS_TABLE);
        final PreparedStatement deleteStatement = connection.getConnection().prepareStatement(deleteQuery);
        try {
            PostgreSQLHelpers.beginTransaction(connection);
            deleteStatement.setInt(1, project.getConfiguration().getId());
            deleteStatement.setInt(2, debugger.getId());
            deleteStatement.execute();
            insertStatement.setInt(1, project.getConfiguration().getId());
            insertStatement.setInt(2, debugger.getId());
            insertStatement.execute();
            PostgreSQLHelpers.endTransaction(connection);
        } finally {
            deleteStatement.close();
            insertStatement.close();
        }
    } catch (final SQLException exception) {
        try {
            PostgreSQLHelpers.rollback(connection);
        } catch (final SQLException e) {
            throw new CouldntSaveDataException("E00056: Could not rollback transaction");
        }
        throw new CouldntSaveDataException("E00057: Could not update project debugger");
    }
    PostgreSQLHelpers.updateModificationDate(connection, "" + CTableNames.PROJECTS_TABLE + "", project.getConfiguration().getId());
}
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 63 with CConnection

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

the class PostgreSQLSectionFunctions method appendSectionComment.

/**
   * This function appends a section comment to the list of section comments associated to a section
   * in the database.
   * 
   * @param provider The provider used to access the database.
   * @param moduleId The id of the module to which the section is associated.
   * @param sectionId The id of the section to which the comment is associated.
   * @param commentText The text of the comment.
   * @param userId The id of the currently active user.
   * 
   * @return The id of the comment generated by the database.
   * 
   * @throws CouldntSaveDataException if the comment could not be saved in the database.
   */
public static Integer appendSectionComment(final SQLProvider provider, final int moduleId, final int sectionId, final String commentText, final Integer userId) throws CouldntSaveDataException {
    Preconditions.checkArgument(moduleId > 0, "Error: module id must be greater then zero");
    Preconditions.checkArgument(sectionId >= 0, "Error: section id must be greater or equal than zero");
    Preconditions.checkNotNull(commentText, "Error: comment text argument can not be null");
    Preconditions.checkNotNull(userId, "Error: user id argument can not be null");
    final CConnection connection = provider.getConnection();
    final String function = " { ? = call append_section_comment(?, ?, ?, ?) } ";
    try (CallableStatement appendCommentFunction = connection.getConnection().prepareCall(function)) {
        appendCommentFunction.registerOutParameter(1, Types.INTEGER);
        appendCommentFunction.setInt(2, moduleId);
        appendCommentFunction.setInt(3, sectionId);
        appendCommentFunction.setInt(4, userId);
        appendCommentFunction.setString(5, commentText);
        appendCommentFunction.execute();
        final int commentId = appendCommentFunction.getInt(1);
        if (appendCommentFunction.wasNull()) {
            throw new CouldntSaveDataException("Error: Got an comment id of null from the database");
        }
        return commentId;
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)

Example 64 with CConnection

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

the class PostgreSQLAddressSpaceFunctions method addModule.

/**
   * Adds a module to an address space. If adding the module was successful, the modification date
   * of the address space is updated.
   * 
   * The module and the address space must both be stored in the database connected to by the
   * provider argument.
   * 
   * @param provider The SQL provider that provides the database connection.
   * @param addressSpace The address space where the module is added.
   * @param module The module that is added to the address space.
   * 
   * @throws CouldntSaveDataException Thrown if the module could not be added to the address space.
   */
public static void addModule(final AbstractSQLProvider provider, final INaviAddressSpace addressSpace, final INaviModule module) throws CouldntSaveDataException {
    checkArguments(provider, addressSpace);
    Preconditions.checkNotNull(module, "IE01859: Module argument can not be null");
    Preconditions.checkArgument(module.inSameDatabase(provider), "IE01860: Module is not part of this database");
    final CConnection connection = provider.getConnection();
    final int addressSpaceId = addressSpace.getConfiguration().getId();
    final int moduleId = module.getConfiguration().getId();
    NaviLogger.info("Adding module %s (%d) to address space %s (%d)", addressSpace.getConfiguration().getName(), addressSpaceId, module.getConfiguration().getName(), moduleId);
    final String query = "INSERT INTO " + CTableNames.SPACE_MODULES_TABLE + " VALUES(" + moduleId + ", " + addressSpaceId + ", 0)";
    try {
        connection.executeUpdate(query, true);
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
    PostgreSQLHelpers.updateModificationDate(connection, CTableNames.ADDRESS_SPACES_TABLE, addressSpace.getConfiguration().getId());
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)

Example 65 with CConnection

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

the class PostgreSQLAddressSpaceFunctions method assignDebugger.

/**
   * Sets or removes the debugger of an address space. If assigning the debugger is successful, the
   * modification date of the address space is updated.
   * 
   * The address space and the debugger must both reside in the database connected to by the
   * provider argument.
   * 
   * @param provider The SQL provider that provides the database connection.
   * @param addressSpace The address space whose debugger is set.
   * @param debugger The debugger that is assigned to the address space or null to remove the
   *        current debugger from the address space.
   * 
   * @throws CouldntSaveDataException Thrown if the debugger could not be assigned to the address
   *         space.
   */
public static void assignDebugger(final AbstractSQLProvider provider, final CAddressSpace addressSpace, final DebuggerTemplate debugger) throws CouldntSaveDataException {
    checkArguments(provider, addressSpace);
    if ((debugger != null) && !debugger.inSameDatabase(provider)) {
        throw new IllegalArgumentException("IE00392: Debugger is not part of this database");
    }
    final CConnection connection = provider.getConnection();
    try {
        final String debuggerValue = debugger == null ? "NULL" : String.valueOf(debugger.getId());
        final String query = String.format("UPDATE %s SET debugger_id = %s WHERE id= %d", CTableNames.ADDRESS_SPACES_TABLE, debuggerValue, addressSpace.getConfiguration().getId());
        connection.executeUpdate(query, true);
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
    PostgreSQLHelpers.updateModificationDate(connection, CTableNames.ADDRESS_SPACES_TABLE, addressSpace.getConfiguration().getId());
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)

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