public TableReferenceSpecification(ITable table, TableReferenceSpecificationScope tableReferenceSpecificationScope, TableReferenceSpecificationKind tableReferenceSpecificationKind, string tableAlias)
 {
     DatabaseSchemaTableSource = $"{table.DatabaseName}.{table.SchemaName}.{table.TableName}".ToLower();
     SchemaTableSource         = $"{table.SchemaName}.{table.TableName}".ToLower();
     Table      = table;
     TableAlias = tableAlias;
     TableReferenceSpecificationKind  = tableReferenceSpecificationKind;
     TableReferenceSpecificationScope = tableReferenceSpecificationScope;
     TableSource = $"{table.TableName}".ToLower();
 }
        public void RemoveTableReferenceSpecificationCollisions(IContextScope contextScope, ref List <ITableReferenceSpecification> tableReferenceSpecifications)
        {
            for (var index = 0; index < tableReferenceSpecifications.Count; index++)
            {
                var tableReferenceSpecification = tableReferenceSpecifications[index];

                var tableReferenceSpecificationCollisions = tableReferenceSpecifications.Where(parameter =>
                {
                    if (tableReferenceSpecification == parameter)
                    {
                        return(false);
                    }

                    const TableReferenceSpecificationKind tableAlias = TableReferenceSpecificationKind.TableAlias;

                    var isTableAliasComparison = tableReferenceSpecification.TableReferenceSpecificationKind == tableAlias &&
                                                 parameter.TableReferenceSpecificationKind == tableAlias;

                    var isTableAliasSame = tableReferenceSpecification.TableAlias == parameter.TableAlias;
                    var isMostExplicitUniqueTableAliasScope = parameter.TableReferenceSpecificationScope == TableReferenceSpecificationScope.AliasServerIdentifier;

                    var isTableAliasCollision = isTableAliasComparison &&
                                                isTableAliasSame &&
                                                !isMostExplicitUniqueTableAliasScope;

                    const TableReferenceSpecificationKind tableSource = TableReferenceSpecificationKind.TableSource;

                    var isTableSourceComparison = tableReferenceSpecification.TableReferenceSpecificationKind == tableSource &&
                                                  parameter.TableReferenceSpecificationKind == tableSource;

                    var isTableSourceSame = tableReferenceSpecification.TableSource == parameter.TableSource ||
                                            tableReferenceSpecification.SchemaTableSource == parameter.SchemaTableSource ||
                                            tableReferenceSpecification.DatabaseSchemaTableSource == parameter.DatabaseSchemaTableSource;

                    var isTableSourceServerDifferent         = !string.Equals(tableReferenceSpecification.Table.ServerName, contextScope.ServerName, StringComparison.CurrentCultureIgnoreCase);
                    var isMostExplicitUniqueTableSourceScope = parameter.TableReferenceSpecificationScope == TableReferenceSpecificationScope.TableSourceServerIdentifier;

                    var isTableSourceCollision = isTableSourceComparison &&
                                                 isTableSourceSame &&
                                                 isTableSourceServerDifferent &&
                                                 !isMostExplicitUniqueTableSourceScope;

                    var isTableReferenceSpecificationCollision = isTableAliasCollision ||
                                                                 isTableSourceCollision;

                    return(isTableReferenceSpecificationCollision);
                })
                                                            .AsList();

                if (!tableReferenceSpecificationCollisions.Any())
                {
                    continue;
                }

                tableReferenceSpecificationCollisions.Add(tableReferenceSpecification);

                foreach (var tableReferenceSpecificationCollision in tableReferenceSpecificationCollisions)
                {
                    tableReferenceSpecifications.Remove(tableReferenceSpecificationCollision);
                }

                index = 0;
            }
        }