protected override DBStructure GetProcedureSchema(IDbConnection cn) { var ds = new DBStructure(); var scn = (SqlConnection)cn; //.GetSchema("Procedures", new string[] { Catalog }); var sb = new System.Text.StringBuilder(); if (scn.ServerVersion.StartsWith("08")) { sb.AppendLine(Properties.Resources.SqlServerGetProcedures2000); } else { sb.AppendLine(Properties.Resources.SqlServerGetProcedures2005); } sb.AppendLine(); sb.AppendLine(Properties.Resources.SqlServerGetParameters); using (SqlCommand cmd = scn.CreateCommand()) { cmd.CommandText = sb.ToString(); cmd.CommandType = CommandType.Text; using (var da = new SqlDataAdapter(cmd)) { da.TableMappings.Add("Table", "INFORMATION_SCHEMA_ROUTINES"); da.TableMappings.Add("Table1", "INFORMATION_SCHEMA_PARAMETERS"); ds.EnforceConstraints = false; da.Fill(ds); } } return(ds); }
public void TestThrowBadIndexesWhenCorruptedRecordMarker2( ) { ITable testTable = m_database.GetTable("People"); IRecord record = testTable.NewRecord(); record.SetValue(1, "zhu"); record.Commit(); m_database.Shutdown(); FileStream file = File.Open("MyPal.People.table.dbUtil", FileMode.Open, FileAccess.Write); file.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[1]; bytes[0] = 255; file.Write(bytes, 0, 1); file.Flush(); file.Close(); DBStructure dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); m_database = dbStruct.OpenDatabase( ); testTable = m_database.GetTable("People"); IResultSet resultSet = testTable.CreateResultSet(1, "zhu"); foreach (IRecord rec in resultSet) { rec.Equals(null); } }
public void SetUp() { try { DBTest.RemoveDBFiles(); DBStructure database = new DBStructure("", "MyPal", DatabaseMode.Create); TableStructure tblPeople = database.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); ColumnStructure colName = tblPeople.CreateColumn("Name", ColumnType.String, false); colName = colName; tblPeople.CreateColumn("Age", ColumnType.Integer, false); tblPeople.CreateColumn("Type", ColumnType.Integer, true); tblPeople.CreateColumn("DateTime", ColumnType.DateTime, true); tblPeople.SetCompoundIndex("Id", "Name"); tblPeople.SetCompoundIndex("Name", "Type"); database.SaveStructure(); database.Shutdown(); database = new DBStructure("", "MyPal"); database.LoadStructure( ); m_database = database.Database; } catch (Exception exc) { Assert.Fail(exc.Message); } }
public void TestBackwardIncompatability( ) { m_database.Shutdown(); FileStream file = File.Open("MyPal.database.struct.dbUtil", FileMode.Open, FileAccess.Write); file.Seek(3, SeekOrigin.Begin); byte[] bytes = new byte[1]; bytes[0] = 24; file.Write(bytes, 0, 1); file.Flush(); file.Close(); DBStructure dbStruct = new DBStructure("", "MyPal"); try { dbStruct.LoadStructure(); } finally { dbStruct.Shutdown(); m_database = null; } }
protected virtual void LoadTablesAndViews() { OpenConnection(); try { List <ITableInfo> listTables = new List <ITableInfo>(); List <IViewInfo> listViews = new List <IViewInfo>(); DBStructure ds = GetTableViewSchema(_cn); foreach (DBStructure.INFORMATION_SCHEMA_TABLESRow row in ds.INFORMATION_SCHEMA_TABLES) { if (row["TABLE_TYPE"].ToString() != "VIEW") { listTables.Add(CreateTableInfo(row)); } else { listViews.Add(CreateViewInfo(row)); } } _Tables = new DataBaseObjectCollection <ITableInfo>(listTables); _Views = new DataBaseObjectCollection <IViewInfo>(listViews); } finally { CloseConnection(); } }
public void CheckSaveLoadIndex( ) { ITable testTable = m_database.GetTable("People"); for (int i = 0; i < 1000; i++) { IRecord record = testTable.NewRecord(); record.SetValue(1, i.ToString()); record.Commit(); Assert.IsTrue(testTable.Count == (i + 1)); } m_database.Shutdown(); DBStructure database = new DBStructure("", "MyPal"); database.LoadStructure(); m_database = database.OpenDatabase( ); testTable = m_database.GetTable("People"); Assert.IsTrue(testTable.Count == 1000); for (int i = 1000; i < 2000; i++) { IRecord record = testTable.NewRecord(); record.SetValue(1, i.ToString()); record.Commit(); Assert.IsTrue(testTable.Count == (i + 1)); } }
public void SetUp() { try { Stream stream = File.Open("resume.txt", FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter text = new BinaryWriter(stream); text.Write("Resume of developer"); text.Close(); stream.Close(); stream = File.Open("resumeTester.txt", FileMode.OpenOrCreate, FileAccess.Write); text = new BinaryWriter(stream); text.Write("Resume of tester"); text.Close(); stream.Close(); DBTest.RemoveDBFiles(); DBStructure database = new DBStructure("", "MyPal"); TableStructure tblPeople = database.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); tblPeople.CreateColumn("Name", ColumnType.String, true); tblPeople.CreateColumn("Resume", ColumnType.BLOB, false); database.SaveStructure(); m_database = database.OpenDatabase( ); m_testTable = m_database.GetTable("People"); } catch (Exception exc) { Assert.Fail(exc.Message); } }
public void SetUp() { try { DBTest.RemoveDBFiles(); DBStructure database = new DBStructure("", "MyPal"); TableStructure tblPeople = database.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); tblPeople.CreateColumn("Name", ColumnType.String, true); database.SaveStructure(); m_database = database.OpenDatabase( ); m_testTable = m_database.GetTable("People"); for (int i = 0; i < 10; i++) { IRecord record = m_testTable.NewRecord(); record.SetValue(Name, (500 - i).ToString()); record.Commit(); Assert.AreEqual((i + 1), m_testTable.Count); } } catch (Exception exc) { Assert.Fail(exc.Message); } }
public void CheckFlushIndexes( ) { ITable testTable = m_database.GetTable("People"); IRecord record = testTable.NewRecord(); record.SetValue(Name, "ZHU1"); record.Commit(); record = testTable.NewRecord(); record.SetValue(Name, "zhu2"); record.Commit(); m_database.Flush(); record = testTable.NewRecord(); record.SetValue(Name, "ZHU3"); record.Commit(); record = testTable.NewRecord(); record.SetValue(Name, "zhu4"); record.Commit(); m_database.Shutdown(); DBStructure dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); m_database = dbStruct.OpenDatabase( ); testTable = m_database.GetTable("People"); Assert.AreEqual(4, testTable.Count); }
public static void StartConnection(string p, DatabaseType databaseType) { Db = new DBConnect(); Db.ConnectionString = p; Ds = new DBStructure(Db.ConnectionString); Ds.Con = Db; dbKind = databaseType; }
public void GetVersionEndBuild( ) { DBTest.CreateDB(); DBStructure database = new DBStructure("", "MyPal"); database.LoadVersionInfo(); Assert.AreEqual(true, database.Build != "undefined"); Assert.AreEqual(true, database.Version > 0); }
public void CheckCreateIndexIfIndexAlreadyExists( ) { DBStructure dbStructure = new DBStructure("", "CheckCreateIndexIfIndexAlreadyExists"); TableStructure tblPeople = dbStructure.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); tblPeople.CreateColumn("Name", ColumnType.String, true); tblPeople.CreateColumn("Age", ColumnType.Integer, false); tblPeople.CreateIndex("Name"); }
public void CheckTryCreateCompoundIndexForNotExistedColumn( ) { DBStructure dbStructure = new DBStructure("", "CheckTryCreateCompoundIndexForNotExistedColumn"); TableStructure tblPeople = dbStructure.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); tblPeople.CreateColumn("Name", ColumnType.String, true); tblPeople.CreateColumn("Age", ColumnType.Integer, true); tblPeople.SetCompoundIndex("Name", "Age1"); }
public void CheckDropIndexIfColumnDoesNotExist( ) { DBStructure dbStructure = new DBStructure("", "CheckDropIndexIfColumnDoesNotExist"); TableStructure tblPeople = dbStructure.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); tblPeople.CreateColumn("Name", ColumnType.String, false); tblPeople.CreateColumn("Age", ColumnType.Integer, false); tblPeople.DropIndex("Name1"); }
public void SelectCompound( ) { DBStructure dbStructure = new DBStructure("", "Test", DatabaseMode.Create); TableStructure tblPeople = dbStructure.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, false); tblPeople.CreateColumn("Name", ColumnType.String, false); tblPeople.CreateColumn("Age", ColumnType.Integer, false); tblPeople.SetCompoundIndex("Id", "Age"); tblPeople.SetCompoundIndex("Name", "Age"); dbStructure.SaveStructure(); dbStructure.Shutdown(); dbStructure = new DBStructure("", "Test", DatabaseMode.Create); dbStructure.LoadStructure( ); IDatabase database = dbStructure.Database; ITable people = database.GetTable("People"); IRecord record = people.NewRecord(); record.SetValue(2, 777); record.Commit(); ICountedResultSet rs = people.CreateModifiableResultSet(0, 0); Assert.AreEqual(1, rs.Count); IRecord rec = rs[0]; Assert.AreEqual(0, rec.GetIntValue(0)); Assert.AreEqual(777, rec.GetIntValue(2)); for (int i = 0; i < 100; i++) { record = people.NewRecord(); record.SetValue(1, "Serg"); record.SetValue(2, i); record.Commit(); } rs.Dispose(); rs = people.CreateResultSet(1, "Serg", 2, 31, false); Assert.AreEqual(1, rs.Count); rs.Dispose(); rs = people.CreateResultSet(1, "Serg", 2, 100, false); Assert.AreEqual(0, rs.Count); rs.Dispose(); rs = people.CreateResultSetForRange(1, "Serg", 2, 10, 50); Assert.AreEqual(41, rs.Count); rs.Dispose(); database.Shutdown(); }
public void TestDateTimeAutoRecovery( ) { ITable testTable = m_database.GetTable("Date"); IRecord record = testTable.NewRecord(); record.SetValue(1, 32); DateTime dt = new DateTime(1972, 7, 26); long ticks = dt.Ticks; ticks = ticks; record.SetValue(2, dt); record.Commit(); m_database.Shutdown(); FileStream file = File.Open("MyPal.Date.table.dbUtil", FileMode.Open, FileAccess.Write); file.Seek(9, SeekOrigin.Begin); byte[] bytes = new byte[8]; for (int i = 0; i < 8; ++i) { bytes[i] = 255; } file.Write(bytes, 0, 8); file.Flush(); file.Close(); DBStructure dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); m_database = dbStruct.OpenDatabase( ); testTable = m_database.GetTable("Date"); IResultSet resultSet = testTable.CreateResultSet(1, 32); testTable.Defragment(); m_database.Shutdown(); dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); dbStruct.RebuildIndexes(true); m_database = dbStruct.OpenDatabase( ); testTable = m_database.GetTable("Date"); Assert.AreEqual(1, testTable.Count); resultSet = testTable.CreateResultSet(1, 32, 2, DateTime.MinValue, true); int count = 0; foreach (IRecord rec in resultSet) { ++count; Assert.AreEqual(DateTime.MinValue, rec.GetDateTimeValue(2)); } Assert.AreEqual(1, count); }
public void TestCorruptedRecordMarker( ) { ITable testTable = m_database.GetTable("People"); IRecord record = testTable.NewRecord(); record.SetValue(1, "zhu"); record.Commit(); record = testTable.NewRecord(); record.SetValue(1, "zhu"); record.Commit(); Assert.AreEqual(2, testTable.Count); m_database.Shutdown(); FileStream file = File.Open("MyPal.People.table.dbUtil", FileMode.Open, FileAccess.Write); file.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[1]; bytes[0] = 255; file.Write(bytes, 0, 1); file.Flush(); file.Close(); DBStructure dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); m_database = dbStruct.OpenDatabase( ); testTable = m_database.GetTable("People"); IResultSet resultSet = testTable.CreateResultSet(1, "zhu"); bool wasException = false; try { foreach (IRecord rec in resultSet) { rec.Equals(null); } } catch (BadIndexesException) { wasException = true; } Assert.AreEqual(true, wasException); m_database.Shutdown(); dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); dbStruct.RebuildIndexes(true); m_database = dbStruct.OpenDatabase( ); testTable = m_database.GetTable("People"); Assert.AreEqual(1, testTable.Count); }
public void SetUp() { try { DBTest.RemoveDBFiles(); DBTest.CreateDB(); DBStructure dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); m_database = dbStruct.OpenDatabase( ); } catch (Exception exc) { Assert.Fail(exc.Message); } }
public static void CreateDB() { DBStructure database = new DBStructure("", "MyPal", DatabaseMode.Create); database.Build = "Build"; TableStructure tblPeople = database.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); ColumnStructure colName = tblPeople.CreateColumn("Name", ColumnType.String, true); colName = colName; ColumnStructure colAge = tblPeople.CreateColumn("Age", ColumnType.Integer, false); colAge = colAge; tblPeople.CreateColumn("Birthday", ColumnType.DateTime, false); tblPeople.SetCompoundIndex("Name", "Age"); TableStructure tblBooks = database.CreateTable("Books"); tblBooks.CreateColumn("Id", ColumnType.Integer, true); ColumnStructure colBookName = tblBooks.CreateColumn("Name", ColumnType.String, true); colBookName = colBookName; ColumnStructure colPrice = tblBooks.CreateColumn("Price", ColumnType.Integer, false); colPrice = colPrice; TableStructure tblDate = database.CreateTable("Date"); tblDate.CreateColumn("Id", ColumnType.Integer, true); tblDate.CreateColumn("Age", ColumnType.Integer, false); tblDate.CreateColumn("Birthday", ColumnType.DateTime, true); tblDate.SetCompoundIndexWithValue("Age", "Birthday", "Id"); TableStructure tblEndMarker = database.CreateTable("EndMarker"); tblEndMarker.CreateColumn("Id", ColumnType.Integer, true); tblEndMarker.CreateColumn("Age", ColumnType.Integer, false); tblEndMarker.CreateColumn("Name", ColumnType.String, true); tblEndMarker.SetCompoundIndex("Name", "Age"); database.SaveStructure(); database.Shutdown(); }
public void TableNameMustBeUnique( ) { DBStructure database = new DBStructure(".", "Test"); database.CreateTable("People"); try { database.CreateTable("People"); } catch (TableAlreadyExistsException) { //it's normal return; } Assert.Fail("'People' table already exists but 'TableAlreadyExists' was not thrown"); }
public void SmokeTestForSetCacheSize( ) { DBStructure database = new DBStructure("", "MyPal"); TableStructure tblPeople = database.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); tblPeople.CreateColumn("Name", ColumnType.String, true); database.SaveStructure(); IDatabase m_database = database.OpenDatabase( ); ITable testTable = m_database.GetTable("People"); testTable = testTable; m_database.Shutdown(); }
public void SetUp() { DBTest.RemoveDBFiles(); DBStructure database = new DBStructure("", "MyPal", DatabaseMode.Create); TableStructure tblPeople = database.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); tblPeople.CreateColumn("Name", ColumnType.String, false); tblPeople.CreateColumn("Age", ColumnType.Integer, false); tblPeople.SetCompoundIndex("Name", "Age"); database.SaveStructure(); database.Shutdown(); database = new DBStructure("", "MyPal"); database.LoadStructure( ); m_database = database.Database; }
public void ColumnNameMustBeUnique( ) { DBStructure database = new DBStructure(".", "Test"); TableStructure table = database.CreateTable("People"); table.CreateColumn("Name", ColumnType.String, true); try { table.CreateColumn("Name", ColumnType.String, true); } catch (ColumnAlreadyExistsException) { //it's normal return; } Assert.Fail("'Name' column already exists but 'ColumnAlreadyExists' was not thrown"); }
public void CheckNotReusingIDs( ) { ITable testTable = m_database.GetTable("People"); IRecord record = testTable.NewRecord(); record.Commit(); int id = record.GetID(); Assert.AreEqual(0, id); record = testTable.NewRecord(); record.Commit(); id = record.GetID(); Assert.AreEqual(1, id); ICountedResultSet resultSet = testTable.CreateModifiableResultSet(Id, 1); Assert.AreEqual(1, resultSet.Count); record = resultSet[0]; record.Delete(); resultSet.Dispose(); resultSet = testTable.CreateModifiableResultSet(Id, 1); Assert.AreEqual(0, resultSet.Count); record = testTable.NewRecord(); record.Commit(); id = record.GetID(); Assert.AreEqual(2, id); m_database.Shutdown(); DBStructure dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); m_database = dbStruct.OpenDatabase( ); testTable = m_database.GetTable("People"); resultSet.Dispose(); resultSet = testTable.CreateModifiableResultSet(Id, 0); Assert.AreEqual(1, resultSet.Count); resultSet.Dispose(); resultSet = testTable.CreateModifiableResultSet(Id, 1); Assert.AreEqual(0, resultSet.Count); resultSet.Dispose(); resultSet = testTable.CreateModifiableResultSet(Id, 2); Assert.AreEqual(1, resultSet.Count); record = testTable.NewRecord(); record.Commit(); id = record.GetID(); Assert.AreEqual(3, id); resultSet.Dispose(); }
public void SetUp() { try { DBTest.RemoveDBFiles(); DBStructure database = new DBStructure("", "MyPal", DatabaseMode.Create); TableStructure tblPeople = database.CreateTable("People"); tblPeople.CreateColumn("Id", ColumnType.Integer, true); ColumnStructure colName = tblPeople.CreateColumn("Name", ColumnType.String, false); colName = colName; tblPeople.CreateColumn("Age", ColumnType.Integer, false); tblPeople.CreateColumn("Type", ColumnType.Integer, true); tblPeople.CreateColumn("DateTime", ColumnType.DateTime, true); tblPeople.SetCompoundIndex("Id", "Name"); tblPeople.SetCompoundIndex("Name", "Type"); TableStructure tblTest = database.CreateTable("Test"); tblTest.CreateColumn("Id", ColumnType.Integer, false); tblTest.CreateColumn("Date", ColumnType.DateTime, false); tblTest.SetCompoundIndex("Id", "Date"); database.SaveStructure(); database.Shutdown(); database = new DBStructure("", "MyPal", DatabaseMode.Create); database.LoadStructure( ); _database = database.Database; _peopleTable = _database.GetTable("People"); _testTable = _database.GetTable("Test"); //m_testTable.InputMode = InputMode.Batch; for (int i = 0; i < 10; i++) { IRecord record = _peopleTable.NewRecord(); Assert.AreEqual(i, record.GetID(), "Id is wrong"); record.SetValue(1, (500 - i).ToString()); record.Commit(); Assert.AreEqual((i + 1), _peopleTable.Count, "Count is wrong"); } } catch (Exception exc) { Assert.Fail(exc.Message); } }
private bool AllTablesExist(DBStructure structure) { foreach (string tableName in new string[] { "PropTypes", "ResourceTypes", "IntProps", "StringProps", "LongStringProps", "StringListProps", "DateProps", "BlobProps", "DoubleProps", "Resources", "Links" }) { try { structure.GetTable(tableName); } catch (TableDoesNotExistException) { return(false); } } return(true); }
public void CheckUnlockDatabaseWhenSaveStructureTest( ) { DBStructure dbStruct = new DBStructure("", "Test"); TableStructure tableStruct = dbStruct.CreateTable("SomeTable"); tableStruct.CreateColumn("SomeString", ColumnType.String, false); tableStruct.CreateColumn("SomeInt", ColumnType.Integer, false); tableStruct.CreateColumn("SomeDate", ColumnType.DateTime, false); dbStruct.SaveStructure(); dbStruct = new DBStructure("", "Test"); dbStruct.LoadStructure(); IDatabase database = dbStruct.OpenDatabase( ); database.Shutdown(); }
public void CheckCompoundIndexKeyFactory( ) { for (int i = 0; i < 13000; i++) { IRecord record = _testTable.NewRecord(); record.SetValue(1, DateTime.Today.AddDays(i)); record.Commit(); } _database.Shutdown(); DBStructure dbStructure = new DBStructure("", "MyPal"); _database = dbStructure.OpenDatabase(); int i1 = ("1308554180").GetHashCode(); i1 = i1; int i2 = ("2117097963").GetHashCode(); i2 = i2; }
public void TestBackwardIncompatabilityCheckVersionAndEndMark( ) { ITable testTable = m_database.GetTable("People"); IRecord record = testTable.NewRecord(); record.SetValue(1, "zhu"); record.Commit(); m_database.Shutdown(); FileStream file = File.Open("MyPal.database.struct.dbUtil", FileMode.Open, FileAccess.Write); file.Seek(3, SeekOrigin.Begin); byte[] bytes = new byte[1]; bytes[0] = 21; file.Write(bytes, 0, 1); file.Flush(); file.Close(); DBStructure dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); Assert.AreEqual(false, dbStruct.IsDatabaseCorrect()); if (!dbStruct.IsDatabaseCorrect()) { dbStruct.RebuildIndexes(); } m_database = dbStruct.OpenDatabase(); testTable = m_database.GetTable("People"); IResultSet resultSet = testTable.CreateResultSet(0, 0); Assert.AreEqual(1, testTable.Count); int count = 0; foreach (IRecord rec in resultSet) { ++count; Assert.AreEqual("zhu", rec.GetStringValue(1)); } Assert.AreEqual(1, count); }
public void SmokeTestLoadDatabase( ) { DBTest.CreateDB(); DBStructure dbStruct = new DBStructure("", "MyPal"); dbStruct.LoadStructure(); IDatabase database = dbStruct.OpenDatabase( ); ITable people = database.GetTable("People"); IRecord record = people.NewRecord(); record.SetValue(Name, "zhu"); record.SetValue(Age, 30); ITable books = database.GetTable("Books"); record = books.NewRecord(); record.SetValue(Name, "Algorithm"); record.SetValue(Price, 1000); database.Shutdown(); }
public long[] Update(Structure[] structures) { Dictionary<DBStructure, int> mapNewObjToIndex = new Dictionary<DBStructure, int>(structures.Length); //Stores the ID of each object manipulated for the return value long[] listID = new long[structures.Length]; try { for (int iObj = 0; iObj < structures.Length; iObj++) { Structure t = structures[iObj]; switch (t.DBAction) { case DBACTION.INSERT: DBStructure newRow = new DBStructure(); t.Sync(newRow); db.DBStructures.InsertOnSubmit(newRow); mapNewObjToIndex.Add(newRow, iObj); break; case DBACTION.UPDATE: DBStructure updateRow; try { updateRow = (from u in db.DBStructures where u.ID == t.ID select u).Single(); } catch (System.ArgumentNullException e) { Debug.WriteLine("Could not find structuretype to update: " + t.ID.ToString()); break; } catch (System.InvalidOperationException e) { Debug.WriteLine("Could not find structuretype to update: " + t.ID.ToString()); break; } t.Sync(updateRow); listID[iObj] = updateRow.ID; // db.DBStructureTypes.(updateType); break; case DBACTION.DELETE: DBStructure deleteRow = new DBStructure(); try { deleteRow = (from u in db.DBStructures where u.ID == t.ID select u).Single(); } catch (System.ArgumentNullException e) { Debug.WriteLine("Could not find structuretype to update: " + t.ID.ToString()); break; } catch (System.InvalidOperationException e) { Debug.WriteLine("Could not find structuretype to update: " + t.ID.ToString()); break; } t.Sync(deleteRow); deleteRow.ID = t.ID; listID[iObj] = deleteRow.ID; db.DBStructures.DeleteOnSubmit(deleteRow); //Remove any links that exist before calling delete foreach (DBStructureLink link in deleteRow.IsSourceOf) { db.DBStructureLinks.DeleteOnSubmit(link); } foreach (DBStructureLink link in deleteRow.IsTargetOf) { db.DBStructureLinks.DeleteOnSubmit(link); } break; } } } catch (Exception e) { Debug.WriteLine(e.ToString()); throw e; } db.SubmitChanges(); //Recover the ID's for new objects foreach (DBStructure newObj in mapNewObjToIndex.Keys) { int iIndex = mapNewObjToIndex[newObj]; listID[iIndex] = newObj.ID; } return listID; }
public CreateStructureRetval CreateStructure(Structure structure, Location location) { try { DBStructure DBStruct = new DBStructure(); structure.Sync(DBStruct); db.DBStructures.InsertOnSubmit(DBStruct); DBLocation DBLoc = new DBLocation(); location.Sync(DBLoc); DBLoc.DBStructure = DBStruct; db.DBLocations.InsertOnSubmit(DBLoc); db.SubmitChanges(); //Return new ID's to the caller CreateStructureRetval retval = new CreateStructureRetval(new Structure(DBStruct, false), new Location(DBLoc)); return retval; } finally { if (db != null) db.Connection.Close(); } return null; }
partial void InsertDBStructure(DBStructure instance);
partial void UpdateDBStructure(DBStructure instance);
partial void DeleteDBStructure(DBStructure instance);
static void CheckOverlap(DBStructureType t, DBStructure structA, DBStructure structB) { IQueryable<DBLocation> LocsA = from l in db.DBLocations where l.ParentID == structA.ID select l; IQueryable<DBLocation> LocsB = from l in db.DBLocations where l.ParentID == structB.ID select l; List<DBLocation> listA = new List<DBLocation>(); List<DBLocation> listB = new List<DBLocation>(); List<GridVector3> pointsA = new List<GridVector3>(); List<GridVector3> pointsB = new List<GridVector3>(); //Scale values double XYScale = 2.18; double ZScale = 90; foreach(DBLocation l in LocsA) { listA.Add(l); pointsA.Add( new GridVector3(l.VolumeX * XYScale, l.VolumeY * XYScale, l.Z * ZScale) ); } foreach (DBLocation l in LocsB) { listB.Add(l); pointsB.Add(new GridVector3(l.VolumeX * XYScale, l.VolumeY * XYScale, l.Z * ZScale)); } if (listB.Count == 0 || listA.Count == 0) { return; } //Find the nearest point int iA; int iB; double distance; GridVector3.Nearest(pointsA.ToArray(), pointsB.ToArray(), out iA, out iB, out distance); if(distance < listA[iA].Radius + listB[iB].Radius) { Console.WriteLine("[" + structA.ParentID.ToString() + "] " + listA[iA].ParentID.ToString() + " <-> " + listB[iB].ParentID.ToString() + " distance " + distance.ToString()); long newParentID = structB.ID; foreach (DBLocation l in LocsA) { l.ParentID = newParentID; } IQueryable<DBLocationLink> locLinks = from l in db.DBLocationLinks where (l.LinkedFrom == listA[iA].ID && l.LinkedTo == listB[iB].ID) || (l.LinkedFrom == listB[iB].ID && l.LinkedTo == listA[iA].ID) select l; bool ExistingLinkFound = false; foreach (DBLocationLink link in locLinks) { ExistingLinkFound = true; break; } if (ExistingLinkFound == false) { DBLocationLink link = new DBLocationLink(); link.LinkedFrom = listA[iA].ID; link.LinkedTo = listB[iB].ID; link.Username = "******"; db.DBLocationLinks.InsertOnSubmit(link); } //Update structureLinks IQueryable<DBStructureLink> linkFrom = from l in db.DBStructureLinks where l.SourceID == structA.ID select l; foreach(DBStructureLink l in linkFrom) { DBStructureLink newLink = new DBStructureLink(); newLink.SourceID = newParentID; newLink.TargetID = l.TargetID; newLink.Username = l.Username; newLink.Tags = l.Tags; newLink.Bidirectional = l.Bidirectional; db.DBStructureLinks.DeleteOnSubmit(l); DBStructureLink existingLink = (from el in db.DBStructureLinks where el.SourceID == newLink.SourceID && el.TargetID == newLink.TargetID select el).SingleOrDefault<DBStructureLink>(); if(existingLink == null) db.DBStructureLinks.InsertOnSubmit(newLink); } IQueryable<DBStructureLink> linkTo = from l in db.DBStructureLinks where l.TargetID == structA.ID select l; foreach(DBStructureLink l in linkTo) { DBStructureLink newLink = new DBStructureLink(); newLink.SourceID = l.SourceID; newLink.TargetID = newParentID; newLink.Username = l.Username; newLink.Bidirectional = l.Bidirectional; newLink.Tags = l.Tags; db.DBStructureLinks.DeleteOnSubmit(l); DBStructureLink existingLink = (from el in db.DBStructureLinks where el.TargetID == newLink.TargetID && el.SourceID == newLink.SourceID select el).SingleOrDefault<DBStructureLink>(); if (existingLink == null) db.DBStructureLinks.InsertOnSubmit(newLink); } db.DBStructures.DeleteOnSubmit(structA); db.SubmitChanges(); } }