using System; using System.Collections; using BCG.Utilities; using bts; namespace BCG { /// /// Represents the schema of the Join Table. This schema is formed by combining the respective /// schemas of the operands. /// public class JoinTableSchema : BlueTableSchema { private string myLeftJc = "", myRightJc = "", myLeftId = "", myRightId = ""; private IDictionary myLeftNameToAlias = new Hashtable (); private IDictionary myRightNameToAlias = new Hashtable (); private IDictionary myAliasToLeftName = new Hashtable(), myAliasToRightName = new Hashtable(); /// /// Constructs the schema of the Jointable with the schema of the left and right tables. Incase of /// a name being there in both the shemas it prefixes the name with "L." or "R." accordingly. Column "id" /// is the last column in the schema /// /// /// /// /// public JoinTableSchema (IBlueTableSchema leftSchema, string leftJc, IBlueTableSchema rightSchema, string rightJc) { myIndexByColOrder = new ColumnInfo[leftSchema.columnNames.Length + rightSchema.columnNames.Length + 1]; myTableName = leftSchema.tableName + " J " + rightSchema.tableName; ColumnInfo[] cis = leftSchema.columnInfo; string assignedColName; bool renamed = false; foreach (ColumnInfo ci in cis) { assignedColName = ci.colName; while (rightSchema[assignedColName] != null) { assignedColName = "L." + assignedColName; renamed = true; while (leftSchema[assignedColName] != null) assignedColName = "L." + assignedColName; } ColumnInfo newCi = new ColumnInfo (assignedColName, ci.colNo, ci.colType); Log.assert(myIndexByColName[newCi.colName] == null); Log.assert(myIndexByColOrder[newCi.colNo] == null); if (renamed) Log.assert(leftSchema[assignedColName] == null); Log.assert(rightSchema[assignedColName] == null); myIndexByColName [newCi.colName] = newCi; myIndexByColOrder [newCi.colNo] = newCi; myLeftNameToAlias[ci.colName] = newCi.colName; myLeftJc = ci.colName == leftJc ? assignedColName : myLeftJc; myLeftId = ci.colName == "id" ? assignedColName : myLeftId; renamed = false; } Log.assert(myLeftJc != "" && myLeftId != ""); int n = leftSchema.columnNames.Length; cis = rightSchema.columnInfo; renamed = false; foreach (ColumnInfo ci in cis) { assignedColName = ci.colName; while (leftSchema[assignedColName] != null) { assignedColName = "R." + assignedColName; renamed = true; while(rightSchema[assignedColName] != null) assignedColName = "R." + assignedColName; } ColumnInfo newCi = new ColumnInfo (assignedColName, ci.colNo + n, ci.colType); Log.assert(myIndexByColName[assignedColName] == null); Log.assert(myIndexByColOrder[newCi.colNo] == null); Log.assert(leftSchema[assignedColName] == null); if (renamed) Log.assert(rightSchema[assignedColName] == null); myIndexByColName [newCi.colName] = newCi; myIndexByColOrder [newCi.colNo] = newCi; myRightNameToAlias[ci.colName] = newCi.colName; myRightJc = ci.colName == rightJc ? assignedColName : myRightJc; myRightId = ci.colName == "id" ? assignedColName : myLeftJc; renamed = false; } myIndexByColName["id"] = new ColumnInfo("id", myIndexByColName.Count, typeof(int)); myIndexByColOrder[myIndexByColOrder.Length - 1] = (ColumnInfo)myIndexByColName["id"]; myAliasToLeftName = ProjectHelper.invertDic(myLeftNameToAlias); myAliasToRightName = ProjectHelper.invertDic(myRightNameToAlias); } public string leftJoinCol {get{return myLeftJc;}} public string rightJoinCol {get{return myRightJc;}} public string getRenamedName (string origName, bool isLeft) { if (isLeft) return (string)myLeftNameToAlias[origName]; return (string)myRightNameToAlias[origName]; } public string getOriginalName(string rename, bool isLeft) { return isLeft ? myAliasToLeftName[rename].ToString() : myAliasToRightName[rename].ToString(); } } }