示例#1
0
 /// <summary>
 /// CRUD on Records changes indexes as well as table, so we need this
 /// </summary>
 /// <param name="db"></param>
 /// <param name="obs"></param>
 /// <param name="c"></param>
 protected SDatabase(SDatabase db, SDict <long, SDbObject> obs, SRole r, long c)
 {
     name    = db.name;
     objects = obs;
     curpos  = c;
     role    = r;
 }
示例#2
0
 protected SDatabase(SDatabase db)
 {
     name    = db.name;
     objects = db.objects;
     curpos  = db.curpos;
     role    = db.role;
 }
示例#3
0
 public STransaction(SDatabase d, bool auto) : base(d)
 {
     autoCommit      = auto;
     rollback        = d._Rollback;
     uid             = _uid;
     readConstraints = SDict <long, bool> .Empty;
 }
示例#4
0
 SDatabase(string fname)
 {
     name    = fname;
     objects = _system.objects;
     role    = _system.role;
     curpos  = 0;
 }
        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 OrderedRowSet(RowSet sce, SSelectStatement sel, Context cx) : base(sce._tr, sel, sce._aggregates, sce.Length)
        {
            _sce = sce;
            var ti = SList <TreeInfo <Serialisable> > .Empty;
            int n  = 0;

            for (var b = sel.order.First(); b != null; b = b.Next())
            {
                ti = ti + (new TreeInfo <Serialisable>(b.Value, 'A', 'D', !b.Value.desc), n++);
            }
            var t = new SMTree <Serialisable>(ti);
            var r = SDict <int, SRow> .Empty;
            int m = 0;

            for (var b = sce.First() as RowBookmark; b != null; b = b.Next() as RowBookmark)
            {
                var k = new Variant[n];
                var i = 0;
                for (var c = sel.order.First(); c != null; c = c.Next())
                {
                    k[i] = new Variant(c.Value.col.Lookup(new Context(b, cx)), !c.Value.desc);
                }
                t  = t.Add(m, k);
                r += (m++, b._ob);
            }
            _tree = t;
            _rows = r;
        }
示例#7
0
 SDatabase()
 {
     name    = "SYSTEM";
     objects = SDict <long, SDbObject> .Empty;
     role    = SRole.Public;
     curpos  = 0;
 }
示例#8
0
 protected SDatabase(SDatabase db)
 {
     name    = db.name;
     objects = db.objects;
     names   = db.names;
     curpos  = db.curpos;
 }
示例#9
0
 protected SDatabase(SDatabase db, SDict <long, SDbObject> obs, SDict <string, SDbObject> nms, long c)
 {
     name    = db.name;
     objects = obs;
     names   = nms;
     curpos  = c;
 }
 public SQuery(SQuery q, SDict <int, string> a, SDict <int, Serialisable> cp,
               SDict <string, Serialisable> cn) : base(q)
 {
     display = a;
     cpos    = cp;
     names   = cn;
 }
示例#11
0
 SDatabase(string fname)
 {
     name    = fname;
     objects = SDict <long, SDbObject> .Empty;
     names   = SDict <string, SDbObject> .Empty;
     curpos  = 0;
 }
示例#12
0
 /// <summary>
 /// Some other set of updates to existing (and maybe named) objects
 /// </summary>
 /// <param name="tr"></param>
 /// <param name="obs"></param>
 /// <param name="nms"></param>
 protected STransaction(STransaction tr, SDict <long, SDbObject> obs, SRole r, long c)
     : base(tr, obs, r, c)
 {
     autoCommit      = tr.autoCommit;
     uid             = tr.uid + 1;
     readConstraints = tr.readConstraints;
 }
示例#13
0
 internal STransaction(SDatabase d, ReaderBase rdr, bool auto) : base(d)
 {
     autoCommit      = auto;
     uid             = _uid;
     readConstraints = SDict <long, bool> .Empty;
     rdr.db          = this;
 }
示例#14
0
 protected STransaction(STransaction tr, long u) : base(tr)
 {
     autoCommit      = tr.autoCommit;
     rollback        = tr.rollback;
     uid             = tr.uid;
     readConstraints = tr.readConstraints + (u, true);
 }
 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 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;
 }
示例#17
0
 protected STransaction(STransaction tr, SDict <long, SDbObject> obs, SDict <string, SDbObject> nms, long c)
     : base(tr, obs, nms, c)
 {
     autoCommit      = tr.autoCommit;
     rollback        = tr.rollback;
     uid             = tr.uid + 1;
     readConstraints = tr.readConstraints;
 }
示例#18
0
 public void Close()
 {
     lock (files)
     {
         var f = dbfiles[name];
         databases = databases - name;
         dbfiles   = dbfiles - name;
         f.Close();
     }
 }
        public DistinctRowSet(RowSet sce) : base(sce._tr, sce._qry, sce._aggregates, null)
        {
            _sce = sce;
            var r = SDict <SRow, bool> .Empty;

            for (var b = sce.First(); b != null; b = b.Next())
            {
                r += (((RowBookmark)b)._ob, true);
            }
            rows = r;
        }
示例#20
0
        public static SDatabase Open(string path, string fname)
        {
            if (dbfiles.Contains(fname))
            {
                return(databases[fname]
                       ?? throw new System.Exception("Database is loading"));
            }
            var db = new SDatabase(fname);

            dbfiles += (fname, new AStream(path + fname));
            db       = db.Load();
            Install(db);
            return(db);
        }
示例#21
0
        public static SDatabase Open(string path, string fname)
        {
            if (dbfiles.Contains(fname))
            {
                return(databases[fname]
                       ?? throw new System.Exception("Database is loading"));
            }
            var db   = new SDatabase(fname);
            var file = new FileStream(path + fname, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

            dbfiles += (fname, file);
            db       = db.Load();
            Install(db);
            return(db);
        }
        public SQuery(Types t, SDict <int, string> a, SDict <int, Serialisable> c,
                      Context cx) : base(t)
        {
            var cp = SDict <int, Serialisable> .Empty;
            var cn = SDict <string, Serialisable> .Empty;
            var ab = a.First();

            for (var cb = c.First(); ab != null && cb != null; ab = ab.Next(), cb = cb.Next())
            {
                var s = cb.Value.Item2.Lookup(cx);
                cp += (cb.Value.Item1, s);
                cn += (ab.Value.Item2, s);
            }
            display = a;
            cpos    = cp;
            names   = cn;
        }
        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 OrderedRowSet(RowSet sce, SList <TreeInfo <Serialisable> > ti, Context cx)
            : base(sce._tr, sce._qry, sce._aggregates, null)
        {
            _sce = sce;
            var t = new SMTree <Serialisable>(ti);
            var r = SDict <int, SRow> .Empty;
            int m = 0;

            for (var b = sce.First() as RowBookmark; b != null; b = b.Next() as RowBookmark)
            {
                var k = new Variant[ti.Length.Value];
                var i = 0;
                for (var c = ti.First(); c != null; c = c.Next())
                {
                    k[i] = new Variant(c.Value.headName.Lookup(new Context(b, cx)));
                }
                t  = t.Add(m, k);
                r += (m++, b._ob);
            }
            _tree = t;
            _rows = r;
        }
        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 static SSelectStatement Get(SDatabase db, Reader f)
        {
            f.GetInt(); // uid for the SSelectStatement probably -1
            var d = f.ReadByte() == 1;
            var n = f.GetInt();
            SDict <int, string>       a = SDict <int, string> .Empty;
            SDict <int, Serialisable> c = SDict <int, Serialisable> .Empty;

            for (var i = 0; i < n; i++)
            {
                a += (i, f.GetString());
                c += (i, f._Get(db));
            }
            var q = (SQuery)f._Get(db);
            var o = SList <SOrder> .Empty;
            var m = f.GetInt();

            for (var i = 0; i < m; i++)
            {
                o += ((SOrder)f._Get(db), i);
            }
            return(new SSelectStatement(d, a, c, q, o, Context.Empty));
        }
        public SDict <K, V> Merge(SDict <K, V> ud)
        {
            var r  = Empty;
            var ob = First();
            var ub = ud.First();

            while (ob != null && ub != null)
            {
                var c = ob.Value.Item1.CompareTo(ub.Value.Item1);
                if (c == 0)
                {
                    r += (ub.Value.Item1, ub.Value.Item2);
                    ob = ob.Next();
                    ub = ub.Next();
                }
                else if (c < 0)
                {
                    r += ob.Value;
                    ob = ob.Next();
                }
                else
                {
                    r += ub.Value;
                    ub = ub.Next();
                }
            }
            for (; ob != null; ob = ob.Next())
            {
                r += ob.Value;
            }
            for (; ub != null; ub = ub.Next())
            {
                r += ub.Value;
            }
            return(r);
        }
        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 static SBookmark <K, V>?Next(SBookmark <K, V>?stk, SDict <K, V>?tree = null)
        {
            SBucket <K, V>?         b;
            ValueTuple <K, object?> d;

            if (stk == null) // following Create or Reset
            {
                // if Tree is empty return null
                if (tree == null || tree.root == null || tree.Length == 0)
                {
                    return(null);
                }
                // The first entry is root.slots[0] or below
                stk = new SBookmark <K, V>(tree.root, 0, null);
                d   = tree.root.Slot(0);
                b   = d.Item2 as SBucket <K, V>;
            }
            else // guaranteed to be at a LEAF
            {
                var stkPos = stk._bpos;
                if (++stkPos == stk._bucket.count) // this is the right test for a leaf
                {
                    // at end of current bucket: pop till we aren't
                    for (; ;)
                    {
                        if (++stkPos <= stk._bucket.count)// this is the right test for a non-leaf; redundantly ok for first time (leaf)
                        {
                            break;
                        }
                        stk = stk._parent;
                        if (stk == null)
                        {
                            break;
                        }
                        stkPos = stk._bpos;
                    }
                    // we may run out of the BTree
                    if (stk == null)
                    {
                        return(null);
                    }
                }
                stk = new SBookmark <K, V>(stk._bucket, stkPos, stk._parent);
                if (stk._bpos == stk._bucket.count)
                {                                                      // will only happen for a non-leaf
                    b = ((SInner <K, V>)stk._bucket).gtr;
                    d = new ValueTuple <K, object?>(default(K), null); // or compiler complains
                }
                else // might be leaf or not
                {
                    d = stk._bucket.Slot(stkPos);
                    b = d.Item2 as SBucket <K, V>;
                }
            }
            while (b != null) // now ensure we are at a leaf
            {
                stk = new SBookmark <K, V>(b, 0, stk);
                d   = stk._bucket.Slot(0);
                b   = d.Item2 as SBucket <K, V>;
            }
            return(stk);
        }
 protected SQuery(SQuery q, AStream f) : base(q, f)
 {
     display = SDict <int, string> .Empty;
     cpos    = SDict <int, Serialisable> .Empty;
     names   = SDict <string, Serialisable> .Empty;
 }