public SQuery(SQuery q, SDict <int, string> a, SDict <int, Serialisable> cp,
               SDict <string, Serialisable> cn) : base(q)
 {
     display = a;
     cpos    = cp;
     names   = cn;
 }
        public override RowSet RowSet(STransaction tr, SQuery top, SDict <long, SFunction> ags, Context cx)
        {
            var lf = left.RowSet(tr, left, ags, cx);
            var rg = right.RowSet(lf._tr, right, ags, cx);

            return(new JoinRowSet(top, this, lf, rg, ags, cx));
        }
 public SGroupQuery(SQuery s, SDict <int, string> d, SDict <int, Serialisable> c,
                    Context cx, SDict <int, string> g, SList <Serialisable> h)
     : base(Types.SGroupQuery, d, c, cx)
 {
     source  = s;
     groupby = g;
     having  = h;
 }
 public override RowSet RowSet(STransaction tr, SQuery top, SDict <long, SFunction> ags, Context cx)
 {
     for (var b = where.First(); b != null; b = b.Next())
     {
         ags = b.Value.Aggregates(ags, cx);
     }
     return(new SearchRowSet(tr, top, this, ags, cx));
 }
        public SSearch(SDatabase db, Reader f) : base(Types.SSearch, f)
        {
            sce = f._Get(db) as SQuery ?? throw new Exception("Query expected");
            var w = SList <Serialisable> .Empty;
            var n = f.GetInt();

            for (var i = 0; i < n; i++)
            {
                w += (f._Get(db).Lookup(new Context(sce.names, null)), i);
            }
            where = w;
        }
        public SGroupQuery(SDatabase db, Reader f) : base(Types.SGroupQuery, f)
        {
            source = f._Get(db) as SQuery ?? throw new Exception("Query expected");
            var g = SDict <int, string> .Empty;
            var h = SList <Serialisable> .Empty;
            var n = f.GetInt();

            for (var i = 0; i < n; i++)
            {
                g += (i, f.GetString());
            }
            n = f.GetInt();
            for (var i = 0; i < n; i++)
            {
                h += (f._Get(db).Lookup(new Context(source.names, null)), i);
            }
            groupby = g;
            having  = h;
        }
        public SJoin(SDatabase db, Reader f) : base(Types.STableExp, _Join(db, f))
        {
            left     = f._Get(db) as SQuery ?? throw new Exception("Query expected");
            outer    = f.GetInt() == 1;
            joinType = (JoinType)f.GetInt();
            right    = f._Get(db) as SQuery ?? throw new Exception("Query expected");
            var n  = f.GetInt();
            var on = SList <SExpression> .Empty;
            var us = SList <string> .Empty;

            if (joinType.HasFlag(JoinType.Natural))
            {
                for (var lb = left.names.First(); lb != null; lb = lb.Next())
                {
                    if (right.names.Contains(lb.Value.Item1))
                    {
                        us += lb.Value.Item1;
                    }
                }
            }
            else if (joinType.HasFlag(JoinType.Named))
            {
                for (var i = 0; i < n; i++)
                {
                    us += f.GetString();
                }
            }
            else if (!joinType.HasFlag(JoinType.Cross))
            {
                for (var i = 0; i < n; i++)
                {
                    var e = f._Get(db) as SExpression
                            ?? throw new Exception("ON exp expected");
                    on += e;
                }
            }
            ons  = on;
            uses = us;
        }
        static RowSet Source(STransaction tr, SQuery top, SSearch sc, SDict <long, SFunction> ags, Context cx)
        {
            RowSet?s       = null;
            var    matches = SDict <long, Serialisable> .Empty;

            if (sc.sce is STable tb)
            {
                for (var wb = sc.where.First(); wb != null; wb = wb.Next())
                {
                    if (wb.Value.Lookup(cx) is SExpression x && x.op == SExpression.Op.Eql)
                    {
                        if (x.left is SColumn c && tb.names.Contains(c.name) &&
                            x.right != null && x.right.isValue)
                        {
                            matches = matches + (c.uid, x.right);
                        }
                        else if (x.right is SColumn cr && tb.names.Contains(cr.name) &&
                                 x.left != null && x.left.isValue)
                        {
                            matches = matches + (cr.uid, x.left);
                        }
                    }
        public override RowSet RowSet(STransaction tr, SQuery top, SDict <long, SFunction> ags, Context cx)
        {
            for (var b = order.First(); b != null; b = b.Next())
            {
                ags = b.Value.col.Aggregates(ags, cx);
            }
            var ags1 = ags;

            for (var b = cpos.First(); b != null; b = b.Next())
            {
                ags1 = b.Value.Item2.Aggregates(ags1, cx);
            }
            RowSet r = new SelectRowSet(qry.RowSet(tr, this, ags1, cx), this, ags, cx);

            // perform another pass on the selectlist just in case
            if (!(qry is SGroupQuery))
            {
                for (var b = cpos.First(); b != null; b = b.Next())
                {
                    ags = b.Value.Item2.Aggregates(ags, cx);
                }
                if (ags.Length != 0)
                {
                    r = new EvalRowSet(((SelectRowSet)r)._source, this, ags, cx);
                }
            }
            if (distinct)
            {
                r = new DistinctRowSet(r);
            }
            if (order.Length != 0)
            {
                r = new OrderedRowSet(r, this, cx);
            }
            return(r);
        }
 public SSearch(SQuery s, SList <Serialisable> w)
     : base(Types.SSearch, s.display, s.cpos, new Context(s.names, null))
 {
     sce   = s;
     where = w;
 }
 public SearchRowSet(STransaction tr, SQuery top, SSearch sc, SDict <long, SFunction> ags, Context cx)
     : this(Source(tr, top, sc, ags, cx), sc, ags)
 {
 }
 public SQuery(SQuery q) : base(q)
 {
     display = q.display;
     cpos    = q.cpos;
     names   = q.names;
 }
 public SJoin(SQuery lf, bool ou, JoinType jt, SQuery rg, SList <SExpression> on,
              SList <string> us, SDict <int, string> d, SDict <int, Serialisable> c, Context cx)
     : base(Types.STableExp, d, c, cx)
 {
     left = lf; right = rg; outer = ou; joinType = jt; ons = on; uses = us;
 }
 public virtual RowSet RowSet(STransaction tr, SQuery top, SDict <long, SFunction> ags, Context cx)
 {
     throw new NotImplementedException();
 }
 public override RowSet RowSet(STransaction tr, SQuery top, SDict <long, SFunction> ags, Context cx)
 {
     return(new GroupRowSet(tr, top, this, ags, cx));
 }
 public SSelectStatement(bool d, SDict <int, string> a, SDict <int, Serialisable> c,
                         SQuery q, SList <SOrder> or, Context cx)
     : base(Types.SSelect, a, c, new Context(q.names, cx))
 {
     distinct = d;  qry = q; order = or;
 }
 public SQuery(Types t, SQuery q) : base(t)
 {
     display = q.display;
     cpos    = q.cpos;
     names   = q.names;
 }
 protected SQuery(SQuery q, AStream f) : base(q, f)
 {
     display = SDict <int, string> .Empty;
     cpos    = SDict <int, Serialisable> .Empty;
     names   = SDict <string, Serialisable> .Empty;
 }
 public RowSet(STransaction tr, SQuery q, SDict <long, SFunction> ags, int?n) : base(n)
 {
     _tr = tr; _qry = q; _aggregates = ags;
 }