Free cookie consent management tool by TermsFeed Policy Generator

Changeset 537


Ignore:
Timestamp:
09/03/08 20:32:02 (16 years ago)
Author:
gkronber
Message:

added tables for items and statements, a class for items and methods to create items and statements and to retrieve items. ticket #261 (Items are stored multiple times in the result entries in the CEDMA DB)

Location:
trunk/sources
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CEDMA.DB.Interfaces/HeuristicLab.CEDMA.DB.Interfaces.csproj

    r416 r537  
    44    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    55    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    6     <ProductVersion>9.0.21022</ProductVersion>
     6    <ProductVersion>9.0.30729</ProductVersion>
    77    <SchemaVersion>2.0</SchemaVersion>
    88    <ProjectGuid>{4F9BB789-D561-436B-B226-2BF44B7D0804}</ProjectGuid>
     
    5555  <ItemGroup>
    5656    <Compile Include="AgentEntry.cs" />
     57    <Compile Include="ItemEntry.cs" />
    5758    <Compile Include="OperatorEntry.cs" />
    5859    <Compile Include="ProcessStatus.cs" />
  • trunk/sources/HeuristicLab.CEDMA.DB.Interfaces/IDatabase.cs

    r503 r537  
    2626using System.ServiceModel;
    2727using System.Data;
     28using System.Linq.Expressions;
    2829
    2930namespace HeuristicLab.CEDMA.DB.Interfaces {
     
    8081
    8182    [OperationContract]
    82     OperatorEntry GetOperator(long agentId);
     83    OperatorEntry GetOperator(long id);
    8384
     85    [OperationContract]
     86    IList<ItemEntry> GetOntologyItems();
     87
     88    [OperationContract]
     89    IList<ItemEntry> GetItems(Guid predicate, Guid property);
     90
     91    [OperationContract]
     92    void LinkItems(ItemEntry subject, ItemEntry predicate, ItemEntry property);
    8493  }
    8594}
  • trunk/sources/HeuristicLab.CEDMA.DB/Database.cs

    r503 r537  
    6666            using(DbCommand cmd = cnn.CreateCommand()) {
    6767              cmd.CommandText = "CREATE TABLE Operator (ID integer primary key autoincrement, Name text, RawData Blob)";
     68              cmd.Transaction = t;
     69              cmd.ExecuteNonQuery();
     70            }
     71            using(DbCommand cmd = cnn.CreateCommand()) {
     72              cmd.CommandText = "CREATE TABLE Item (guid GUID primary key, RawData Blob)";
     73              cmd.Transaction = t;
     74              cmd.ExecuteNonQuery();
     75            }
     76            using(DbCommand cmd = cnn.CreateCommand()) {
     77              cmd.CommandText = "CREATE TABLE Statement (subject GUID, predicate GUID, property GUID)";
    6878              cmd.Transaction = t;
    6979              cmd.ExecuteNonQuery();
     
    619629    }
    620630    #endregion
     631
     632    public IList<ItemEntry> GetOntologyItems() {
     633      List<ItemEntry> ontologies = new List<ItemEntry>();
     634      List<ItemEntry> items = new List<ItemEntry>();
     635      rwLock.EnterReadLock();
     636      try {
     637        using(DbConnection cnn = new SQLiteConnection(connectionString)) {
     638          cnn.Open();
     639          using(DbCommand c = cnn.CreateCommand()) {
     640            c.CommandText = "Select guid, rawdata from Item,(select subject from Statement where subject=property) where subject=guid";
     641            using(DbDataReader r = c.ExecuteReader()) {
     642              while(r.Read()) {
     643                ItemEntry ontology = new ItemEntry();
     644                ontology.Guid = r.GetGuid(0);
     645                ontology.RawData = (byte[])r.GetValue(1);
     646                ontologies.Add(ontology);
     647              }
     648            }
     649          }
     650          foreach(ItemEntry ontology in ontologies) {
     651            using(DbCommand c = cnn.CreateCommand()) {
     652              c.CommandText = "Select guid, rawdata from Item,(select property from Statement where subject=@ontology) where guid=property";
     653              DbParameter ontologyParam = c.CreateParameter();
     654              ontologyParam.ParameterName = "@ontology";
     655              ontologyParam.Value = ontology.Guid.ToString();
     656              c.Parameters.Add(ontologyParam);
     657              using(DbDataReader r = c.ExecuteReader()) {
     658                while(r.Read()) {
     659                  ItemEntry item = new ItemEntry();
     660                  item.Guid = r.GetGuid(0);
     661                  item.RawData = (byte[])r.GetValue(1);
     662                  items.Add(item);
     663                }
     664              }
     665            }
     666          }
     667        }
     668      } finally {
     669        rwLock.ExitReadLock();
     670      }
     671      return items;
     672    }
     673
     674    public IList<ItemEntry> GetItems(Guid predicate, Guid property) {
     675      List<ItemEntry> items = new List<ItemEntry>();
     676      rwLock.EnterReadLock();
     677      try {
     678        using(DbConnection cnn = new SQLiteConnection(connectionString)) {
     679          cnn.Open();
     680          using(DbCommand c = cnn.CreateCommand()) {
     681            c.CommandText = "Select guid, rawdata from Item,(select subject from Statement where predicate=@Predicate and property=@Property) where subject=guid";
     682            DbParameter predParam = c.CreateParameter();
     683            predParam.ParameterName = "@Predicate";
     684            predParam.Value = predicate;
     685            c.Parameters.Add(predParam);
     686            DbParameter propertyParam = c.CreateParameter();
     687            propertyParam.ParameterName = "@Property";
     688            propertyParam.Value = property;
     689            c.Parameters.Add(propertyParam);
     690            using(DbDataReader r = c.ExecuteReader()) {
     691              r.Read();
     692              ItemEntry item = new ItemEntry();
     693              item.Guid = r.GetGuid(0);
     694              item.RawData = (byte[])r.GetValue(1);
     695              items.Add(item);
     696            }
     697          }
     698        }
     699      } finally {
     700        rwLock.ExitReadLock();
     701      }
     702      return items;
     703    }
     704
     705    public void LinkItems(ItemEntry subject, ItemEntry predicate, ItemEntry property) {
     706      rwLock.EnterWriteLock();
     707      try {
     708        using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
     709          cnn.Open();
     710          using(SQLiteTransaction t = cnn.BeginTransaction()) {
     711            ItemEntry[] items = new ItemEntry[] { subject, predicate, property };
     712            foreach(ItemEntry item in items) {
     713              long n = 0;
     714              using(SQLiteCommand c = cnn.CreateCommand()) {
     715                c.Transaction = t;
     716                c.CommandText = "select count(guid) from item where guid=@guid";
     717                DbParameter guidParam = c.CreateParameter();
     718                guidParam.ParameterName = "@guid";
     719                guidParam.Value = item.Guid.ToString();
     720                c.Parameters.Add(guidParam);
     721                n = (long)c.ExecuteScalar();
     722              }
     723              if(n == 0) using(SQLiteCommand c = cnn.CreateCommand()) {
     724                  c.Transaction = t;
     725                  c.CommandText = "insert into item (Guid, RawData) values (@Guid, @RawData)";
     726                  DbParameter guidParam = c.CreateParameter();
     727                  guidParam.ParameterName = "@Guid";
     728                  guidParam.Value = item.Guid.ToString();
     729                  c.Parameters.Add(guidParam);
     730                  DbParameter rawDataParam = c.CreateParameter();
     731                  rawDataParam.ParameterName = "@RawData";
     732                  rawDataParam.Value = item.RawData;
     733                  c.Parameters.Add(rawDataParam);
     734                  c.ExecuteNonQuery();
     735                }
     736            }
     737            using(SQLiteCommand c = cnn.CreateCommand()) {
     738              c.Transaction = t;
     739              c.CommandText = "insert into Statement (Subject, Predicate, Property) values (@Subject, @Predicate, @Property)";
     740              DbParameter subjectParam = c.CreateParameter();
     741              subjectParam.ParameterName = "@Subject";
     742              subjectParam.Value = subject.Guid.ToString();
     743              c.Parameters.Add(subjectParam);
     744              DbParameter predParam = c.CreateParameter();
     745              predParam.ParameterName = "@Predicate";
     746              predParam.Value = predicate.Guid.ToString();
     747              c.Parameters.Add(predParam);
     748              DbParameter propertyParam = c.CreateParameter();
     749              propertyParam.ParameterName = "@Property";
     750              propertyParam.Value = property.Guid.ToString();
     751              c.Parameters.Add(propertyParam);
     752              c.ExecuteNonQuery();
     753            }
     754            t.Commit();
     755          }
     756        }
     757      } finally {
     758        rwLock.ExitWriteLock();
     759      }
     760    }
    621761  }
    622762}
Note: See TracChangeset for help on using the changeset viewer.