public IEnumerable <IColumnRelationship> ParseBooleanComparisonExpression(BooleanComparisonExpression booleanComparisonExpression, IDictionary <string, ITable> tableByAliasIdentifier, IList <int> booleanComparisonExpressionIdentifiers, RelationshipOrigin relationshipOrigin)
        {
            if (booleanComparisonExpression.ComparisonType == BooleanComparisonType.NotEqualToBrackets || booleanComparisonExpression.ComparisonType == BooleanComparisonType.NotEqualToExclamation)
            {
                yield break;
            }

            var columnReferenceExpressions = _visitorProvider.GetColumnReferenceExpressions(booleanComparisonExpression).AsList();

            if (columnReferenceExpressions.Count != 2)
            {
                yield break;
            }

            var fromColumnReferenceExpression = columnReferenceExpressions[0];

            if (!TryParseColumn(fromColumnReferenceExpression, tableByAliasIdentifier, out IColumn fromColumn))
            {
                yield break;
            }

            var toColumnReferenceExpression = columnReferenceExpressions[1];

            if (!TryParseColumn(toColumnReferenceExpression, tableByAliasIdentifier, out IColumn toColumn))
            {
                yield break;
            }

            var booleanComparisonExpressionIdentifier = booleanComparisonExpression.FirstTokenIndex + booleanComparisonExpression.LastTokenIndex;

            if (booleanComparisonExpressionIdentifiers.Contains(booleanComparisonExpressionIdentifier))
            {
                yield break;
            }

            booleanComparisonExpressionIdentifiers.Add(booleanComparisonExpressionIdentifier);

            var columnRelationship = new ColumnRelationship(fromColumn, toColumn, relationshipOrigin);

            yield return(columnRelationship);
        }
        public IEnumerable <IColumnRelationship> ParseViewTableColumnToBaseTableColumnRelationships(TSqlStatement tsqlStatement, string contextualServerName, string contextualDatabaseName, RelationshipOrigin relationshipOrigin)
        {
            var viewStatementBody = tsqlStatement as ViewStatementBody;

            if (viewStatementBody == null)
            {
                yield break;
            }

            if (!TryParseTable(contextualServerName, contextualDatabaseName, viewStatementBody.SchemaObjectName, out ITable viewTable))
            {
                yield break;
            }

            var selectStatement    = viewStatementBody.SelectStatement;
            var queryExpression    = selectStatement.QueryExpression;
            var querySpecification = queryExpression as QuerySpecification;

            if (!(queryExpression is QuerySpecification))
            {
                yield break;
            }

            var selectElements = querySpecification.SelectElements;

            if (viewTable.Columns.Count != selectElements.Count)
            {
                yield break;
            }

            var viewStatementFromClause = querySpecification.FromClause;

            if (!TryParseTableByAliasIdentifier(viewStatementFromClause, contextualServerName, contextualDatabaseName, out IDictionary <string, ITable> viewTableByAliasIdentifier))
            {
                yield break;
            }

            for (var index = 0; index < selectElements.Count; index++)
            {
                var selectElement             = selectElements[index];
                var selectScalarExpression    = (SelectScalarExpression)selectElement;
                var scalarExpression          = selectScalarExpression.Expression;
                var columnReferenceExpression = scalarExpression as ColumnReferenceExpression;

                if (!(scalarExpression is ColumnReferenceExpression))
                {
                    continue;
                }

                var viewColumns     = viewTable.Columns.AsList();
                var viewTableColumn = viewColumns[index];

                if (!TryParseColumn(columnReferenceExpression, viewTableByAliasIdentifier, out IColumn baseTableColumn))
                {
                    continue;
                }

                var columnRelationship = new ColumnRelationship(viewTableColumn, baseTableColumn, relationshipOrigin);

                yield return(columnRelationship);
            }
        }