Exemple #1
0
        public void TestSubqueryWithAlias()
        {
            var actual = new Subquery()
            {
                SelectStatement = new SelectStatement()
                {
                    SelectElements = new List <SelectExpression>()
                    {
                        new SelectScalarExpression()
                        {
                            Expression = new ColumnReference()
                            {
                                Identifiers = new List <string>()
                                {
                                    "c1"
                                }
                            }
                        }
                    }
                },
                Alias = "t"
            }.Print();
            var expected = "(SELECT c1) t";

            actual.Should().Be(expected);
        }
Exemple #2
0
        public void TestSubqueryAccept()
        {
            Mock <KoraliumSqlVisitor> mock = new Mock <KoraliumSqlVisitor>();
            Subquery subquery = new Subquery();

            subquery.Accept(mock.Object);
            mock.Verify(x => x.VisitSubquery(subquery));
        }
        internal MappingContext(PersistenceAdapter adapter, Subquery subquery, IMapping mapping, MappingContext parentContext = null)
        {
            this.Adapter         = adapter;
            this.ParentContext   = parentContext;
            this.CurrentSubquery = subquery;
            this.CurrentMapping  = mapping;

            this.Reset();
        }
        internal override string ToString(string indent)
        {
            var sb = new StringBuilder();

            sb.Append(Expression.ToString(indent));
            sb.AppendFormat(" {0} \r\n", TsqlFragmentToString.BooleanComparisonType(ComparisonType));
            sb.Append(Subquery.ToString(indent + " "));

            return(sb.ToString());
        }
 public override void AcceptChildren(WSqlFragmentVisitor visitor)
 {
     if (Expression != null)
     {
         Expression.Accept(visitor);
     }
     if (Subquery != null)
     {
         Subquery.Accept(visitor);
     }
     base.AcceptChildren(visitor);
 }
        public void TestVisitSubquery()
        {
            Mock <SelectStatement> mock = new Mock <SelectStatement>();
            Subquery subquery           = new Subquery()
            {
                SelectStatement = mock.Object
            };

            KoraliumSqlVisitor koraliumSqlVisitor = new KoraliumSqlVisitor();

            koraliumSqlVisitor.Visit(subquery);

            mock.Verify(x => x.Accept(koraliumSqlVisitor));
        }
        internal override string ToString(string indent)
        {
            var sb = new StringBuilder(512);

            sb.AppendFormat(CultureInfo.CurrentCulture, Expression.ToString(indent));

            sb.Append(NotDefined ? " NOT IN " : " IN ");

            if (Values != null)
            {
                var newLine = false;

                for (var i = 0; i < Values.Count; i++)
                {
                    sb.Append(i > 0 ? ", " : "(");

                    if (Values[i].OneLine())
                    {
                        sb.Append(Values[i].ToString(""));
                    }
                    else
                    {
                        sb.Append("\r\n");
                        sb.Append(Values[i].ToString(indent + " "));
                        newLine = true;
                    }
                }

                if (newLine)
                {
                    sb.Append("\r\n");
                    sb.AppendFormat("{0})", indent);
                }
                else
                {
                    sb.Append(")");
                }
            }
            else if (Subquery != null)
            {
                sb.Append("\r\n");
                sb.Append(Subquery.ToString(indent + " "));
            }

            return(sb.ToString());
        }
        protected override BoundExpression RewriteSingleRowSubselect(BoundSingleRowSubselect node)
        {
            var relation = RewriteRelation(node.Relation);

            var factory = node.Value.Factory;

            // TODO: If the query is guranteed to return a single, e.g. if it is a aggregated but not grouped,
            //       we should not emit the additional aggregation and assertion.

            // 1. We need to know whether it returns more then one row.

            var valueSlot   = node.Value;
            var anyOutput   = factory.CreateTemporary(valueSlot.Type);
            var countOutput = factory.CreateTemporary(typeof(int));

            var anyAggregateSymbol = BuiltInAggregates.Any;
            var anyAggregatable    = anyAggregateSymbol.Definition.CreateAggregatable(valueSlot.Type);

            var countAggregatedSymbol = BuiltInAggregates.Count;
            var countAggregatable     = countAggregatedSymbol.Definition.CreateAggregatable(typeof(int));

            var aggregates = new[]
            {
                new BoundAggregatedValue(anyOutput, anyAggregateSymbol, anyAggregatable, Expression.Value(valueSlot)),
                new BoundAggregatedValue(countOutput, countAggregatedSymbol, countAggregatable, Expression.Literal(0))
            };

            var aggregation = new BoundGroupByAndAggregationRelation(relation, Enumerable.Empty <BoundComparedValue>(), aggregates);

            // 2. Now we can assert that the number of rows returned is at most one.

            var condition = Expression.LessThan(Expression.Value(countOutput), Expression.Literal(1));

            var message        = Resources.SubqueryReturnedMoreThanRow;
            var assertRelation = new BoundAssertRelation(aggregation, condition, message);

            var subquery   = new Subquery(SubqueryKind.Subselect, anyOutput, assertRelation, CurrentPassthru);
            var subqueries = _subqueryStack.Peek();

            subqueries.Add(subquery);

            return(Expression.Value(anyOutput));
        }
        protected override BoundExpression RewriteExistsSubselect(BoundExistsSubselect node)
        {
            var relation = RewriteRelation(node.Relation);

            // TODO: This isn't ideal. In many cases, the relation will not have any output values.
            //       This means, we've to create a new value slot factory which in turn means that
            //       we have create multiple slots with the same name. It seems we should think of
            //       a better way to carry the factories. Maybe we should just expose on specific
            //       bound nodes?

            var facory = node.Relation.GetOutputValues().FirstOrDefault()?.Factory ?? new ValueSlotFactory();

            var valueSlot  = facory.CreateTemporary(typeof(bool));
            var subquery   = new Subquery(SubqueryKind.Exists, valueSlot, relation, CurrentPassthru);
            var subqueries = _subqueryStack.Peek();

            subqueries.Add(subquery);

            return(Expression.Value(valueSlot));
        }
 public override void AcceptChildren(WSqlFragmentVisitor visitor)
 {
     if (Expression != null)
     {
         Expression.Accept(visitor);
     }
     if (Subquery != null)
     {
         Subquery.Accept(visitor);
     }
     if (Values != null)
     {
         var index = 0;
         for (var count = Values.Count; index < count; ++index)
         {
             Values[index].Accept(visitor);
         }
     }
     base.AcceptChildren(visitor);
 }
Exemple #11
0
        public void TestCloneSubquery()
        {
            Subquery subquery = new Subquery()
            {
                Alias           = "t",
                SelectStatement = new SelectStatement()
                {
                    FromClause = new FromClause()
                    {
                        TableReference = new FromTableReference()
                        {
                            TableName = "test"
                        }
                    }
                }
            };

            var clone = subquery.Clone() as Subquery;

            Assert.AreEqual(subquery, clone);
            Assert.IsFalse(ReferenceEquals(subquery, clone));
            Assert.IsFalse(ReferenceEquals(subquery.SelectStatement, clone.SelectStatement));
        }
 MappingContext IMapping.CreateContext(PersistenceAdapter adapter, Subquery subquery)
 {
     return(new MappingContext <T>(adapter, subquery, this));
 }
 private static BoundJoinRelation EmitSubselect(BoundRelation result, Subquery subselect)
 {
     return(new BoundJoinRelation(BoundJoinType.LeftOuter, result, subselect.Relation, null, null, subselect.Passthru));
 }
 private static BoundJoinRelation EmitExists(BoundRelation result, Subquery existsSubquery)
 {
     return(new BoundJoinRelation(BoundJoinType.LeftSemi, result, existsSubquery.Relation, null, existsSubquery.ValueSlot, existsSubquery.Passthru));
 }
Exemple #15
0
        public void TestSubqueryEquals()
        {
            Subquery first = new Subquery()
            {
                Alias           = "t",
                SelectStatement = new SelectStatement()
                {
                    FromClause = new FromClause()
                    {
                        TableReference = new FromTableReference()
                        {
                            TableName = "test"
                        }
                    }
                }
            };

            Subquery firstClone = new Subquery()
            {
                Alias           = "t",
                SelectStatement = new SelectStatement()
                {
                    FromClause = new FromClause()
                    {
                        TableReference = new FromTableReference()
                        {
                            TableName = "test"
                        }
                    }
                }
            };

            Subquery second = new Subquery()
            {
                Alias           = "t2",
                SelectStatement = new SelectStatement()
                {
                    FromClause = new FromClause()
                    {
                        TableReference = new FromTableReference()
                        {
                            TableName = "test"
                        }
                    }
                }
            };

            Subquery third = new Subquery()
            {
                Alias           = "t",
                SelectStatement = new SelectStatement()
                {
                    FromClause = new FromClause()
                    {
                        TableReference = new FromTableReference()
                        {
                            TableName = "test2"
                        }
                    }
                }
            };

            //Equals
            Assert.IsTrue(Equals(first, firstClone));
            Assert.IsFalse(Equals(first, null));
            Assert.IsFalse(Equals(first, second));
            Assert.IsFalse(Equals(first, third));
            Assert.IsFalse(Equals(first, "other type"));

            //Hash code
            Assert.AreEqual(first.GetHashCode(), firstClone.GetHashCode());
            Assert.AreNotEqual(first.GetHashCode(), second.GetHashCode());
            Assert.AreNotEqual(first.GetHashCode(), third.GetHashCode());
        }
Exemple #16
0
 MappingContext IMapping.CreateContext(PersistenceAdapter adapter, Subquery subquery, MappingDirection direction)
 {
     return(new MappingContext <T>(adapter, subquery, this, direction));
 }
Exemple #17
0
 public virtual void VisitSubquery(Subquery subquery)
 {
     Visit(subquery.SelectStatement);
     //DONE
 }