Refactored the stores to use the accumulator.
Fixed the accumulator.
This commit is contained in:
parent
01239f82f1
commit
6b64a2e28d
|
@ -65,55 +65,24 @@ type MemoryForumStore struct {
|
|||
create *sql.Stmt
|
||||
count *sql.Stmt
|
||||
updateCache *sql.Stmt
|
||||
addTopicsToForum *sql.Stmt
|
||||
removeTopicsFromForum *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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue