using bts;
using Bts.DataStreamCore;
using System;
using System.Collections;
namespace BCG
{
internal class JoinRow : IBlueRow
{
RowNode? myLeftNode;
RowNode? myRightNode;
protected IBlueRow myLeftRow;
protected IBlueRow? myRightRow;
protected int myId;
protected JoinTableSchema mySchema;
private object?[] myValues;
#region Constructor(s)
///
/// Constructs a row, assigning it the same ID as that of the left row.
///
/// left row of the join
/// right row of the join
/// schema of the joined row
public JoinRow (IBlueRow leftRow, IBlueRow? rightRow, JoinTableSchema schema)
: this(leftRow.Id(), leftRow, rightRow, schema) { }
///
/// Constructs a row assigning it the ID provioded as the first argument
///
/// id of the joined row
/// left row of the join
/// right row of the join
/// schema of the joined row
public JoinRow (int id, IBlueRow leftRow, IBlueRow? rightRow, JoinTableSchema schema) {
myId = id;
myLeftRow = leftRow;
myRightRow = rightRow;
mySchema = schema;
myValues = new object [schema.columnNames.Length];
}
#endregion
#region IBlueRow members
public IBlueTableSchema schema {get{return mySchema;}}
#endregion
public bool isPartialJoin{get{return myRightRow == null;}}
public RowNode? leftNode
{
get{return myLeftNode;}
set{myLeftNode = value;}
}
public RowNode? rightNode
{
get{return myRightNode;}
set{myRightNode = value;}
}
#region ICollection members
public int Count{get{return mySchema.columnNames.Length;}}
public bool IsSynchronized {get{return false;}}
public object SyncRoot{get{return this;}}
public void CopyTo (Array array, int index)
{
int i = 0;
foreach (object o in this as IEnumerable)
array.SetValue (o, i++);
}
#endregion
#region IDictionary members
public bool IsFixedSize {get{return true;}}
public bool IsReadOnly {get{return true;}}
///
/// The key can either be the string corrsponding to the column name or
/// the order of the column
///
public virtual object? this[object key]
{
get
{
if (key is string)
return this[(string)key];
return this[(int)key];
}
set {throw new NotSupportedException ("The IBlueRow is read only");}
}
public object? this[int index]
{
get
{
if (index == mySchema.columnNames.Length - 1)
return myId;
else if (index < myLeftRow.Count)
{
if (myValues[index] == null)
myValues[index] = myLeftRow[index];
return myValues[index];
}
else if (index >= myLeftRow.Count && index < mySchema.columnNames.Length - 1)
{
if (myValues[index] == null)
myValues[index] = (myRightRow == null) ? DBNull.Value : myRightRow[index - myLeftRow.Count];
return myValues[index];
}
Log.assert(false, "Incorrect index in JoinRow");
return null;
}
}
public object? this[string colName] =>
this[mySchema[colName].colNo];
public ICollection Keys{get{return mySchema.columnNames;}}
public ICollection Values
{
get
{
object[] retVal = new object[mySchema.columnNames.Length];
CopyTo (retVal, 0);
return retVal;
}
}
public void Add (object key, object? value) =>
throw new NotSupportedException ("The IBlueRow is readonly");
public void Clear () {throw new NotSupportedException ("The IBlueRow is readonly");}
public bool Contains (object key){return mySchema[(string)key] != null;}
///
/// Note, the default enumerator is the IDictionary's enumerator, so if user needs to
/// iterate through the values, typecast this into an IEnumerable
///
///
public IDictionaryEnumerator GetEnumerator() {return new BlueRowDictionaryEnumerator(this);}
IEnumerator IEnumerable.GetEnumerator () {return new BlueRowEnumerator(this);}
public void Remove (object key) {throw new NotSupportedException ("The IBlueRow is readonly");}
#endregion
}
}