Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/09/08 10:34:10 (16 years ago)
Author:
gkronber
Message:

worked on #187 - code is still a mess

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CEDMA.DB/Database.cs

    r357 r372  
    1 using System;
     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;
    223using System.Collections.Generic;
    324using System.Linq;
     
    627using HeuristicLab.CEDMA.DB.Interfaces;
    728using System.ServiceModel;
     29using System.Data;
     30using System.Data.SQLite;
     31using System.Data.Common;
    832
    933namespace HeuristicLab.CEDMA.DB {
    10   [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
    11   public class Database : DataContext, IDatabase {
    12     Table<Agent> Agents;
    13 
    14     public Database() : this("c:\\cedma.mdf") { }
    15 
    16     public Database(string connection) : base(connection) {
     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;
    1739    }
    1840
    19     public IList<IAgent> GetAgents() {
    20       List<IAgent> result = new List<IAgent>();
    21       foreach(Agent a in Agents) {
    22         result.Add(a);
     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();
    2355      }
    24       return result;
    2556    }
    2657
    27     public IAgent CreateAgent() {
    28       Agent newAgent = new Agent();
    29       newAgent.Name = DateTime.Now.ToString();
    30       newAgent.Status = AgentStatus.Waiting;
    31       newAgent.RawData = null;
    32       Agents.InsertOnSubmit(newAgent);
    33       this.SubmitChanges();
    34       return newAgent;
     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();
    35134    }
    36135  }
Note: See TracChangeset for help on using the changeset viewer.