Esempio n. 1
0
        public void UpdatePostState(Post post, PostState state)
        {
            updatePostState.ExecuteNonQuery(new Dictionary <string, object>()
            {
                ["id"]    = post.id,
                ["state"] = state.ToString(),
            });

            post.state = state;

            TriggerCallbacks();
        }
Esempio n. 2
0
        public int CountAllPosts(PostState state)
        {
            var result = countPosts.ExecuteReader(new Dictionary <string, object>()
            {
                ["state"] = state.ToString(),
            });

            result.Read();
            int rv = (int)(long)result[0];

            result.Close();

            return(rv);
        }
Esempio n. 3
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());
        }