-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDependencyLookup.cs
101 lines (87 loc) · 3.32 KB
/
DependencyLookup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using Microsoft.Data.Sqlite;
namespace AWPM
{
internal class DependencyLookup
{
private Database database;
public DependencyLookup(Database database)
{
this.database = database;
}
public (bool, List<Package>) calculateDependencies(List<string> packageNames)
{
List<Package> packages = new List<Package>();
List<Package> missingPackages = new List<Package>();
foreach (var packageName in packageNames)
{
var package = this.getPackageFromDatabase(packageName);
if (package != null)
{
packages.Add(package.Value);
var dependencies = this.getDependencies(package.Value.packageName);
}
else
{
missingPackages.Add(package.Value);
}
}
if (missingPackages.Count > 0)
{
return (false, missingPackages);
}
return (true, packages);
}
private Package? getPackageFromDatabase(string packageName)
{
string query = "SELECT id, name, version_major, version_minor, " +
$"version_patch, version_build FROM Packages WHERE name = {packageName}";
Package? package = null;
database.executeQuery(query, reader =>
{
if (reader.Read())
{
package = new Package
{
packageName = reader.GetString(1),
versionMajor = reader.GetInt32(2),
versionMinor = reader.GetInt32(3),
versionPatch = reader.GetInt32(4),
versionBuild = reader.GetInt32(5),
url = reader.GetString(6),
checksum = reader["checksum"] as byte[],
state = ReturnStates.PACKAGE_SETUP
};
}
});
return package;
}
private List<Package> getDependencies(string packageName)
{
List<Package> dependencies = new List<Package>();
string query =
"SELECT p.name, p.version_major, p.version_minor, p.version_patch, p.version_build, " +
"p.url, p.checksum " +
"FROM Dependencies d " +
"JOIN Packages p ON d.dependencyPackageId = p.id " +
"JOIN Packages pkg ON d.dependentPackageId = pkg.id " +
$"WHERE pkg.name = {packageName}; ";
database.executeQuery(query, reader =>
{
dependencies.Add(new Package
{
packageName = reader.GetString(1),
versionMajor = reader.GetInt32(2),
versionMinor = reader.GetInt32(3),
versionPatch = reader.GetInt32(4),
versionBuild = reader.GetInt32(5),
url = reader.GetString(6),
checksum = reader["checksum"] as byte[],
state = ReturnStates.PACKAGE_SETUP
});
});
return dependencies;
}
}
}