From 6b64a2e28d4e70ed6758a54978271e1188be793a Mon Sep 17 00:00:00 2001 From: Azareal Date: Mon, 6 Nov 2017 07:23:32 +0000 Subject: [PATCH] Refactored the stores to use the accumulator. Fixed the accumulator. --- forum_store.go | 71 +++++++++---------------------- group_store.go | 16 ++----- query_gen/lib/accumulator.go | 6 ++- topic_store.go | 42 +++++------------- user_store.go | 82 +++++++----------------------------- 5 files changed, 54 insertions(+), 163 deletions(-) diff --git a/forum_store.go b/forum_store.go index 55f779a5..59c09680 100644 --- a/forum_store.go +++ b/forum_store.go @@ -59,61 +59,30 @@ type MemoryForumStore struct { forumView atomic.Value // []*Forum //fids []int - get *sql.Stmt - getAll *sql.Stmt - delete *sql.Stmt - create *sql.Stmt - count *sql.Stmt - updateCache *sql.Stmt - addTopicsToForum *sql.Stmt - removeTopicsFromForum *sql.Stmt + get *sql.Stmt + getAll *sql.Stmt + delete *sql.Stmt + create *sql.Stmt + count *sql.Stmt + updateCache *sql.Stmt + addTopics *sql.Stmt + removeTopics *sql.Stmt } // NewMemoryForumStore gives you a new instance of MemoryForumStore func NewMemoryForumStore() (*MemoryForumStore, error) { - getStmt, err := qgen.Builder.SimpleSelect("forums", "name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "fid = ?", "", "") - if err != nil { - return nil, err - } - getAllStmt, err := qgen.Builder.SimpleSelect("forums", "fid, name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "", "fid ASC", "") - if err != nil { - return nil, err - } + acc := qgen.Builder.Accumulator() // TODO: Do a proper delete - deleteStmt, err := qgen.Builder.SimpleUpdate("forums", "name= '', active = 0", "fid = ?") - if err != nil { - return nil, err - } - createStmt, err := qgen.Builder.SimpleInsert("forums", "name, desc, active, preset", "?,?,?,?") - if err != nil { - return nil, err - } - forumCountStmt, err := qgen.Builder.SimpleCount("forums", "name != ''", "") - if err != nil { - return nil, err - } - updateCacheStmt, err := qgen.Builder.SimpleUpdate("forums", "lastTopicID = ?, lastReplyerID = ?", "fid = ?") - if err != nil { - return nil, err - } - addTopicsToForumStmt, err := qgen.Builder.SimpleUpdate("forums", "topicCount = topicCount + ?", "fid = ?") - if err != nil { - return nil, err - } - removeTopicsFromForumStmt, err := qgen.Builder.SimpleUpdate("forums", "topicCount = topicCount - ?", "fid = ?") - if err != nil { - return nil, err - } return &MemoryForumStore{ - get: getStmt, - getAll: getAllStmt, - delete: deleteStmt, - create: createStmt, - count: forumCountStmt, - updateCache: updateCacheStmt, - addTopicsToForum: addTopicsToForumStmt, - removeTopicsFromForum: removeTopicsFromForumStmt, - }, nil + get: acc.SimpleSelect("forums", "name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "fid = ?", "", ""), + getAll: acc.SimpleSelect("forums", "fid, name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "", "fid ASC", ""), + delete: acc.SimpleUpdate("forums", "name= '', active = 0", "fid = ?"), + create: acc.SimpleInsert("forums", "name, desc, active, preset", "?,?,?,?"), + count: acc.SimpleCount("forums", "name != ''", ""), + updateCache: acc.SimpleUpdate("forums", "lastTopicID = ?, lastReplyerID = ?", "fid = ?"), + addTopics: acc.SimpleUpdate("forums", "topicCount = topicCount + ?", "fid = ?"), + removeTopics: acc.SimpleUpdate("forums", "topicCount = topicCount - ?", "fid = ?"), + }, acc.FirstError() } // TODO: Add support for subforums @@ -355,7 +324,7 @@ func (mfs *MemoryForumStore) AddTopic(tid int, uid int, fid int) error { if err != nil { return err } - _, err = mfs.addTopicsToForum.Exec(1, fid) + _, err = mfs.addTopics.Exec(1, fid) if err != nil { return err } @@ -365,7 +334,7 @@ func (mfs *MemoryForumStore) AddTopic(tid int, uid int, fid int) error { // TODO: Update the forum cache with the latest topic func (mfs *MemoryForumStore) RemoveTopic(fid int) error { - _, err := mfs.removeTopicsFromForum.Exec(1, fid) + _, err := mfs.removeTopics.Exec(1, fid) if err != nil { return err } diff --git a/group_store.go b/group_store.go index 0f8d5dcf..98065708 100644 --- a/group_store.go +++ b/group_store.go @@ -43,21 +43,13 @@ type MemoryGroupStore struct { } func NewMemoryGroupStore() (*MemoryGroupStore, error) { - getAllStmt, err := qgen.Builder.SimpleSelect("users_groups", "gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "", "", "") - if err != nil { - return nil, err - } - getGroupStmt, err := qgen.Builder.SimpleSelect("users_groups", "name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "gid = ?", "", "") - if err != nil { - return nil, err - } - + acc := qgen.Builder.Accumulator() return &MemoryGroupStore{ groups: make(map[int]*Group), groupCount: 0, - getAll: getAllStmt, - get: getGroupStmt, - }, nil + getAll: acc.SimpleSelect("users_groups", "gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "", "", ""), + get: acc.SimpleSelect("users_groups", "name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "gid = ?", "", ""), + }, acc.FirstError() } // TODO: Move this query from the global stmt store into this store diff --git a/query_gen/lib/accumulator.go b/query_gen/lib/accumulator.go index d1df3508..0a99b7c8 100644 --- a/query_gen/lib/accumulator.go +++ b/query_gen/lib/accumulator.go @@ -1,7 +1,9 @@ /* WIP: A version of the builder which accumulates errors, we'll see if we can't unify the implementations at some point */ package qgen -import "database/sql" +import ( + "database/sql" +) type accBuilder struct { conn *sql.DB @@ -34,7 +36,7 @@ func (build *accBuilder) recordError(err error) { if err == nil { return } - if build.firstErr != nil { + if build.firstErr == nil { build.firstErr = err } } diff --git a/topic_store.go b/topic_store.go index 80a139af..8d7c685b 100644 --- a/topic_store.go +++ b/topic_store.go @@ -63,25 +63,14 @@ type MemoryTopicStore struct { // NewMemoryTopicStore gives you a new instance of MemoryTopicStore func NewMemoryTopicStore(capacity int) (*MemoryTopicStore, error) { - getStmt, err := qgen.Builder.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", "") - if err != nil { - return nil, err - } - existsStmt, err := qgen.Builder.SimpleSelect("topics", "tid", "tid = ?", "", "") - if err != nil { - return nil, err - } - topicCountStmt, err := qgen.Builder.SimpleCount("topics", "", "") - if err != nil { - return nil, err - } + acc := qgen.Builder.Accumulator() return &MemoryTopicStore{ items: make(map[int]*Topic), capacity: capacity, - get: getStmt, - exists: existsStmt, - topicCount: topicCountStmt, - }, nil + get: acc.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", ""), + exists: acc.SimpleSelect("topics", "tid", "tid = ?", "", ""), + topicCount: acc.SimpleCount("topics", "", ""), + }, acc.FirstError() } func (mts *MemoryTopicStore) CacheGet(id int) (*Topic, error) { @@ -267,23 +256,12 @@ type SQLTopicStore struct { } func NewSQLTopicStore() (*SQLTopicStore, error) { - getStmt, err := qgen.Builder.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", "") - if err != nil { - return nil, err - } - existsStmt, err := qgen.Builder.SimpleSelect("topics", "tid", "tid = ?", "", "") - if err != nil { - return nil, err - } - topicCountStmt, err := qgen.Builder.SimpleCount("topics", "", "") - if err != nil { - return nil, err - } + acc := qgen.Builder.Accumulator() return &SQLTopicStore{ - get: getStmt, - exists: existsStmt, - topicCount: topicCountStmt, - }, nil + get: acc.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", ""), + exists: acc.SimpleSelect("topics", "tid", "tid = ?", "", ""), + topicCount: acc.SimpleCount("topics", "", ""), + }, acc.FirstError() } func (sts *SQLTopicStore) Get(id int) (*Topic, error) { diff --git a/user_store.go b/user_store.go index 3f3a69f3..9f65ae5f 100644 --- a/user_store.go +++ b/user_store.go @@ -56,42 +56,17 @@ type MemoryUserStore struct { // NewMemoryUserStore gives you a new instance of MemoryUserStore func NewMemoryUserStore(capacity int) (*MemoryUserStore, error) { - getStmt, err := qgen.Builder.SimpleSelect("users", "name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip, temp_group", "uid = ?", "", "") - if err != nil { - return nil, err - } - - existsStmt, err := qgen.Builder.SimpleSelect("users", "uid", "uid = ?", "", "") - if err != nil { - return nil, err - } - - // Add an admin version of register_stmt with more flexibility? - // create_account_stmt, err = db.Prepare("INSERT INTO - registerStmt, err := qgen.Builder.SimpleInsert("users", "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt", "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()") - if err != nil { - return nil, err - } - - usernameExistsStmt, err := qgen.Builder.SimpleSelect("users", "name", "name = ?", "", "") - if err != nil { - return nil, err - } - - userCountStmt, err := qgen.Builder.SimpleCount("users", "", "") - if err != nil { - return nil, err - } - + acc := qgen.Builder.Accumulator() + // TODO: Add an admin version of registerStmt with more flexibility? return &MemoryUserStore{ items: make(map[int]*User), capacity: capacity, - get: getStmt, - exists: existsStmt, - register: registerStmt, - usernameExists: usernameExistsStmt, - userCount: userCountStmt, - }, nil + get: acc.SimpleSelect("users", "name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip, temp_group", "uid = ?", "", ""), + exists: acc.SimpleSelect("users", "uid", "uid = ?", "", ""), + register: acc.SimpleInsert("users", "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt", "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()"), + usernameExists: acc.SimpleSelect("users", "name", "name = ?", "", ""), + userCount: acc.SimpleCount("users", "", ""), + }, acc.FirstError() } func (mus *MemoryUserStore) CacheGet(id int) (*User, error) { @@ -385,40 +360,15 @@ type SQLUserStore struct { } func NewSQLUserStore() (*SQLUserStore, error) { - getStmt, err := qgen.Builder.SimpleSelect("users", "name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip, temp_group", "uid = ?", "", "") - if err != nil { - return nil, err - } - - existsStmt, err := qgen.Builder.SimpleSelect("users", "uid", "uid = ?", "", "") - if err != nil { - return nil, err - } - - // Add an admin version of register_stmt with more flexibility? - // create_account_stmt, err = db.Prepare("INSERT INTO - registerStmt, err := qgen.Builder.SimpleInsert("users", "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt", "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()") - if err != nil { - return nil, err - } - - usernameExistsStmt, err := qgen.Builder.SimpleSelect("users", "name", "name = ?", "", "") - if err != nil { - return nil, err - } - - userCountStmt, err := qgen.Builder.SimpleCount("users", "", "") - if err != nil { - return nil, err - } - + acc := qgen.Builder.Accumulator() + // TODO: Add an admin version of registerStmt with more flexibility? return &SQLUserStore{ - get: getStmt, - exists: existsStmt, - register: registerStmt, - usernameExists: usernameExistsStmt, - userCount: userCountStmt, - }, nil + get: acc.SimpleSelect("users", "name, group, is_super_admin, session, email, avatar, message, url_prefix, url_name, level, score, last_ip, temp_group", "uid = ?", "", ""), + exists: acc.SimpleSelect("users", "uid", "uid = ?", "", ""), + register: acc.SimpleInsert("users", "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt", "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()"), + usernameExists: acc.SimpleSelect("users", "name", "name = ?", "", ""), + userCount: acc.SimpleCount("users", "", ""), + }, acc.FirstError() } func (mus *SQLUserStore) Get(id int) (*User, error) {