Search in sources :

Example 36 with CConnection

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

the class PostgreSQLAddressSpaceFunctions method setName.

/**
   * Changes the name of an address space. If updating the name was successful, the modification
   * date of the address space is updated.
   * 
   * The address space must 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 whose name is changed.
   * @param name The new name of the address space.
   * 
   * @throws CouldntSaveDataException Thrown if the name of the address space could not be changed.
   */
public static void setName(final AbstractSQLProvider provider, final CAddressSpace addressSpace, final String name) throws CouldntSaveDataException {
    checkArguments(provider, addressSpace);
    Preconditions.checkNotNull(name, "IE00399: Name argument can not be null");
    final CConnection connection = provider.getConnection();
    PostgreSQLHelpers.setName(connection, addressSpace.getConfiguration().getId(), name, CTableNames.ADDRESS_SPACES_TABLE);
    PostgreSQLHelpers.updateModificationDate(connection, CTableNames.ADDRESS_SPACES_TABLE, addressSpace.getConfiguration().getId());
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection)

Example 37 with CConnection

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

the class PostgreSQLAddressSpaceFunctions method removeModule.

/**
   * Removes a module from an address space. If removing the module was successful, the modification
   * date of the address space is updated.
   * 
   * The address space and the module must both reside in the database connected to by the provider
   * argument.
   * 
   * TODO (timkornau): What happens if the module does not belong to the address space? This
   * situation should be handled explicitly in the future.
   * 
   * @param provider The SQL provider that provides the database connection.
   * @param addressSpace The address space from which the module is removed.
   * @param module The module to be removed from the address space.
   * 
   * @throws CouldntDeleteException Thrown if the module could not be removed from the address
   *         space.
   * @throws CouldntSaveDataException Thrown if the modification time could not be set.
   */
public static void removeModule(final AbstractSQLProvider provider, final INaviAddressSpace addressSpace, final INaviModule module) throws CouldntDeleteException, CouldntSaveDataException {
    checkArguments(provider, addressSpace);
    Preconditions.checkNotNull(module, "IE00393: Module argument can not be null");
    Preconditions.checkArgument(module.inSameDatabase(provider), "IE00394: Module is not part of this database");
    final CConnection connection = provider.getConnection();
    final String query = "DELETE FROM " + CTableNames.SPACE_MODULES_TABLE + " WHERE address_space_id = " + addressSpace.getConfiguration().getId() + " AND module_id = " + module.getConfiguration().getId();
    try {
        connection.executeUpdate(query, true);
    } catch (final SQLException e) {
        throw new CouldntDeleteException(e);
    }
    PostgreSQLHelpers.updateModificationDate(connection, CTableNames.ADDRESS_SPACES_TABLE, addressSpace.getConfiguration().getId());
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException) SQLException(java.sql.SQLException)

Example 38 with CConnection

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

the class PostgreSQLEdgeLoader method loadEdges.

/**
   * Loads the edges of a view.
   *
   * @param provider The connection to the database.
   * @param view The view whose edges are loaded.
   * @param nodeLookup Maps between node IDs and their corresponding node objects.
   * @param edgeToGlobalCommentMap Maps between edge IDs and their associated comments.
   *
   * @return The loaded edges.
   *
   * @throws CouldntLoadDataException
   */
private static List<INaviEdge> loadEdges(final AbstractSQLProvider provider, final INaviView view, final Map<Integer, INaviViewNode> nodeLookup, final Map<Integer, ArrayList<IComment>> edgeToGlobalCommentMap) throws CouldntLoadDataException {
    final String query = "SELECT * FROM load_view_edges(" + view.getConfiguration().getId() + ")";
    List<CBend> currentPaths = new ArrayList<CBend>();
    final Map<Integer, INaviEdge> commentIdToEdge = new HashMap<Integer, INaviEdge>();
    final Map<Integer, INaviEdge> edgeIdToEdge = new HashMap<Integer, INaviEdge>();
    try {
        final CConnection connection = provider.getConnection();
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                final int edgeId = resultSet.getInt("id");
                if (edgeIdToEdge.containsKey(edgeId)) {
                    final INaviEdge edge = edgeIdToEdge.get(edgeId);
                    final double pathX = resultSet.getDouble("x");
                    final double pathY = resultSet.getDouble("y");
                    if (!resultSet.wasNull()) {
                        edge.addBend(pathX, pathY);
                    }
                    continue;
                }
                final int sourceNode = resultSet.getInt("source_node_id");
                final int targetNode = resultSet.getInt("target_node_id");
                Integer localCommentId = resultSet.getInt("comment_id");
                if (resultSet.wasNull()) {
                    localCommentId = null;
                }
                final double x1 = resultSet.getDouble("x1");
                final double y1 = resultSet.getDouble("y1");
                final double x2 = resultSet.getDouble("x2");
                final double y2 = resultSet.getDouble("y2");
                final EdgeType type = EdgeType.valueOf(resultSet.getString("type").toUpperCase());
                final Color color = new Color(resultSet.getInt("color"));
                final boolean visible = resultSet.getBoolean("visible");
                final boolean selected = resultSet.getBoolean("selected");
                final INaviViewNode source = nodeLookup.get(sourceNode);
                final INaviViewNode target = nodeLookup.get(targetNode);
                final double pathX = resultSet.getDouble("x");
                final double pathY = resultSet.getDouble("y");
                if (!resultSet.wasNull()) {
                    currentPaths.add(new CBend(pathX, pathY));
                }
                final CNaviViewEdge edge = new CNaviViewEdge(edgeId, source, target, type, x1, y1, x2, y2, color, selected, visible, null, currentPaths, provider);
                if (localCommentId != null) {
                    commentIdToEdge.put(localCommentId, edge);
                }
                final ArrayList<IComment> globalComments = edgeToGlobalCommentMap.containsKey(edgeId) ? edgeToGlobalCommentMap.get(edgeId) : null;
                if ((globalComments != null) && (globalComments.size() != 0)) {
                    initializeGlobalComment(edge, globalComments, provider);
                }
                source.addOutgoingEdge(edge);
                target.addIncomingEdge(edge);
                edgeIdToEdge.put(edge.getId(), edge);
                currentPaths = new ArrayList<CBend>();
            }
            if (!commentIdToEdge.isEmpty()) {
                final HashMap<Integer, ArrayList<IComment>> commentIdToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToEdge.keySet());
                for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdToComments.entrySet()) {
                    commentIdToEdge.get(commentIdToComment.getKey()).initializeLocalComment(commentIdToComment.getValue());
                }
            }
        } finally {
            resultSet.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException("Error: Loading of view edges failed");
    }
    return Lists.newArrayList(edgeIdToEdge.values());
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) CBend(com.google.security.zynamics.zylib.gui.zygraph.edges.CBend) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) Color(java.awt.Color) PreparedStatement(java.sql.PreparedStatement) EdgeType(com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CNaviViewEdge(com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge)

Example 39 with CConnection

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

the class PostgreSQLViewSaver method save.

/**
   * Saves a view to the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param view The view to save to the database.
   * 
   * @throws CouldntSaveDataException Thrown if the view could not be saved to the database.
   */
public static void save(final AbstractSQLProvider provider, final CView view) throws CouldntSaveDataException {
    PostgreSQLViewSaver.checkArguments(provider, view);
    final CConnection connection = provider.getConnection();
    try {
        PostgreSQLHelpers.beginTransaction(connection);
        final int viewId = view.getConfiguration().getId();
        final List<INaviViewNode> nodes = view.getGraph().getNodes();
        final List<INaviEdge> edges = view.getGraph().getEdges();
        PostgreSQLViewSaver.deleteNodes(connection, viewId);
        // Store all nodes
        PostgreSQLNodeSaver.writeNodes(provider, viewId, nodes);
        // Store all edges
        PostgreSQLEdgeSaver.writeEdges(provider, edges);
        PostgreSQLHelpers.endTransaction(connection);
    } catch (final SQLException exception) {
        try {
            PostgreSQLHelpers.rollback(connection);
        } catch (final SQLException e) {
            CUtilityFunctions.logException(e);
        }
        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) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge)

Example 40 with CConnection

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

the class PostgreSQLViewFunctions method saveSettings.

/**
   * Stores the settings map of a view to the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param view The view whose settings are stored.
   * @param settings The settings map to store to the database.
   * 
   * @throws CouldntSaveDataException Thrown if the view settings could not be stored in the
   *         database.
   */
public static void saveSettings(final AbstractSQLProvider provider, final CView view, final Map<String, String> settings) throws CouldntSaveDataException {
    checkArguments(provider, view);
    Preconditions.checkNotNull(settings, "IE02414: settings argument can not be null");
    if (settings.isEmpty()) {
        return;
    }
    final CConnection connection = provider.getConnection();
    final StringBuilder deleteQuery = new StringBuilder("DELETE FROM " + CTableNames.VIEW_SETTINGS_TABLE + " WHERE ");
    final StringBuilder insertQuery = new StringBuilder("INSERT INTO " + CTableNames.VIEW_SETTINGS_TABLE + " VALUES");
    boolean first = true;
    for (final Map.Entry<String, String> pair : settings.entrySet()) {
        final String value = pair.getValue();
        final String key = pair.getKey();
        if ((value == null) || (key == null)) {
            continue;
        } else {
            if (!first) {
                deleteQuery.append("OR");
                insertQuery.append(',');
            }
            deleteQuery.append(" (view_id = " + view.getConfiguration().getId() + " AND name = '" + key + "') ");
            insertQuery.append(" (" + view.getConfiguration().getId() + ", '" + key + "', '" + value + "' ) ");
        }
        first = false;
    }
    try {
        connection.executeUpdate(deleteQuery.toString(), true);
        connection.executeUpdate(insertQuery.toString(), true);
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException("E00115: Could not update settings in " + CTableNames.VIEW_SETTINGS_TABLE);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) HashMap(java.util.HashMap) Map(java.util.Map)

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