public Snapshot scan(String run, Target target, List entries) {
String targetSchema = schema(target);
try (Connection targetConnection = factory.open(targetSchema, target.alias())) {
Map<String, Connection> connections = new HashMap<>();
connections.put(targetSchema, targetConnection);
try {
List<ObjectInfo> objects = new ArrayList<>();
try (PreparedStatement s = targetConnection.prepareStatement(
"SELECT OBJECT_NAME, OBJECT_TYPE FROM USER_OBJECTS " +
"WHERE STATUS='INVALID' ORDER BY OBJECT_TYPE, OBJECT_NAME")) {
s.setQueryTimeout(20);
try (ResultSet rs = s.executeQuery()) {
while (rs.next()) {
String name = rs.getString(1);
String type = rs.getString(2);
List<String> errors = new ArrayList<>();
List<String> deps = new ArrayList<>();
try (PreparedStatement q = targetConnection.prepareStatement(
"SELECT LINE, POSITION, TEXT FROM USER_ERRORS " +
"WHERE NAME=? AND TYPE=? ORDER BY SEQUENCE")) {
q.setQueryTimeout(10);
q.setString(1, name);
q.setString(2, type);
try (ResultSet er = q.executeQuery()) {
while (er.next()) {
errors.add("line " + er.getInt(1) + ":" +
er.getInt(2) + " " + er.getString(3));
}
}
}
try (PreparedStatement q = targetConnection.prepareStatement(
"SELECT REFERENCED_NAME FROM USER_DEPENDENCIES " +
"WHERE NAME=? AND TYPE=? AND REFERENCED_OWNER=USER")) {
q.setQueryTimeout(10);
q.setString(1, name);
q.setString(2, type);
try (ResultSet dr = q.executeQuery()) {
while (dr.next()) {
deps.add(dr.getString(1));
}
}
}
objects.add(new ObjectInfo(name, type, "INVALID", errors, deps));
}
}
}
Set<String> satisfied = new HashSet<>();
for (LibraryEntry entry : entries) {
String segment = entry.executionSegment().equals("CURRENT")
? target.segment()
: entry.executionSegment();
String executionSchema = names.build(
SchemaInputParser.parse(target.input()).base(),
segment
);
Connection checkConnection = connections.get(executionSchema);
if (checkConnection == null) {
checkConnection = factory.open(executionSchema, target.alias());
connections.put(executionSchema, checkConnection);
}
if (check(checkConnection, entry, target)) {
satisfied.add(entry.id());
}
}
return new Snapshot(objects, satisfied);
} finally {
for (Connection connection : connections.values()) {
if (connection != targetConnection) {
try {
connection.close();
} catch (SQLException ignored) {
}
}
}
}
} catch (SQLException e) {
throw databaseError(e);
}
}
Top comments (0)