コード例 #1
0
 protected STransaction(STransaction tr, long u) : base(tr)
 {
     autoCommit      = tr.autoCommit;
     rollback        = tr.rollback;
     uid             = tr.uid;
     readConstraints = tr.readConstraints + (u, true);
 }
コード例 #2
0
 protected SDatabase(SDatabase db)
 {
     name    = db.name;
     objects = db.objects;
     curpos  = db.curpos;
     role    = db.role;
 }
コード例 #3
0
 internal STransaction(SDatabase d, ReaderBase rdr, bool auto) : base(d)
 {
     autoCommit      = auto;
     uid             = _uid;
     readConstraints = SDict <long, bool> .Empty;
     rdr.db          = this;
 }
コード例 #4
0
 public STransaction(SDatabase d, bool auto) : base(d)
 {
     autoCommit      = auto;
     rollback        = d._Rollback;
     uid             = _uid;
     readConstraints = SDict <long, bool> .Empty;
 }
コード例 #5
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;
 }
コード例 #6
0
 protected SDatabase(SDatabase db)
 {
     name    = db.name;
     objects = db.objects;
     names   = db.names;
     curpos  = db.curpos;
 }
コード例 #7
0
 protected SDatabase(SDatabase db, SDict <long, SDbObject> obs, SDict <string, SDbObject> nms, long c)
 {
     name    = db.name;
     objects = obs;
     names   = nms;
     curpos  = c;
 }
コード例 #8
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;
 }
コード例 #9
0
        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;
        }
コード例 #10
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);
        }
コード例 #11
0
        SIndex(SDatabase d, AStream f) : base(Types.SIndex, f)
        {
            table = f.GetLong();
            var n = f.ReadByte();
            var c = new long[n];

            for (var i = 0; i < n; i++)
            {
                c[i] = f.GetInt();
            }
            cols = SList <long> .New(c);

            rows = new SMTree(Info((STable)d.objects.Lookup(table), cols));
        }
コード例 #12
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);
        }
コード例 #13
0
        public SDatabase Commit()
        {
            var       f     = dbfiles[name];
            SDatabase db    = databases[name];
            var       rdr   = new Reader(f, curpos);
            var       since = rdr.GetAll(db, db.curpos);

            for (var i = 0; i < since.Length; i++)
            {
                if (since[i].Check(readConstraints))
                {
                    throw new Exception("Transaction conflict with read");
                }
                for (var b = objects.PositionAt(_uid); b != null; b = b.Next())
                {
                    if (since[i].Conflicts(b.Value.Item2))
                    {
                        throw new Exception("Transaction conflict on " + b.Value);
                    }
                }
            }
            lock (f)
            {
                since = rdr.GetAll(this, f.length);
                for (var i = 0; i < since.Length; i++)
                {
                    if (since[i].Check(readConstraints))
                    {
                        throw new Exception("Transaction conflict with read");
                    }
                    for (var b = objects.PositionAt(_uid); b != null; b = b.Next())
                    {
                        if (since[i].Conflicts(b.Value.Item2))
                        {
                            throw new Exception("Transaction conflict on " + b.Value);
                        }
                    }
                }
                db = f.Commit(db, this);
                f.CommitDone();
            }
            Install(db);
            return(db);
        }
コード例 #14
0
        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;
        }
コード例 #15
0
        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;
        }
コード例 #16
0
        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));
        }
コード例 #17
0
 protected SOrder(SDatabase db, Reader f) : base(Types.SOrder)
 {
     col  = f._Get(db);
     desc = f.ReadByte() == 1;
 }
コード例 #18
0
 public static SGroupQuery Get(SDatabase d, Reader f)
 {
     return(new SGroupQuery(d, f));
 }
コード例 #19
0
 public static SSearch Get(SDatabase d, Reader f)
 {
     return(new SSearch(d, f));
 }
コード例 #20
0
 public new static SAliasedTable Get(SDatabase d, Reader f)
 {
     return(new SAliasedTable(STable.Get(d, f), f.GetString()));
 }
コード例 #21
0
 public static SJoin Get(SDatabase d, Reader f)
 {
     return(new SJoin(d, f));
 }
コード例 #22
0
        static SQuery _Join(SDatabase db, Reader f)
        {
            f.GetInt();
            var st       = f.pos;
            var d        = SDict <int, string> .Empty;
            var c        = SDict <int, Serialisable> .Empty;
            var left     = f._Get(db) as SQuery ?? throw new Exception("Query expected");
            var outer    = f.GetInt() == 1;
            var joinType = (JoinType)f.GetInt();
            var right    = f._Get(db) as SQuery ?? throw new Exception("Query expected");
            var ab       = left.Display.First();
            var uses     = SDict <string, bool> .Empty;

            if (joinType.HasFlag(JoinType.Named))
            {
                var n = f.GetInt();
                for (var i = 0; i < n; i++)
                {
                    uses += (f.GetString(), true);
                }
            }
            var k = 0;

            for (var lb = left.cpos.First(); ab != null && lb != null; ab = ab.Next(), lb = lb.Next())
            {
                var col = lb.Value;
                var n   = ab.Value.Item2;
                if (right.names.Contains(n) &&
                    ((!joinType.HasFlag(JoinType.Natural)) || uses.Contains(n)))
                {
                    n = left.Alias + "." + n;
                }
                d += (k, n);
                c += (k, col.Item2);
                k++;
            }
            ab = right.Display.First();
            for (var rb = right.cpos.First(); ab != null && rb != null; ab = ab.Next(), rb = rb.Next())
            {
                if (joinType == JoinType.Natural && left.names.Contains(ab.Value.Item2))
                {
                    continue;
                }
                if (uses.Contains(ab.Value.Item2))
                {
                    continue;
                }
                var col = rb.Value;
                var n   = ab.Value.Item2;
                if (left.names.Contains(n) &&
                    ((!joinType.HasFlag(JoinType.Natural)) || uses.Contains(n)))
                {
                    n = right.Alias + "." + n;
                }
                d += (k, n);
                c += (k, col.Item2);
                k++;
            }
            f.pos = st;
            return(new SQuery(Types.STableExp, d, c, Context.Empty));
        }
コード例 #23
0
 public SRecord Get(SDatabase db)
 {
     return(db.Get(value()));
 }
コード例 #24
0
 public static void Install(SDatabase db)
 {
     lock (files) databases += (db.name, db);
 }
コード例 #25
0
 public static SOrder Get(SDatabase db, Reader f)
 {
     return(new SOrder(db, f));
 }
コード例 #26
0
        static void Main(string[] args)
        {
            // Tests for SList (unordered list)
            var sl = SList <string> .New("Red", "Blue", "Green");

            sl = sl.InsertAt("Yellow", 0);
            var s2 = sl;

            sl = sl.RemoveAt(3);
            sl = sl.UpdateAt("Pink", 1);
            sl = sl.InsertAt("Orange", 2);
            Check <string>(sl.ToArray(), "Yellow", "Pink", "Orange", "Blue");
            Check <string>(s2.ToArray(), "Yellow", "Red", "Blue", "Green");
            Console.WriteLine("SList done");
            // Tests for SArray
            var sa = new SArray <string>("Red", "Blue", "Green");

            sa = sa.InsertAt(0, "Yellow");
            sa = sa.RemoveAt(3);
            var sb = sa;

            sa = sa.InsertAt(2, "Orange", "Violet");
            Check(sa.ToArray(), "Yellow", "Red", "Orange", "Violet", "Blue");
            Check(sb.ToArray(), "Yellow", "Red", "Blue");
            Console.WriteLine("SArray done");
            // Tests for SSearchTree<string>
            var ss = SSearchTree <string> .New("InfraRed", "Red", "Orange", "Yellow", "Green", "Blue", "Violet");

            Check(ss.ToArray(), "Blue", "Green", "InfraRed", "Orange", "Red", "Violet", "Yellow");
            var si = SSearchTree <int> .New(56, 22, 24, 31, 23);

            Check(si.ToArray(), 22, 23, 24, 31, 56);
            Console.WriteLine("SSearchTree done");
            // Tests for SDict
            var sd = SDict <string, string> .Empty;

            sd = sd.Add("Y", "Yellow");
            sd = sd.Add("G", "Green");
            sd = sd.Add("B", "Blue");
            sd = sd.Remove("G");
            var sr = new string[sd.Count];
            var j  = 0;

            for (var b = sd.First(); b != null; b = b.Next())
            {
                sr[j++] = b.Value.key + ": " + b.Value.val;
            }
            Check(sr, "B: Blue", "Y: Yellow");
            Console.WriteLine("SDict done");
            // Tests for SMTree
            var ti = SList <TreeInfo> .Empty;

            ti = ti.InsertAt(new TreeInfo("0", 'D', 'D'), 0); // onDuplicate must be Disallow on all except last entry
            ti = ti.InsertAt(new TreeInfo("1", 'A', 'A'), 1);
            var sm   = new SMTree(ti);
            var test = new string[] { "BALTIM", "ANNU", "A", "ANNO", "BALTIC", "BRAIL" };

            for (var i = 0; i < test.Length; i++)
            {
                sm = Add(sm, test[i], i);
            }
            var sorted = new string[test.Length];

            j = 0;
            for (var b = sm.First(); b != null; b = b.Next())
            {
                sorted[j++] = test[((MTreeBookmark)b).value()];
            }
            // we are only sorting on the first two letters!
            // Check() we should offer some alternatives here
            Check(sorted, "A", "ANNU", "ANNO", "BALTIM", "BALTIC", "BRAIL");
            Console.WriteLine("SMTree done");
            File.Delete("strong");
            File.Create("strong").Close();
            var d  = SDatabase.Open("strong");
            var tr = new STransaction(d);

            tr = new STransaction(tr, new SString("This is Strong"));
            new STable(tr, "tbl");
            var c = tr.Commit();

            d.Close();
            d = SDatabase.Open("strong");
            Console.WriteLine(d.objects.First().Value.val.ToString());
            Console.ReadKey();
        }
コード例 #27
0
 protected SDatabase(SDatabase db, long pos) : this(db)
 {
     curpos = pos;
 }