use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.
the class PostgreSQLTagFunctions method setName.
/**
* Changes the name of a tag.
*
* @param provider The connection to the database.
* @param tag The tag whose name is changed.
* @param name The new name of the tag.
*
* @throws CouldntSaveDataException Thrown if changing the tag name failed.
*/
public static void setName(final AbstractSQLProvider provider, final CTag tag, final String name) throws CouldntSaveDataException {
checkArguments(provider, tag);
Preconditions.checkNotNull(name, "IE00565: Name argument can not be null");
final CConnection connection = provider.getConnection();
final String query = "update " + CTableNames.TAGS_TABLE + " set name = ? where id = ?";
try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
statement.setString(1, name);
statement.setInt(2, tag.getId());
statement.executeUpdate();
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.
the class PostgreSQLTagFunctions method createTag.
/**
* Creates a new tag in the database.
*
* @param provider The connection to the database.
* @param parent The parent tag of the tag.
* @param name The name of the new tag.
* @param description The description of the new tag.
* @param type The type of the new tag.
*
* @return The new tag.
*
* @throws CouldntSaveDataException Thrown if creating the tag failed.
*/
public static CTag createTag(final AbstractSQLProvider provider, final CTag parent, final String name, final String description, final TagType type) throws CouldntSaveDataException {
checkArguments(provider, parent, type);
Preconditions.checkNotNull(name, "IE00556: Name argument can not be null");
Preconditions.checkNotNull(description, "IE00557: Description argument can not be null");
final CConnection connection = provider.getConnection();
final String query = "insert into " + CTableNames.TAGS_TABLE + "(parent_id, name, description, type) values(?, ?, ?, ?::tag_type) returning id";
try (PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
if (parent.getId() == 0) {
statement.setNull(1, Types.INTEGER);
} else {
statement.setInt(1, parent.getId());
}
statement.setString(2, name);
statement.setString(3, description);
statement.setString(4, tagToString(type));
Integer id = null;
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
if (resultSet.isFirst()) {
id = resultSet.getInt(1);
}
}
}
if (id != null) {
return new CTag(id, name, description, type, provider);
} else {
throw new IllegalStateException("IE02141: Error id can not be null");
}
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.
the class PostgreSQLTagFunctions method deleteTag.
/**
* Deletes a tag from the database.
*
* @param provider The connection to the database.
* @param tag The tag to delete.
*
* @throws CouldntDeleteException Thrown if the tag could not be deleted.
*/
public static void deleteTag(final AbstractSQLProvider provider, final ITreeNode<CTag> tag) throws CouldntDeleteException {
checkArguments(provider, tag);
Preconditions.checkNotNull(tag.getParent(), "IE00558: Can not delete the root tag");
final CConnection connection = provider.getConnection();
try {
final ITreeNode<CTag> parent = tag.getParent();
final String parentId = parent.getObject().getId() == 0 ? "null" : String.valueOf(parent.getObject().getId());
final String query_1 = String.format("UPDATE %s SET parent_id = %s WHERE parent_id = ?", CTableNames.TAGS_TABLE, parentId);
try (PreparedStatement statement_1 = connection.getConnection().prepareStatement(query_1)) {
statement_1.setInt(1, tag.getObject().getId());
statement_1.executeUpdate();
}
final String query_2 = String.format("DELETE FROM %s WHERE id = ?", CTableNames.TAGS_TABLE);
try (PreparedStatement statement_2 = connection.getConnection().prepareStatement(query_2)) {
statement_2.setInt(1, tag.getObject().getId());
statement_2.executeUpdate();
}
} catch (final SQLException e) {
throw new CouldntDeleteException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.
the class PostgreSQLTagFunctions method deleteTagSubtree.
/**
* Deletes a tag and all of its subtrees.
*
* @param provider The connection to the database.
* @param tag The root tag of the subtree to delete.
*
* @throws CouldntDeleteException Thrown if the subtree could not be deleted.
*/
public static void deleteTagSubtree(final AbstractSQLProvider provider, final ITreeNode<CTag> tag) throws CouldntDeleteException {
checkArguments(provider, tag);
Preconditions.checkNotNull(tag.getParent(), "IE00559: Can not delete the root tag");
final CConnection connection = provider.getConnection();
try {
final StringBuilder query = new StringBuilder(String.format("delete from %s where id = %d", CTableNames.TAGS_TABLE, tag.getObject().getId()));
final List<Integer> idsToDelete = new ArrayList<>();
for (final ITreeNode<CTag> child : DepthFirstSorter.getSortedList(tag)) {
idsToDelete.add(child.getObject().getId());
query.append(" or id = " + tag.getObject().getId());
}
connection.executeUpdate(query.toString(), true);
} catch (final SQLException e) {
throw new CouldntDeleteException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.
the class PostgreSQLTraceFunctions method createTrace.
/**
* Creates a new trace list object in the database.
*
* @param provider The connection to the database.
* @param tracesTable The traces table where the trace is added.
* @param tracesColumn Identifies the view container column for which the trace is created.
* @param containerTable Identifies the view container table.
* @param containerId ID of the view container for which the trace is created.
* @param name The name of the new trace.
* @param description The description of the new trace.
*
* @return The created trace list.
*
* @throws CouldntSaveDataException Thrown if the trace list could not be created.
*/
// ESCA-JAVA0138:
private static TraceList createTrace(final AbstractSQLProvider provider, final String tracesTable, final String tracesColumn, final String containerTable, final int containerId, final String name, final String description) throws CouldntSaveDataException {
Preconditions.checkNotNull(name, "IE00568: Name argument can not be null");
Preconditions.checkNotNull(description, "IE00569: Description argument can not be null");
final CConnection connection = provider.getConnection();
final String query = "INSERT INTO " + CTableNames.TRACES_TABLE + "(view_id, name, description) VALUES(?, ?, ?) RETURNING id";
try {
final PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
Integer listId = null;
try {
statement.setInt(1, 0);
statement.setString(2, name);
statement.setString(3, description);
final ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
if (resultSet.isFirst()) {
listId = resultSet.getInt(1);
}
}
} finally {
resultSet.close();
}
} finally {
statement.close();
}
connection.executeUpdate("INSERT INTO " + tracesTable + "(" + tracesColumn + ", trace_id) " + " VALUES(" + containerId + ", " + listId + ")", true);
PostgreSQLHelpers.updateModificationDate(connection, containerTable, containerId);
return new TraceList(listId, name, description, provider);
} catch (final SQLException exception) {
throw new CouldntSaveDataException(exception);
}
}
Aggregations