use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException in project binnavi by google.
the class CGenericSQLUserFunctions method loadUsers.
/**
* Loads the complete list of users known to the database.
*
* @param provider The provider used to access the database.
*
* @return A list of all users known to the database.
* @throws CouldntLoadDataException
*/
public static List<IUser> loadUsers(final SQLProvider provider) throws CouldntLoadDataException {
Preconditions.checkNotNull(provider, "IE00206: provider argument can not be null");
final CConnection connection = provider.getConnection();
final String query = "SELECT user_id, user_name FROM " + CTableNames.USER_TABLE;
final ArrayList<IUser> users = new ArrayList<>();
try (ResultSet resultSet = connection.executeQuery(query, true)) {
while (resultSet.next()) {
final int userId = resultSet.getInt(1);
final String userName = resultSet.getString(2);
users.add(new CUser(userId, userName));
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
return users;
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException in project binnavi by google.
the class CCodeNodeParser method parse.
/**
* Takes the information from the components passed into the constructor and creates a list of
* nodes from that information.
*
* @return The list of nodes created by the parser.
*
* @throws ParserException Thrown if the instruction data could not be loaded.
* @throws CPartialLoadException Thrown if not all necessary modules are loaded.
*/
public List<CCodeNode> parse() throws ParserException, CPartialLoadException {
// it to point to the proper data.
if (!dataProvider.next()) {
return new ArrayList<CCodeNode>();
}
// Generate the nodes from the raw data.
while (true) {
if (dataProvider.isAfterLast()) {
if (currentNode != null) {
nodes.add(currentNode);
}
break;
}
nodes.add(extractNode(dataProvider));
}
final HashSet<Integer> allComments = Sets.newHashSet();
allComments.addAll(localCommentIdToCodeNode.keySet());
allComments.addAll(globalCommentIdToCodeNode.keySet());
allComments.addAll(globalCommentIdToInstruction.keySet());
allComments.addAll(localCommentIdToInstruction.keySet());
try {
final HashMap<Integer, ArrayList<IComment>> commentIdToComments = sqlProvider.loadMultipleCommentsById(allComments);
for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdToComments.entrySet()) {
if (localCommentIdToCodeNode.containsKey(commentIdToComment.getKey())) {
localCommentIdToCodeNode.get(commentIdToComment.getKey()).getComments().initializeLocalCodeNodeComment(commentIdToComment.getValue());
}
if (globalCommentIdToCodeNode.containsKey(commentIdToComment.getKey())) {
globalCommentIdToCodeNode.get(commentIdToComment.getKey()).getComments().initializeGlobalCodeNodeComment(commentIdToComment.getValue());
}
if (localCommentIdToInstruction.containsKey(commentIdToComment.getKey())) {
final Pair<INaviInstruction, INaviCodeNode> instructionToCodeNode = localCommentIdToInstruction.get(commentIdToComment.getKey());
instructionToCodeNode.second().getComments().initializeLocalInstructionComment(instructionToCodeNode.first(), commentIdToComment.getValue());
}
if (globalCommentIdToInstruction.containsKey(commentIdToComment.getKey())) {
globalCommentIdToInstruction.get(commentIdToComment.getKey()).initializeGlobalComment(commentIdToComment.getValue());
}
}
} catch (final CouldntLoadDataException exception) {
throw new CPartialLoadException("Error: Comments could not be loaded.", null);
}
return nodes;
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException in project binnavi by google.
the class PostgreSQLSettingsFunctions method readSetting.
/**
* Reads a view container setting from the database.
*
* @param connection The connection to the database.
* @param containerId The ID of the view container.
* @param key The name of the setting to read.
* @param column The name of the column that identifies the view container ID.
* @param table The name of the table that identifies the view container table.
*
* @return The loaded setting.
*
* @throws CouldntLoadDataException Thrown if the setting could not be loaded.
*/
private static String readSetting(final CConnection connection, final int containerId, final String key, final String column, final String table) throws CouldntLoadDataException {
try {
final PreparedStatement statement = connection.getConnection().prepareStatement("select value from " + table + " where name = ? and " + column + " = ?");
try {
statement.setString(1, key);
statement.setInt(2, containerId);
final ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
return PostgreSQLHelpers.readString(resultSet, "value");
}
} finally {
resultSet.close();
}
return null;
} finally {
statement.close();
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException 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.Exceptions.CouldntLoadDataException in project binnavi by google.
the class PostgreSQLSectionFunctions method deleteSection.
/**
* Deletes a section from the database.
*
* @param provider The {@link SQLProvider} used to access the database.
* @param section The {@link Section} that will be deleted.
*
* @throws CouldntLoadDataException if the {@link Section} could not be deleted from the database.
*/
public static void deleteSection(final SQLProvider provider, final Section section) throws CouldntLoadDataException {
Preconditions.checkNotNull(provider, "Error: provider argument can not be null");
Preconditions.checkNotNull(section, "Error: section argument can not be null");
final String query = " { call delete_section(?, ?) } ";
try (CallableStatement procedure = provider.getConnection().getConnection().prepareCall(query)) {
procedure.setInt(1, section.getModule().getConfiguration().getId());
procedure.setInt(2, section.getId());
procedure.execute();
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
}
Aggregations