This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
DB.cs
executable file
·1204 lines (1056 loc) · 32.8 KB
/
DB.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DB.cs
*
* Authors:
* Rolf Bjarne Kvinge (RKvinge@novell.com)
*
* Copyright 2009 Novell, Inc. (http://www.novell.com)
*
* See the LICENSE file included with the distribution for details.
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using Npgsql;
using NpgsqlTypes;
using log4net;
using MonkeyWrench.Database;
using MonkeyWrench.DataClasses;
namespace MonkeyWrench
{
public class DB : IDisposable, IDB
{
static readonly ILog log = LogManager.GetLogger (typeof (DB));
NpgsqlConnection dbcon;
LargeObjectManager manager;
TimeSpan db_time_difference;
public LargeObjectManager Manager
{
get
{
if (manager == null)
manager = new LargeObjectManager (dbcon);
return manager;
}
}
/// <summary>
/// Creates a command with a default timeout of 300 seconds.
/// </summary>
/// <returns></returns>
public IDbCommand CreateCommand ()
{
return CreateCommand (TimeSpan.FromSeconds (300));
}
/// <summary>
/// Creates a command with the specified timeout. A timeout of 0 means infinity.
/// </summary>
/// <param name="Timeout"></param>
/// <returns></returns>
public IDbCommand CreateCommand (TimeSpan Timeout)
{
NpgsqlCommand result = dbcon.CreateCommand ();
result.CommandTimeout = (int) Timeout.TotalSeconds;
return result;
}
public IDbCommand CreateCommand(string query) {
var cmd = CreateCommand ();
cmd.CommandText = query;
return cmd;
}
public IDbTransaction BeginTransaction ()
{
return dbcon.BeginTransaction ();
}
public DB () : this(true)
{}
public DB (bool Connect)
{
if (Connect)
this.Connect ();
}
public static void CreateParameter (IDbCommand cmd, string name, object value)
{
DBRecord.CreateParameter (cmd, name, value);
}
private void Connect ()
{
try {
string connectionString = String.Format("Server={0};Database={1};User ID={2};Password={3};",
Configuration.DatabaseHost,
Configuration.DatabaseName,
Configuration.DatabaseUser,
Configuration.DatabasePassword
);
if (Configuration.DatabasePort != 0)
connectionString += string.Format ("Port={0};", Configuration.DatabasePort);
dbcon = new NpgsqlConnection (connectionString);
log.DebugFormat ("Connecting to database, connection string: {0}", connectionString);
dbcon.Open ();
object db_now_obj = ExecuteScalar ("SELECT now();");
DateTime db_now;
DateTime machine_now = DateTime.Now;
db_now = (DateTime) db_now_obj;
db_time_difference = db_now - machine_now;
log.DebugFormat ("DB now: {0:yyyy/MM/dd HH:mm:ss.ffff}, current machine's now: {1:yyyy/MM/dd HH:mm:ss.ffff}, adjusted now: {3}, diff: {2:yyyy/MM/dd HH:mm:ss.ffff} ms", db_now, machine_now, db_time_difference.TotalMilliseconds, Now);
} catch (Exception) {
if (dbcon != null) {
dbcon.Dispose ();
dbcon = null;
}
throw;
}
}
public void Dispose ()
{
if (dbcon != null) {
dbcon.Close ();
dbcon = null;
}
}
private class DBFileStream : Stream
{
IDbTransaction transaction = null;
LargeObject obj;
public DBFileStream (DBFile file, DB db)
{
try {
transaction = db.dbcon.BeginTransaction ();
obj = db.Manager.Open (file.file_id.Value);
} catch {
if (transaction != null) {
transaction.Rollback ();
transaction = null;
}
}
}
protected override void Dispose (bool disposing)
{
base.Dispose (disposing);
if (transaction != null) {
transaction.Rollback ();
transaction = null;
}
}
public override int Read (byte [] buffer, int offset, int count)
{
return obj.Read (buffer, offset, count);
}
public override void Write (byte [] buffer, int offset, int count)
{
obj.Write (buffer, offset, count);
}
public override long Seek (long offset, SeekOrigin origin)
{
throw new NotImplementedException ();
}
public override void SetLength (long value)
{
throw new NotImplementedException ();
}
public override void Flush ()
{
// nop
}
public override long Position
{
get
{
return obj.Tell ();
}
set
{
throw new NotImplementedException ();
}
}
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override long Length
{
get { return obj.Size (); }
}
}
public Stream Download (DBWorkFile file)
{
return new DBFileStream (DBFile_Extensions.Create (this, file.file_id), this);
}
public Stream Download (DBFile file)
{
if (file.file_id.HasValue) {
return new DBFileStream (file, this);
} else {
return new System.IO.Compression.GZipStream (new FileStream (DBFile_Extensions.GetFullPath (file.md5), FileMode.Open, FileAccess.Read), System.IO.Compression.CompressionMode.Decompress);
}
}
public Stream Download (DBWorkFileView file)
{
return new DBFileStream (DBFile_Extensions.Create (this, file.file_id), this);
}
public int GetSize (int file_id)
{
using (IDbCommand cmd = CreateCommand ()) {
object o = ExecuteScalar ("SELECT file_id FROM File where File.id = " + file_id.ToString ());
if (!(o is int))
throw new Exception ("File_id doesn't exist.");
return GetLargeObjectSize ((int) o);
}
}
public int GetLargeObjectSize (int oid)
{
using (IDbTransaction transaction = BeginTransaction ()) {
int result;
LargeObject obj = Manager.Open (oid);
result = obj.Size ();
obj.Close ();
return result;
}
}
public DBFile UploadString (string contents, string extension, bool hidden)
{
string tmpfile = null;
try {
tmpfile = Path.GetTempFileName ();
File.WriteAllText (tmpfile, contents);
return Upload (tmpfile, extension, hidden, null);
} finally {
try {
File.Delete (tmpfile);
} catch (Exception ex) {
log.ErrorFormat ("Error while deleting temporary file: {0}", ex);
// ignore exceptions
}
}
}
/// <summary>
///
/// </summary>
/// <param name="Filename"></param>
/// <returns></returns>
public DBFile Upload (string filename, string extension, bool hidden, string compressed_mime)
{
string md5;
// first check if the file is already in the database
using (FileStream st = new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
md5 = FileUtilities.CalculateMD5 (st);
}
return Upload (md5, filename, filename, extension, hidden, compressed_mime);
}
public DBFile Upload (string md5, string path_to_contents, string filename, string extension, bool hidden, string compressed_mime)
{
IDbTransaction transaction = null;
LargeObjectManager manager;
LargeObject obj;
int? oid;
DBFile result;
long filesize;
string gzFilename = null;
try {
filesize = new FileInfo (path_to_contents).Length;
if (filesize > 1024 * 1024 * 500)
throw new Exception ("Max file size is 500 MB");
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM File WHERE md5 = '" + md5 + "'";
using (IDataReader reader = cmd.ExecuteReader ()) {
if (reader.Read ())
return new DBFile (reader);
}
}
//Console.WriteLine ("Uploading {0} {1} with compressed mime: {2}", Filename, md5, compressed_mime);
// The file is not in the database
// Note: there is a race condition here,
// the same file might get added to the db before we do it here.
// not quite sure how to deal with that except retrying the above if the insert below fails.
if (compressed_mime == MimeTypes.GZ) {
gzFilename = path_to_contents;
} else {
gzFilename = FileUtilities.GZCompress (path_to_contents);
compressed_mime = MimeTypes.GZ;
}
transaction = BeginTransaction ();
if (Configuration.StoreFilesInDB) {
manager = new LargeObjectManager (this.dbcon);
oid = manager.Create (LargeObjectManager.READWRITE);
obj = manager.Open (oid.Value, LargeObjectManager.READWRITE);
using (FileStream st = new FileStream (gzFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
byte [] buffer = new byte [1024];
int read = -1;
while (read != 0) {
read = st.Read (buffer, 0, buffer.Length);
obj.Write (buffer, 0, read);
}
}
obj.Close ();
} else {
oid = null;
string fn = FileUtilities.CreateFilename (md5, true, true);
File.Copy (gzFilename, fn, true);
log.DebugFormat ("Saved file to: {0}", fn);
}
result = new DBFile ();
result.file_id = oid;
result.filename = Path.GetFileName (filename);
result.md5 = md5;
result.size = (int) filesize;
result.hidden = hidden;
switch (extension.ToLower ()) {
case ".log":
case ".stdout":
case ".stderr":
result.mime = MimeTypes.LOG;
break;
case ".txt":
result.mime = MimeTypes.TXT;
break;
case ".htm":
case ".html":
result.mime = MimeTypes.HTML;
break;
case ".png":
result.mime = MimeTypes.PNG;
break;
case ".jpg":
result.mime = MimeTypes.JPG;
break;
case ".bmp":
result.mime = MimeTypes.BMP;
break;
case ".tar":
result.mime = MimeTypes.TAR;
break;
case ".bz":
result.mime = MimeTypes.BZ;
break;
case ".bz2":
result.mime = MimeTypes.BZ2;
break;
case ".zip":
result.mime = MimeTypes.ZIP; ;
break;
case ".gz":
result.mime = MimeTypes.GZ;
break;
case ".xpi":
result.mime = MimeTypes.XPI;
break;
case ".crx":
result.mime = MimeTypes.CRX;
break;
default:
result.mime = MimeTypes.OCTET_STREAM;
break;
}
result.compressed_mime = compressed_mime;
result.Save (this);
transaction.Commit ();
transaction = null;
return result;
} finally {
FileUtilities.TryDeleteFile (gzFilename);
if (transaction != null)
transaction.Rollback ();
}
}
public int ExecuteNonQuery (string sql)
{
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = sql;
return cmd.ExecuteNonQuery ();
}
}
public object ExecuteScalar (string sql)
{
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = sql;
return cmd.ExecuteScalar ();
}
}
public DBLane CloneLane (int lane_id, string new_name, bool copy_files)
{
DBLane result = null;
DBLane master = DBLane_Extensions.Create (this, lane_id);
if (this.LookupLane (new_name, false) != null)
throw new Exception (string.Format ("The lane '{0}' already exists.", new_name));
try {
using (IDbTransaction transaction = BeginTransaction ()) {
result = new DBLane ();
result.lane = new_name;
result.max_revision = master.max_revision;
result.min_revision = master.min_revision;
result.repository = master.repository;
result.source_control = master.source_control;
result.parent_lane_id = master.parent_lane_id;
result.enabled = master.enabled;
result.Save (this);
foreach (DBLanefile filemaster in master.GetFiles (this, null)) {
int fid;
if (copy_files) {
DBLanefile clone = new DBLanefile ();
clone.contents = filemaster.contents;
clone.mime = filemaster.mime;
clone.name = filemaster.name;
clone.Save (this);
fid = clone.id;
} else {
fid = filemaster.id;
}
DBLanefiles lane_files = new DBLanefiles ();
lane_files.lane_id = result.id;
lane_files.lanefile_id = fid;
lane_files.Save (this);
}
foreach (DBCommand cmdmaster in GetCommands (master.id)) {
DBCommand clone = new DBCommand ();
clone.lane_id = result.id;
clone.alwaysexecute = cmdmaster.alwaysexecute;
clone.arguments = cmdmaster.arguments;
clone.command = cmdmaster.command;
clone.filename = cmdmaster.filename;
clone.nonfatal = cmdmaster.nonfatal;
clone.sequence = cmdmaster.sequence;
clone.timeout = cmdmaster.timeout;
clone.working_directory = cmdmaster.working_directory;
clone.upload_files = cmdmaster.upload_files;
clone.Save (this);
}
foreach (DBHostLaneView hostlanemaster in master.GetHosts (this)) {
DBHostLane clone = new DBHostLane ();
clone.enabled = false;
clone.lane_id = result.id;
clone.host_id = hostlanemaster.host_id;
clone.Save (this);
}
foreach (DBEnvironmentVariable env in master.GetEnvironmentVariables (this)) {
DBEnvironmentVariable clone = new DBEnvironmentVariable ();
clone.host_id = env.host_id;
clone.lane_id = result.id;
clone.name = env.name;
clone.value = env.value;
clone.Save (this);
}
foreach (DBLaneNotification notification in master.GetNotifications (this)) {
DBLaneNotification clone = new DBLaneNotification ();
clone.lane_id = result.id;
clone.notification_id = notification.notification_id;
clone.Save (this);
}
foreach (var tag in master.GetTags (this)) {
var clone = new DBLaneTag ();
clone.lane_id = result.id;
clone.tag = tag.tag;
clone.Save (this);
}
transaction.Commit ();
}
} catch {
result = null;
throw;
}
return result;
}
public DBLane LookupLane (string lane, bool throwOnError)
{
DBLane result = null;
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM Lane WHERE lane = @lane";
DB.CreateParameter (cmd, "lane", lane);
using (IDataReader reader = cmd.ExecuteReader ()) {
if (!reader.Read ()) {
if (!throwOnError)
return null;
throw new Exception (string.Format ("Could not find the lane '{0}'.", lane));
}
result = new DBLane (reader);
if (reader.Read ()) {
if (!throwOnError)
return null;
throw new Exception (string.Format ("Found more than one lane named '{0}'.", lane));
}
}
}
return result;
}
public DBLane LookupLane (string lane)
{
return LookupLane (lane, true);
}
public DBHost LookupHost (string host)
{
return LookupHost (host, true);
}
public DBHost LookupHost (string host, bool throwOnError)
{
DBHost result = null;
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM Host WHERE host = @host";
DB.CreateParameter (cmd, "host", host);
using (IDataReader reader = cmd.ExecuteReader ()) {
if (!reader.Read ()) {
if (!throwOnError)
return null;
throw new Exception (string.Format ("Could not find the host '{0}'.", host));
}
result = new DBHost (reader);
if (reader.Read ()) {
if (!throwOnError)
return null;
throw new Exception (string.Format ("Found more than one host named '{0}'.", host));
}
}
}
return result;
}
/// <summary>
/// Returns all the lanes in the database.
/// </summary>
/// <returns></returns>
public List<DBLane> GetAllLanes ()
{
List<DBLane> result = new List<DBLane> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM Lane ORDER BY lane";
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
result.Add (new DBLane (reader));
}
}
}
return result;
}
public List<DBLanefiles> GetAllLaneFiles ()
{
var result = new List<DBLanefiles> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM LaneFiles";
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
result.Add (new DBLanefiles (reader));
}
}
}
return result;
}
public List<DBHost> GetHosts ()
{
List<DBHost> result = new List<DBHost> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM Host ORDER BY host";
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
result.Add (new DBHost (reader));
}
}
}
return result;
}
public List<DBHostLane> GetAllHostLanes ()
{
List<DBHostLane> result = new List<DBHostLane> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM HostLane ORDER BY host_id, lane_id";
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
result.Add (new DBHostLane (reader));
}
}
}
return result;
}
public List<DBHost> GetHostsForLane (int lane_id)
{
List<DBHost> result = new List<DBHost> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT *, HostLane.lane_id AS lane_id FROM Host INNER JOIN HostLane ON Host.id = HostLane.host_id WHERE lane_id = @lane_id";
DB.CreateParameter (cmd, "lane_id", lane_id);
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
result.Add (new DBHost (reader));
}
}
}
return result;
}
/// <summary>
/// This will return a list of lane ids:
/// [0]: input lane_id
/// [1]: [0]'s parent lane id
/// [2]: [1]'s parent lane id
/// etc
/// </summary>
/// <param name="lane_id"></param>
/// <returns></returns>
public List<int> GetLaneHierarchy (int lane_id)
{
var result = new List<int> ();
using (var cmd = CreateCommand ()) {
cmd.CommandText = @"
WITH RECURSIVE Family AS (
SELECT lane.id, lane.parent_lane_id, 0 AS depth FROM lane WHERE lane.id = @lane_id
UNION ALL
SELECT lane2.id, lane2.parent_lane_id, depth + 1 FROM lane lane2 JOIN family ON family.parent_lane_id = lane2.id)
SELECT family.id FROM family ORDER BY depth ASC;";
DB.CreateParameter (cmd, "lane_id", lane_id);
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
result.Add (reader.GetInt32 (0));
}
}
}
return result;
}
public List<DBLane> GetLanesForHost (int host_id, bool only_enabled)
{
List<DBLane> result = new List<DBLane> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT *, HostLane.host_id AS host_id, HostLane.enabled AS lane_enabled FROM Lane INNER JOIN HostLane ON Lane.id = HostLane.lane_id WHERE host_id = @host_id ";
if (only_enabled)
cmd.CommandText += " AND HostLane.enabled = true;";
DB.CreateParameter (cmd, "host_id", host_id);
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
result.Add (new DBLane (reader));
}
}
}
return result;
}
/// <summary>
/// Returns lane that matches lane_id in the database
/// </summary>
/// <returns></returns>
public DBLane GetLane (int id)
{
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM Lane WHERE id = @id ";
DB.CreateParameter (cmd, "id", id);
using (IDataReader reader = cmd.ExecuteReader ()) {
if (!reader.Read ())
return null;
if (reader.IsDBNull (0))
return null;
DBLane lane = new DBLane ();
lane.Load (reader);
return lane;
}
}
}
/// <summary>
/// Returns all the lanes for which there are revisions in the database
/// </summary>
/// <returns></returns>
public List<string> GetLanes ()
{
List<string> result = new List<string> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT DISTINCT lane FROM Revision";
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ())
result.Add (reader.GetString (0));
}
}
return result;
}
public List<DBCommand> GetCommands (int lane_id)
{
List<DBCommand> result = new List<DBCommand> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM Command ";
if (lane_id > 0) {
cmd.CommandText += "WHERE lane_id = @lane_id ";
DB.CreateParameter (cmd, "lane_id", lane_id);
}
cmd.CommandText += "ORDER BY sequence ASC";
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ())
result.Add (new DBCommand (reader));
}
}
return result;
}
public Dictionary<string, DBRevision> GetDBRevisions (int lane_id, bool ordered)
{
Dictionary<string, DBRevision> result = new Dictionary<string, DBRevision> ();
foreach (DBRevision rev in GetDBRevisions (lane_id, 0, false)) {
result.Add (rev.revision, rev);
}
return result;
}
public List<DBRevision> GetDBRevisions (int lane_id, int limit, bool ordered = true)
{
return GetDBRevisions (lane_id, limit, 0, ordered);
}
public List<DBRevision> GetDBRevisions (int lane_id, int limit, int offset, bool ordered = true)
{
List<DBRevision> result = new List<DBRevision> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT * FROM Revision WHERE lane_id = @lane_id";
if (ordered)
cmd.CommandText += " ORDER BY date DESC";
if (limit > 0)
cmd.CommandText += " LIMIT " + limit.ToString ();
if (offset > 0)
cmd.CommandText += " OFFSET " + offset.ToString ();
DB.CreateParameter (cmd, "lane_id", lane_id);
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
result.Add (new DBRevision (reader));
}
}
}
return result;
}
public List<int> GetRevisions (string lane, int limit)
{
List<int> result = new List<int> ();
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "SELECT DISTINCT CAST (revision as int) FROM revisions WHERE lane = @lane ORDER BY revision DESC";
if (limit > 0)
cmd.CommandText += " LIMIT " + limit.ToString ();
DB.CreateParameter (cmd, "lane", lane);
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ())
result.Add (reader.GetInt32 (0));
}
}
return result;
}
public void ClearAllWork (int lane_id, int host_id)
{
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "UPDATE Work SET state = 0, summary = '' " +
"WHERE lane_id = @lane_id " +
"AND host_id = @host_id;";
DB.CreateParameter (cmd, "lane_id", lane_id);
DB.CreateParameter (cmd, "host_id", host_id);
cmd.ExecuteNonQuery ();
}
}
public void IgnoreWork (int lane_id, int revision_id, int host_id)
{
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = @"
UPDATE
Work SET state = 11, summary = DEFAULT, starttime = DEFAULT, endtime = DEFAULT, duration = DEFAULT, logfile = DEFAULT, host_id = DEFAULT
WHERE
Work.revisionwork_id IN
(SELECT RevisionWork.id
FROM RevisionWork
WHERE RevisionWork.host_id = @host_id AND RevisionWork.lane_id = @lane_id AND RevisionWork.revision_id = @revision_id);
UPDATE
RevisionWork SET state = 11, lock_expires = DEFAULT, completed = true, workhost_id = DEFAULT, endtime = DEFAULT
WHERE
lane_id = @lane_id
AND revision_id = @revision_id
AND host_id = @host_id;
";
DB.CreateParameter (cmd, "lane_id", lane_id);
DB.CreateParameter (cmd, "revision_id", revision_id);
DB.CreateParameter (cmd, "host_id", host_id);
cmd.ExecuteNonQuery ();
}
}
public void ClearWork (int lane_id, int revision_id, int host_id)
{
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = @"
UPDATE
Work SET state = DEFAULT, summary = DEFAULT, starttime = DEFAULT, endtime = DEFAULT, duration = DEFAULT, logfile = DEFAULT, host_id = DEFAULT
WHERE
Work.revisionwork_id =
(SELECT RevisionWork.id
FROM RevisionWork
WHERE RevisionWork.host_id = @host_id AND RevisionWork.lane_id = @lane_id AND RevisionWork.revision_id = @revision_id);
UPDATE
RevisionWork SET state = DEFAULT, lock_expires = DEFAULT, completed = DEFAULT, workhost_id = DEFAULT,
createdtime = DEFAULT, assignedtime = DEFAULT, startedtime = DEFAULT, endtime = DEFAULT
WHERE
lane_id = @lane_id
AND revision_id = @revision_id
AND host_id = @host_id;
";
DB.CreateParameter (cmd, "lane_id", lane_id);
DB.CreateParameter (cmd, "revision_id", revision_id);
DB.CreateParameter (cmd, "host_id", host_id);
cmd.ExecuteNonQuery ();
}
}
/// <summary>
/// Deletes all the files related to the work in the revision 'revision_id' of lane 'lane' on the host 'host'.
/// </summary>
/// <param name="host"></param>
/// <param name="lane"></param>
/// <param name="revision_id"></param>
public void DeleteFiles (int host_id, int lane_id, int revision_id)
{
using (IDbTransaction transaction = BeginTransaction ()) {
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = @"
SELECT WorkFile.id AS id
INTO TEMP WorkFile_delete_tmpfile
FROM WorkFile
INNER JOIN Work ON Work.id = WorkFile.work_id
INNER JOIN RevisionWork ON RevisionWork.id = Work.revisionwork_id
WHERE
RevisionWork.lane_id = @lane_id AND
RevisionWork.host_id = @host_id AND
RevisionWork.revision_id = @revision_id;
DELETE FROM WorkFile
WHERE id IN (select * from WorkFile_delete_tmpfile);
DROP TABLE WorkFile_delete_tmpfile;
";
DB.CreateParameter (cmd, "lane_id", lane_id);
DB.CreateParameter (cmd, "host_id", host_id);
DB.CreateParameter (cmd, "revision_id", revision_id);
cmd.ExecuteNonQuery ();
transaction.Commit ();
}
}
}
public void DeleteLinks (int host_id, int lane_id, int revision_id)
{
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = @"
DELETE FROM FileLink WHERE work_id IN
(SELECT id FROM Work WHERE Work.revisionwork_id IN
(SELECT RevisionWork.id
FROM RevisionWork
WHERE RevisionWork.host_id = @host_id AND RevisionWork.lane_id = @lane_id AND RevisionWork.revision_id = @revision_id
)
);
";
DB.CreateParameter (cmd, "lane_id", lane_id);
DB.CreateParameter (cmd, "revision_id", revision_id);
DB.CreateParameter (cmd, "host_id", host_id);
cmd.ExecuteNonQuery ();
}
}
public void DeleteAllWork (int lane_id, int host_id)
{
using (IDbCommand cmd = CreateCommand ()) {
cmd.CommandText = "DELETE FROM Work WHERE lane_id = @lane_id AND host_id = @host_id;";
DB.CreateParameter (cmd, "lane_id", lane_id);
DB.CreateParameter (cmd, "host_id", host_id);
cmd.ExecuteNonQuery ();
}
//TODO: Directory.Delete(Configuration.GetDataRevisionDir(lane, revision), true);
}
public void DeleteWork (int lane_id, int revision_id, int host_id)
{
using (IDbCommand cmd = CreateCommand ()) {
// cmd.CommandText = "DELETE FROM Work WHERE lane_id = @lane_id AND revision_id = @revision_id AND host_id = @host_id;";
cmd.CommandText = @"
DELETE FROM Work
WHERE Work.revisionwork_id =
(SELECT id
FROM RevisionWork
WHERE lane_id = @lane_id
AND revision_id = @revision_id
AND host_id = @host_id
);
UPDATE RevisionWork SET state = 10 WHERE lane_id = @lane_id AND host_id = @host_id AND revision_id = @revision_id;";
DB.CreateParameter (cmd, "lane_id", lane_id);
DB.CreateParameter (cmd, "revision_id", revision_id);
DB.CreateParameter (cmd, "host_id", host_id);
cmd.ExecuteNonQuery ();
}
//TODO: Directory.Delete(Configuration.GetDataRevisionDir(lane, revision), true);
}
public DBRevision GetRevision (string lane, int revision)
{
using (IDbCommand cmd = CreateCommand ()) {