private string ProcessRelation(Expression fullExpression, Expression leftExpression, string memberName)
        {
            if (leftExpression.NodeType == ExpressionType.Convert)
            {
                leftExpression = ((UnaryExpression)leftExpression).Operand;
            }

            var parentMetaData = _metaData[leftExpression];

            if (parentMetaData != null)
            {
                if (_metaData.ContainsKey(fullExpression))
                {
                    return(null); // relation already visited
                }
                var iterator = GetRootIterator(parentMetaData.Iterator);

                var relation  = parentMetaData.Schema.Relations[memberName];
                var leftAlias = _relationAliases[iterator][parentMetaData.Key];

                if (relation != null && relation.IsToOne)
                {
                    if (!_relationAliases[iterator].ContainsKey(relation))
                    {
                        var sqlJoin = new SqlJoinDefinition(
                            new SqlJoinPart(parentMetaData.Schema, relation.LocalField, leftAlias),
                            new SqlJoinPart(relation.ForeignSchema, relation.ForeignField, SqlNameGenerator.NextTableAlias())
                            );

                        CurrentQuery.Joins.Add(sqlJoin);

                        _relationAliases[iterator][relation] = sqlJoin.Right.Alias;
                    }

                    _metaData[fullExpression] = new ExpressionMetaData {
                        Iterator = iterator, Relation = relation, Schema = relation.ForeignSchema
                    };
                }
                else if (relation == null)
                {
                    return(leftAlias + "." + (_schema.FieldsByFieldName[memberName]?.MappedName ?? memberName));
                }
            }

            return(null);
        }
Beispiel #2
0
 public virtual string JoinSql(SqlJoinDefinition j)
 {
     return($"{(j.Type == SqlJoinType.Inner ? "inner" : "left outer")} join {QuoteTable(j.Right.Schema.MappedName)} {j.Right.Alias} on {QuoteField(j.Left.Alias + "." + j.Left.Field.MappedName)}={QuoteField(j.Right.Alias + "." + j.Right.Field.MappedName)}");
 }