use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLTypeFunctions method createType.
/**
* Create a new base type. The members have to be inserted separately.
*
* @param connection The connection to the database.
* @param moduleId The id of the module which contains the type.
* @param name The name of the type.
* @param size The size of the type in bits.
* @param childPointerTypeId The id of the the preceding base type in the pointer hierarchy, or
* null if the new type is a value type.
* @param signed Specifies whether the type can represent signed values.
*/
public static final int createType(final Connection connection, final int moduleId, final String name, final int size, final Integer childPointerTypeId, final boolean signed, final BaseTypeCategory category) throws CouldntSaveDataException {
try {
final String query = String.format("INSERT INTO %s (module_id, id, name, size, pointer, signed, category) " + "VALUES (?, nextval('bn_base_types_id_seq'), ?, ?, ?, ?, ?) returning id", CTableNames.BASE_TYPES_TABLE);
final PreparedStatement statement = connection.prepareStatement(query);
try {
statement.setInt(1, moduleId);
statement.setString(2, name);
statement.setInt(3, size);
if (childPointerTypeId == null) {
statement.setNull(4, Types.INTEGER);
} else {
statement.setInt(4, childPointerTypeId);
}
statement.setBoolean(5, signed);
statement.setObject(6, category.toString(), Types.OTHER);
final ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt(1);
} else {
throw new CouldntSaveDataException("Empty result set while inserting base type.");
}
} finally {
statement.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 PostgreSQLTypeFunctions method appendTypeInstanceComment.
/**
* This function appends a comment to the list of type instance comments associated to this type
* instance.
*
* @param provider The provider to access the database.
* @param moduleId The id of the module to witch the type instance is associated to.
* @param instanceId The if of the type instance to which the comment is associated to.
* @param commentText The comment text of the comment.
* @param userId The 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 Integer appendTypeInstanceComment(final SQLProvider provider, final int moduleId, final int instanceId, final String commentText, final Integer userId) throws CouldntSaveDataException {
Preconditions.checkArgument(moduleId > 0, "Error: module id must be greater then zero");
Preconditions.checkArgument(instanceId >= 0, "instance id must be greater or equal to zero");
Preconditions.checkNotNull(commentText, "Error: comment text argument can not be null");
Preconditions.checkNotNull(userId, "Error: user id argument can not be null");
final CConnection connection = provider.getConnection();
final String function = " { ? = call append_type_instance_comment(?, ?, ?, ?) } ";
try {
final CallableStatement appendCommentFunction = connection.getConnection().prepareCall(function);
try {
appendCommentFunction.registerOutParameter(1, Types.INTEGER);
appendCommentFunction.setInt(2, moduleId);
appendCommentFunction.setInt(3, instanceId);
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 PostgreSQLTypeFunctions method createTypeInstance.
/**
* Creates a new type instance in the database.
*
* @param connection The connection used to access the database.
* @param moduleId The id of the module the type instance is associated to.
* @param name The name of the type instance.
* @param commentId The id of the comment associated with the type instance.
* @param typeId The id of the type which is the base type for this type instance.
* @param sectionId The id of the section where this type instance is located in.
* @param sectionOffset The offset in the section at which the type instance starts.
*
* @return The id of the generated type instance in the database.
*
* @throws CouldntSaveDataException if the type instance could not be generated in the database.
*/
public static int createTypeInstance(final Connection connection, final int moduleId, final String name, final Integer commentId, final int typeId, final int sectionId, final long sectionOffset) 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.checkNotNull(name, "Error: name argument can not be null");
Preconditions.checkArgument(typeId >= 0, "Error: type id must be greater than zero");
Preconditions.checkArgument(sectionId >= 0, "Error: section id must be larger than zero");
Preconditions.checkArgument(sectionOffset >= 0, "Error: section offset must be larger or equal to zero");
try {
final String query = " { ? = call create_type_instance(?, ?, ?, ?, ?, ?) } ";
final CallableStatement procedure = connection.prepareCall(query);
try {
procedure.registerOutParameter(1, Types.INTEGER);
procedure.setInt(2, moduleId);
procedure.setString(3, name);
if (commentId == null) {
procedure.setNull(4, Types.INTEGER);
} else {
procedure.setInt(4, commentId);
}
procedure.setInt(5, typeId);
procedure.setInt(6, sectionId);
procedure.setLong(7, sectionOffset);
procedure.execute();
final int typeInstanceId = procedure.getInt(1);
if (procedure.wasNull()) {
throw new CouldntSaveDataException("Error: the type instance id returned from the database was null");
}
return typeInstanceId;
} finally {
procedure.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 PostgreSQLTypeFunctions method updateTypeMember.
/**
* Updates the member belonging to the given base type in the database.
*
* @param connection The connection to the database.
* @param member The member to update.
* @param newName The new name of the member.
* @param newBaseType The new base type of the member.
* @param newNumberOfElements The new number of elements the member has.
* @param newArgumentIndex The new argument index of the member.
* @param module The module which contains the type member.
* @throws CouldntSaveDataException Thrown if the member couldn't be updated in the database.
*/
public static void updateTypeMember(final Connection connection, final TypeMember member, final String newName, final BaseType newBaseType, final Optional<Integer> offset, final Optional<Integer> newNumberOfElements, final Optional<Integer> newArgumentIndex, final INaviModule module) throws CouldntSaveDataException {
Preconditions.checkNotNull(member, "Error: member argument can not be null.");
Preconditions.checkNotNull(newName, "Error: new name argument can not be null");
Preconditions.checkNotNull(newBaseType, "Error: new base type argument can not be null.");
Preconditions.checkNotNull(offset, "Error: offset argument can not be null");
Preconditions.checkNotNull(newNumberOfElements, "Error: new number of elements argument can not be null.");
Preconditions.checkNotNull(newArgumentIndex, "Error: new argument index argument can not be null.");
try {
final PreparedStatement statement = connection.prepareStatement("UPDATE " + CTableNames.TYPE_MEMBERS_TABLE + " SET name = ?, base_type = ?, parent_id = ?, \"offset\" = ?, " + "number_of_elements = ?, argument = ? WHERE module_id = ? AND id = ?");
try {
statement.setString(1, newName);
statement.setInt(2, newBaseType.getId());
statement.setInt(3, member.getParentType().getId());
if (offset.isPresent()) {
statement.setInt(4, offset.get());
} else {
statement.setNull(4, Types.INTEGER);
}
if (newNumberOfElements.isPresent()) {
statement.setInt(5, newNumberOfElements.get());
} else {
statement.setNull(5, Types.INTEGER);
}
if (newArgumentIndex.isPresent()) {
statement.setInt(6, newArgumentIndex.get());
} else {
statement.setNull(6, Types.INTEGER);
}
statement.setInt(7, module.getConfiguration().getId());
statement.setInt(8, member.getId());
statement.executeUpdate();
} finally {
statement.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 PostgreSQLEdgeFunctions method appendLocalEdgeComment.
/**
* Appends a local edge comment to the list of local edge comments associated with this edge.
*
* @param provider The provider to access the database with.
* @param edge The Edge where to comment is associated with.
* @param commentText The text of the comment which will be appended.
* @param userId The user id of the currently active user.
*
* @throws CouldntSaveDataException If the data could not be stored in the database.
*/
public static int appendLocalEdgeComment(final AbstractSQLProvider provider, final INaviEdge edge, final String commentText, final Integer userId) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE00486: provider argument can not be null");
Preconditions.checkNotNull(edge, "IE00502: edge argument can not be null");
Preconditions.checkNotNull(commentText, "IE00503: comment argument can not be null");
Preconditions.checkNotNull(userId, "IE00504: userId argument can not be null");
final Connection connection = provider.getConnection().getConnection();
final String function = "{ ? = call append_local_edge_comment( ?, ?, ?) }";
try {
final CallableStatement appendCommentFunction = connection.prepareCall(function);
try {
appendCommentFunction.registerOutParameter(1, Types.INTEGER);
appendCommentFunction.setInt(2, edge.getId());
appendCommentFunction.setInt(3, userId);
appendCommentFunction.setString(4, commentText);
appendCommentFunction.execute();
final int commentId = appendCommentFunction.getInt(1);
if (appendCommentFunction.wasNull()) {
throw new CouldntSaveDataException("Error: Database returned null for comment id");
}
return commentId;
} finally {
appendCommentFunction.close();
}
} catch (final SQLException exception) {
throw new CouldntSaveDataException(exception);
}
}
Aggregations