use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.
the class PostgreSQLProviderTest method testCGenericSQLHelpersRollBack1.
@Test
public void testCGenericSQLHelpersRollBack1() throws SQLException {
final AbstractSQLProvider connectionProvider = (AbstractSQLProvider) getProvider();
final CConnection connection = connectionProvider.getConnection();
PostgreSQLHelpers.beginTransaction(connection);
PostgreSQLHelpers.rollback(connection);
}
use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.
the class PostgreSQLTagFunctions method insertTag.
/**
* Inserts a new tag into a tag hierarchy.
*
* @param provider The connection to the database.
* @param parent Parent tag under which the new tag is inserted.
* @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 insertTag(final AbstractSQLProvider provider, final ITreeNode<CTag> parent, final String name, final String description, final TagType type) throws CouldntSaveDataException {
checkArguments(provider, parent, type);
Preconditions.checkNotNull(name, "IE00563: Name argument can not be null");
Preconditions.checkNotNull(description, "IE00564: Description argument can not be null");
final CConnection connection = provider.getConnection();
final String query = String.format("update %s set parent_id = ? where parent_id = ? and id <> ?", CTableNames.TAGS_TABLE);
try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
final CTag tag = createTag(provider, parent.getObject(), name, description, type);
statement.setInt(1, tag.getId());
statement.setInt(2, parent.getObject().getId());
statement.setInt(3, tag.getId());
statement.executeUpdate();
return tag;
} 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 setDescription.
/**
* Changes the description of a tag.
*
* @param provider The connection to the database.
* @param tag The tag whose description is changed.
* @param description The new description of the tag.
*
* @throws CouldntSaveDataException Thrown if changing the tag description failed.
*/
public static void setDescription(final AbstractSQLProvider provider, final CTag tag, final String description) throws CouldntSaveDataException {
checkArguments(provider, tag);
Preconditions.checkNotNull(description, "IE00713: Description argument can not be null");
final CConnection connection = provider.getConnection();
final String query = "update " + CTableNames.TAGS_TABLE + " set description = ? where id = ?";
try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
statement.setString(1, description);
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 PostgreSQLTagManagerFunctions method loadTagManager.
/**
* Loads a tag manager from the database.
*
* @param provider The SQL provider that provides the connection.
* @param type Type of the tags managed by the tag manager.
*
* @return The tag manager that is loaded from the database.
*
* @throws CouldntLoadDataException Thrown if the tag manager could not be loaded.
*/
public static CTagManager loadTagManager(final AbstractSQLProvider provider, final TagType type) throws CouldntLoadDataException {
Preconditions.checkNotNull(type, "IE00567: Tag type argument can't be null");
final CConnection connection = provider.getConnection();
// should always have a tags table
if (!PostgreSQLHelpers.hasTable(connection, CTableNames.TAGS_TABLE)) {
final CTag rootTag = new CTag(0, "Root Node", "", type, provider);
return new CTagManager(new Tree<CTag>(new TreeNode<CTag>(rootTag)), type, provider);
}
final String query = String.format("select id, parent_id, name, description from %s where type = '%s'", CTableNames.TAGS_TABLE, PostgreSQLTagFunctions.tagToString(type));
try {
final ResultSet resultSet = connection.executeQuery(query, true);
try {
final HashMap<Integer, Pair<TreeNode<CTag>, Integer>> treeNodes = new HashMap<Integer, Pair<TreeNode<CTag>, Integer>>();
final CTag rootTag = new CTag(0, "Root Node", "", type, provider);
final TreeNode<CTag> rootTreeNode = new TreeNode<CTag>(rootTag);
treeNodes.put(0, new Pair<TreeNode<CTag>, Integer>(rootTreeNode, -1));
while (resultSet.next()) {
final int tagId = resultSet.getInt("id");
final int parentId = resultSet.getInt("parent_id");
final TreeNode<CTag> treeNode = new TreeNode<CTag>(new CTag(tagId, PostgreSQLHelpers.readString(resultSet, "name"), PostgreSQLHelpers.readString(resultSet, "description"), type, provider));
final Pair<TreeNode<CTag>, Integer> pair = new Pair<TreeNode<CTag>, Integer>(treeNode, parentId);
treeNodes.put(tagId, pair);
}
for (final Entry<Integer, Pair<TreeNode<CTag>, Integer>> e : treeNodes.entrySet()) {
if (e.getKey() == 0) {
continue;
}
final TreeNode<CTag> child = e.getValue().first();
final TreeNode<CTag> parent = treeNodes.get(e.getValue().second()).first();
child.setParent(parent);
parent.addChild(child);
}
return new CTagManager(new Tree<CTag>(rootTreeNode), type, provider);
} finally {
resultSet.close();
}
} catch (final SQLException e) {
throw new CouldntLoadDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.
the class PostgreSQLTraceFunctions method save.
/**
* Saves a trace to the database.
*
* @param provider The SQL provider that provides the connection.
* @param trace The trace to save to the database.
*
* @throws CouldntSaveDataException Thrown if the trace could not be saved to the database.
*/
public static void save(final AbstractSQLProvider provider, final TraceList trace) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE00579: Provider argument can not be null");
Preconditions.checkNotNull(trace, "IE00580: List argument can not be null");
Preconditions.checkArgument(trace.inSameDatabase(provider), "IE00581: List is not part of this database");
final CConnection connection = provider.getConnection();
if (trace.getEventCount() != 0) {
saveEvents(connection, trace);
saveEventValues(connection, trace);
}
}
Aggregations