Search in sources :

Example 51 with CConnection

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

the class PostgreSQLProviderTest method testCGenericSQLHelpersRollBack1.

@Test
public void testCGenericSQLHelpersRollBack1() throws SQLException {
    final AbstractSQLProvider connectionProvider = (AbstractSQLProvider) getProvider();
    final CConnection connection = connectionProvider.getConnection();
    PostgreSQLHelpers.beginTransaction(connection);
    PostgreSQLHelpers.rollback(connection);
}
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 52 with CConnection

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

the class PostgreSQLTagFunctions method insertTag.

/**
   * Inserts a new tag into a tag hierarchy.
   * 
   * @param provider The connection to the database.
   * @param parent Parent tag under which the new tag is inserted.
   * @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 insertTag(final AbstractSQLProvider provider, final ITreeNode<CTag> parent, final String name, final String description, final TagType type) throws CouldntSaveDataException {
    checkArguments(provider, parent, type);
    Preconditions.checkNotNull(name, "IE00563: Name argument can not be null");
    Preconditions.checkNotNull(description, "IE00564: Description argument can not be null");
    final CConnection connection = provider.getConnection();
    final String query = String.format("update %s set parent_id = ? where parent_id = ? and id <> ?", CTableNames.TAGS_TABLE);
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
        final CTag tag = createTag(provider, parent.getObject(), name, description, type);
        statement.setInt(1, tag.getId());
        statement.setInt(2, parent.getObject().getId());
        statement.setInt(3, tag.getId());
        statement.executeUpdate();
        return tag;
    } 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) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) PreparedStatement(java.sql.PreparedStatement)

Example 53 with CConnection

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

the class PostgreSQLTagFunctions method setDescription.

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

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

the class PostgreSQLTagManagerFunctions method loadTagManager.

/**
   * Loads a tag manager from the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param type Type of the tags managed by the tag manager.
   * 
   * @return The tag manager that is loaded from the database.
   * 
   * @throws CouldntLoadDataException Thrown if the tag manager could not be loaded.
   */
public static CTagManager loadTagManager(final AbstractSQLProvider provider, final TagType type) throws CouldntLoadDataException {
    Preconditions.checkNotNull(type, "IE00567: Tag type argument can't be null");
    final CConnection connection = provider.getConnection();
    // should always have a tags table
    if (!PostgreSQLHelpers.hasTable(connection, CTableNames.TAGS_TABLE)) {
        final CTag rootTag = new CTag(0, "Root Node", "", type, provider);
        return new CTagManager(new Tree<CTag>(new TreeNode<CTag>(rootTag)), type, provider);
    }
    final String query = String.format("select id, parent_id, name, description from %s where type = '%s'", CTableNames.TAGS_TABLE, PostgreSQLTagFunctions.tagToString(type));
    try {
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            final HashMap<Integer, Pair<TreeNode<CTag>, Integer>> treeNodes = new HashMap<Integer, Pair<TreeNode<CTag>, Integer>>();
            final CTag rootTag = new CTag(0, "Root Node", "", type, provider);
            final TreeNode<CTag> rootTreeNode = new TreeNode<CTag>(rootTag);
            treeNodes.put(0, new Pair<TreeNode<CTag>, Integer>(rootTreeNode, -1));
            while (resultSet.next()) {
                final int tagId = resultSet.getInt("id");
                final int parentId = resultSet.getInt("parent_id");
                final TreeNode<CTag> treeNode = new TreeNode<CTag>(new CTag(tagId, PostgreSQLHelpers.readString(resultSet, "name"), PostgreSQLHelpers.readString(resultSet, "description"), type, provider));
                final Pair<TreeNode<CTag>, Integer> pair = new Pair<TreeNode<CTag>, Integer>(treeNode, parentId);
                treeNodes.put(tagId, pair);
            }
            for (final Entry<Integer, Pair<TreeNode<CTag>, Integer>> e : treeNodes.entrySet()) {
                if (e.getKey() == 0) {
                    continue;
                }
                final TreeNode<CTag> child = e.getValue().first();
                final TreeNode<CTag> parent = treeNodes.get(e.getValue().second()).first();
                child.setParent(parent);
                parent.addChild(child);
            }
            return new CTagManager(new Tree<CTag>(rootTreeNode), type, provider);
        } finally {
            resultSet.close();
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) TreeNode(com.google.security.zynamics.zylib.types.trees.TreeNode) ResultSet(java.sql.ResultSet) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 55 with CConnection

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

the class PostgreSQLTraceFunctions method save.

/**
   * Saves a trace to the database.
   *
   * @param provider The SQL provider that provides the connection.
   * @param trace The trace to save to the database.
   *
   * @throws CouldntSaveDataException Thrown if the trace could not be saved to the database.
   */
public static void save(final AbstractSQLProvider provider, final TraceList trace) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE00579: Provider argument can not be null");
    Preconditions.checkNotNull(trace, "IE00580: List argument can not be null");
    Preconditions.checkArgument(trace.inSameDatabase(provider), "IE00581: List is not part of this database");
    final CConnection connection = provider.getConnection();
    if (trace.getEventCount() != 0) {
        saveEvents(connection, trace);
        saveEventValues(connection, trace);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection)

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