コード例 #1
0
 public override bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount)
 {
     if (classInfo.Cardinality == ClassCardinality.Small || classInfo.Cardinality == ClassCardinality.Medium)
         return true;
     else
         return false;
 }
コード例 #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();
                }
            }
        }
コード例 #3
0
        protected override IEnumerable ExecuteSelect(System.Web.UI.DataSourceSelectArguments arguments)
        {
            object value;

            if (_owner.List != null)
            {
                value = _owner.List;
            }
            else if (_owner.Key != null)
            {
                value = SoodaTransaction.ActiveTransaction.GetObject(_owner.ClassName, _owner.Key);
            }
            else
            {
                ISoodaObjectFactory factory = SoodaTransaction.ActiveTransaction.GetFactory(_owner.ClassName, true);
                SoodaOrderBy        orderBy = SoodaOrderBy.Unsorted;

                if (arguments.SortExpression != "")
                {
                    orderBy = SoodaOrderBy.Parse(arguments.SortExpression);
                }

                value = factory.GetList(
                    SoodaTransaction.ActiveTransaction,
                    new SoodaWhereClause(_owner.WhereClause),
                    orderBy,
                    SoodaSnapshotOptions.Default);
            }
            return(value as IEnumerable ?? new object[] { value });
        }
コード例 #4
0
ファイル: LabelTest.cs プロジェクト: valery-shinkevich/sooda
 public void Count()
 {
     using (new SoodaTransaction())
     {
         GroupList groups = Group.GetList(true, SoodaOrderBy.Ascending("Id"));
         Assert.AreEqual(2, groups.Count);
         Assert.AreEqual("Sooda.UnitTests.BaseObjects.ContactList", groups[0].GetLabel(false));
         Assert.AreEqual("Sooda.UnitTests.BaseObjects.ContactList", groups[1].GetLabel(false));
     }
 }
コード例 #5
0
ファイル: LabelTest.cs プロジェクト: valery-shinkevich/sooda
 public void Int()
 {
     using (new SoodaTransaction())
     {
         RoleList roles = Role.GetList(true, SoodaOrderBy.Ascending("Id"));
         Assert.AreEqual(3, roles.Count);
         Assert.AreEqual("1", roles[0].GetLabel(false));
         Assert.AreEqual("2", roles[1].GetLabel(false));
         Assert.AreEqual("3", roles[2].GetLabel(false));
     }
 }
コード例 #6
0
        public void SetOrderBy(SoodaOrderBy orderBy)
        {
            OrderByExpressions.AddRange(orderBy.OrderByExpressions);
            foreach (SortOrder so in orderBy.SortOrders)
            {
                if (so == SortOrder.Ascending)
                    OrderByOrder.Add("asc");
                else
                    OrderByOrder.Add("desc");
            }

        }
コード例 #7
0
ファイル: LabelTest.cs プロジェクト: valery-shinkevich/sooda
 public void Basic()
 {
     using (new SoodaTransaction())
     {
         ContactList contacts = Contact.GetList(true, SoodaOrderBy.Ascending("ContactId"));
         Assert.AreEqual(7, contacts.Count);
         Assert.AreEqual("Mary Manager", contacts[0].GetLabel(false));
         Assert.AreEqual("Ed Employee", contacts[1].GetLabel(false));
         Assert.AreEqual("Eva Employee", contacts[2].GetLabel(false));
         Assert.AreEqual("Catie Customer", contacts[3].GetLabel(false));
         Assert.AreEqual("Caroline Customer", contacts[4].GetLabel(false));
         Assert.AreEqual("Chris Customer", contacts[5].GetLabel(false));
         Assert.AreEqual("Chuck Customer", contacts[6].GetLabel(false));
     }
 }
コード例 #8
0
 public void SetOrderBy(SoodaOrderBy orderBy)
 {
     OrderByExpressions.AddRange(orderBy.OrderByExpressions);
     foreach (SortOrder so in orderBy.SortOrders)
     {
         if (so == SortOrder.Ascending)
         {
             OrderByOrder.Add("asc");
         }
         else
         {
             OrderByOrder.Add("desc");
         }
     }
 }
コード例 #9
0
ファイル: LabelTest.cs プロジェクト: valery-shinkevich/sooda
 public void ReferencedObject()
 {
     using (new SoodaTransaction())
     {
         VehicleList vehicles = Vehicle.GetList(true, SoodaOrderBy.Ascending("Id"));
         Assert.AreEqual(9, vehicles.Count);
         Assert.AreEqual(string.Empty, vehicles[0].GetLabel(false));
         Assert.AreEqual("Mary Manager", vehicles[1].GetLabel(false));
         Assert.AreEqual("Ed Employee", vehicles[2].GetLabel(false));
         Assert.AreEqual(string.Empty, vehicles[3].GetLabel(false));
         Assert.AreEqual(string.Empty, vehicles[4].GetLabel(false));
         Assert.AreEqual(string.Empty, vehicles[5].GetLabel(false));
         Assert.AreEqual(string.Empty, vehicles[6].GetLabel(false));
         Assert.AreEqual(string.Empty, vehicles[7].GetLabel(false));
         Assert.AreEqual(string.Empty, vehicles[8].GetLabel(false));
     }
 }
コード例 #10
0
        public void OrderByTest3()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();

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

                    l = Contact.GetList(tran, new SoodaWhereClause("Name like '%customer'"), SoodaOrderBy.Ascending("Name"));

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

                    Assert.IsTrue(l.IndexOf(Contact.Mary) == -1);
                    Assert.IsTrue(l.IndexOf(Contact.Ed) == -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();
                }
            }
        }
コード例 #11
0
 public override bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount)
 {
     return false;
 }
コード例 #12
0
 public override bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount)
 {
     if (classInfo.Cardinality == ClassCardinality.Small)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #13
0
        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);
                }
            }
        }
コード例 #14
0
        public SoodaObjectListSnapshot(SoodaTransaction t, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, SoodaSnapshotOptions options, ClassInfo ci)
        {
            this.classInfo = ci;
            string[] involvedClasses = null;

            bool useCache;
            if ((options & SoodaSnapshotOptions.NoCache) != 0)
                useCache = false;
            else if ((options & SoodaSnapshotOptions.Cache) != 0)
                useCache = true;
            else
                useCache = t.CachingPolicy.ShouldCacheCollection(ci, whereClause, orderBy, startIdx, pageCount);

            if (whereClause != null && whereClause.WhereExpression != null)
            {
                if ((options & SoodaSnapshotOptions.NoWriteObjects) == 0 || useCache)
                {
                    try
                    {
                        GetInvolvedClassesVisitor gic = new GetInvolvedClassesVisitor(classInfo);
                        gic.GetInvolvedClasses(whereClause.WhereExpression);
                        involvedClasses = gic.ClassNames;
                    }
                    catch
                    {
                        // logger.Warn("{0}", ex);
                        // cannot detect involved classes (probably because of RAWQUERY)
                        // - precommit all objects
                        // if we get here, involvedClasses remains set to null
                    }
                }
            }
            else
            {
                // no where clause

                involvedClasses = new string[] { ci.Name };
            }

            if ((options & SoodaSnapshotOptions.NoWriteObjects) == 0)
                t.PrecommitClasses(involvedClasses);

            LoadList(t, whereClause, orderBy, startIdx, pageCount, options, involvedClasses, useCache);
        }
コード例 #15
0
 public bool GetExpirationTimeout(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, int itemCount, out TimeSpan expirationTimeout, out bool slidingExpiration)
 {
     expirationTimeout = _expirationTimeout;
     slidingExpiration = _slidingExpiration;
     return(true);
 }
コード例 #16
0
        public override IDataReader LoadObjectList(SchemaInfo schemaInfo, ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, SoodaSnapshotOptions options, out TableInfo[] tables)
        {
            try
            {
                Queue <_QueueItem> queue = new Queue <_QueueItem>();

                List <TableInfo>    tablesArrayList = new List <TableInfo>(classInfo.UnifiedTables.Count);
                SoqlQueryExpression queryExpression = new SoqlQueryExpression();
                queryExpression.StartIdx  = startIdx;
                queryExpression.PageCount = pageCount;
                queryExpression.From.Add(classInfo.Name);
                queryExpression.FromAliases.Add("");
                foreach (TableInfo ti in classInfo.UnifiedTables)
                {
                    tablesArrayList.Add(ti);
                    foreach (FieldInfo fi in ti.Fields)
                    {
                        SoqlPathExpression pathExpr = new SoqlPathExpression(fi.Name);
                        queryExpression.SelectExpressions.Add(pathExpr);
                        queryExpression.SelectAliases.Add("");

                        if (fi.ReferencedClass != null && fi.PrefetchLevel > 0 && ((options & SoodaSnapshotOptions.PrefetchRelated) != 0))
                        {
                            _QueueItem item = new _QueueItem();
                            item.classInfo = fi.ReferencedClass;
                            item.level     = fi.PrefetchLevel;
                            item.prefix    = pathExpr;
                            queue.Enqueue(item);
                        }
                    }
                }

                while (queue.Count > 0)
                {
                    _QueueItem it = queue.Dequeue();

                    foreach (TableInfo ti in it.classInfo.UnifiedTables)
                    {
                        tablesArrayList.Add(ti);

                        foreach (FieldInfo fi in ti.Fields)
                        {
                            // TODO - this relies on the fact that path expressions
                            // are never reconstructed or broken. We simply share previous prefix
                            // perhaps it's cleaner to Clone() the expression here

                            SoqlPathExpression extendedExpression = new SoqlPathExpression(it.prefix, fi.Name);

                            queryExpression.SelectExpressions.Add(extendedExpression);
                            queryExpression.SelectAliases.Add("");

                            if (it.level >= 1 && fi.PrefetchLevel > 0 && fi.ReferencedClass != null)
                            {
                                _QueueItem newItem = new _QueueItem();
                                newItem.classInfo = fi.ReferencedClass;
                                newItem.prefix    = extendedExpression;
                                newItem.level     = it.level - 1;
                                queue.Enqueue(newItem);
                            }
                        }
                    }
                }

                if (whereClause != null && whereClause.WhereExpression != null)
                {
                    queryExpression.WhereClause = whereClause.WhereExpression;
                }

                if (orderBy != null)
                {
                    queryExpression.SetOrderBy(orderBy);
                }

                string query = SoqlToSql(queryExpression, schemaInfo, false);

                IDbCommand cmd = Connection.CreateCommand();

                try
                {
                    cmd.CommandTimeout = CommandTimeout;
                }
                catch (NotSupportedException e)
                {
                    logger.Debug("CommandTimeout not supported. {0}", e.Message);
                }

                if (Transaction != null)
                {
                    cmd.Transaction = this.Transaction;
                }

                SqlBuilder.BuildCommandWithParameters(cmd, false, query, whereClause.Parameters, false);

                tables = tablesArrayList.ToArray();
                return(TimedExecuteReader(cmd));
            }
            catch (Exception ex)
            {
                logger.Error("Exception in LoadObjectList: {0}", ex);
                throw;
            }
        }
コード例 #17
0
 public override bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount)
 {
     return(true);
 }
コード例 #18
0
 protected ISoodaObjectList Sort2(Sooda.QL.SoqlExpression sortExpression, SortOrder sortOrder)
 {
     return(_theList.Sort(SoodaOrderBy.FromExpression(sortExpression, sortOrder).GetComparer()));
 }
コード例 #19
0
 protected ISoodaObjectList Sort2(string sortOrder)
 {
     return(_theList.Sort(SoodaOrderBy.Parse(sortOrder).GetComparer()));
 }
コード例 #20
0
 ISoodaObjectList ISoodaObjectList.Sort(Sooda.QL.SoqlExpression sortExpression)
 {
     return(_theList.Sort(SoodaOrderBy.FromExpression(sortExpression, SortOrder.Ascending).GetComparer()));
 }
コード例 #21
0
        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);
                }
            }
        }
コード例 #22
0
        public override IDataReader LoadMatchingPrimaryKeys(SchemaInfo schemaInfo, ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount)
        {
            try
            {
                SoqlQueryExpression queryExpression = new SoqlQueryExpression();
                foreach (FieldInfo fi in classInfo.GetPrimaryKeyFields())
                {
                    queryExpression.SelectExpressions.Add(new SoqlPathExpression(fi.Name));
                    queryExpression.SelectAliases.Add("");

                }
                if (schemaInfo.GetSubclasses(classInfo).Count > 0)
                {
                    queryExpression.SelectExpressions.Add(new SoqlPathExpression(classInfo.SubclassSelectorField.Name));
                    queryExpression.SelectAliases.Add("");
                }
                queryExpression.StartIdx = startIdx;
                queryExpression.PageCount = pageCount;
                queryExpression.From.Add(classInfo.Name);
                queryExpression.FromAliases.Add("");
                if (whereClause != null && whereClause.WhereExpression != null)
                {
                    queryExpression.WhereClause = whereClause.WhereExpression;
                }

                if (orderBy != null)
                {
                    queryExpression.SetOrderBy(orderBy);
                }

                string query = SoqlToSql(queryExpression, schemaInfo, false);

                IDbCommand cmd = Connection.CreateCommand();
                try
                {
                    cmd.CommandTimeout = CommandTimeout;
                }
                catch (NotSupportedException e)
                {
                    logger.Debug("CommandTimeout not supported. {0}", e.Message);
                }

                if (Transaction != null)
                    cmd.Transaction = this.Transaction;

                SqlBuilder.BuildCommandWithParameters(cmd, false, query, whereClause.Parameters, false);

                return TimedExecuteReader(cmd);
            }
            catch (Exception ex)
            {
                logger.Error("Exception in LoadMatchingPrimaryKeys: {0}", ex);
                throw;
            }
        }
コード例 #23
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);
                }
            }
        }
コード例 #24
0
        public override IDataReader LoadObjectList(SchemaInfo schemaInfo, ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, SoodaSnapshotOptions options, out TableInfo[] tables)
        {
            try
            {
                Queue<_QueueItem> queue = new Queue<_QueueItem>();

                List<TableInfo> tablesArrayList = new List<TableInfo>(classInfo.UnifiedTables.Count);
                SoqlQueryExpression queryExpression = new SoqlQueryExpression();
                queryExpression.StartIdx = startIdx;
                queryExpression.PageCount = pageCount;
                queryExpression.From.Add(classInfo.Name);
                queryExpression.FromAliases.Add("");
                foreach (TableInfo ti in classInfo.UnifiedTables)
                {
                    tablesArrayList.Add(ti);
                    foreach (FieldInfo fi in ti.Fields)
                    {
                        SoqlPathExpression pathExpr = new SoqlPathExpression(fi.Name);
                        queryExpression.SelectExpressions.Add(pathExpr);
                        queryExpression.SelectAliases.Add("");

                        if (fi.ReferencedClass != null && fi.PrefetchLevel > 0 && ((options & SoodaSnapshotOptions.PrefetchRelated) != 0))
                        {
                            _QueueItem item = new _QueueItem();
                            item.classInfo = fi.ReferencedClass;
                            item.level = fi.PrefetchLevel;
                            item.prefix = pathExpr;
                            queue.Enqueue(item);
                        }
                    }
                }

                while (queue.Count > 0)
                {
                    _QueueItem it = queue.Dequeue();

                    foreach (TableInfo ti in it.classInfo.UnifiedTables)
                    {
                        tablesArrayList.Add(ti);

                        foreach (FieldInfo fi in ti.Fields)
                        {
                            // TODO - this relies on the fact that path expressions
                            // are never reconstructed or broken. We simply share previous prefix
                            // perhaps it's cleaner to Clone() the expression here

                            SoqlPathExpression extendedExpression = new SoqlPathExpression(it.prefix, fi.Name);

                            queryExpression.SelectExpressions.Add(extendedExpression);
                            queryExpression.SelectAliases.Add("");

                            if (it.level >= 1 && fi.PrefetchLevel > 0 && fi.ReferencedClass != null)
                            {
                                _QueueItem newItem = new _QueueItem();
                                newItem.classInfo = fi.ReferencedClass;
                                newItem.prefix = extendedExpression;
                                newItem.level = it.level - 1;
                                queue.Enqueue(newItem);
                            }
                        }
                    }
                }

                if (whereClause != null && whereClause.WhereExpression != null)
                {
                    queryExpression.WhereClause = whereClause.WhereExpression;
                }

                if (orderBy != null)
                {
                    queryExpression.SetOrderBy(orderBy);
                }

                string query = SoqlToSql(queryExpression, schemaInfo, false);

                IDbCommand cmd = Connection.CreateCommand();

                try
                {
                    cmd.CommandTimeout = CommandTimeout;
                }
                catch (NotSupportedException e)
                {
                    logger.Debug("CommandTimeout not supported. {0}", e.Message);
                }

                if (Transaction != null)
                    cmd.Transaction = this.Transaction;

                SqlBuilder.BuildCommandWithParameters(cmd, false, query, whereClause.Parameters, false);

                tables = tablesArrayList.ToArray();
                return TimedExecuteReader(cmd);
            }
            catch (Exception ex)
            {
                logger.Error("Exception in LoadObjectList: {0}", ex);
                throw;
            }
        }
コード例 #25
0
        public override IDataReader LoadMatchingPrimaryKeys(SchemaInfo schemaInfo, ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount)
        {
            try
            {
                SoqlQueryExpression queryExpression = new SoqlQueryExpression();
                foreach (FieldInfo fi in classInfo.GetPrimaryKeyFields())
                {
                    queryExpression.SelectExpressions.Add(new SoqlPathExpression(fi.Name));
                    queryExpression.SelectAliases.Add("");
                }
                if (schemaInfo.GetSubclasses(classInfo).Count > 0)
                {
                    queryExpression.SelectExpressions.Add(new SoqlPathExpression(classInfo.SubclassSelectorField.Name));
                    queryExpression.SelectAliases.Add("");
                }
                queryExpression.StartIdx  = startIdx;
                queryExpression.PageCount = pageCount;
                queryExpression.From.Add(classInfo.Name);
                queryExpression.FromAliases.Add("");
                if (whereClause != null && whereClause.WhereExpression != null)
                {
                    queryExpression.WhereClause = whereClause.WhereExpression;
                }

                if (orderBy != null)
                {
                    queryExpression.SetOrderBy(orderBy);
                }

                string query = SoqlToSql(queryExpression, schemaInfo, false);

                IDbCommand cmd = Connection.CreateCommand();
                try
                {
                    cmd.CommandTimeout = CommandTimeout;
                }
                catch (NotSupportedException e)
                {
                    logger.Debug("CommandTimeout not supported. {0}", e.Message);
                }

                if (Transaction != null)
                {
                    cmd.Transaction = this.Transaction;
                }

                SqlBuilder.BuildCommandWithParameters(cmd, false, query, whereClause.Parameters, false);

                return(TimedExecuteReader(cmd));
            }
            catch (Exception ex)
            {
                logger.Error("Exception in LoadMatchingPrimaryKeys: {0}", ex);
                throw;
            }
        }
コード例 #26
0
 public bool GetExpirationTimeout(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, int itemCount, out TimeSpan expirationTimeout, out bool slidingExpiration)
 {
     expirationTimeout = _expirationTimeout;
     slidingExpiration = _slidingExpiration;
     return true;
 }
コード例 #27
0
 public abstract bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount);
コード例 #28
0
        public SoodaObjectListSnapshot(SoodaTransaction t, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, SoodaSnapshotOptions options, ClassInfo ci)
        {
            this.classInfo = ci;
            string[] involvedClasses = null;

            bool useCache;

            if ((options & SoodaSnapshotOptions.NoCache) != 0)
            {
                useCache = false;
            }
            else if ((options & SoodaSnapshotOptions.Cache) != 0)
            {
                useCache = true;
            }
            else
            {
                useCache = t.CachingPolicy.ShouldCacheCollection(ci, whereClause, orderBy, startIdx, pageCount);
            }

            if (whereClause != null && whereClause.WhereExpression != null)
            {
                if ((options & SoodaSnapshotOptions.NoWriteObjects) == 0 || useCache)
                {
                    try
                    {
                        GetInvolvedClassesVisitor gic = new GetInvolvedClassesVisitor(classInfo);
                        gic.GetInvolvedClasses(whereClause.WhereExpression);
                        involvedClasses = gic.ClassNames;
                    }
                    catch
                    {
                        // logger.Warn("{0}", ex);
                        // cannot detect involved classes (probably because of RAWQUERY)
                        // - precommit all objects
                        // if we get here, involvedClasses remains set to null
                    }
                }
            }
            else
            {
                // no where clause

                involvedClasses = new string[] { ci.Name };
            }

            if ((options & SoodaSnapshotOptions.NoWriteObjects) == 0)
            {
                t.PrecommitClasses(involvedClasses);
            }

            LoadList(t, whereClause, orderBy, startIdx, pageCount, options, involvedClasses, useCache);
        }
コード例 #29
0
 public abstract bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount);
コード例 #30
0
ファイル: sample10.cs プロジェクト: valery-shinkevich/sooda
    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];
            }
        }
    }