@@ -10,16 +10,7 @@ public class LocalDatabase
10
10
{
11
11
public string DatabaseFile { get ; set ; } = "pollination.db" ;
12
12
private SqliteConnection _connection ;
13
- private SqliteConnection connection
14
- {
15
- get
16
- {
17
- _connection = _connection ?? CreateConnection ( ) ;
18
- if ( _connection . State == System . Data . ConnectionState . Closed )
19
- _connection . Open ( ) ;
20
- return _connection ;
21
- }
22
- }
13
+ private SqliteConnection connection => GetConnection ( ) ;
23
14
24
15
private static LocalDatabase _instance ;
25
16
public static LocalDatabase Instance
@@ -31,17 +22,41 @@ public static LocalDatabase Instance
31
22
}
32
23
}
33
24
25
+ SqliteConnection GetConnection ( )
26
+ {
27
+ try
28
+ {
29
+ _connection = _connection ?? CreateConnection ( ) ;
30
+ if ( _connection . State == System . Data . ConnectionState . Closed )
31
+ _connection . Open ( ) ;
32
+ return _connection ;
33
+ }
34
+ catch ( Exception e )
35
+ {
36
+ throw LogHelper . LogReturnError ( e ) ;
37
+ }
38
+
39
+ }
34
40
35
41
SqliteConnection CreateConnection ( )
36
42
{
37
-
38
- SqliteConnection con ;
39
- var file = GetDatabaseFile ( ) ;
40
- var fileExist = File . Exists ( file ) ;
41
- con = new SqliteConnection ( $ "Data Source={ file } ") ;
42
- con . Open ( ) ;
43
- if ( ! fileExist ) InitDatabase ( con ) ;
44
- return con ;
43
+ try
44
+ {
45
+ SqliteConnection con ;
46
+ var file = GetDatabaseFile ( ) ;
47
+ var fileExist = File . Exists ( file ) ;
48
+ con = new SqliteConnection ( $ "Data Source={ file } ") ;
49
+ con . Open ( ) ;
50
+ if ( ! fileExist )
51
+ InitDatabase ( con ) ;
52
+
53
+ return con ;
54
+ }
55
+ catch ( Exception e )
56
+ {
57
+ throw LogHelper . LogReturnError ( e ) ;
58
+ }
59
+
45
60
}
46
61
47
62
public void DeleteDatabase ( )
@@ -51,15 +66,25 @@ public void DeleteDatabase()
51
66
File . Delete ( file ) ;
52
67
53
68
}
69
+
54
70
public string GetDatabaseFile ( )
55
71
{
56
- var file = DatabaseFile ;
57
- //C:\Users\mingo\.pollination
58
- var userDir = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
59
- var dir = Path . Combine ( userDir , ".pollination" ) ;
60
- Directory . CreateDirectory ( dir ) ;
61
- var filePath = Path . Combine ( dir , file ) ;
62
- return filePath ;
72
+ try
73
+ {
74
+ var file = DatabaseFile ;
75
+ //C:\Users\mingo\.pollination
76
+ var userDir = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
77
+ var dir = Path . Combine ( userDir , ".pollination" ) ;
78
+ Directory . CreateDirectory ( dir ) ;
79
+ var filePath = Path . Combine ( dir , file ) ;
80
+ LogHelper . LogInfo ( filePath ) ;
81
+ return filePath ;
82
+ }
83
+ catch ( Exception e )
84
+ {
85
+ throw LogHelper . LogReturnError ( e ) ;
86
+ }
87
+
63
88
}
64
89
65
90
static void InitDatabase ( SqliteConnection con )
@@ -129,70 +154,97 @@ public bool Add(IEnumerable<ScheduledJobInfo> jobResults)
129
154
130
155
static bool Add ( SqliteConnection connection , ScheduledJobInfo schJob )
131
156
{
132
- using ( var con = connection )
157
+ try
133
158
{
134
- var cmd = con . CreateCommand ( ) ;
135
- cmd . CommandText =
136
- @"
159
+ using ( var con = connection )
160
+ {
161
+ var cmd = con . CreateCommand ( ) ;
162
+ cmd . CommandText =
163
+ @"
137
164
INSERT INTO JobTable (ProjSlug, JobID, DateTime, JobInfo)
138
165
VALUES ($ProjSlug, $JobID, $DateTime, $JobInfo)
139
166
" ;
140
- cmd . Parameters . AddWithValue ( "$ProjSlug" , SqliteType . Text ) . Value = schJob . ProjectSlug . ToLower ( ) ;
141
- cmd . Parameters . AddWithValue ( "$JobID" , SqliteType . Text ) . Value = schJob . JobID ;
142
- cmd . Parameters . AddWithValue ( "$DateTime" , SqliteType . Text ) . Value = DateTime . Now ;
143
- cmd . Parameters . AddWithValue ( "$JobInfo" , SqliteType . Blob ) . Value = schJob . Serialize_Binary ( ) ;
144
- var done = cmd . ExecuteNonQuery ( ) ;
145
- con . Close ( ) ;
146
- return done == 1 ;
167
+ cmd . Parameters . AddWithValue ( "$ProjSlug" , SqliteType . Text ) . Value = schJob . ProjectSlug . ToLower ( ) ;
168
+ cmd . Parameters . AddWithValue ( "$JobID" , SqliteType . Text ) . Value = schJob . JobID ;
169
+ cmd . Parameters . AddWithValue ( "$DateTime" , SqliteType . Text ) . Value = DateTime . Now ;
170
+ cmd . Parameters . AddWithValue ( "$JobInfo" , SqliteType . Blob ) . Value = schJob . Serialize_Binary ( ) ;
171
+ LogHelper . LogInfo ( $ "Adding { schJob . ProjectSlug } /{ schJob . JobID } ") ;
172
+ var done = cmd . ExecuteNonQuery ( ) ;
173
+ con . Close ( ) ;
174
+ return done == 1 ;
175
+ }
176
+
177
+ }
178
+ catch ( Exception e )
179
+ {
180
+ throw LogHelper . LogReturnError ( e , $ "Failed to add job to local database:\n { schJob } ") ;
147
181
}
148
-
182
+
149
183
}
150
184
151
185
static List < ScheduledJobInfo > Get ( SqliteConnection connection , string condition )
152
186
{
153
- var res = new List < ScheduledJobInfo > ( ) ;
154
- condition = string . IsNullOrEmpty ( condition ) ? "1=1" : condition ;
155
- using ( var con = connection )
187
+
188
+ try
156
189
{
157
- var cmd = con . CreateCommand ( ) ;
158
- cmd . CommandText =
159
- $@ "
190
+ var res = new List < ScheduledJobInfo > ( ) ;
191
+ condition = string . IsNullOrEmpty ( condition ) ? "1=1" : condition ;
192
+ using ( var con = connection )
193
+ {
194
+ var cmd = con . CreateCommand ( ) ;
195
+ cmd . CommandText =
196
+ $@ "
160
197
SELECT JobInfo
161
198
FROM JobTable
162
199
WHERE { condition }
163
200
" ;
164
- using ( var reader = cmd . ExecuteReader ( ) )
165
- {
166
- while ( reader . Read ( ) )
201
+ LogHelper . LogInfo ( $ "Getting a job { condition } ") ;
202
+ using ( var reader = cmd . ExecuteReader ( ) )
167
203
{
168
- var package = ( byte [ ] ) reader . GetValue ( 0 ) ;
169
- var re = ScheduledJobInfo . Deserialize_Binary ( package ) ;
170
- res . Add ( re ) ;
204
+ while ( reader . Read ( ) )
205
+ {
206
+ var package = ( byte [ ] ) reader . GetValue ( 0 ) ;
207
+ var re = ScheduledJobInfo . Deserialize_Binary ( package ) ;
208
+ res . Add ( re ) ;
209
+ }
171
210
}
211
+ con . Close ( ) ;
172
212
}
173
- con . Close ( ) ;
213
+ return res ;
174
214
}
175
- return res ;
215
+ catch ( Exception e )
216
+ {
217
+ throw LogHelper . LogReturnError ( e , $ "Failed to get a job from local database:\n { condition } ") ;
218
+ }
219
+
176
220
}
177
221
178
222
static bool Delete ( SqliteConnection connection , string projID , string jobId )
179
223
{
180
- using ( var con = connection )
224
+ try
181
225
{
182
- var cmd = con . CreateCommand ( ) ;
183
- cmd . CommandText =
184
- @"
226
+ using ( var con = connection )
227
+ {
228
+ var cmd = con . CreateCommand ( ) ;
229
+ cmd . CommandText =
230
+ @"
185
231
DELETE
186
232
FROM JobTable
187
233
WHERE ProjSlug = $ProjSlug AND JobID = $JobID
188
234
" ;
189
- cmd . Parameters . AddWithValue ( "$ProjSlug" , SqliteType . Blob ) . Value = projID ;
190
- cmd . Parameters . AddWithValue ( "$JobID" , SqliteType . Text ) . Value = jobId ;
191
-
192
- var done = cmd . ExecuteNonQuery ( ) ;
193
- con . Close ( ) ;
194
- return done >= 1 ;
235
+ cmd . Parameters . AddWithValue ( "$ProjSlug" , SqliteType . Blob ) . Value = projID ;
236
+ cmd . Parameters . AddWithValue ( "$JobID" , SqliteType . Text ) . Value = jobId ;
237
+ LogHelper . LogInfo ( $ "Deleting a job:\n ProjSlug={ projID } AND JobID={ jobId } ") ;
238
+ var done = cmd . ExecuteNonQuery ( ) ;
239
+ con . Close ( ) ;
240
+ return done >= 1 ;
241
+ }
195
242
}
243
+ catch ( Exception e )
244
+ {
245
+ throw LogHelper . LogReturnError ( e , $ "Failed to delete a job from local database:\n ProjSlug={ projID } AND JobID={ jobId } ") ;
246
+ }
247
+
196
248
}
197
249
}
198
250
}
0 commit comments