Refactored the stores to use the accumulator.
Fixed the accumulator.
This commit is contained in:
parent
01239f82f1
commit
6b64a2e28d
|
@ -59,61 +59,30 @@ type MemoryForumStore struct {
|
||||||
forumView atomic.Value // []*Forum
|
forumView atomic.Value // []*Forum
|
||||||
//fids []int
|
//fids []int
|
||||||
|
|
||||||
get *sql.Stmt
|
get *sql.Stmt
|
||||||
getAll *sql.Stmt
|
getAll *sql.Stmt
|
||||||
delete *sql.Stmt
|
delete *sql.Stmt
|
||||||
create *sql.Stmt
|
create *sql.Stmt
|
||||||
count *sql.Stmt
|
count *sql.Stmt
|
||||||
updateCache *sql.Stmt
|
updateCache *sql.Stmt
|
||||||
addTopicsToForum *sql.Stmt
|
addTopics *sql.Stmt
|
||||||
removeTopicsFromForum *sql.Stmt
|
removeTopics *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMemoryForumStore gives you a new instance of MemoryForumStore
|
// NewMemoryForumStore gives you a new instance of MemoryForumStore
|
||||||
func NewMemoryForumStore() (*MemoryForumStore, error) {
|
func NewMemoryForumStore() (*MemoryForumStore, error) {
|
||||||
getStmt, err := qgen.Builder.SimpleSelect("forums", "name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "fid = ?", "", "")
|
acc := qgen.Builder.Accumulator()
|
||||||
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
|
|
||||||
}
|
|
||||||
// TODO: Do a proper delete
|
// 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{
|
return &MemoryForumStore{
|
||||||
get: getStmt,
|
get: acc.SimpleSelect("forums", "name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "fid = ?", "", ""),
|
||||||
getAll: getAllStmt,
|
getAll: acc.SimpleSelect("forums", "fid, name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "", "fid ASC", ""),
|
||||||
delete: deleteStmt,
|
delete: acc.SimpleUpdate("forums", "name= '', active = 0", "fid = ?"),
|
||||||
create: createStmt,
|
create: acc.SimpleInsert("forums", "name, desc, active, preset", "?,?,?,?"),
|
||||||
count: forumCountStmt,
|
count: acc.SimpleCount("forums", "name != ''", ""),
|
||||||
updateCache: updateCacheStmt,
|
updateCache: acc.SimpleUpdate("forums", "lastTopicID = ?, lastReplyerID = ?", "fid = ?"),
|
||||||
addTopicsToForum: addTopicsToForumStmt,
|
addTopics: acc.SimpleUpdate("forums", "topicCount = topicCount + ?", "fid = ?"),
|
||||||
removeTopicsFromForum: removeTopicsFromForumStmt,
|
removeTopics: acc.SimpleUpdate("forums", "topicCount = topicCount - ?", "fid = ?"),
|
||||||
}, nil
|
}, acc.FirstError()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add support for subforums
|
// TODO: Add support for subforums
|
||||||
|
@ -355,7 +324,7 @@ func (mfs *MemoryForumStore) AddTopic(tid int, uid int, fid int) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = mfs.addTopicsToForum.Exec(1, fid)
|
_, err = mfs.addTopics.Exec(1, fid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
// TODO: Update the forum cache with the latest topic
|
||||||
func (mfs *MemoryForumStore) RemoveTopic(fid int) error {
|
func (mfs *MemoryForumStore) RemoveTopic(fid int) error {
|
||||||
_, err := mfs.removeTopicsFromForum.Exec(1, fid)
|
_, err := mfs.removeTopics.Exec(1, fid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,21 +43,13 @@ type MemoryGroupStore struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMemoryGroupStore() (*MemoryGroupStore, error) {
|
func NewMemoryGroupStore() (*MemoryGroupStore, error) {
|
||||||
getAllStmt, err := qgen.Builder.SimpleSelect("users_groups", "gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "", "", "")
|
acc := qgen.Builder.Accumulator()
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
return &MemoryGroupStore{
|
return &MemoryGroupStore{
|
||||||
groups: make(map[int]*Group),
|
groups: make(map[int]*Group),
|
||||||
groupCount: 0,
|
groupCount: 0,
|
||||||
getAll: getAllStmt,
|
getAll: acc.SimpleSelect("users_groups", "gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "", "", ""),
|
||||||
get: getGroupStmt,
|
get: acc.SimpleSelect("users_groups", "name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "gid = ?", "", ""),
|
||||||
}, nil
|
}, acc.FirstError()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Move this query from the global stmt store into this store
|
// 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 */
|
/* WIP: A version of the builder which accumulates errors, we'll see if we can't unify the implementations at some point */
|
||||||
package qgen
|
package qgen
|
||||||
|
|
||||||
import "database/sql"
|
import (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
type accBuilder struct {
|
type accBuilder struct {
|
||||||
conn *sql.DB
|
conn *sql.DB
|
||||||
|
@ -34,7 +36,7 @@ func (build *accBuilder) recordError(err error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if build.firstErr != nil {
|
if build.firstErr == nil {
|
||||||
build.firstErr = err
|
build.firstErr = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,25 +63,14 @@ type MemoryTopicStore struct {
|
||||||
|
|
||||||
// NewMemoryTopicStore gives you a new instance of MemoryTopicStore
|
// NewMemoryTopicStore gives you a new instance of MemoryTopicStore
|
||||||
func NewMemoryTopicStore(capacity int) (*MemoryTopicStore, error) {
|
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 = ?", "", "")
|
acc := qgen.Builder.Accumulator()
|
||||||
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
|
|
||||||
}
|
|
||||||
return &MemoryTopicStore{
|
return &MemoryTopicStore{
|
||||||
items: make(map[int]*Topic),
|
items: make(map[int]*Topic),
|
||||||
capacity: capacity,
|
capacity: capacity,
|
||||||
get: getStmt,
|
get: acc.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", ""),
|
||||||
exists: existsStmt,
|
exists: acc.SimpleSelect("topics", "tid", "tid = ?", "", ""),
|
||||||
topicCount: topicCountStmt,
|
topicCount: acc.SimpleCount("topics", "", ""),
|
||||||
}, nil
|
}, acc.FirstError()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mts *MemoryTopicStore) CacheGet(id int) (*Topic, error) {
|
func (mts *MemoryTopicStore) CacheGet(id int) (*Topic, error) {
|
||||||
|
@ -267,23 +256,12 @@ type SQLTopicStore struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSQLTopicStore() (*SQLTopicStore, error) {
|
func NewSQLTopicStore() (*SQLTopicStore, error) {
|
||||||
getStmt, err := qgen.Builder.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", "")
|
acc := qgen.Builder.Accumulator()
|
||||||
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
|
|
||||||
}
|
|
||||||
return &SQLTopicStore{
|
return &SQLTopicStore{
|
||||||
get: getStmt,
|
get: acc.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", ""),
|
||||||
exists: existsStmt,
|
exists: acc.SimpleSelect("topics", "tid", "tid = ?", "", ""),
|
||||||
topicCount: topicCountStmt,
|
topicCount: acc.SimpleCount("topics", "", ""),
|
||||||
}, nil
|
}, acc.FirstError()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sts *SQLTopicStore) Get(id int) (*Topic, error) {
|
func (sts *SQLTopicStore) Get(id int) (*Topic, error) {
|
||||||
|
|
|
@ -56,42 +56,17 @@ type MemoryUserStore struct {
|
||||||
|
|
||||||
// NewMemoryUserStore gives you a new instance of MemoryUserStore
|
// NewMemoryUserStore gives you a new instance of MemoryUserStore
|
||||||
func NewMemoryUserStore(capacity int) (*MemoryUserStore, error) {
|
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 = ?", "", "")
|
acc := qgen.Builder.Accumulator()
|
||||||
if err != nil {
|
// TODO: Add an admin version of registerStmt with more flexibility?
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
return &MemoryUserStore{
|
return &MemoryUserStore{
|
||||||
items: make(map[int]*User),
|
items: make(map[int]*User),
|
||||||
capacity: capacity,
|
capacity: capacity,
|
||||||
get: getStmt,
|
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: existsStmt,
|
exists: acc.SimpleSelect("users", "uid", "uid = ?", "", ""),
|
||||||
register: registerStmt,
|
register: acc.SimpleInsert("users", "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt", "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()"),
|
||||||
usernameExists: usernameExistsStmt,
|
usernameExists: acc.SimpleSelect("users", "name", "name = ?", "", ""),
|
||||||
userCount: userCountStmt,
|
userCount: acc.SimpleCount("users", "", ""),
|
||||||
}, nil
|
}, acc.FirstError()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mus *MemoryUserStore) CacheGet(id int) (*User, error) {
|
func (mus *MemoryUserStore) CacheGet(id int) (*User, error) {
|
||||||
|
@ -385,40 +360,15 @@ type SQLUserStore struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSQLUserStore() (*SQLUserStore, error) {
|
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 = ?", "", "")
|
acc := qgen.Builder.Accumulator()
|
||||||
if err != nil {
|
// TODO: Add an admin version of registerStmt with more flexibility?
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
return &SQLUserStore{
|
return &SQLUserStore{
|
||||||
get: getStmt,
|
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: existsStmt,
|
exists: acc.SimpleSelect("users", "uid", "uid = ?", "", ""),
|
||||||
register: registerStmt,
|
register: acc.SimpleInsert("users", "name, email, password, salt, group, is_super_admin, session, active, message, createdAt, lastActiveAt", "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()"),
|
||||||
usernameExists: usernameExistsStmt,
|
usernameExists: acc.SimpleSelect("users", "name", "name = ?", "", ""),
|
||||||
userCount: userCountStmt,
|
userCount: acc.SimpleCount("users", "", ""),
|
||||||
}, nil
|
}, acc.FirstError()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mus *SQLUserStore) Get(id int) (*User, error) {
|
func (mus *SQLUserStore) Get(id int) (*User, error) {
|
||||||
|
|
Loading…
Reference in New Issue