using System; using System.Collections; using System.IO; using BCG.Utilities.General; namespace BCG { internal class JoinTable : IBlueRowCollection { protected JoinTableSchema mySchema; protected IDictionary myIdIndex = new Hashtable (); protected JoinDeletionIndex myLeftIndex, myRightIndex; private int myNewRowId; public JoinTable (JoinTableSchema schema) { mySchema = schema; myLeftIndex = new JoinDeletionIndex (mySchema.getRenamedName ("id", true), true); myRightIndex = new JoinDeletionIndex (mySchema.getRenamedName ("id", false), false); } public IBlueRow this [int id] { get { if (myIdIndex[id] == null) throw new BlueDataException ("ID " + id.ToString() + " does not exist in table " + mySchema.tableName); return (IBlueRow)myIdIndex[id]; } } #region JoinTable members public virtual void addRow (IBlueRow leftRow, IBlueRow rightRow, DataPacket dp) { JoinRow newRow = new JoinRow (myNewRowId, leftRow, rightRow, mySchema); myIdIndex[myNewRowId ++] = newRow; ((BlueTable)(dp.addedRows)).addRow (newRow); myLeftIndex.add (newRow); if (rightRow != null) //in case of left joins when right row is null myRightIndex.add (newRow); } public virtual void deleteRow (bool isLeft, IBlueRow deletedRow, DataPacket dp) { JoinDeletionIndex thisIndex = myRightIndex, otherIndex = myLeftIndex; if (isLeft) { thisIndex = myLeftIndex; otherIndex = myRightIndex; } RowList matchingNodes = thisIndex.getMatchingRows (deletedRow["id"]); foreach (RowNode node in matchingNodes) { JoinRow row = (JoinRow)node.row; RowNode correspondingNode = isLeft ? row.rightNode : row.leftNode; if (correspondingNode != null) otherIndex.remove (correspondingNode); //in case of partial left joinss myIdIndex.Remove (row["id"]); ((BlueTable)dp.deletedRows).addRow (row); } thisIndex.removeAll (deletedRow["id"]); } #endregion #region ICollection members public int Count{get{return myIdIndex.Count;}} public bool IsSynchronized {get{return false;}} public object SyncRoot{get{return this;}} public void CopyTo (Array array, int index) {myIdIndex.Values.CopyTo (array, index);} #endregion #region IEnumerable members public IEnumerator GetEnumerator () {return myIdIndex.Values.GetEnumerator ();} #endregion #region IBlueRowCollection members public IBlueTableSchema schema {get{return mySchema;}} public bool containsID (int id) {return myIdIndex[id] != null;} #endregion } }