Search in sources :

Example 26 with CConnection

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

the class PostgreSQLProjectFlowgraphsLoader method loadFlowGraphInformation.

public static ImmutableNaviViewConfiguration loadFlowGraphInformation(final SQLProvider provider, final INaviProject project, final Integer viewId) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE02618: provider argument can not be null");
    Preconditions.checkNotNull(project, "IE02619: project argument can not be null");
    Preconditions.checkNotNull(viewId, "IE02620: viewId argument can not be null");
    final CConnection connection = provider.getConnection();
    final String query = " SELECT * FROM load_project_flowGraph(?,?) ";
    try {
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        statement.setInt(1, project.getConfiguration().getId());
        statement.setInt(2, viewId);
        final ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            final int databaseViewId = resultSet.getInt("view_id");
            final String name = PostgreSQLHelpers.readString(resultSet, "name");
            final String description = PostgreSQLHelpers.readString(resultSet, "description");
            final ViewType viewType = resultSet.getString("type").equalsIgnoreCase("native") ? ViewType.Native : ViewType.NonNative;
            final Timestamp creationDate = resultSet.getTimestamp("creation_date");
            final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
            final boolean isStared = resultSet.getBoolean("stared");
            final int nodeCount = resultSet.getInt("bbcount");
            final int edgeCount = resultSet.getInt("edgecount");
            final ImmutableNaviViewConfiguration viewConfiguration = new ImmutableNaviViewConfiguration(databaseViewId, name, description, viewType, creationDate, modificationDate, isStared, nodeCount, edgeCount);
            return viewConfiguration;
        }
        return null;
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ImmutableNaviViewConfiguration(com.google.security.zynamics.binnavi.disassembly.views.ImmutableNaviViewConfiguration) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) ViewType(com.google.security.zynamics.zylib.disassembly.ViewType)

Example 27 with CConnection

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

the class PostgreSQLProjectFlowgraphsLoader method loadFlowgraphs.

/**
   * Loads the flow graph views of a project.
   * 
   * @param provider The connection to the database.
   * @param project The project whose flow graph views are loaded.
   * @param viewTagManager Tag manager responsible for tagging the loaded views.
   * @param nodeTagManager The tag manager responsible for tagging view nodes.
   * 
   * @return The loaded flow graph views.
   * 
   * @throws CouldntLoadDataException Thrown if the flow graph views could not be loaded.
   */
public static List<IFlowgraphView> loadFlowgraphs(final SQLProvider provider, final INaviProject project, final CTagManager viewTagManager, final CTagManager nodeTagManager) throws CouldntLoadDataException {
    checkArguments(provider, project, viewTagManager);
    final CConnection connection = provider.getConnection();
    final String query = " SELECT * FROM load_project_flow_graphs(?, ?) ";
    try {
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        statement.setInt(1, project.getConfiguration().getId());
        statement.setObject(2, "non-native", Types.OTHER);
        final ResultSet resultSet = statement.executeQuery();
        final Map<Integer, Set<CTag>> tags = loadTags(connection, project, viewTagManager);
        return new ArrayList<IFlowgraphView>(processQueryResults(resultSet, project, tags, nodeTagManager, provider, new ArrayList<CView>(), ViewType.NonNative, GraphType.FLOWGRAPH));
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) Set(java.util.Set) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 28 with CConnection

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

the class PostgreSQLProjectMixedGraphsLoader method loadMixedgraphs.

/**
   * Loads the non-native mixed-graph views of a project.
   * 
   * The project, the view tag manager, and the node tag manager 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 from where the views are loaded.
   * @param viewTagManager View tag manager that contains all view tags of the database.
   * @param nodeTagManager The tag manager responsible for tagging view nodes.
   * 
   * @return A list of non-native mixed-graph views.
   * 
   * @throws CouldntLoadDataException Thrown if the views could not be loaded.
   */
public static List<INaviView> loadMixedgraphs(final AbstractSQLProvider provider, final CProject project, final CTagManager viewTagManager, final CTagManager nodeTagManager) throws CouldntLoadDataException {
    checkArguments(provider, project, viewTagManager);
    final String query = "SELECT * FROM load_module_mixed_graph(?)";
    try {
        final CConnection connection = provider.getConnection();
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        statement.setInt(1, project.getConfiguration().getId());
        final ResultSet resultSet = statement.executeQuery();
        final Map<Integer, Set<CTag>> tags = loadTags(connection, project, viewTagManager);
        return new ArrayList<INaviView>(processQueryResults(resultSet, project, tags, nodeTagManager, provider, new ArrayList<CView>(), ViewType.NonNative, GraphType.MIXED_GRAPH));
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) Set(java.util.Set) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 29 with CConnection

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

the class PostgreSQLNodeSaver method writeNodes.

/**
   * Writes the nodes of a view to the database.
   *
   * @param provider The connection to the database.
   * @param newViewId The ID of the view the nodes belong to.
   * @param nodes The nodes to save.
   * @throws SQLException Thrown if saving the nodes failed.
   */
public static void writeNodes(final AbstractSQLProvider provider, final int newViewId, final List<INaviViewNode> nodes) throws SQLException {
    Preconditions.checkNotNull(provider, "IE01992: Provider argument can not be null");
    Preconditions.checkArgument(newViewId > 0, "IE01993: New View ID argument must be greater then zero");
    Preconditions.checkNotNull(nodes, "IE01994: Nodes argument can not be null");
    if (nodes.isEmpty()) {
        return;
    }
    final ArrayList<Integer> functionNodeIndices = new ArrayList<Integer>();
    final ArrayList<Integer> codeNodeIndices = new ArrayList<Integer>();
    final ArrayList<Integer> textNodeIndices = new ArrayList<Integer>();
    final ArrayList<Integer> groupNodeIndices = new ArrayList<Integer>();
    final BiMap<Integer, INaviGroupNode> groupNodeMap = HashBiMap.create();
    final int firstNode = saveNodes(provider, newViewId, nodes, functionNodeIndices, codeNodeIndices, textNodeIndices, groupNodeIndices, groupNodeMap);
    // After this point, the nodes table has been filled
    // After each saving, the node IDs have to be updated
    PostgreSQLNodeSaver.updateNodeIds(nodes, firstNode);
    // Now, the individual node type tables can be saved
    PostgreSQLNodeSaver.saveGroupNodes(provider, nodes, firstNode, PostgreSQLNodeSaver.sortGroupNodes(groupNodeIndices, groupNodeMap));
    PostgreSQLNodeSaver.saveFunctionNodes(provider, nodes, firstNode, functionNodeIndices);
    PostgreSQLNodeSaver.saveCodeNodes(provider, nodes, firstNode, codeNodeIndices);
    PostgreSQLNodeSaver.saveTextNodes(provider, nodes, firstNode, textNodeIndices);
    // Once all nodes are saved, the parent nodes can be saved too
    final CConnection connection = provider.getConnection();
    PostgreSQLNodeSaver.saveParentGroups(connection, nodes, firstNode, groupNodeMap);
    // And finally, we can save the tags associated with the nodes
    PostgreSQLNodeSaver.saveTags(connection, nodes, firstNode);
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ArrayList(java.util.ArrayList) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)

Example 30 with CConnection

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

the class PostgreSQLProviderTest method testCGenericSQLHelpersDeleteByID3.

@Test(expected = NullPointerException.class)
public void testCGenericSQLHelpersDeleteByID3() throws CouldntDeleteException {
    final AbstractSQLProvider connectionProvider = (AbstractSQLProvider) getProvider();
    final CConnection connection = connectionProvider.getConnection();
    PostgreSQLHelpers.deleteById(connection, null, 0);
}
Also used : AbstractSQLProvider(com.google.security.zynamics.binnavi.Database.AbstractSQLProvider) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

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