-
Notifications
You must be signed in to change notification settings - Fork 0
/
modDatabase.cs
172 lines (149 loc) · 5.09 KB
/
modDatabase.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;
namespace XRFAgent
{
internal class modDatabase
{
private static SQLiteConnection conn;
/// <summary>
/// Loads the database module: Creates database connection and creates tables
/// </summary>
public static void Load()
{
conn = new SQLiteConnection(Properties.Settings.Default.Database_FileURI);
conn.ExecuteScalar<int>("PRAGMA journal_mode = WAL;");
conn.CreateTable<Config>();
conn.CreateTable<InstalledSoftware>();
conn.CreateTable<LocalQueue>();
}
/// <summary>
/// Unloads the database module: Closes database connection
/// </summary>
public static void Unload()
{
conn.Close();
conn.Dispose();
}
/// <summary>
/// Class defining the CONFIG table
/// </summary>
[Table("CONFIG")]
public class Config
{
[PrimaryKey, MaxLength(100)]
public string Key { get; set; }
public string Value { get; set; }
}
/// <summary>
/// Adds a new setting to the CONFIG table
/// </summary>
/// <param name="config">(Config) Setting to add</param>
/// <returns>(int) Number of rows added</returns>
public static int AddConfig(Config config)
{
int result = conn.Insert(config);
return result;
}
/// <summary>
/// Updates an existing setting in the CONFIG table
/// </summary>
/// <param name="config">(Config) Setting to update</param>
/// <returns>(int) Number of rows updated</returns>
public static int UpdateConfig(Config config)
{
int result = 0;
result = conn.Update(config);
return result;
}
/// <summary>
/// Updates an existing setting in the CONFIG table or adds it if it does not exist
/// </summary>
/// <param name="config">(Config) Setting to add or update</param>
/// <returns>(int) Number of rows updated</returns>
public static int AddOrUpdateConfig(Config config)
{
int result = UpdateConfig(config);
if (result == 0)
{
result = AddConfig(config);
}
return result;
}
/// <summary>
/// Gets the value of a setting in the CONFIG table
/// </summary>
/// <param name="key">(string) Name of setting</param>
/// <returns>(string) Value of setting</returns>
public static string GetConfig(string key)
{
var Value = from c in conn.Table<Config>()
where c.Key == key
select c.Value;
return Value.FirstOrDefault();
}
/// <summary>
/// Class defining the LOCALQUEUE table
/// </summary>
[Table("LOCALQUEUE")]
public class LocalQueue
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Src { get; set; }
public string Auth { get; set; }
public string Dest { get; set; }
public string Mesg { get; set; }
public bool Recv { get; set; }
}
public static int EnqueueLocalMessage(LocalQueue localQueue)
{
int result = conn.Insert(localQueue);
return result;
}
/// <summary>
/// Class defining the INSTALLEDSOFTWARE table
/// </summary>
[Table("INSTALLEDSOFTWARE")]
public class InstalledSoftware
{
[PrimaryKey]
public string Name { get; set; }
public string Version { get; set; }
public string Publisher { get; set; }
public string InstallDate { get; set; }
}
/// <summary>
/// Adds a new application to the INSTALLEDSOFTWARE table
/// </summary>
/// <param name="software">(InstalledSoftware) Application to add</param>
/// <returns>(int) Number of rows added</returns>
public static int AddSoftware(InstalledSoftware software)
{
int result = conn.Insert(software);
return result;
}
/// <summary>
/// Updates an existing application in the INSTALLEDSOFTWARE table
/// </summary>
/// <param name="software">(InstalledSoftware) Application to update</param>
/// <returns>(int) Number of rows updated</returns>
public static int UpdateSoftware(InstalledSoftware software)
{
int result = 0;
result = conn.Update(software);
return result;
}
/// <summary>
/// Dumps the current contents of the INSTALLEDSOFTWARE table
/// </summary>
public static void TruncateSoftware()
{
conn.DeleteAll<InstalledSoftware>();
}
}
}