Exemplo n.º 1
0
        public void Test1()
        {
            string s, s2;

            using (SoodaTransaction tran = new SoodaTransaction())
            {
                Contact c = Contact.Mary;
                c.PersistentValue = "test123";
                s = tran.Serialize();
            }

            Console.WriteLine(s);

            using (SoodaTransaction tran = new SoodaTransaction())
            {
                tran.Deserialize(s);
                Contact c = Contact.Mary;
                Assert.AreEqual("test123", c.PersistentValue);
                s2 = tran.Serialize();
            }
            Assert.AreEqual(s, s2);

            using (SoodaTransaction tran = new SoodaTransaction())
            {
                tran.Deserialize(s);
                Contact c = Contact.Mary;
                Assert.AreEqual("test123", c.PersistentValue);
                s2 = tran.Serialize();
            }
            Assert.AreEqual(s, s2);
        }
Exemplo n.º 2
0
        public void OrderByTest()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);
                    ContactList l;

                    Contact.Mary.Name = "Aaa";  // to make it temporarily first
                    Contact.Ed.Name = "ZZZZ";   // to make it temporarily last

                    l = Contact.GetList(tran, SoodaWhereClause.Unrestricted, SoodaOrderBy.Ascending("Name"));

                    foreach (Contact c in l)
                    {
                        Console.WriteLine("c: {0}", c.Name);
                    }

                    Assert.IsTrue(l.IndexOf(Contact.Mary) == 0);
                    Assert.IsTrue(l.IndexOf(Contact.Ed) == l.Count - 1);
                    for (int i = 0; i < l.Count - 1; ++i)
                    {
                        if (String.CompareOrdinal((string)l[i].Name, (string)l[i + 1].Name) > 0)
                            Assert.Fail("Invalid sort!");
                    }
                    tran.Commit();
                }
            }
        }
Exemplo n.º 3
0
        public void Bug1()
        {
            string ser1, ser2;

            using (SoodaTransaction tran = new SoodaTransaction())
            {
                Group g1 = new Group();
                Group g2 = new Group();
                Contact c1 = new Contact();
                Contact c2 = new Contact();
                Contact c3 = new Contact();
                g1.Members.Add(c1);
                g1.Members.Add(c2);
                g1.Members.Add(c3);
                g1.Manager = c1;
                g2.Manager = c2;

                ser1 = tran.Serialize(SoodaSerializeOptions.Canonical);
            }
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                tran.Deserialize(ser1);

                ser2 = tran.Serialize(SoodaSerializeOptions.Canonical);
                Console.WriteLine("ser1 {0}", ser1);
                Console.WriteLine("ser2 {0}", ser2);
                Assert.AreEqual(ser1, ser2, "Serialization is stable");
            }
        }
Exemplo n.º 4
0
 public void AbstractTest10()
 {
     using (SoodaTransaction tran = new SoodaTransaction())
     {
         ConcreteMegaSuperBikeB v = ConcreteMegaSuperBikeB.GetRef(12);
     }
 }
        public void MissingReference()
        {
            int id1;
            int id2;
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                EightFields o1 = new EightFields();
                EightFields o2 = new EightFields();
                o1.TimeSpan = TimeSpan.FromSeconds(1);
                o2.TimeSpan = TimeSpan.FromSeconds(2);
                id1 = o1.Id;
                id2 = o2.Id;
                o1.Parent = o2;
                o2.Parent = o1;
                tran.Commit();
            }

            using (SoodaTransaction tran = new SoodaTransaction())
            {
                tran.CachingPolicy = new Sooda.Caching.SoodaCacheAllPolicy();
                EightFields o1 = EightFields.GetRef(id1);
                EightFields o2 = EightFields.GetRef(id2);
                Assert.AreEqual(o2, o1.Parent);
                Assert.AreEqual(o1, o2.Parent);
                tran.Commit();
            }
        }
 public ConcreteMegaSuperBikeA(SoodaTransaction transaction)
     : base(transaction)
 {
     //
     // TODO: Add construction logic here.
     //
 }
Exemplo n.º 7
0
        public void InvalidateTest()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();
                SoodaCache.DefaultCache.Clear();;

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);

                    ContactList cl = Contact.GetList(ContactField.Name == "Mary Manager", SoodaSnapshotOptions.Cache);
                    Assert.AreEqual(1, cl.Count);

                    cl[0].Name = "modified";
                    tran.Commit();
                }

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);

                    ContactList cl = Contact.GetList(ContactField.Name == "Mary Manager", SoodaSnapshotOptions.Cache);
                    Assert.AreEqual(0, cl.Count);
                }
            }
        }
Exemplo n.º 8
0
        public void MultiTableDelete()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);
                    ExtendedBike.Load(10).MarkForDelete();
                    tran.Commit();
                }

                try
                {
                    using (SoodaTransaction tran = new SoodaTransaction())
                    {
                        tran.RegisterDataSource(testDataSource);
                        ExtendedBike b = ExtendedBike.Load(10);
                    }
                    Assert.Fail("Object not deleted!");
                }
                catch (SoodaObjectNotFoundException)
                {
                    Assert.IsTrue(true);
                }
            }
        }
Exemplo n.º 9
0
        public void EventsTest()
        {
            using (TestSqlDataSource ds = new TestSqlDataSource("default"))
            {
                ds.Open();
                int roleID;

                using (SoodaTransaction t = new SoodaTransaction())
                {
                    t.RegisterDataSource(ds);
                    Role r = new Role();
                    r.Name = "aaa";
                    Console.WriteLine("Precommitting...");
                    t.SaveObjectChanges();
                    Console.WriteLine("Comitting.");
                    t.Commit();
                    roleID = r.Id;
                    Assert.AreEqual(0, r.BeforeObjectInsertEventCounter);
                    Assert.AreEqual(2, r.AfterObjectInsertEventCounter);
                    Assert.AreEqual(-1, r.BeforeObjectUpdateEventCounter);
                    Assert.AreEqual(-1, r.AfterObjectUpdateEventCounter);

                    Assert.AreEqual(1, r.Second.BeforeObjectInsertEventCounter);
                    Assert.AreEqual(3, r.Second.AfterObjectInsertEventCounter);
                    Assert.AreEqual(-1, r.Second.BeforeObjectUpdateEventCounter);
                    Assert.AreEqual(-1, r.Second.AfterObjectUpdateEventCounter);
                }
            }
        }
Exemplo n.º 10
0
 public void AbstractTest4()
 {
     using (SoodaTransaction tran = new SoodaTransaction())
     {
         Vehicle v = Vehicle.GetRef(4);
     }
 }
Exemplo n.º 11
0
 static void AddDateTimeField(SoodaTransaction tran)
 {
     DynamicFieldManager.Add(new FieldInfo {
             ParentClass = tran.Schema.FindClassByName("PKInt32"),
             Name = DateTimeField,
             Type = typeof(DateTime?)
         }, tran);
 }
Exemplo n.º 12
0
 public void Test1()
 {
     using (SoodaTransaction tran = new SoodaTransaction())
     {
         Contact.Mary.Name = "temp1";
         ContactList cl = Contact.GetList(tran, new SoodaWhereClause("Name = {0}", "Mary Manager"), SoodaOrderBy.Unsorted);
         Assert.AreEqual(0, cl.Count);
     }
 }
Exemplo n.º 13
0
 public void LoadTest2()
 {
     using (SoodaTransaction tran = new SoodaTransaction())
     {
         Assert.AreEqual(Bike.Load(3).GetType(), typeof(Bike));
         Assert.AreEqual(Bike.Load(4).GetType(), typeof(SuperBike));
         Assert.AreEqual(Bike.Load(6).GetType(), typeof(MegaSuperBike));
     }
 }
 public SoodaObjectOneToManyCollection(SoodaTransaction tran, Type childType, SoodaObject parentObject, string childRefField, Sooda.Schema.ClassInfo classInfo, SoodaWhereClause additionalWhereClause, bool cached)
     : base(tran, classInfo)
 {
     this.childType = childType;
     this.parentObject = parentObject;
     this.childRefField = childRefField;
     this.additionalWhereClause = additionalWhereClause;
     this.cached = cached;
 }
Exemplo n.º 15
0
 static void AddIntField(SoodaTransaction tran)
 {
     DynamicFieldManager.Add(new FieldInfo {
             ParentClass = tran.Schema.FindClassByName("PKInt32"),
             Name = IntField,
             TypeName = "Integer",
             IsNullable = false
         }, tran);
 }
Exemplo n.º 16
0
 static void AddReferenceField(SoodaTransaction tran)
 {
     DynamicFieldManager.Add(new FieldInfo {
             ParentClass = tran.Schema.FindClassByName("PKInt32"),
             Name = ReferenceField,
             Type = typeof(Contact),
             IsNullable = false
         }, tran);
 }
Exemplo n.º 17
0
        public void Test4()
        {
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                Group.GetRef(10).Name = "temp7";

                ContactList cl = Contact.GetList(tran, new SoodaWhereClause("PrimaryGroup.Name = {0}", "temp7"), SoodaOrderBy.Unsorted);
                Assert.AreEqual(4, cl.Count);
            }
        }
        public SoodaObjectManyToManyCollection(SoodaTransaction transaction, int masterColumn, object masterValue, Type relationType, Sooda.Schema.RelationInfo relationInfo)
            : base(transaction, masterColumn == 0 ? relationInfo.GetRef1ClassInfo() : relationInfo.GetRef2ClassInfo())
        {
            this.relationInfo = relationInfo;
            this.masterValue = masterValue;
            this.masterColumn = masterColumn;
            this.relationType = relationType;

            _factory = transaction.GetFactory(classInfo);
        }
Exemplo n.º 19
0
 public void CollectionNto1Test()
 {
     using (SoodaTransaction tran = new SoodaTransaction())
     {
         ContactList list = Contact.GetList(SoodaWhereClause.Unrestricted);
         for (int i = 0; i < list.Count; ++i)
         {
             Assert.AreEqual(i, list.IndexOf(list[i]), "IndexOf");
         }
     }
 }
Exemplo n.º 20
0
        public void Int32Test()
        {
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                PKInt32 test2 = PKInt32.Load(7777778);

                Assert.AreEqual(test2.Parent.Id, 7777777);
                Assert.AreEqual((string)test2.Parent.Data, "test data");
                Assert.AreEqual((string)test2.Data, "test data 2");
            }
        }
Exemplo n.º 21
0
        public void DateTimeTest()
        {
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                PKDateTime test2 = PKDateTime.Load(new DateTime(2000, 1, 1, 1, 0, 0));

                Assert.AreEqual(test2.Parent.Id, new DateTime(2000, 1, 1, 0, 0, 0));
                Assert.AreEqual((string)test2.Parent.Data, "test data");
                Assert.AreEqual((string)test2.Data, "test data 2");
            }
        }
Exemplo n.º 22
0
        public void BoolTest()
        {
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                PKBool test2 = PKBool.Load(false);

                Assert.AreEqual(test2.Parent.Id, true);
                Assert.AreEqual((string)test2.Parent.Data, "test data");
                Assert.AreEqual((string)test2.Data, "test data 2");
            }
        }
Exemplo n.º 23
0
        public static SoodaObject TryGetRefFieldValue(ref SoodaObject refCache, object fieldValue, SoodaTransaction tran, ISoodaObjectFactory factory)
        {
            if (refCache != null)
                return refCache;

            if (fieldValue == null)
                return null;

            refCache = factory.TryGet(tran, fieldValue);
            return refCache;
        }
Exemplo n.º 24
0
        public void InsertTest()
        {
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                Vehicle v = new Car();
                Assert.AreEqual(v.Type, 1);
                Car c = Car.GetRef(v.Id);

                Assert.AreSame(v, c);
                Assert.IsTrue(v is Car);
            }
        }
Exemplo n.º 25
0
        public static SoodaObject GetRefFieldValue(ref SoodaObject refCache, SoodaObject theObject, int tableNumber, int fieldOrdinal, SoodaTransaction tran, ISoodaObjectFactory factory)
        {
            if (refCache != null)
                return refCache;

            theObject.EnsureDataLoaded(tableNumber);

            if (theObject._fieldValues.IsNull(fieldOrdinal))
                return null;

            refCache = factory.GetRef(tran, theObject._fieldValues.GetBoxedFieldValue(fieldOrdinal));
            return refCache;
        }
Exemplo n.º 26
0
        public void Transaction()
        {
            using (SoodaTransaction trans1 = new SoodaTransaction())
            {
                using (new SoodaTransaction())
                {
                    Contact c50 = Contact.Load(50);
                    c50.Name = "Barbra Streisland";

                    IEnumerable<Contact> ce = Contact.Linq(trans1, SoodaSnapshotOptions.Default).Where(c => c.Name == "Barbara Streisland");
                    Assert.AreEqual(0, ce.Count());
                }
            }
        }
Exemplo n.º 27
0
        public void GetListTest()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);
                    foreach (MultiKey mk in MultiKey.GetList(SoodaWhereClause.Unrestricted))
                    {
                        Console.WriteLine("mk: {0},{1} = {2}", mk.Contact, mk.Group, mk.Value);
                    }
                }
            }
        }
Exemplo n.º 28
0
        public void DateTimeTest()
        {
            string ser;

            using (SoodaTransaction tran = new SoodaTransaction())
            {
                PKDateTime test = new PKDateTime();
                PKDateTime test2 = PKDateTime.Load(new DateTime(2000, 1, 1, 0, 0, 0, 0));

                Assert.AreEqual((string)test2.Data, "test data");

                ser = tran.Serialize();
                tran.Deserialize(ser);
                Assert.AreEqual(ser, tran.Serialize());
            }
        }
Exemplo n.º 29
0
        public void BooleanTest()
        {
            string ser;

            using (SoodaTransaction tran = new SoodaTransaction())
            {
                PKBool test = new PKBool();
                PKBool test2 = PKBool.Load(true);

                Assert.AreEqual((string)test2.Data, "test data");

                ser = tran.Serialize();
                tran.Deserialize(ser);
                Assert.AreEqual(ser, tran.Serialize());
            }
        }
Exemplo n.º 30
0
        public void Test1()
        {
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                Assert.AreEqual("Mary Manager", Contact.Mary.Name);
                Assert.AreEqual("Ed Employee", Contact.Ed.Name);
                Assert.AreEqual("Group1", (string)Contact.Mary.PrimaryGroup.Name);

                Assert.AreEqual(ContactType.Manager, Contact.Mary.Type);
                Assert.AreEqual(ContactType.Employee, Contact.Ed.Type);
                Assert.AreEqual(ContactType.Employee, Contact.Eva.Type);

                Assert.IsTrue(Contact.Mary.PrimaryGroup.Members.Contains(Contact.Mary));
                Assert.IsTrue(Contact.Ed.PrimaryGroup.Members.Contains(Contact.Ed));
                Assert.IsTrue(Contact.Eva.PrimaryGroup.Members.Contains(Contact.Eva));
            }
        }
Exemplo n.º 31
0
        public void Test11()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();

                SoodaInProcessCache myCache = new SoodaInProcessCache();
                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);
                    tran.Cache         = myCache;
                    tran.CachingPolicy = new SoodaCacheAllPolicy();

                    string      ck1 = SoodaCache.GetCollectionKey("Vehicle", new SoodaWhereClause(true));
                    VehicleList c1  = Vehicle.GetList(true, SoodaSnapshotOptions.KeysOnly);

                    string   ck2 = SoodaCache.GetCollectionKey("Bike", new SoodaWhereClause(true));
                    BikeList c2  = Bike.GetList(true, SoodaSnapshotOptions.KeysOnly);

                    string   ck3 = SoodaCache.GetCollectionKey("Bike", new SoodaWhereClause(false));
                    BikeList c3  = Bike.GetList(false, SoodaSnapshotOptions.KeysOnly);

                    Assert.IsNotNull(myCache.LoadCollection(ck1));
                    Assert.IsNotNull(myCache.LoadCollection(ck2));
                    Assert.IsNotNull(myCache.LoadCollection(ck3));

                    myCache.Evict("Vehicle", 1);

                    // nothing is evicted from collection cache because we requested KeysOnly

                    Assert.IsNotNull(myCache.LoadCollection(ck1));
                    Assert.IsNotNull(myCache.LoadCollection(ck2));
                    Assert.IsNotNull(myCache.LoadCollection(ck3));
                }
            }
        }
Exemplo n.º 32
0
        public void SharedCollection1ToNTest()
        {
            using (SoodaTransaction tran = new SoodaTransaction())
            {
                Group g1 = Group.GetRef(10);

                Contact newManager = new Contact();
                newManager.Type = ContactType.Manager;

                Assert.AreEqual(g1.Managers.Count, 1);
                Assert.IsTrue(g1.Members.Contains(Contact.Mary));
                Assert.IsTrue(g1.Managers.Contains(Contact.Mary));
                g1.Managers.Remove(Contact.Mary);
                Assert.AreEqual(g1.Managers.Count, 0);
                Assert.IsTrue(!g1.Members.Contains(Contact.Mary));
                Assert.IsTrue(!g1.Managers.Contains(Contact.Mary));
                g1.Managers.Add(Contact.Mary);
                Assert.AreEqual(g1.Managers.Count, 1);
                Assert.IsTrue(g1.Members.Contains(Contact.Mary));
                Assert.IsTrue(g1.Managers.Contains(Contact.Mary));
                g1.Members.Add(newManager);
                Assert.AreEqual(g1.Managers.Count, 2);
            }
        }
Exemplo n.º 33
0
        public void Test9()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();

                SoodaInProcessCache myCache = new SoodaInProcessCache();
                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);
                    tran.Cache         = myCache;
                    tran.CachingPolicy = new SoodaCacheAllPolicy();

                    string      ck1 = SoodaCache.GetCollectionKey("Vehicle", new SoodaWhereClause(true));
                    VehicleList c1  = Vehicle.GetList(true);

                    string   ck2 = SoodaCache.GetCollectionKey("Bike", new SoodaWhereClause(true));
                    BikeList c2  = Bike.GetList(true);

                    string   ck3 = SoodaCache.GetCollectionKey("Bike", new SoodaWhereClause(false));
                    BikeList c3  = Bike.GetList(false);

                    Assert.IsNotNull(myCache.LoadCollection(ck1));
                    Assert.IsNotNull(myCache.LoadCollection(ck2));
                    Assert.IsNotNull(myCache.LoadCollection(ck3));

                    myCache.Evict("Vehicle", 3);

                    // ck3 is not evicted because it contains zero objects

                    Assert.IsNull(myCache.LoadCollection(ck1));
                    Assert.IsNull(myCache.LoadCollection(ck2));
                    Assert.IsNotNull(myCache.LoadCollection(ck3));
                }
            }
        }
Exemplo n.º 34
0
    static void Main()
    {
        using (SoodaTransaction transaction = new SoodaTransaction())
        {
            Category c = Category.Load(1);

            // display all beverage products
            foreach (Product p in c.ProductsInThisCategory)
            {
                Console.WriteLine("Product: {0} category: {1} price: {2}", p.Name, p.Category.Name, p.UnitPrice);
            }

            Console.WriteLine("The total number of beverages: {0}", c.ProductsInThisCategory.Count);

            Product p2 = Product.Load(1);

            // remove a particular product from the collection
            p2.ProductsInThisCategory.Remove(p2);

            // the new p2.Category == null (the product is no longer in the collection)
            Console.WriteLine("New category of product #1 is null: {0}", p2.Category == null);

            // iterate over first 3 products (in no particular order)
            foreach (Product p in c.ProductsInThisCategory.SelectFirst(3))
            {
                // do something
            }

            // note that the order is fixed after the collection is first accessed. subsequent accesses
            // can rely on the fact.

            // iterate over last 3 products (in no particular order)
            foreach (Product p in c.ProductsInThisCategory.SelectLast(3))
            {
                // do something
            }

            // iterate over the specified range of products (in no particular order)
            foreach (Product p in c.ProductsInThisCategory.SelectRange(3, 10))
            {
                // do something
            }

            // iterate over the products sorted by name
            foreach (Product p in c.ProductsInThisCategory.Sort(SoodaOrderBy.Ascending("Name").GetComparer()))
            {
                // do something
            }

            // get the snapshot of this collection. Updates to c.ProductsInThisCategory (direct or indirect)
            // will not affect the snapshot
            ProductList snapshot = c.ProductsInThisCategory.GetSnapshot();

            foreach (Product p in snapshot)
            {
                // do something
            }

            // another way to iterate the collection. Note that ProductList returns strongly-typed instances
            // of Product

            for (int i = 0; i < snapshot.Count; ++i)
            {
                Product p3 = snapshot[i];
            }
        }
    }
Exemplo n.º 35
0
 protected override void OnPreInit(EventArgs e)
 {
     _transaction = new SoodaTransaction(ObjectsAssembly);
 }
Exemplo n.º 36
0
        public static SoodaObject GetRefFieldValue(ref SoodaObject refCache, SoodaObject theObject, int tableNumber, int fieldOrdinal, SoodaTransaction tran, ISoodaObjectFactory factory)
        {
            if (refCache != null)
            {
                return(refCache);
            }

            theObject.EnsureDataLoaded(tableNumber);

            if (theObject._fieldValues.IsNull(fieldOrdinal))
            {
                return(null);
            }

            refCache = factory.GetRef(tran, theObject._fieldValues.GetBoxedFieldValue(fieldOrdinal));
            return(refCache);
        }
Exemplo n.º 37
0
 public PKInt32(SoodaTransaction transaction)
     :
     base(transaction)
 {
 }
        private void LoadList(SoodaTransaction transaction, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, SoodaSnapshotOptions options, string[] involvedClassNames, bool useCache)
        {
            ISoodaObjectFactory factory = transaction.GetFactory(classInfo);
            string cacheKey             = null;

            if (useCache)
            {
                // cache makes sense only on clean database
                if (!transaction.HasBeenPrecommitted(classInfo))
                {
                    cacheKey = SoodaCache.GetCollectionKey(classInfo, whereClause);
                }

                IEnumerable keysCollection = transaction.LoadCollectionFromCache(cacheKey, logger);
                if (keysCollection != null)
                {
                    foreach (object o in keysCollection)
                    {
                        SoodaObject obj = factory.GetRef(transaction, o);
                        // this binds to cache
                        obj.EnsureFieldsInited();
                        items.Add(obj);
                    }

                    if (orderBy != null)
                    {
                        items.Sort(orderBy.GetComparer());
                    }
                    count = items.Count;

                    if (startIdx > 0)
                    {
                        if (startIdx < count)
                        {
                            items.RemoveRange(0, startIdx);
                        }
                        else
                        {
                            items.Clear();
                        }
                    }

                    if (pageCount != -1 && pageCount < items.Count)
                    {
                        items.RemoveRange(pageCount, items.Count - pageCount);
                    }

                    return;
                }
            }

            SoodaDataSource ds = transaction.OpenDataSource(classInfo.GetDataSource());

            if ((options & SoodaSnapshotOptions.KeysOnly) != 0)
            {
                if (pageCount != -1)
                {
                    using (IDataReader reader = ds.LoadMatchingPrimaryKeys(transaction.Schema, classInfo, whereClause, orderBy, 0, -1))
                    {
                        count = 0;
                        while (reader.Read())
                        {
                            count++;
                        }
                    }
                }
                using (IDataReader reader = ds.LoadMatchingPrimaryKeys(transaction.Schema, classInfo, whereClause, orderBy, startIdx, pageCount))
                {
                    while (reader.Read())
                    {
                        SoodaObject obj = SoodaObject.GetRefFromKeyRecordHelper(transaction, factory, reader);
                        items.Add(obj);
                    }
                    if (pageCount == -1)
                    {
                        count = items.Count;
                    }
                }
            }
            else
            {
                if (pageCount != -1)
                {
                    using (IDataReader reader = ds.LoadMatchingPrimaryKeys(transaction.Schema, classInfo, whereClause, orderBy, 0, -1))
                    {
                        count = 0;
                        while (reader.Read())
                        {
                            count++;
                        }
                    }
                }

                TableInfo[] loadedTables;

                using (IDataReader reader = ds.LoadObjectList(transaction.Schema, classInfo, whereClause, orderBy, startIdx, pageCount, options, out loadedTables))
                {
                    while (reader.Read())
                    {
                        SoodaObject obj = SoodaObject.GetRefFromRecordHelper(transaction, factory, reader, 0, loadedTables, 0);
                        if ((options & SoodaSnapshotOptions.VerifyAfterLoad) != 0 && whereClause != null && !whereClause.Matches(obj, false))
                        {
                            continue; // don't add the object
                        }
                        items.Add(obj);
                    }
                    if (pageCount == -1)
                    {
                        count = items.Count;
                    }
                }
            }

            if (cacheKey != null && useCache && startIdx == 0 && pageCount == -1 && involvedClassNames != null)
            {
                TimeSpan expirationTimeout;
                bool     slidingExpiration;

                if (transaction.CachingPolicy.GetExpirationTimeout(
                        classInfo, whereClause, orderBy, startIdx, pageCount, items.Count,
                        out expirationTimeout, out slidingExpiration))
                {
                    transaction.StoreCollectionInCache(cacheKey, classInfo, items, involvedClassNames, (options & SoodaSnapshotOptions.KeysOnly) == 0, expirationTimeout, slidingExpiration);
                }
            }
        }
Exemplo n.º 39
0
        public static SoodaObject TryGetRefFieldValue(ref SoodaObject refCache, object fieldValue, SoodaTransaction tran, ISoodaObjectFactory factory)
        {
            if (refCache != null)
            {
                return(refCache);
            }

            if (fieldValue == null)
            {
                return(null);
            }

            refCache = factory.TryGet(tran, fieldValue);
            return(refCache);
        }
Exemplo n.º 40
0
 protected SoodaObjectCollectionBase(SoodaTransaction transaction, ClassInfo classInfo)
 {
     this.transaction = transaction;
     this.classInfo   = classInfo;
 }
Exemplo n.º 41
0
        public void UnrestrictedWhere()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();
                SoodaCache.DefaultCache.Clear();;

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);

                    int ch0 = tran.Statistics.CollectionCacheHits;
                    int cm0 = tran.Statistics.CollectionCacheMisses;

                    ContactList cl = Contact.GetList(SoodaWhereClause.Unrestricted, SoodaSnapshotOptions.Cache);
                    Assert.AreEqual(7, cl.Count);

                    // we've had a miss
                    int ch1 = tran.Statistics.CollectionCacheHits;
                    int cm1 = tran.Statistics.CollectionCacheMisses;
                    Assert.AreEqual(ch0, ch1);
                    // Assert.AreNotEqual(cm0, cm1);

                    ContactList cl2 = Contact.GetList(SoodaWhereClause.Unrestricted, SoodaSnapshotOptions.Cache);
                    Assert.AreEqual(7, cl2.Count);

                    // we've had a hit
                    int ch2 = tran.Statistics.CollectionCacheHits;
                    int cm2 = tran.Statistics.CollectionCacheMisses;
                    // Assert.AreNotEqual(ch1, ch2);
                    Assert.AreEqual(cm1, cm2);

                    ContactList cl3 = Contact.GetList(SoodaWhereClause.Unrestricted, 3, SoodaSnapshotOptions.Cache);
                    Assert.AreEqual(3, cl3.Count);

                    // Dump("after top no sort", cl3);

                    // we've had a hit
                    int ch3 = tran.Statistics.CollectionCacheHits;
                    int cm3 = tran.Statistics.CollectionCacheMisses;
                    // Assert.AreNotEqual(ch2, ch3);
                    Assert.AreEqual(cm1, cm3);

                    ContactList cl4 = Contact.GetList(SoodaWhereClause.Unrestricted, SoodaOrderBy.Descending("Name"), SoodaSnapshotOptions.Cache);
                    Assert.AreEqual(7, cl4.Count);

                    Assert.AreSame(cl4[0], Contact.Mary);
                    // Dump("after sort, no top", cl4);

                    // we've had a hit
                    int ch4 = tran.Statistics.CollectionCacheHits;
                    int cm4 = tran.Statistics.CollectionCacheMisses;
                    // Assert.AreNotEqual(ch3, ch4);
                    Assert.AreEqual(cm1, cm4);

                    ContactList cl5 = Contact.GetList(SoodaWhereClause.Unrestricted, SoodaOrderBy.Descending("LastSalary"), 4, SoodaSnapshotOptions.Cache);
                    Assert.AreEqual(4, cl5.Count);

                    Assert.AreSame(cl5[0], Contact.Eva);
                    // Dump("after sort by salary, with top", cl5);

                    // we've had a hit
                    int ch5 = tran.Statistics.CollectionCacheHits;
                    int cm5 = tran.Statistics.CollectionCacheMisses;
                    // Assert.AreNotEqual(ch4, ch5);
                    Assert.AreEqual(cm1, cm5);
                }
            }
        }
Exemplo n.º 42
0
 static void Main()
 {
     using (SoodaTransaction transaction = new SoodaTransaction()) {
     }
 }
Exemplo n.º 43
0
 public Vehicle(SoodaTransaction transaction) : base(transaction)
 {
     //
     // TODO: Add construction logic here.
     //
 }
Exemplo n.º 44
0
        public void Collection1toNTest(bool quiet)
        {
            string serialized;

            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);
                    Contact c1;
                    Group   g = Group.Load(10);

                    Assert.AreEqual((string)g.Manager.Name, "Mary Manager");
                    Assert.AreEqual(g.Members.Count, 4);
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(53)));
                    Console.WriteLine("oldgroup: {0}", Contact.GetRef(53).PrimaryGroup);
                    g.Members.Remove(Contact.GetRef(53));
                    Console.WriteLine("newgroup: {0}", Contact.GetRef(53).PrimaryGroup);
                    Assert.AreEqual(g.Members.Count, 3);
                    Assert.IsTrue(!g.Members.Contains(Contact.GetRef(53)));

                    g.Members.Add(c1 = new Contact());
                    c1.Name          = "Nancy Newcomer";
                    c1.Active        = true;
                    c1.Type          = ContactType.Employee;
                    Assert.AreEqual(g.Members.Count, 4);

                    //Console.WriteLine(ContactType.Employee.GetPrimaryKeyValue());
                    //Console.WriteLine(ContactType.Employee.Code);
                    //Console.WriteLine(ContactType.Employee.Description);

                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(51)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(1)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(2)));
                    int times = 0;
                    foreach (Contact c in g.Members)
                    {
                        if (!quiet)
                        {
                            Console.WriteLine("Got {0} [{1}]", c.Name, c.ContactId);
                        }
                        times++;
                        Assert.IsTrue(
                            c == Contact.GetRef(51) ||
                            c == Contact.GetRef(1) ||
                            c == c1 ||
                            c == Contact.GetRef(2));
                    }
                    ;
                    Assert.AreEqual(times, 4, "foreach() loop gets called 4 times");
                    Assert.IsTrue(!g.Members.Contains(Contact.GetRef(53)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(51)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(1)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(2)));
                    Assert.IsTrue(g.Members.Contains(c1));
                    Assert.AreEqual(g.Members.Count, 4);

                    foreach (Contact c in g.Members)
                    {
                        if (!quiet)
                        {
                            Console.WriteLine("before serialization, member: {0}", c.Name);
                        }
                    }
                    serialized = tran.Serialize(SoodaSerializeOptions.IncludeNonDirtyFields | SoodaSerializeOptions.IncludeNonDirtyObjects | SoodaSerializeOptions.Canonical);
                    //serialized = tran.Serialize();
                    if (!quiet)
                    {
                        Console.WriteLine("Serialized as\n{0}", serialized);
                    }
                }

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);
                    tran.Deserialize(serialized);

                    Console.WriteLine(ContactType.Employee.Description);
                    string serialized2 = tran.Serialize(SoodaSerializeOptions.IncludeNonDirtyFields | SoodaSerializeOptions.IncludeNonDirtyObjects | SoodaSerializeOptions.Canonical);
                    //string serialized2 = tran.Serialize();
                    if (serialized == serialized2)
                    {
                        if (!quiet)
                        {
                            Console.WriteLine("Serialization is stable\n{0}", serialized);
                        }
                    }
                    else
                    {
                        if (!quiet)
                        {
                            Console.WriteLine("Serialized again as\n{0}", serialized2);
                        }
                    }
                    Assert.AreEqual(serialized, serialized2, "Serialization preserves state");


                    Group g = Group.Load(10);

                    foreach (Contact c in g.Members)
                    {
                        //if (!quiet)
                        Console.WriteLine("after deserialization, member: {0}", c.Name);
                    }
                    Assert.AreEqual("Mary Manager", g.Manager.Name);
                    Assert.AreEqual(4, g.Members.Count);
                    Assert.IsTrue(!g.Members.Contains(Contact.GetRef(53)));

                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(51)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(1)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(2)));
                    int times = 0;
                    foreach (Contact c in g.Members)
                    {
                        times++;
                        Assert.IsTrue(
                            c == Contact.GetRef(51) ||
                            c == Contact.GetRef(1) ||
                            (string)c.Name == "Nancy Newcomer" ||
                            c == Contact.GetRef(2));
                    }
                    ;
                    Assert.AreEqual(times, 4, "foreach() loop gets called 4 times");
                    Assert.IsTrue(!g.Members.Contains(Contact.GetRef(53)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(51)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(1)));
                    Assert.IsTrue(g.Members.Contains(Contact.GetRef(2)));
                    Assert.AreEqual(g.Members.Count, 4);
                    tran.Commit();
                }
            }
        }
Exemplo n.º 45
0
        public void LongTest()
        {
            // run multiple Sooda transactions in a single SQL transaction
            // this is achieved by a hacked SqlDataSource which ignores
            // Close(), Commit() requests

            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();

                int ebKey;
                int bikeKey;
                int gvKey;

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);

                    Vehicle v = new Bike();
                    Assert.AreEqual(v.Type, 2);
                    ((Bike)v).TwoWheels = 1;

                    ExtendedBike eb = new ExtendedBike();
                    Assert.AreEqual(eb.Type, 7);
                    eb.TwoWheels        = 1;
                    eb.ExtendedBikeInfo = "some info here";
                    ebKey   = eb.Id;
                    bikeKey = v.Id;

                    Contact.Mary.Bikes.Add((Bike)v);
                    Contact.Mary.Bikes.Add(eb);

                    Contact.Mary.Vehicles.Add(v);
                    Contact.Mary.Vehicles.Add(eb);

                    v     = new Bike();
                    gvKey = v.Id;
                    Contact.Mary.Vehicles.Add(v);

                    tran.Commit();
                }

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);

                    Vehicle v1 = Vehicle.GetRef(ebKey);
                    Assert.IsTrue(v1 is ExtendedBike);

                    Vehicle v2 = Vehicle.GetRef(bikeKey);
                    Assert.IsTrue(v2.GetType() == typeof(Bike));

                    Assert.IsTrue(Contact.Mary.Vehicles.Contains(Vehicle.GetRef(gvKey)));
                    Assert.IsTrue(Contact.Mary.Vehicles.Contains(v1));
                    Assert.IsTrue(Contact.Mary.Vehicles.Contains(v2));

                    Contact.Mary.Vehicles.Remove(v1);
                    Contact.Mary.Vehicles.Remove(v2);

                    Assert.IsTrue(!Contact.Mary.Vehicles.Contains(v1));
                    Assert.IsTrue(!Contact.Mary.Vehicles.Contains(v2));

                    Assert.IsTrue(Contact.Mary.Bikes.Contains((Bike)v1));
                    Assert.IsTrue(Contact.Mary.Bikes.Contains((Bike)v2));

                    Contact.Mary.Bikes.Remove((Bike)v1);

                    try
                    {
                        Vehicle v3 = ExtendedBike.GetRef(bikeKey);
                        Assert.Fail("Vehicle v3 = ExtendedBike.GetRef(bikeKey); should fail here.");
                    }
                    catch (SoodaObjectNotFoundException)
                    {
                        Console.WriteLine("Got exception as expected");
                    }

                    tran.Commit();
                }
                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);

                    Vehicle v1 = Vehicle.GetRef(ebKey);
                    Assert.IsTrue(v1 is ExtendedBike);

                    Vehicle v2 = Vehicle.GetRef(bikeKey);
                    Assert.IsTrue(v2.GetType() == typeof(Bike));

                    Assert.IsTrue(!Contact.Mary.Vehicles.Contains(v1));
                    Assert.IsTrue(!Contact.Mary.Vehicles.Contains(v2));

                    Assert.IsTrue(!Contact.Mary.Bikes.Contains((Bike)v1));
                    Assert.IsTrue(Contact.Mary.Bikes.Contains((Bike)v2));

                    tran.Commit();
                }
            }
        }
Exemplo n.º 46
0
        static void Remove(string name, SoodaTransaction tran)
        {
            FieldInfo fi = tran.Schema.FindClassByName("PKInt32").FindFieldByName(name);

            DynamicFieldManager.Remove(fi, tran);
        }