use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLNodeFunctions method appendLocalCodeNodeComment.
/**
* Appends a comment as local code node comment to a code node.
*
* @param provider The provider used to access the database.
* @param codeNode The code node where the comment will be appended.
* @param commentText The text of the comment which will be appended.
* @param userId the user id of the currently active user.
*
* @throws CouldntSaveDataException
*/
public static int appendLocalCodeNodeComment(final SQLProvider provider, final INaviCodeNode codeNode, final String commentText, final Integer userId) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE02453: provider argument can not be null");
Preconditions.checkNotNull(codeNode, "IE02454: codeNode argument can not be null");
Preconditions.checkNotNull(commentText, "IE02455: commentText argument can not be null");
Preconditions.checkNotNull(userId, "IE02456: userId argument can not be null");
final Connection connection = provider.getConnection().getConnection();
final String function = "{ ? = call append_local_code_node_comment( ?, ?, ?, ?) }";
Integer moduleId = null;
final int nodeId = codeNode.getId();
try {
moduleId = codeNode.getParentFunction().getModule().getConfiguration().getId();
} catch (final MaybeNullException exception) {
throw new CouldntSaveDataException(exception);
}
try {
final CallableStatement appendCommentFunction = connection.prepareCall(function);
try {
appendCommentFunction.registerOutParameter(1, Types.INTEGER);
appendCommentFunction.setInt(2, moduleId);
appendCommentFunction.setInt(3, nodeId);
appendCommentFunction.setInt(4, userId);
appendCommentFunction.setString(5, commentText);
appendCommentFunction.execute();
final int commentId = appendCommentFunction.getInt(1);
if (appendCommentFunction.wasNull()) {
throw new CouldntSaveDataException("E00037: ");
}
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 PostgreSQLNodeFunctions method appendLocalFunctionNodeComment.
/**
* Appends a local function node comment.
*
* @param provider The provider used to access the database.
* @param functionNode The function node where to comment will be appended.
* @param commentText The text of the comment which will be appended.
* @param userId The user id of the currently active user.
*
* @return The id of the newly generated comment.
*
* @throws CouldntSaveDataException if the comment could not be stored to the database.
*/
public static int appendLocalFunctionNodeComment(final SQLProvider provider, final INaviFunctionNode functionNode, final String commentText, final Integer userId) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE02457: provider argument can not be null");
Preconditions.checkNotNull(functionNode, "IE02458: functionNode argument can not be null");
Preconditions.checkNotNull(commentText, "IE02459: comment argument can not be null");
Preconditions.checkNotNull(userId, "IE02460: userId argument can not be null");
final Connection connection = provider.getConnection().getConnection();
final int moduleId = functionNode.getFunction().getModule().getConfiguration().getId();
final String function = " { ? = call append_function_node_comment(?, ?, ?, ?) } ";
try {
final CallableStatement appendCommentFunction = connection.prepareCall(function);
try {
appendCommentFunction.registerOutParameter(1, Types.INTEGER);
appendCommentFunction.setInt(2, moduleId);
appendCommentFunction.setInt(3, functionNode.getId());
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 PostgreSQLProjectFunctions method createAddressSpace.
/**
* Creates an address space in a project.
*
* The project must be stored in the database connected to by the provider argument.
*
* @param provider The SQL provider that provides the connection.
* @param project The project where the address space is created.
* @param name The name of the created address space.
*
* @return The created address space.
*
* @throws CouldntSaveDataException Thrown if the address space could not be created.
*/
public static CAddressSpace createAddressSpace(final AbstractSQLProvider provider, final INaviProject project, final String name) throws CouldntSaveDataException {
checkArguments(provider, project);
Preconditions.checkNotNull(name, "IE00521: Address space names can not be null");
Preconditions.checkArgument(!("".equals(name)), "IE00522: Address space names can not be empty");
final CConnection connection = provider.getConnection();
final int projectId = project.getConfiguration().getId();
NaviLogger.info("Creating a new address space with name %s in project %s (%d)", name, project.getConfiguration().getName(), projectId);
try {
// The new address space gets the current date as creation and
// modification dates.
final String query = "insert into " + CTableNames.ADDRESS_SPACES_TABLE + "(project_id, name, description, creation_date, modification_date) values(?, ?, '', NOW(), NOW()) returning id";
final PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
try {
statement.setInt(1, projectId);
statement.setString(2, name);
Integer addressSpaceId = null;
final ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
if (resultSet.isFirst()) {
addressSpaceId = resultSet.getInt(1);
break;
}
}
} finally {
resultSet.close();
}
Preconditions.checkNotNull(addressSpaceId, "IE02130: Error address space id may not be null after project creation");
return PostgreSQLProjectFunctions.readAddressSpace(provider, addressSpaceId, project);
} finally {
statement.close();
}
} 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 PostgreSQLSectionFunctions method createSection.
/**
* This function creates a new {@link Section} in the database.
*
* @param connection The {@link Connection} to access the database with.
* @param moduleId The id of the module in which to create the section.
* @param name The name of the section.
* @param commentId The id of the comment associated with the section.
* @param startAddress The start address of the section.
* @param endAddress The end address of the section.
* @param permission The {@link SectionPermission} of the section.
* @param data The data of the section.
*
* @return The id of the section generated by the database.
*
* @throws CouldntSaveDataException if the section could not be created in the database.
*/
public static int createSection(final Connection connection, final int moduleId, final String name, final Integer commentId, final BigInteger startAddress, final BigInteger endAddress, final SectionPermission permission, final byte[] data) throws CouldntSaveDataException {
Preconditions.checkNotNull(connection, "Error: connection argument can not be null");
Preconditions.checkArgument(moduleId > 0, "Error: module id must be greater then zero");
Preconditions.checkNotNull(name, "Error: name argument can not be null");
Preconditions.checkNotNull(startAddress, "Error: startAddress argument can not be null");
Preconditions.checkNotNull(endAddress, "Error: endAddress argument can not be null");
Preconditions.checkNotNull(permission, "Error: permission argument can not be null");
final String query = " { ? = call create_section( ?, ?, ?, ?, ?, ?, ?) } ";
try (CallableStatement createSectionProcedure = connection.prepareCall(query)) {
createSectionProcedure.registerOutParameter(1, Types.INTEGER);
createSectionProcedure.setInt(2, moduleId);
createSectionProcedure.setString(3, name);
if (commentId == null) {
createSectionProcedure.setNull(4, Types.INTEGER);
} else {
createSectionProcedure.setInt(4, commentId);
}
createSectionProcedure.setObject(5, startAddress, Types.BIGINT);
createSectionProcedure.setObject(6, endAddress, Types.BIGINT);
createSectionProcedure.setObject(7, permission.name(), Types.OTHER);
createSectionProcedure.setBytes(8, data);
createSectionProcedure.execute();
final int sectionId = createSectionProcedure.getInt(1);
if (createSectionProcedure.wasNull()) {
throw new CouldntSaveDataException("Error: Got a section id of null from the database.");
}
return sectionId;
} 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 PostgreSQLSectionFunctions method setSectionName.
/**
* Sets the name of the given section.
*
* @param connection The connection to the database.
* @param moduleId The id of the module that contains the section.
* @param sectionId The id of the section.
* @param name The new name for the section.
* @throws CouldntSaveDataException Thrown if the name could not be written to the database.
*/
public static void setSectionName(final Connection connection, final int moduleId, final int sectionId, final String name) throws CouldntSaveDataException {
Preconditions.checkNotNull(connection, "Error: connection argument can not be null");
Preconditions.checkArgument(moduleId > 0, "Error: module id must be greater than zero");
Preconditions.checkArgument(sectionId >= 0, "Error: section id must be greater or equal than zero");
Preconditions.checkNotNull(name, "Error: name argument can not be null");
final String query = " { call set_section_name(?, ?, ?) } ";
try (CallableStatement procedure = connection.prepareCall(query)) {
procedure.setInt(1, moduleId);
procedure.setInt(2, sectionId);
procedure.setString(3, name);
procedure.execute();
} catch (final SQLException exception) {
throw new CouldntSaveDataException(exception);
}
}
Aggregations