Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.DB/Database.cs @ 375

Last change on this file since 375 was 375, checked in by gkronber, 16 years ago

worked on #188

File size: 19.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Data.Linq;
27using HeuristicLab.CEDMA.DB.Interfaces;
28using System.ServiceModel;
29using System.Data;
30using System.Data.SQLite;
31using System.Data.Common;
32
33namespace HeuristicLab.CEDMA.DB {
34  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
35  public class Database : IDatabase {
36    private string connectionString;
37    private object bigLock = new object();
38    public Database(string connectionString) {
39      this.connectionString = connectionString;
40    }
41
42    #region create empty database
43    public void CreateNew() {
44      lock(bigLock) {
45        using(DbConnection cnn = new SQLiteConnection(connectionString)) {
46          cnn.Open();
47          using(DbTransaction t = cnn.BeginTransaction()) {
48            using(DbCommand cmd = cnn.CreateCommand()) {
49              cmd.CommandText = "CREATE TABLE Project (ID integer primary key autoincrement, Name text, Description text, CreationTime DateTime)";
50              cmd.Transaction = t;
51              cmd.ExecuteNonQuery();
52            }
53            using(DbCommand cmd = cnn.CreateCommand()) {
54              cmd.CommandText = "CREATE TABLE Agent (ID integer primary key autoincrement, ProjectId integer, Name text, Status text default 'Unknown', RawData Blob)";
55              cmd.Transaction = t;
56              cmd.ExecuteNonQuery();
57            }
58            using(DbCommand cmd = cnn.CreateCommand()) {
59              cmd.CommandText = "CREATE TABLE Run (ID integer primary key autoincrement, AgentId integer, CreationTime DateTime, StartTime DateTime, FinishedTime DateTime, Status text default 'Unknown', RawData Blob)";
60              cmd.Transaction = t;
61              cmd.ExecuteNonQuery();
62            }
63            using(DbCommand cmd = cnn.CreateCommand()) {
64              cmd.CommandText = "CREATE TABLE Result (ID integer primary key autoincrement, RunId integer, ResultId integer, CreationTime DateTime, RawData Blob)";
65              cmd.Transaction = t;
66              cmd.ExecuteNonQuery();
67            }
68            t.Commit();
69          }
70        }
71      }
72    }
73    #endregion
74
75    #region insert agent/run/result/sub-result
76    public long InsertAgent(string name, byte[] rawData) {
77      lock(bigLock) {
78        using(DbConnection cnn = new SQLiteConnection(connectionString)) {
79          cnn.Open();
80          long id;
81          using(DbTransaction t = cnn.BeginTransaction()) {
82            using(DbCommand c = cnn.CreateCommand()) {
83              c.Transaction = t;
84              c.CommandText = "Insert into Agent (Name, RawData) values (@Name, @RawData); select last_insert_rowid()";
85              DbParameter nameParam = c.CreateParameter();
86              nameParam.ParameterName = "@Name";
87              nameParam.Value = name;
88              c.Parameters.Add(nameParam);
89              DbParameter dataParam = c.CreateParameter();
90              dataParam.ParameterName = "@RawData";
91              dataParam.Value = rawData;
92              c.Parameters.Add(dataParam);
93              id = (long)c.ExecuteScalar();
94            }
95            t.Commit();
96            return id;
97          }
98        }
99      }
100    }
101
102    public long InsertRun(long agentId, byte[] rawData) {
103      lock(bigLock) {
104        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
105          cnn.Open();
106          long id;
107          using(DbTransaction t = cnn.BeginTransaction()) {
108            using(DbCommand c = cnn.CreateCommand()) {
109              c.Transaction = t;
110              c.CommandText = "Insert into Run (AgentId, CreationTime, RawData) values (@AgentId, @CreationTime, @RawData); select last_insert_rowid()";
111              DbParameter agentIdParam = c.CreateParameter();
112              agentIdParam.ParameterName = "@AgentId";
113              agentIdParam.Value = agentId;
114              c.Parameters.Add(agentIdParam);
115              DbParameter creationParam = c.CreateParameter();
116              creationParam.ParameterName = "@CreationTime";
117              DateTime now = DateTime.Now;
118              creationParam.Value = now;
119              c.Parameters.Add(creationParam);
120              DbParameter rawDataParam = c.CreateParameter();
121              rawDataParam.ParameterName = "@RawData";
122              rawDataParam.Value = rawData;
123              c.Parameters.Add(rawDataParam);
124              id = (long)c.ExecuteScalar();
125            }
126            t.Commit();
127            return id;
128          }
129        }
130      }
131    }
132
133    public long InsertResult(long runId, byte[] rawData) {
134      lock(bigLock) {
135        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
136          cnn.Open();
137          long id;
138          using(DbTransaction t = cnn.BeginTransaction()) {
139            using(DbCommand c = cnn.CreateCommand()) {
140              c.Transaction = t;
141              c.CommandText = "Insert into Result (RunId, CreationTime, RawData) values (@RunId, @CreationTime, @RawData); select last_insert_rowid()";
142              DbParameter runIdParam = c.CreateParameter();
143              runIdParam.ParameterName = "@RunId";
144              runIdParam.Value = runId;
145              c.Parameters.Add(runIdParam);
146              DbParameter creationParam = c.CreateParameter();
147              creationParam.ParameterName = "@CreationTime";
148              DateTime now = DateTime.Now;
149              creationParam.Value = now;
150              c.Parameters.Add(creationParam);
151              DbParameter rawDataParam = c.CreateParameter();
152              rawDataParam.ParameterName = "@RawData";
153              rawDataParam.Value = rawData;
154              c.Parameters.Add(rawDataParam);
155              id = (long)c.ExecuteScalar();
156            }
157            t.Commit();
158            return id;
159          }
160        }
161      }
162    }
163
164    public long InsertSubResult(long resultId, byte[] rawData) {
165      lock(bigLock) {
166
167        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
168          cnn.Open();
169          long id;
170          using(DbTransaction t = cnn.BeginTransaction()) {
171            using(DbCommand c = cnn.CreateCommand()) {
172              c.Transaction = t;
173              c.CommandText = "Insert into Result (ResultId, CreationTime, RawData) values (@ResultId, @CreationTime, @RawData); select last_insert_rowid()";
174              DbParameter resultIdParam = c.CreateParameter();
175              resultIdParam.ParameterName = "@ResultId";
176              resultIdParam.Value = resultId;
177              c.Parameters.Add(resultIdParam);
178              DbParameter creationParam = c.CreateParameter();
179              creationParam.ParameterName = "@CreationTime";
180              DateTime now = DateTime.Now;
181              creationParam.Value = now;
182              c.Parameters.Add(creationParam);
183              DbParameter rawDataParam = c.CreateParameter();
184              rawDataParam.ParameterName = "@RawData";
185              rawDataParam.Value = rawData;
186              c.Parameters.Add(rawDataParam);
187              id = (long)c.ExecuteScalar();
188            }
189            t.Commit();
190            return id;
191          }
192        }
193      }
194    }
195    #endregion
196
197    #region update agent/run
198    public void UpdateAgent(long id, string name) {
199      lock(bigLock) {
200        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
201          cnn.Open();
202          using(SQLiteTransaction t = cnn.BeginTransaction()) {
203            using(SQLiteCommand c = cnn.CreateCommand()) {
204              c.Transaction = t;
205              c.CommandText = "Update Agent set Name=@Name where id=@Id";
206              DbParameter nameParam = c.CreateParameter();
207              DbParameter idParam = c.CreateParameter();
208              nameParam.ParameterName = "@Name";
209              nameParam.Value = name;
210              idParam.ParameterName = "@Id";
211              idParam.Value = id;
212              c.Parameters.Add(nameParam);
213              c.Parameters.Add(idParam);
214              c.ExecuteNonQuery();
215            }
216            t.Commit();
217          }
218        }
219      }
220    }
221
222    public void UpdateAgent(long id, ProcessStatus status) {
223      lock(bigLock) {
224        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
225          cnn.Open();
226          using(SQLiteTransaction t = cnn.BeginTransaction()) {
227            using(SQLiteCommand c = cnn.CreateCommand()) {
228              c.Transaction = t;
229              c.CommandText = "Update Agent set Status=@Status where id=@Id";
230              DbParameter statusParam = c.CreateParameter();
231              DbParameter idParam = c.CreateParameter();
232              statusParam.ParameterName = "@Status";
233              statusParam.Value = status;
234              idParam.ParameterName = "@Id";
235              idParam.Value = id;
236              c.Parameters.Add(statusParam);
237              c.Parameters.Add(idParam);
238              c.ExecuteNonQuery();
239            }
240            t.Commit();
241          }
242        }
243      }
244    }
245
246    public void UpdateAgent(long id, byte[] rawData) {
247      lock(bigLock) {
248        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
249          cnn.Open();
250          using(SQLiteTransaction t = cnn.BeginTransaction()) {
251            using(SQLiteCommand c = cnn.CreateCommand()) {
252              c.Transaction = t;
253              c.CommandText = "Update Agent set RawData=@RawData where id=@Id";
254              DbParameter rawDataParam = c.CreateParameter();
255              DbParameter idParam = c.CreateParameter();
256              rawDataParam.ParameterName = "@RawData";
257              rawDataParam.Value = rawData;
258              idParam.ParameterName = "@Id";
259              idParam.Value = id;
260              c.Parameters.Add(rawDataParam);
261              c.Parameters.Add(idParam);
262              c.ExecuteNonQuery();
263            }
264            t.Commit();
265          }
266        }
267      }
268    }
269
270    public void UpdateRunStart(long runId, DateTime startTime) {
271      lock(bigLock) {
272        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
273          cnn.Open();
274          using(SQLiteTransaction t = cnn.BeginTransaction()) {
275            using(SQLiteCommand c = cnn.CreateCommand()) {
276              c.Transaction = t;
277              c.CommandText = "Update Run set StartTime=@StartTime where id=@Id";
278              DbParameter startTimeParam = c.CreateParameter();
279              DbParameter idParam = c.CreateParameter();
280              startTimeParam.ParameterName = "@StartTime";
281              startTimeParam.Value = startTime;
282              idParam.ParameterName = "@Id";
283              idParam.Value = runId;
284              c.Parameters.Add(startTimeParam);
285              c.Parameters.Add(idParam);
286              c.ExecuteNonQuery();
287            }
288            t.Commit();
289          }
290        }
291      }
292    }
293
294    public void UpdateRunFinished(long runId, DateTime finishedTime) {
295      lock(bigLock) {
296        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
297          cnn.Open();
298          using(SQLiteTransaction t = cnn.BeginTransaction()) {
299            using(SQLiteCommand c = cnn.CreateCommand()) {
300              c.Transaction = t;
301              c.CommandText = "Update Run set FinishedTime=@FinishedTime where id=@Id";
302              DbParameter finishedTimeParam = c.CreateParameter();
303              DbParameter idParam = c.CreateParameter();
304              finishedTimeParam.ParameterName = "@FinishedTime";
305              finishedTimeParam.Value = finishedTime;
306              idParam.ParameterName = "@Id";
307              idParam.Value = runId;
308              c.Parameters.Add(finishedTimeParam);
309              c.Parameters.Add(idParam);
310              c.ExecuteNonQuery();
311            }
312            t.Commit();
313          }
314        }
315      }
316    }
317
318    public void UpdateRunStatus(long runId, ProcessStatus status) {
319      lock(bigLock) {
320        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
321          cnn.Open();
322          using(SQLiteTransaction t = cnn.BeginTransaction()) {
323            using(SQLiteCommand c = cnn.CreateCommand()) {
324              c.Transaction = t;
325              c.CommandText = "Update Run set Status=@Status where id=@Id";
326              DbParameter statusParam = c.CreateParameter();
327              DbParameter idParam = c.CreateParameter();
328              statusParam.ParameterName = "@Status";
329              statusParam.Value = status;
330              idParam.ParameterName = "@Id";
331              idParam.Value = runId;
332              c.Parameters.Add(statusParam);
333              c.Parameters.Add(idParam);
334              c.ExecuteNonQuery();
335            }
336            t.Commit();
337          }
338        }
339      }
340    }
341
342
343
344    #endregion
345
346
347    #region get agent/run/result/sub-result
348
349    public ICollection<AgentEntry> GetAgents(ProcessStatus status) {
350      lock(bigLock) {
351        List<AgentEntry> agents = new List<AgentEntry>();
352        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
353          cnn.Open();
354          SQLiteCommand c = cnn.CreateCommand();
355          c.CommandText = "Select id, name, status, rawdata from Agent where Status=@Status";
356          DbParameter statusParameter = c.CreateParameter();
357          statusParameter.ParameterName = "@Status";
358          statusParameter.Value = status;
359          c.Parameters.Add(statusParameter);
360
361          SQLiteDataReader r = c.ExecuteReader();
362          while(r.Read()) {
363            AgentEntry agent = new AgentEntry();
364            agent.Id = r.GetInt32(0);
365            agent.Name = r.GetString(1);
366            agent.Status = (ProcessStatus)Enum.Parse(typeof(ProcessStatus), r.GetString(2));
367            agent.RawData = (byte[])r.GetValue(3);
368            agents.Add(agent);
369          }
370        }
371        return agents;
372      }
373    }
374
375    public ICollection<AgentEntry> GetAgents() {
376      lock(bigLock) {
377        List<AgentEntry> agents = new List<AgentEntry>();
378        using(DbConnection cnn = new SQLiteConnection(connectionString)) {
379          cnn.Open();
380          using(DbCommand c = cnn.CreateCommand()) {
381            c.CommandText = "Select id, name, status, rawdata from Agent";
382            using(DbDataReader r = c.ExecuteReader()) {
383              while(r.Read()) {
384                AgentEntry agent = new AgentEntry();
385                agent.Id = r.GetInt32(0);
386                agent.Name = r.GetString(1);
387                agent.Status = (ProcessStatus)Enum.Parse(typeof(ProcessStatus), r.GetString(2));
388                agent.RawData = (byte[])r.GetValue(3);
389                agents.Add(agent);
390              }
391            }
392          }
393        }
394        return agents;
395      }
396    }
397
398    //(ID integer primary key autoincrement, AgentId integer, CreationTime DateTime, StartTime DateTime, FinishedTime DateTime, Status text default 'Unknown', RawData Blob)";
399
400    public ICollection<RunEntry> GetRuns() {
401      lock(bigLock) {
402        List<RunEntry> runs = new List<RunEntry>();
403        using(DbConnection cnn = new SQLiteConnection(connectionString)) {
404          cnn.Open();
405          using(DbCommand c = cnn.CreateCommand()) {
406            c.CommandText = "Select Id, AgentId, CreationTime, StartTime, FinishedTime, Status, Rawdata from Run";
407            using(DbDataReader r = c.ExecuteReader()) {
408              while(r.Read()) {
409                RunEntry run = new RunEntry();
410                run.Id = r.GetInt32(0);
411                run.AgentId = r.GetInt32(1);
412                run.CreationTime = r.GetDateTime(2);
413                run.StartTime = r.GetDateTime(3);
414                run.FinishedTime = r.GetDateTime(4);
415                run.Status = (ProcessStatus)Enum.Parse(typeof(ProcessStatus), r.GetString(5));
416                run.RawData = (byte[])r.GetValue(6);
417                runs.Add(run);
418              }
419            }
420          }
421        }
422        return runs;
423      }
424    }
425
426    public ICollection<RunEntry> GetRuns(ProcessStatus status) {
427      lock(bigLock) {
428        List<RunEntry> runs = new List<RunEntry>();
429        using(DbConnection cnn = new SQLiteConnection(connectionString)) {
430          cnn.Open();
431          using(DbCommand c = cnn.CreateCommand()) {
432            c.CommandText = "Select Id, AgentId, CreationTime, StartTime, FinishedTime, Status, Rawdata from Run where Status=@Status";
433            DbParameter statusParameter = c.CreateParameter();
434            statusParameter.ParameterName = "@Status";
435            statusParameter.Value = status;
436            c.Parameters.Add(statusParameter);
437
438            using(DbDataReader r = c.ExecuteReader()) {
439              while(r.Read()) {
440                RunEntry run = new RunEntry();
441                run.Id = r.GetInt32(0);
442                run.AgentId = r.GetInt32(1);
443                run.CreationTime = r.GetDateTime(2);
444                run.StartTime = r.IsDBNull(3)? null : new Nullable<DateTime>(r.GetDateTime(3));
445                run.FinishedTime = r.IsDBNull(4) ? null:new Nullable<DateTime>(r.GetDateTime(4));
446                run.Status = (ProcessStatus)Enum.Parse(typeof(ProcessStatus), r.GetString(5));
447                run.RawData = (byte[])r.GetValue(6);
448                runs.Add(run);
449              }
450            }
451          }
452        }
453        return runs;
454      }
455    }
456
457    // (ID integer primary key autoincrement, RunId integer, ResultId integer, CreationTime DateTime, RawData Blob)";
458    public ICollection<ResultEntry> GetResults(long runId) {
459      lock(bigLock) {
460        List<ResultEntry> results = new List<ResultEntry>();
461        using(DbConnection cnn = new SQLiteConnection(connectionString)) {
462          cnn.Open();
463          using(DbCommand c = cnn.CreateCommand()) {
464            c.CommandText = "Select Id, RunId, CreationTime, Rawdata from Result";
465            using(DbDataReader r = c.ExecuteReader()) {
466              while(r.Read()) {
467                ResultEntry result = new ResultEntry();
468                result.Id = r.GetInt32(0);
469                result.RunId = r.GetInt32(1);
470                result.CreationTime = r.GetDateTime(2);
471                result.RawData = (byte[])r.GetValue(3);
472                results.Add(result);
473              }
474            }
475          }
476        }
477        return results;
478      }
479    }
480
481    public ICollection<ResultEntry> GetSubResults(long resultId) {
482      throw new NotImplementedException();
483    }
484    #endregion
485  }
486}
Note: See TracBrowser for help on using the repository browser.