Changeset 543 for trunk/sources/HeuristicLab.CEDMA.DB
- Timestamp:
- 09/10/08 10:29:28 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.CEDMA.DB/Database.cs
r541 r543 66 66 using(DbCommand cmd = cnn.CreateCommand()) { 67 67 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 (id integer primary key autoincrement, subject GUID, predicate GUID, property GUID, CreationTime DateTime)";78 68 cmd.Transaction = t; 79 69 cmd.ExecuteNonQuery(); … … 629 619 } 630 620 #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.ToString();685 c.Parameters.Add(predParam);686 DbParameter propertyParam = c.CreateParameter();687 propertyParam.ParameterName = "@Property";688 propertyParam.Value = property.ToString();689 c.Parameters.Add(propertyParam);690 using(DbDataReader r = c.ExecuteReader()) {691 while(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 }700 } finally {701 rwLock.ExitReadLock();702 }703 return items;704 }705 706 public void LinkItems(ItemEntry subject, ItemEntry predicate, ItemEntry property) {707 rwLock.EnterWriteLock();708 try {709 using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {710 cnn.Open();711 using(SQLiteTransaction t = cnn.BeginTransaction()) {712 ItemEntry[] items = new ItemEntry[] { subject, predicate, property };713 foreach(ItemEntry item in items) {714 long n = 0;715 using(SQLiteCommand c = cnn.CreateCommand()) {716 c.Transaction = t;717 c.CommandText = "select count(guid) from item where guid=@guid";718 DbParameter guidParam = c.CreateParameter();719 guidParam.ParameterName = "@guid";720 guidParam.Value = item.Guid.ToString();721 c.Parameters.Add(guidParam);722 n = (long)c.ExecuteScalar();723 }724 if(n == 0) using(SQLiteCommand c = cnn.CreateCommand()) {725 c.Transaction = t;726 c.CommandText = "insert into item (Guid, RawData) values (@Guid, @RawData)";727 DbParameter guidParam = c.CreateParameter();728 guidParam.ParameterName = "@Guid";729 guidParam.Value = item.Guid.ToString();730 c.Parameters.Add(guidParam);731 DbParameter rawDataParam = c.CreateParameter();732 rawDataParam.ParameterName = "@RawData";733 rawDataParam.Value = item.RawData;734 c.Parameters.Add(rawDataParam);735 c.ExecuteNonQuery();736 }737 }738 using(SQLiteCommand c = cnn.CreateCommand()) {739 c.Transaction = t;740 c.CommandText = "insert into Statement (Subject, Predicate, Property, CreationTime) values (@Subject, @Predicate, @Property, @CreationTime)";741 DbParameter subjectParam = c.CreateParameter();742 subjectParam.ParameterName = "@Subject";743 subjectParam.Value = subject.Guid.ToString();744 c.Parameters.Add(subjectParam);745 DbParameter predParam = c.CreateParameter();746 predParam.ParameterName = "@Predicate";747 predParam.Value = predicate.Guid.ToString();748 c.Parameters.Add(predParam);749 DbParameter propertyParam = c.CreateParameter();750 propertyParam.ParameterName = "@Property";751 propertyParam.Value = property.Guid.ToString();752 c.Parameters.Add(propertyParam);753 DbParameter timeParam = c.CreateParameter();754 timeParam.ParameterName = "@CreationTime";755 timeParam.Value = DateTime.Now;756 c.Parameters.Add(timeParam);757 c.ExecuteNonQuery();758 }759 t.Commit();760 }761 }762 } finally {763 rwLock.ExitWriteLock();764 }765 }766 621 } 767 622 }
Note: See TracChangeset
for help on using the changeset viewer.