Search in sources :

Example 46 with CouldntLoadDataException

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

the class PostgreSQLFunctionNodeLoader method load.

/**
   * Loads the function nodes of a view.
   * 
   * @param provider The connection to the database.
   * @param view The view whose function nodes are loaded.
   * @param nodes The loaded nodes are stored here.
   * 
   * @throws CPartialLoadException Thrown if loading the nodes failed because a necessary module was
   *         not loaded.
   * @throws CouldntLoadDataException
   */
public static void load(final AbstractSQLProvider provider, final INaviView view, final List<INaviViewNode> nodes) throws CPartialLoadException, CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE02510: provider argument can not be null");
    Preconditions.checkNotNull(view, "IE02511: view argument can not be null");
    Preconditions.checkNotNull(nodes, "IE02512: nodes argument can not be null");
    // TODO (timkornau): query needs to go into the database.
    final String query = "SELECT nodes.view_id, nodes.id, functions.module_id, " + " function, fnodes.comment_id as local_comment, x, y, width, height, " + " color, selected, visible FROM " + CTableNames.NODES_TABLE + " AS nodes JOIN " + CTableNames.FUNCTION_NODES_TABLE + " AS fnodes " + " ON nodes.id = fnodes.node_id JOIN " + CTableNames.FUNCTIONS_TABLE + " AS functions ON functions.address = fnodes.function " + " AND functions.module_id = fnodes.module_id  WHERE view_id = ?";
    final Map<Integer, INaviFunctionNode> commentIdToFunctionNode = new HashMap<Integer, INaviFunctionNode>();
    try {
        final PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query);
        statement.setInt(1, view.getConfiguration().getId());
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                final int moduleId = resultSet.getInt("module_id");
                final INaviModule module = provider.findModule(moduleId);
                if (!module.isLoaded()) {
                    try {
                        module.load();
                    } catch (final CouldntLoadDataException e) {
                        throw new CPartialLoadException("E00064: The view could not be loaded because not all modules that form the view are loaded", module);
                    } catch (final LoadCancelledException e) {
                        throw new CPartialLoadException("E00065: The view could not be loaded because not all modules that form the view are loaded", module);
                    }
                }
                final IAddress address = PostgreSQLHelpers.loadAddress(resultSet, "function");
                final INaviFunction function = module.getContent().getFunctionContainer().getFunction(address);
                final int nodeId = resultSet.getInt("id");
                Integer commentId = resultSet.getInt("local_comment");
                if (resultSet.wasNull()) {
                    commentId = null;
                }
                final double posX = resultSet.getDouble("x");
                final double posY = resultSet.getDouble("y");
                final double width = resultSet.getDouble("width");
                final double height = resultSet.getDouble("height");
                final Color color = new Color(resultSet.getInt("color"));
                final boolean selected = resultSet.getBoolean("selected");
                final boolean visible = resultSet.getBoolean("visible");
                final INaviFunctionNode functionNode = new CFunctionNode(nodeId, function, posX, posY, width, height, color, selected, visible, null, new HashSet<CTag>(), provider);
                nodes.add(functionNode);
                if (commentId != null) {
                    commentIdToFunctionNode.put(commentId, functionNode);
                }
            }
        } finally {
            resultSet.close();
        }
        if (!commentIdToFunctionNode.isEmpty()) {
            final HashMap<Integer, ArrayList<IComment>> commentIdsToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToFunctionNode.keySet());
            for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdsToComments.entrySet()) {
                commentIdToFunctionNode.get(commentIdToComment.getKey()).initializeLocalFunctionComment(commentIdToComment.getValue());
            }
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) CPartialLoadException(com.google.security.zynamics.binnavi.Database.Exceptions.CPartialLoadException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) Color(java.awt.Color) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) ResultSet(java.sql.ResultSet) INaviFunctionNode(com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode) LoadCancelledException(com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Example 47 with CouldntLoadDataException

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

the class PostgreSQLTypeFunctions method loadRawTypeInstance.

/**
   * Loads a single {@link RawTypeInstance type instance} from the database.
   *
   * @param provider The {@link SQLProvider} to access the database with.
   * @param module The {@link INaviModule} the {@link RawTypeInstance type instance} is associated
   *        to.
   * @param typeInstanceId The id of the {@link RawTypeInstance type instance} to load from the
   *        database.
   *
   * @return The {@link RawTypeInstance type instance} from the database which matches the given
   *         arguments.
   * @throws CouldntLoadDataException if the {@link RawTypeInstance type instance} could not be
   *         loaded from the database.
   */
public static RawTypeInstance loadRawTypeInstance(final SQLProvider provider, final INaviModule module, final Integer typeInstanceId) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "Error: provider argument can not be null");
    Preconditions.checkNotNull(module, "Error: module argument can not be null");
    Preconditions.checkNotNull(typeInstanceId, "Error: typeInstanceId argument can not be null");
    final String query = " SELECT * FROM load_type_instance(?, ?) ";
    try {
        final PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        statement.setInt(1, module.getConfiguration().getId());
        statement.setInt(2, typeInstanceId);
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                if (resultSet.first()) {
                    final int moduleId = resultSet.getInt("module_id");
                    final int id = resultSet.getInt("id");
                    final String name = resultSet.getString("name");
                    final int commentId = resultSet.getInt("comment_id");
                    final int typeId = resultSet.getInt("type_id");
                    final int sectionId = resultSet.getInt("section_id");
                    final long sectionOffset = resultSet.getLong("section_offset");
                    return new RawTypeInstance(moduleId, id, name, commentId, typeId, sectionId, sectionOffset);
                }
            }
        } finally {
            resultSet.close();
            statement.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
    throw new CouldntLoadDataException("Error: could not load singe type instance from the database.");
}
Also used : SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) RawTypeInstance(com.google.security.zynamics.binnavi.disassembly.types.RawTypeInstance) PreparedStatement(java.sql.PreparedStatement)

Example 48 with CouldntLoadDataException

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

the class PostgreSQLTypeFunctions method loadRawTypeInstances.

/**
   * Loads all type instances for the given module from the database.
   *
   * @param connection The connection to the database.
   * @param module The module for which to load all type instances.
   * @return The list of type instances for the given module.
   * @throws CouldntLoadDataException Thrown if the type instances could not be loaded from the
   *         database.
   */
public static List<RawTypeInstance> loadRawTypeInstances(final Connection connection, final INaviModule module) throws CouldntLoadDataException {
    final List<RawTypeInstance> instances = new ArrayList<RawTypeInstance>();
    try {
        final String query = "SELECT * FROM load_type_instances(?)";
        final PreparedStatement statement = connection.prepareStatement(query);
        statement.setInt(1, module.getConfiguration().getId());
        try {
            final ResultSet result = statement.executeQuery();
            while (result.next()) {
                final int id = result.getInt("id");
                final String name = result.getString("name");
                Integer commentId = result.getInt("comment_id");
                if (result.wasNull()) {
                    commentId = null;
                }
                final int typeId = result.getInt("type_id");
                final int sectionId = result.getInt("section_id");
                final long sectionOffset = result.getLong("section_offset");
                instances.add(new RawTypeInstance(module.getConfiguration().getId(), id, name, commentId, typeId, sectionId, sectionOffset));
            }
        } finally {
            statement.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
    return instances;
}
Also used : BigInteger(java.math.BigInteger) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) RawTypeInstance(com.google.security.zynamics.binnavi.disassembly.types.RawTypeInstance) PreparedStatement(java.sql.PreparedStatement)

Example 49 with CouldntLoadDataException

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

the class PostgreSQLViewFunctions method getDerivedViews.

/**
   * Returns the derived views of the given view.
   * 
   * @param provider Provides the connection to the database.
   * @param view The view whose derived views are returned.
   * 
   * @return The derived views.
   * 
   * @throws CouldntLoadDataException Thrown if the derived views could not be determined.
   */
public static List<INaviView> getDerivedViews(final AbstractSQLProvider provider, final INaviView view) throws CouldntLoadDataException {
    checkArguments(provider, view);
    final List<INaviView> views = new ArrayList<INaviView>();
    // currently project views can not have derived views.
    if (view.getConfiguration().getModule() == null) {
        return views;
    }
    final String query = "SELECT * FROM get_derived_views(?)";
    try (PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query)) {
        // TODO(timkornau): this should be changed to the ViewManager.
        final List<INaviView> moduleViews = view.getConfiguration().getModule().getContent().getViewContainer().getViews();
        statement.setInt(1, view.getConfiguration().getId());
        final ResultSet resultSet = statement.executeQuery();
        if (resultSet == null) {
            return views;
        }
        while (resultSet.next()) {
            final int viewId = resultSet.getInt(1);
            if (viewId != view.getConfiguration().getId()) {
                for (final INaviView moduleView : moduleViews) {
                    if (moduleView.getConfiguration().getId() == viewId) {
                        views.add(moduleView);
                        break;
                    }
                }
            }
        }
        return views;
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
}
Also used : INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 50 with CouldntLoadDataException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException 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)

Aggregations

CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)85 SQLException (java.sql.SQLException)53 ResultSet (java.sql.ResultSet)47 ArrayList (java.util.ArrayList)30 PreparedStatement (java.sql.PreparedStatement)27 CConnection (com.google.security.zynamics.binnavi.Database.CConnection)20 LoadCancelledException (com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException)17 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)16 HashMap (java.util.HashMap)12 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)10 BigInteger (java.math.BigInteger)9 CPartialLoadException (com.google.security.zynamics.binnavi.Database.Exceptions.CPartialLoadException)8 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)8 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)8 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)8 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)7 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)6 Set (java.util.Set)6 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)5 CDefaultProgressOperation (com.google.security.zynamics.binnavi.Gui.Progress.CDefaultProgressOperation)5