Search in sources :

Example 31 with CConnection

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

the class PostgreSQLProviderTest method testCGenericSQLHelpersDeleteByID1.

@Test
public void testCGenericSQLHelpersDeleteByID1() throws CouldntDeleteException {
    final AbstractSQLProvider connectionProvider = (AbstractSQLProvider) getProvider();
    final CConnection connection = connectionProvider.getConnection();
    final String tableName = CTableNames.ADDRESS_SPACES_TABLE;
    PostgreSQLHelpers.deleteById(connection, tableName, 1);
}
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)

Example 32 with CConnection

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

the class PostgreSQLProviderTest method testCGenericSQLHelpersDeleteByID4.

@Test(expected = IllegalArgumentException.class)
public void testCGenericSQLHelpersDeleteByID4() throws CouldntDeleteException {
    final AbstractSQLProvider connectionProvider = (AbstractSQLProvider) getProvider();
    final CConnection connection = connectionProvider.getConnection();
    final String tableName = CTableNames.ADDRESS_SPACES_TABLE;
    PostgreSQLHelpers.deleteById(connection, tableName, 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)

Example 33 with CConnection

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

the class PostgreSQLProjectCreator method createProject.

/**
   * Creates a new project in the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param name The name of the new project.
   * 
   * @return The created project.
   * 
   * @throws CouldntSaveDataException Thrown if the project could not be created.
   */
public static CProject createProject(final AbstractSQLProvider provider, final String name) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE00513: Provider argument can not be null");
    Preconditions.checkNotNull(name, "IE00514: Project names can not be null");
    Preconditions.checkArgument(!("".equals(name)), "IE00515: Project names can not be empty");
    final CConnection connection = provider.getConnection();
    NaviLogger.info("Creating new project %s", name);
    final String query = "INSERT INTO " + CTableNames.PROJECTS_TABLE + "(name, description, creation_date, modification_date) VALUES(?, '', NOW(), NOW()) RETURNING id";
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
        statement.setString(1, name);
        ResultSet resultSet = statement.executeQuery();
        Integer id = null;
        while (resultSet.next()) {
            if (resultSet.isFirst()) {
                id = resultSet.getInt(1);
                break;
            }
        }
        Preconditions.checkNotNull(id, "IE02044: Error id for a project after creation may not be null");
        return PostgreSQLProjectCreator.loadProject(provider, id);
    } 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) PreparedStatement(java.sql.PreparedStatement)

Example 34 with CConnection

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

the class PostgreSQLViewCreator method createView.

/**
   * Inserts a new view in the database by copying an existing view.
   * 
   * @param provider The connection to the database.
   * @param containerId The ID of the container where the view is created.
   * @param view The view to be copied.
   * @param name The name of the new view.
   * @param description The description of the new view.
   * @param containerTable Name of the view container table.
   * @param viewContainerTable Name of the view container views table.
   * @param generator Generates the view.
   * @return The created view.
   * @throws CouldntSaveDataException Thrown if the view could not be created.
   */
private static CView createView(final AbstractSQLProvider provider, final int containerId, final INaviView view, final String name, final String description, final String containerTable, final String viewContainerTable, final ViewGenerator generator) throws CouldntSaveDataException {
    final CConnection connection = provider.getConnection();
    try {
        PostgreSQLHelpers.beginTransaction(connection);
        final int viewId = insertView(connection, name, description);
        // Mark the view as a module view
        connection.executeUpdate("INSERT INTO " + viewContainerTable + " VALUES(" + containerId + ", " + viewId + ")", true);
        final List<INaviViewNode> nodes = view.getGraph().getNodes();
        final List<INaviEdge> edges = view.getGraph().getEdges();
        // Store all nodes
        PostgreSQLNodeSaver.writeNodes(provider, viewId, nodes);
        // Store all edges
        PostgreSQLEdgeSaver.writeEdges(provider, edges);
        PostgreSQLHelpers.endTransaction(connection);
        final String query = "SELECT creation_date, modification_date FROM " + CTableNames.VIEWS_TABLE + " WHERE id = " + viewId;
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                final Timestamp creationDate = resultSet.getTimestamp("creation_date");
                final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
                PostgreSQLHelpers.updateModificationDate(connection, containerTable, containerId);
                return generator.generate(viewId, name, description, ViewType.NonNative, view.getGraphType(), creationDate, modificationDate, view.getNodeCount(), view.getEdgeCount(), new HashSet<CTag>(), new HashSet<CTag>(), false);
            }
            throw new CouldntSaveDataException("Error: Couldnt't load the created view");
        } finally {
            resultSet.close();
        }
    } catch (final SQLException exception) {
        CUtilityFunctions.logException(exception);
        try {
            PostgreSQLHelpers.rollback(connection);
        } catch (final SQLException e) {
            CUtilityFunctions.logException(e);
        }
        throw new CouldntSaveDataException(exception);
    }
}
Also used : SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) Timestamp(java.sql.Timestamp) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode)

Example 35 with CConnection

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

the class PostgreSQLAddressSpaceFunctions method setImageBase.

/**
   * Changes the image base of a module inside an address space. If updating the image base was
   * successful, the modification date of the address space is updated.
   * 
   * The address space and the module must both be stored 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 the module belongs to.
   * @param module The module whose image base is changed.
   * @param address The new image base of the module in the address space.
   * 
   * @throws CouldntSaveDataException Thrown if the new image base value could not be stored in the
   *         database.
   */
public static void setImageBase(final AbstractSQLProvider provider, final INaviAddressSpace addressSpace, final INaviModule module, final IAddress address) throws CouldntSaveDataException {
    checkArguments(provider, addressSpace);
    Preconditions.checkNotNull(module, "IE00396: Module argument can not be null");
    Preconditions.checkNotNull(address, "IE00397: Address argument can not be null");
    Preconditions.checkArgument(module.inSameDatabase(provider), "IE00398: Module is not part of this database");
    final CConnection connection = provider.getConnection();
    try {
        final String query = String.format("UPDATE %s SET image_base = %s " + " WHERE module_id = %d AND address_space_id = %d", CTableNames.SPACE_MODULES_TABLE, address.toBigInteger().toString(), module.getConfiguration().getId(), addressSpace.getConfiguration().getId());
        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)

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