use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLFunctionFunctions method setDescription.
/**
* Changes the description of a function.
*
* The function must be stored in the database connected to by the provider argument.
*
* @param provider The SQL provider that provides the connection.
* @param function The function whose description is changed.
* @param description The new description of the function.
*
* @throws CouldntSaveDataException Thrown if the new description could not be saved to the
* database.
*/
public static void setDescription(final AbstractSQLProvider provider, final INaviFunction function, final String description) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE00448: Provider argument can not be null");
Preconditions.checkNotNull(function, "IE00449: Function argument can not be null");
Preconditions.checkNotNull(description, "IE00450: Comment argument can not be null");
Preconditions.checkArgument(function.inSameDatabase(provider), "IE00451: Function is not part of this database");
final CConnection connection = provider.getConnection();
final int module = function.getModule().getConfiguration().getId();
final IAddress address = function.getAddress();
final String query = "UPDATE " + CTableNames.FUNCTIONS_TABLE + " SET description = ? WHERE module_id = ? AND address = ?";
try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
statement.setString(1, description);
statement.setInt(2, module);
statement.setObject(3, address.toBigInteger(), Types.BIGINT);
statement.executeUpdate();
} catch (final SQLException exception) {
throw new CouldntSaveDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLInstructionFunctions method appendGlobalInstructionComment.
/**
* This function appends a global instruction comment to the list of global
* instruction comments associated with this instruction in the database.
*
* @param provider The provider to access the database.
* @param instruction The instruction to which the comment is associated.
* @param commentText The comment text of the comment.
* @param userId The user id of the currently active user.
* @return The id of the comment generated by the database.
*
* @throws CouldntSaveDataException if the comment could not be stored in the
* database.
*/
public static int appendGlobalInstructionComment(final SQLProvider provider, final INaviInstruction instruction, final String commentText, final Integer userId) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE01648: provider argument can not be null");
Preconditions.checkNotNull(instruction, "IE02040: instruction argument can not be null");
Preconditions.checkNotNull(commentText, "IE02041: commentText argument can not be null");
Preconditions.checkNotNull(userId, "IE02142: userId argument can not be null");
final CConnection connection = provider.getConnection();
final String function = "{ ? = call append_global_instruction_comment(?, ?, ?, ?) }";
try {
final CallableStatement appendCommentFunction = connection.getConnection().prepareCall(function);
try {
appendCommentFunction.registerOutParameter(1, Types.INTEGER);
appendCommentFunction.setInt(2, instruction.getModule().getConfiguration().getId());
appendCommentFunction.setObject(3, instruction.getAddress().toBigInteger(), Types.BIGINT);
appendCommentFunction.setInt(4, userId);
appendCommentFunction.setString(5, commentText);
appendCommentFunction.execute();
final int commentId = appendCommentFunction.getInt(1);
if (appendCommentFunction.wasNull()) {
throw new CouldntSaveDataException("Error: Got an comment id of null from the database");
}
return commentId;
} finally {
appendCommentFunction.close();
}
} catch (final SQLException exception) {
throw new CouldntSaveDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLInstructionFunctions method appendLocalInstructionComment.
/**
* This function appends a local instruction comment to the list of local
* instruction comments associated with the given instruction residing in the
* given code node to the database.
*
* @param provider The provider to access the database.
* @param codeNode The code node in which the instruction resides.
* @param instruction The instruction to which the comment is associated.
* @param commentText The text of the comment to be appended.
* @param userId The user id of the currently active user.
* @return The id of the comment generated by the database.
*
* @throws CouldntSaveDataException if the comment could not be stored in the
* database.
*/
public static int appendLocalInstructionComment(final SQLProvider provider, final INaviCodeNode codeNode, final INaviInstruction instruction, final String commentText, final Integer userId) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE02423: provider argument can not be null");
Preconditions.checkNotNull(codeNode, "IE02424: codeNode argument can not be null");
Preconditions.checkNotNull(instruction, "IE02425: instruction argument can not be null");
Preconditions.checkNotNull(commentText, "IE02426: comment argument can not be null");
Preconditions.checkNotNull(userId, "IE02427: userId argument can not be null");
final CConnection connection = provider.getConnection();
final String function = "{ ? = call append_local_instruction_comment( ?, ?, ?, ?, ?) }";
try {
final CallableStatement appendCommentFunction = connection.getConnection().prepareCall(function);
try {
appendCommentFunction.registerOutParameter(1, Types.INTEGER);
appendCommentFunction.setInt(2, instruction.getModule().getConfiguration().getId());
appendCommentFunction.setInt(3, codeNode.getId());
appendCommentFunction.setObject(4, instruction.getAddress().toBigInteger(), Types.BIGINT);
appendCommentFunction.setInt(5, userId);
appendCommentFunction.setString(6, commentText);
appendCommentFunction.execute();
final int commentId = appendCommentFunction.getInt(1);
if (appendCommentFunction.wasNull()) {
throw new CouldntSaveDataException("Error: Got an comment id of null from the database");
}
return commentId;
} finally {
appendCommentFunction.close();
}
} catch (final SQLException exception) {
throw new CouldntSaveDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgresSQLDebuggerFunctions method createDebuggerTemplate.
/**
* Creates a new debugger template in the database.
*
* @param provider SQL provider of the new debugger template.
* @param name Name of the new debugger template. This argument must be non-empty.
* @param host Host of the new debugger template. This argument must be non-empty.
* @param port Port of the new debugger template. This argument must be a valid port number.
*
* @return The new debugger template.
*
* @throws CouldntSaveDataException Thrown if the new debugger template could not be written to
* the database.
*/
public static DebuggerTemplate createDebuggerTemplate(final AbstractSQLProvider provider, final String name, final String host, final int port) throws CouldntSaveDataException {
Preconditions.checkNotNull(name, "IE00417: Debugger names can not be null");
Preconditions.checkArgument(!name.isEmpty(), "IE00418: Debugger names can not be empty");
Preconditions.checkNotNull(host, "IE00419: Debugger host can not be null");
Preconditions.checkArgument(!host.isEmpty(), "IE00418: Debugger host can not be empty");
Preconditions.checkArgument((port > 0) && (port <= 65535), "IE00421: Debugger port is out of bounds");
NaviLogger.info("Creating new debugger %s (%s:%d)", name, host, port);
final CConnection connection = provider.getConnection();
final String query = "INSERT INTO " + CTableNames.DEBUGGERS_TABLE + "(name, host, port) VALUES(?, ?, ?) RETURNING id";
try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
statement.setString(1, name);
statement.setString(2, host);
statement.setInt(3, port);
int id = -1;
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
id = resultSet.getInt("id");
}
}
return new DebuggerTemplate(id, name, host, port, provider);
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLDataFunctions method saveData.
/**
* Saves the data of a module to the database.
*
* @param provider Provides the connection to the database.
* @param module The module whose data is stored in the database.
* @param data The data of the module to store in the database.
*
* @throws CouldntSaveDataException Thrown if the module data could not be stored.
*/
public static void saveData(final AbstractSQLProvider provider, final INaviModule module, final byte[] data) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE01267: Provider argument can not be null");
Preconditions.checkNotNull(module, "IE01268: Module argument can not be null");
Preconditions.checkNotNull(data, "IE01269: Data argument can not be null");
final CConnection connection = provider.getConnection();
try {
connection.executeUpdate("DELETE FROM " + CTableNames.DATA_PARTS_TABLE + " WHERE module_id = " + module.getConfiguration().getId(), true);
} catch (final SQLException exception) {
throw new CouldntSaveDataException(exception);
}
final String preparedStatement = "INSERT INTO " + CTableNames.DATA_PARTS_TABLE + "(module_id, part_id, data) VALUES(?, ?, ?)";
try (PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(preparedStatement)) {
statement.setInt(1, module.getConfiguration().getId());
statement.setInt(2, 0);
statement.setBinaryStream(3, new ByteArrayInputStream(data, 0, data.length), data.length);
statement.execute();
} catch (final SQLException exception) {
throw new CouldntSaveDataException(exception);
}
}
Aggregations