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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Data.Linq;
|
---|
27 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
28 | using System.ServiceModel;
|
---|
29 | using System.Data;
|
---|
30 | using System.Data.SQLite;
|
---|
31 | using System.Data.Common;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.CEDMA.DB {
|
---|
34 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, UseSynchronizationContext = true)]
|
---|
35 | public class Database : IDatabase {
|
---|
36 | private string connectionString;
|
---|
37 | public Database(string connectionString) {
|
---|
38 | this.connectionString = connectionString;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void CreateNew() {
|
---|
42 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
43 | cnn.ConnectionString = connectionString;
|
---|
44 | cnn.Open();
|
---|
45 |
|
---|
46 | SQLiteCommand cAgent = cnn.CreateCommand();
|
---|
47 | cAgent.CommandText = "CREATE TABLE Agent (ID integer primary key autoincrement, ProjectId integer, Name text, Status text default 'Unknown', RawData Blob)";
|
---|
48 | SQLiteCommand cProject = cnn.CreateCommand();
|
---|
49 | cProject.CommandText = "CREATE TABLE Project (ID integer primary key autoincrement, Name text, Description text, CreationDate DateTime)";
|
---|
50 | SQLiteCommand cResult = cnn.CreateCommand();
|
---|
51 | cResult.CommandText = "CREATE TABLE Result (ID integer primary key autoincrement, AgentId integer, ParentResultId integer, CreationDate DateTime, RawData Blob)";
|
---|
52 | cAgent.ExecuteNonQuery();
|
---|
53 | cProject.ExecuteNonQuery();
|
---|
54 | cResult.ExecuteNonQuery();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public long CreateAgent() {
|
---|
59 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
60 | cnn.Open();
|
---|
61 | SQLiteCommand c = cnn.CreateCommand();
|
---|
62 | c.CommandText = "Insert into Agent (Name) values (@Name); select last_insert_rowid()";
|
---|
63 | DbParameter nameParam = c.CreateParameter();
|
---|
64 | nameParam.ParameterName = "@Name";
|
---|
65 | nameParam.Value = DateTime.Now.ToString();
|
---|
66 | c.Parameters.Add(nameParam);
|
---|
67 | long id = (long)c.ExecuteScalar();
|
---|
68 | return id;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public ICollection<AgentEntry> GetAgentEntries() {
|
---|
73 | List<AgentEntry> agents = new List<AgentEntry>();
|
---|
74 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
75 | cnn.Open();
|
---|
76 | SQLiteCommand c = cnn.CreateCommand();
|
---|
77 | c.CommandText = "Select id, name, status, rawdata from Agent";
|
---|
78 | SQLiteDataReader r = c.ExecuteReader();
|
---|
79 | while(r.Read()) {
|
---|
80 | AgentEntry agent = new AgentEntry(r.GetInt32(0), r.GetString(1), (AgentStatus)Enum.Parse(typeof(AgentStatus), r.GetString(2)), (byte[])r.GetValue(3));
|
---|
81 | agents.Add(agent);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return agents;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public ICollection<AgentEntry> GetAgentEntries(AgentStatus status) {
|
---|
88 | List<AgentEntry> agents = new List<AgentEntry>();
|
---|
89 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
90 | cnn.Open();
|
---|
91 | SQLiteCommand c = cnn.CreateCommand();
|
---|
92 | c.CommandText = "Select id, name, status, rawdata from Agent where Status=@Status";
|
---|
93 | DbParameter statusParameter = c.CreateParameter();
|
---|
94 | statusParameter.ParameterName = "@Status";
|
---|
95 | statusParameter.Value = (int)status;
|
---|
96 | c.Parameters.Add(statusParameter);
|
---|
97 |
|
---|
98 | SQLiteDataReader r = c.ExecuteReader();
|
---|
99 | while(r.Read()) {
|
---|
100 | AgentEntry agent = new AgentEntry(r.GetInt32(0), r.GetString(1), (AgentStatus)Enum.Parse(typeof(AgentStatus), r.GetString(2)), (byte[])r.GetValue(3));
|
---|
101 | agents.Add(agent);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return agents;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void Update(AgentEntry entry) {
|
---|
108 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
109 | cnn.Open();
|
---|
110 | SQLiteCommand c = cnn.CreateCommand();
|
---|
111 | c.CommandText = "Update Agent set Name=@Name, Status=@Status, RawData=@RawData where id=@Id";
|
---|
112 | DbParameter nameParam = c.CreateParameter();
|
---|
113 | DbParameter statusParam = c.CreateParameter();
|
---|
114 | DbParameter rawDataParam = c.CreateParameter();
|
---|
115 | DbParameter idParam = c.CreateParameter();
|
---|
116 | nameParam.ParameterName = "@Name";
|
---|
117 | nameParam.Value = entry.Name;
|
---|
118 | statusParam.ParameterName = "@Status";
|
---|
119 | statusParam.Value = entry.Status;
|
---|
120 | rawDataParam.ParameterName = "@RawData";
|
---|
121 | rawDataParam.Value = entry.RawData;
|
---|
122 | idParam.ParameterName = "@Id";
|
---|
123 | idParam.Value = entry.Id;
|
---|
124 | c.Parameters.Add(nameParam);
|
---|
125 | c.Parameters.Add(statusParam);
|
---|
126 | c.Parameters.Add(rawDataParam);
|
---|
127 | c.Parameters.Add(idParam);
|
---|
128 | c.ExecuteNonQuery();
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void Update(ResultEntry result) {
|
---|
133 | throw new NotImplementedException();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | } |
---|