Example #1
0
        public void LimitBehaviorTypesRender(LimitBehavior limitBehavior, OffsetBehavior offsetBehavior, string expectedResult)
        {
            var dialect = new GenericDialect {
                LimitBehavior = limitBehavior, OffsetBehavior = offsetBehavior
            };
            var context = new RenderContext(dialect);

            var sut = new Select
            {
                Columns = "Column",
                From    = "Table",
                Limit   = 5
            } as ISqlElement;

            sut.Render(context);

            Assert.Equal(expectedResult, context.CommandText);
        }
Example #2
0
        public Post[] ReadAllPosts(PostState state, int limit, LimitBehavior limitBehavior)
        {
            var result = new List <Post>();

            SQLiteDataReader posts;

            if (limitBehavior == LimitBehavior.All)
            {
                posts = readPosts.ExecuteReader(new Dictionary <string, object>()
                {
                    ["state"] = state.ToString(),
                });
            }
            else if (limitBehavior == LimitBehavior.First)
            {
                posts = readPostsLimit.ExecuteReader(new Dictionary <string, object>()
                {
                    ["state"] = state.ToString(),
                    ["limit"] = limit.ToString(),
                });
            }
            else if (limitBehavior == LimitBehavior.Last)
            {
                posts = readPostsLimitReversed.ExecuteReader(new Dictionary <string, object>()
                {
                    ["state"] = state.ToString(),
                    ["limit"] = limit.ToString(),
                });
            }
            else
            {
                return(null);
            }

            while (posts.Read())
            {
                while (true)
                {
                    string id = posts.GetField <string>("id");

                    Post post   = null;
                    var  oldRef = activePosts.GetOrAdd(id, id =>
                    {
                        post = new Post();

                        // If post isn't null, we already have the right data
                        ReadPostFromReader(posts, post);

                        return(new WeakReference <Post>(post));
                    });

                    post ??= oldRef.TryGetTarget();

                    if (post != null)
                    {
                        // Success!
                        result.Add(post);
                        break;
                    }

                    // We didn't insert our object, but we also didn't get a valid object
                    // This suggests that we got a WeakReference without a valid pointer
                    // That's OK; (try to) remove it and try again.
                    activePosts.Remove(id, oldRef);
                }
            }
            posts.Close();

            return(result.ToArray());
        }