use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException in project binnavi by google.
the class PostgreSQLAddressSpaceFunctions method loadModules.
/**
* Loads the modules of an address space and their respective image bases inside the address
* space.
*
* The address space must be stored in the database connected to by the provider argument.
*
* @param provider The SQL provider that provides the database connection.
* @param addressSpace The address space whose modules are loaded.
*
* @return A list of the modules of the address space and their image base.
*
* @throws CouldntLoadDataException Thrown if the modules that belong to the address space could
* not be loaded.
*/
public static List<Pair<IAddress, INaviModule>> loadModules(final AbstractSQLProvider provider, final CAddressSpace addressSpace) throws CouldntLoadDataException {
checkArguments(provider, addressSpace);
final CConnection connection = provider.getConnection();
final List<Pair<IAddress, INaviModule>> modules = new ArrayList<Pair<IAddress, INaviModule>>();
final String query = "SELECT id, name, md5, sha1, description, import_time, " + CTableNames.SPACE_MODULES_TABLE + ".image_base " + " FROM " + CTableNames.SPACE_MODULES_TABLE + " JOIN " + CTableNames.MODULES_TABLE + " ON id = module_id WHERE address_space_id = " + addressSpace.getConfiguration().getId();
try (ResultSet resultSet = connection.executeQuery(query, true)) {
while (resultSet.next()) {
final IAddress imageBase = PostgreSQLHelpers.loadAddress(resultSet, "image_base");
final INaviModule module = provider.findModule(resultSet.getInt("id"));
modules.add(new Pair<IAddress, INaviModule>(imageBase, module));
}
return modules;
} catch (final SQLException e) {
throw new CouldntLoadDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException in project binnavi by google.
the class PostgreSQLCommentFunctions method loadMultipleCommentsById.
/**
* Loads multiple comments from the database at once. The function uses the passed array list of
* integer values to create a PostgreSQL array and retrieves all of those comments and their
* respective ancestors.
*
* @param provider The provider to access the database.
* @param commentIds The list of comments ids which will be fetched from the database.
* @return A Map from comment id to list of comments.
*
* @throws CouldntLoadDataException
*/
public static HashMap<Integer, ArrayList<IComment>> loadMultipleCommentsById(final SQLProvider provider, final Collection<Integer> commentIds) throws CouldntLoadDataException {
Preconditions.checkNotNull(provider, "IE00480: provider argument can not be null");
Preconditions.checkNotNull(commentIds, "IE00481: commentIds argument can not be null");
final String query = "SELECT * FROM get_all_comment_ancestors_multiple(?)";
final HashMap<Integer, IComment> commentIdToComment = new HashMap<>();
final Object[] commentIdsArray = commentIds.toArray();
final HashMap<Integer, ArrayList<IComment>> commentIdToComments = new HashMap<>();
try (PreparedStatement statement = provider.getConnection().getConnection().prepareCall(query)) {
statement.setArray(1, provider.getConnection().getConnection().createArrayOf("int4", commentIdsArray));
final ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
final int rootComment = resultSet.getInt("commentid");
resultSet.getInt("level");
final int commentId = resultSet.getInt("id");
final int parentId = resultSet.getInt("parent_id");
final int userId = resultSet.getInt("user_id");
final String commentText = resultSet.getString("comment");
final IUser user = CUserManager.get(provider).getUserById(userId);
final CComment comment = new CComment(commentId, user, commentIdToComment.get(parentId), commentText);
commentIdToComment.put(commentId, comment);
if (commentIdToComments.containsKey(rootComment)) {
commentIdToComments.get(rootComment).add(comment);
} else {
commentIdToComments.put(rootComment, Lists.<IComment>newArrayList(comment));
}
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
return commentIdToComments;
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException in project binnavi by google.
the class PostgreSQLTypeFunctions method loadRawTypeSubstitution.
/**
* Loads a single type substitution from the database.
*
* @param provider The {@link SQLProvider} used to access the database.
* @param module The {@link INaviModule} this {@link RawTypeSubstitution} is associated to.
* @param address of the {@link INaviInstruction instruction} where the
* {@link RawTypeSubstitution} is associated to.
* @param position of the {@link INaviOperandTree operand tree} where the
* {@link RawTypeSubstitution} is associated to.
* @param expressionId of the {@link INaviOperandTreeNode operand tree node} in the
* {@link INaviOperandTree operand tree} where the {@link RawTypeSubstitution type
* substitution} is associated to.
*
* @return The {@link RawTypeSubstitution type substitution} from the database which matches the
* given arguments.
* @throws CouldntLoadDataException if the single {@link RawTypeSubstitution type substitution}
* could not be loaded from the database.
*/
public static RawTypeSubstitution loadRawTypeSubstitution(final SQLProvider provider, final INaviModule module, final BigInteger address, final Integer position, final Integer expressionId) throws CouldntLoadDataException {
Preconditions.checkNotNull(provider, "Error: provider argument can not be null");
Preconditions.checkNotNull(module, "Error: module argument can not be null");
Preconditions.checkNotNull(address, "Error: address argument can not be null");
Preconditions.checkNotNull(position, "Error: position argument can not be null");
Preconditions.checkNotNull(expressionId, "Error: expressionId argument can not be null");
final String query = " SELECT * FROM load_type_substitution(?, ?, ?, ?) ";
try {
final PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
statement.setInt(1, module.getConfiguration().getId());
statement.setObject(2, address, Types.BIGINT);
statement.setInt(3, position);
statement.setInt(4, expressionId);
final ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
if (resultSet.first()) {
final int baseTypeId = resultSet.getInt("base_type_id");
final Array arr = resultSet.getArray("path");
final Integer[] path = resultSet.wasNull() ? new Integer[0] : (Integer[]) arr.getArray();
Integer offset = resultSet.getInt("offset");
if (resultSet.wasNull()) {
offset = null;
}
return new RawTypeSubstitution(new CAddress(address), position, expressionId, baseTypeId, path, offset);
}
}
} finally {
resultSet.close();
statement.close();
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
throw new CouldntLoadDataException("Error: could not load single type substitution from the database.");
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException in project binnavi by google.
the class PostgreSQLTypeFunctions method loadRawTypeMember.
/**
* Loads a single {@link RawTypeMember type member} from the database
*
* @param provider The {@link SQLProvider} to access the database with.
* @param module The {@link INaviModule} to which this {@link RawTypeMember type member} is
* associated to.
* @param typeId The id of the {@link RawTypeMember type member} to load from the database.
*
* @return The {@link RawTypeMember type member} from the database which matches the given
* arguments.
* @throws CouldntLoadDataException if the {@link RawTypeMember type member} could not be loaded
* from the database.
*/
public static RawTypeMember loadRawTypeMember(final SQLProvider provider, final INaviModule module, final Integer typeId) throws CouldntLoadDataException {
Preconditions.checkNotNull(provider, "Error: provider argument can not be null");
Preconditions.checkNotNull(module, "Error: module argument can not be null");
Preconditions.checkNotNull(typeId, "Error: typeId argument can not be null");
final String query = " SELECT * FROM load_type_member(?, ?) ";
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, typeId);
final ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
if (resultSet.first()) {
final int id = resultSet.getInt("id");
final String name = resultSet.getString("name");
final int baseTypeId = resultSet.getInt("base_type");
final Integer parentId = resultSet.getInt("parent_id");
Integer offset = resultSet.getInt("offset");
if (resultSet.wasNull()) {
offset = null;
}
Integer argument = resultSet.getInt("argument");
if (resultSet.wasNull()) {
argument = null;
}
Integer numberOfElements = resultSet.getInt("number_of_elements");
if (resultSet.wasNull()) {
numberOfElements = null;
}
return new RawTypeMember(id, name, baseTypeId, parentId, offset, argument, numberOfElements);
}
}
} finally {
resultSet.close();
statement.close();
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
throw new CouldntLoadDataException("Error: could not load single type member from the database.");
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException in project binnavi by google.
the class PostgreSQLDatabaseFunctions method getAssignedDebuggers.
/**
* Determines the debuggers that are assigned to a project.
*
* @param connection Connection to the SQL database where the information is stored.
* @param projectId ID of the project in question.
* @param debuggerManager Debugger manager object that belongs to the given database.
* @return A list that contains the debugger templates assigned to the given project.
* @throws CouldntLoadDataException Thrown if the debugger templates could not be loaded.
*/
protected static List<DebuggerTemplate> getAssignedDebuggers(final CConnection connection, final int projectId, final DebuggerTemplateManager debuggerManager) throws CouldntLoadDataException {
Preconditions.checkNotNull(connection, "IE02264: Connection argument can not be null");
Preconditions.checkArgument(projectId > 0, "Project id %s must be a positive integer.", projectId);
Preconditions.checkNotNull(debuggerManager, "IE02265: debugger manager argument can not be null");
final List<DebuggerTemplate> debuggerIds = new ArrayList<>();
final String query = String.format("SELECT debugger_id FROM %s WHERE project_id = %d", CTableNames.PROJECT_DEBUGGERS_TABLE, projectId);
try (ResultSet resultSet = connection.executeQuery(query, true)) {
while (resultSet.next()) {
debuggerIds.add(debuggerManager.findDebugger(resultSet.getInt("debugger_id")));
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
return debuggerIds;
}
Aggregations