Search in sources :

Example 41 with CConnection

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

the class PostgreSQLViewFunctions method loadSettings.

/**
   * Loads the settings of a view from the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param view The view whose settings are loaded.
   * 
   * @return The settings map of the view.
   * 
   * @throws CouldntLoadDataException Thrown if the settings could not be loaded.
   */
public static Map<String, String> loadSettings(final AbstractSQLProvider provider, final CView view) throws CouldntLoadDataException {
    checkArguments(provider, view);
    final CConnection connection = provider.getConnection();
    final String query = "SELECT name, value FROM " + CTableNames.VIEW_SETTINGS_TABLE + " WHERE view_id = " + view.getConfiguration().getId();
    try (ResultSet resultSet = connection.executeQuery(query, true)) {
        final HashMap<String, String> settings = new HashMap<>();
        while (resultSet.next()) {
            settings.put(PostgreSQLHelpers.readString(resultSet, "name"), PostgreSQLHelpers.readString(resultSet, "value"));
        }
        return settings;
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet)

Example 42 with CConnection

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

the class PostgreSQLViewFunctions method tagView.

/**
   * Tags a view.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param view The view to tag.
   * @param tag The tag to tag the view.
   * 
   * @throws CouldntSaveDataException Thrown if the view could not be tagged.
   */
public static void tagView(final AbstractSQLProvider provider, final INaviView view, final CTag tag) throws CouldntSaveDataException {
    checkArguments(provider, view);
    Preconditions.checkNotNull(tag, "IE00615: Tag argument can not be null");
    Preconditions.checkArgument(tag.inSameDatabase(provider), "IE00616: Tag is not part of this database");
    final String query = String.format("insert into %s(view_id, tag_id) values(%d, %d)", CTableNames.TAGGED_VIEWS_TABLE, view.getConfiguration().getId(), tag.getId());
    final CConnection connection = provider.getConnection();
    try {
        connection.executeUpdate(query, true);
    } 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)

Example 43 with CConnection

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

the class PostgresSQLDebuggerFunctions method loadDebuggers.

/**
   * Loads all debugger templates of a database.
   * 
   * The debugger template manager must belong to the database connected to by the provider
   * argument.
   * 
   * @param provider The connection to the database.
   * @param manager Debugger template manager where the loaded debuggers are added to.
   * 
   * @throws CouldntLoadDataException Thrown if the debugger templates could not be loaded.
   */
public static void loadDebuggers(final AbstractSQLProvider provider, final DebuggerTemplateManager manager) throws CouldntLoadDataException {
    final CConnection connection = provider.getConnection();
    final String query = "SELECT * FROM " + CTableNames.DEBUGGERS_TABLE;
    try (ResultSet resultSet = connection.executeQuery(query, true)) {
        while (resultSet.next()) {
            final DebuggerTemplate debugger = new DebuggerTemplate(resultSet.getInt("id"), PostgreSQLHelpers.readString(resultSet, "name"), PostgreSQLHelpers.readString(resultSet, "host"), resultSet.getInt("port"), provider);
            manager.addDebugger(debugger);
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) DebuggerTemplate(com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet)

Example 44 with CConnection

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

the class PostgreSQLEdgeSaver method writeEdges.

/**
   * Writes the edges of a view to the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param edges The edges to write to the database.
   * 
   * @throws SQLException Thrown if the edges could not be saved.
   */
public static void writeEdges(final SQLProvider provider, final List<INaviEdge> edges) throws SQLException {
    Preconditions.checkNotNull(provider, "IE02253: Provider argument can not be null");
    Preconditions.checkNotNull(edges, "IE02254: Edges argument can not be null");
    for (final INaviEdge edge : edges) {
        Preconditions.checkArgument(edge.inSameDatabase(provider), "IE02255: Edge list contains an edge that is not part of this database");
    }
    if (edges.isEmpty()) {
        return;
    }
    final CConnection connection = provider.getConnection();
    fillEdgesTable(connection, edges);
    fillEdgepathsTable(connection, edges);
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge)

Example 45 with CConnection

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

the class PostgreSQLTracesLoader method loadTraces.

/**
   * Loads all traces of a view container.
   *
   * @param provider The connection to the database.
   * @param tableName The table name of the view container.
   * @param columnName The column name of the view container.
   * @param containerId The ID of the view container.
   * @param modules List of all modules stored in the database.
   *
   * @return The loaded traces.
   *
   * @throws CouldntLoadDataException Thrown if loading the traces failed.
   */
public static IFilledList<TraceList> loadTraces(final AbstractSQLProvider provider, final String tableName, final String columnName, final int containerId, final List<? extends INaviModule> modules) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE00590: Provider argument can not be null");
    Preconditions.checkNotNull(tableName, "IE00591: Table name argument can not be null");
    Preconditions.checkNotNull(columnName, "IE00592: Column name argument can not be null");
    final String query = "select id, name, description from " + CTableNames.TRACES_TABLE + " join " + tableName + " on " + tableName + ".trace_id = " + CTableNames.TRACES_TABLE + ".id where " + tableName + "." + columnName + " = " + containerId;
    final CConnection connection = provider.getConnection();
    final IFilledList<TraceList> traces = new FilledList<TraceList>();
    try {
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                final int traceId = resultSet.getInt("id");
                final String name = PostgreSQLHelpers.readString(resultSet, "name");
                final String description = PostgreSQLHelpers.readString(resultSet, "description");
                final TraceList traceList = new TraceList(traceId, name, description, provider);
                loadTraceEvents(connection, traceList, modules);
                traces.add(traceList);
            }
        } finally {
            resultSet.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
    return traces;
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) FilledList(com.google.security.zynamics.zylib.types.lists.FilledList) IFilledList(com.google.security.zynamics.zylib.types.lists.IFilledList) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) TraceList(com.google.security.zynamics.binnavi.debug.models.trace.TraceList)

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