Converted more queries over to the new OO builder syntax.

Renamed accBuilder to Accumulator so that it can be used in type hints outside the query generator.
The DbInit accumulator is now initialised in the caller rather than the callee.
This commit is contained in:
Azareal 2017-11-12 03:29:05 +00:00
parent 6bae378db0
commit 2545d4adde
23 changed files with 176 additions and 157 deletions

View File

@ -14,8 +14,7 @@ type LogStmts struct {
var logStmts LogStmts var logStmts LogStmts
func init() { func init() {
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
logStmts = LogStmts{ logStmts = LogStmts{
addModLogEntry: acc.Insert("moderation_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(), addModLogEntry: acc.Insert("moderation_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),
addAdminLogEntry: acc.Insert("administration_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(), addAdminLogEntry: acc.Insert("administration_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),

View File

@ -48,9 +48,9 @@ type DefaultAuth struct {
func NewDefaultAuth() (*DefaultAuth, error) { func NewDefaultAuth() (*DefaultAuth, error) {
acc := qgen.Builder.Accumulator() acc := qgen.Builder.Accumulator()
return &DefaultAuth{ return &DefaultAuth{
login: acc.SimpleSelect("users", "uid, password, salt", "name = ?", "", ""), login: acc.Select("users").Columns("uid, password, salt").Where("name = ?").Prepare(),
logout: acc.SimpleUpdate("users", "session = ''", "uid = ?"), logout: acc.Update("users").Set("session = ''").Where("uid = ?").Prepare(),
updateSession: acc.SimpleUpdate("users", "session = ?", "uid = ?"), updateSession: acc.Update("users").Set("session = ?").Where("uid = ?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }

View File

@ -2,6 +2,8 @@ package common
import ( import (
"database/sql" "database/sql"
"../query_gen/lib"
) )
// nolint I don't want to write comments for each of these o.o // nolint I don't want to write comments for each of these o.o
@ -62,13 +64,14 @@ func (slice StringList) Contains(needle string) bool {
return false return false
} }
type dbInits []func() error type dbInits []func(acc *qgen.Accumulator) error
var DbInits dbInits var DbInits dbInits
func (inits dbInits) Run() error { func (inits dbInits) Run() error {
for _, init := range inits { for _, init := range inits {
err := init() acc := qgen.Builder.Accumulator()
err := init(acc)
if err != nil { if err != nil {
return err return err
} }
@ -76,6 +79,6 @@ func (inits dbInits) Run() error {
return nil return nil
} }
func (inits dbInits) Add(init ...func() error) { func (inits dbInits) Add(init ...func(acc *qgen.Accumulator) error) {
DbInits = dbInits(append(DbInits, init...)) DbInits = dbInits(append(DbInits, init...))
} }

View File

@ -145,8 +145,7 @@ type ExtendStmts struct {
var extendStmts ExtendStmts var extendStmts ExtendStmts
func init() { func init() {
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
extendStmts = ExtendStmts{ extendStmts = ExtendStmts{
getPlugins: acc.Select("plugins").Columns("uname, active, installed").Prepare(), getPlugins: acc.Select("plugins").Columns("uname, active, installed").Prepare(),
} }

View File

@ -54,10 +54,9 @@ type ForumStmts struct {
var forumStmts ForumStmts var forumStmts ForumStmts
func init() { func init() {
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
forumStmts = ForumStmts{ forumStmts = ForumStmts{
update: acc.SimpleUpdate("forums", "name = ?, desc = ?, active = ?, preset = ?", "fid = ?"), update: acc.Update("forums").Set("name = ?, desc = ?, active = ?, preset = ?").Where("fid = ?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })

View File

@ -74,14 +74,14 @@ func NewMemoryForumStore() (*MemoryForumStore, error) {
acc := qgen.Builder.Accumulator() acc := qgen.Builder.Accumulator()
// TODO: Do a proper delete // TODO: Do a proper delete
return &MemoryForumStore{ return &MemoryForumStore{
get: acc.SimpleSelect("forums", "name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "fid = ?", "", ""), get: acc.Select("forums").Columns("name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID").Where("fid = ?").Prepare(),
getAll: acc.SimpleSelect("forums", "fid, name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID", "", "fid ASC", ""), getAll: acc.Select("forums").Columns("fid, name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID").Orderby("fid ASC").Prepare(),
delete: acc.SimpleUpdate("forums", "name= '', active = 0", "fid = ?"), delete: acc.Update("forums").Set("name= '', active = 0").Where("fid = ?").Prepare(),
create: acc.SimpleInsert("forums", "name, desc, active, preset", "?,?,?,?"), create: acc.Insert("forums").Columns("name, desc, active, preset").Fields("?,?,?,?").Prepare(),
count: acc.SimpleCount("forums", "name != ''", ""), count: acc.Count("forums").Where("name != ''").Prepare(),
updateCache: acc.SimpleUpdate("forums", "lastTopicID = ?, lastReplyerID = ?", "fid = ?"), updateCache: acc.Update("forums").Set("lastTopicID = ?, lastReplyerID = ?").Where("fid = ?").Prepare(),
addTopics: acc.SimpleUpdate("forums", "topicCount = topicCount + ?", "fid = ?"), addTopics: acc.Update("forums").Set("topicCount = topicCount + ?").Where("fid = ?").Prepare(),
removeTopics: acc.SimpleUpdate("forums", "topicCount = topicCount - ?", "fid = ?"), removeTopics: acc.Update("forums").Set("topicCount = topicCount - ?").Where("fid = ?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }

View File

@ -37,10 +37,9 @@ type GroupStmts struct {
var groupStmts GroupStmts var groupStmts GroupStmts
func init() { func init() {
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
groupStmts = GroupStmts{ groupStmts = GroupStmts{
updateGroupRank: acc.SimpleUpdate("users_groups", "is_admin = ?, is_mod = ?, is_banned = ?", "gid = ?"), updateGroupRank: acc.Update("users_groups").Set("is_admin = ?, is_mod = ?, is_banned = ?").Where("gid = ?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })

View File

@ -48,9 +48,9 @@ func NewMemoryGroupStore() (*MemoryGroupStore, error) {
return &MemoryGroupStore{ return &MemoryGroupStore{
groups: make(map[int]*Group), groups: make(map[int]*Group),
groupCount: 0, groupCount: 0,
getAll: acc.SimpleSelect("users_groups", "gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "", "", ""), getAll: acc.Select("users_groups").Columns("gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Prepare(),
get: acc.SimpleSelect("users_groups", "name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", "gid = ?", "", ""), get: acc.Select("users_groups").Columns("name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Where("gid = ?").Prepare(),
count: acc.SimpleCount("users_groups", "", ""), count: acc.Count("users_groups").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }

View File

@ -23,8 +23,8 @@ type SQLProfileReplyStore struct {
func NewSQLProfileReplyStore() (*SQLProfileReplyStore, error) { func NewSQLProfileReplyStore() (*SQLProfileReplyStore, error) {
acc := qgen.Builder.Accumulator() acc := qgen.Builder.Accumulator()
return &SQLProfileReplyStore{ return &SQLProfileReplyStore{
get: acc.SimpleSelect("users_replies", "uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress", "rid = ?", "", ""), get: acc.Select("users_replies").Columns("uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress").Where("rid = ?").Prepare(),
create: acc.SimpleInsert("users_replies", "uid, content, parsed_content, createdAt, createdBy, ipaddress", "?,?,?,UTC_TIMESTAMP(),?,?"), create: acc.Insert("users_replies").Columns("uid, content, parsed_content, createdAt, createdBy, ipaddress").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }

View File

@ -71,14 +71,13 @@ type ReplyStmts struct {
} }
func init() { func init() {
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
replyStmts = ReplyStmts{ replyStmts = ReplyStmts{
isLiked: acc.SimpleSelect("likes", "targetItem", "sentBy = ? and targetItem = ? and targetType = 'replies'", "", ""), isLiked: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType = 'replies'").Prepare(),
createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy").Fields("?,?,?,?").Prepare(), createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy").Fields("?,?,?,?").Prepare(),
delete: acc.SimpleDelete("replies", "rid = ?"), delete: acc.Delete("replies").Where("rid = ?").Prepare(),
addLikesToReply: acc.SimpleUpdate("replies", "likeCount = likeCount + ?", "rid = ?"), addLikesToReply: acc.Update("replies").Set("likeCount = likeCount + ?").Where("rid = ?").Prepare(),
removeRepliesFromTopic: acc.SimpleUpdate("topics", "postCount = postCount - ?", "tid = ?"), removeRepliesFromTopic: acc.Update("topics").Set("postCount = postCount - ?").Where("tid = ?").Prepare(),
getParent: acc.SimpleLeftJoin("replies", "topics", "topics.tid, topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, topics.data", "replies.tid = topics.tid", "rid = ?", "", ""), getParent: acc.SimpleLeftJoin("replies", "topics", "topics.tid, topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, topics.data", "replies.tid = topics.tid", "rid = ?", "", ""),
} }
return acc.FirstError() return acc.FirstError()

View File

@ -18,8 +18,8 @@ type SQLReplyStore struct {
func NewSQLReplyStore() (*SQLReplyStore, error) { func NewSQLReplyStore() (*SQLReplyStore, error) {
acc := qgen.Builder.Accumulator() acc := qgen.Builder.Accumulator()
return &SQLReplyStore{ return &SQLReplyStore{
get: acc.SimpleSelect("replies", "tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount", "rid = ?", "", ""), get: acc.Select("replies").Columns("tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount").Where("rid = ?").Prepare(),
create: acc.SimpleInsert("replies", "tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy", "?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?"), create: acc.Insert("replies").Columns("tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }

View File

@ -35,8 +35,7 @@ var settingStmts SettingStmts
func init() { func init() {
SettingBox.Store(SettingMap(make(map[string]interface{}))) SettingBox.Store(SettingMap(make(map[string]interface{})))
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
settingStmts = SettingStmts{ settingStmts = SettingStmts{
getFull: acc.Select("settings").Columns("name, content, type, constraints").Prepare(), getFull: acc.Select("settings").Columns("name, content, type, constraints").Prepare(),
} }

View File

@ -24,11 +24,10 @@ var lastSync time.Time
func init() { func init() {
lastSync = time.Now() lastSync = time.Now()
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
taskStmts = TaskStmts{ taskStmts = TaskStmts{
getExpiredScheduledGroups: acc.SimpleSelect("users_groups_scheduler", "uid", "UTC_TIMESTAMP() > revert_at AND temporary = 1", "", ""), getExpiredScheduledGroups: acc.Select("users_groups_scheduler").Columns("uid").Where("UTC_TIMESTAMP() > revert_at AND temporary = 1").Prepare(),
getSync: acc.SimpleSelect("sync", "last_update", "", "", ""), getSync: acc.Select("sync").Columns("last_update").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })

View File

@ -83,8 +83,7 @@ var themeStmts ThemeStmts
func init() { func init() {
DefaultThemeBox.Store(fallbackTheme) DefaultThemeBox.Store(fallbackTheme)
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
themeStmts = ThemeStmts{ themeStmts = ThemeStmts{
getThemes: acc.Select("themes").Columns("uname, default").Prepare(), getThemes: acc.Select("themes").Columns("uname, default").Prepare(),
} }

View File

@ -126,8 +126,7 @@ type TopicStmts struct {
var topicStmts TopicStmts var topicStmts TopicStmts
func init() { func init() {
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
topicStmts = TopicStmts{ topicStmts = TopicStmts{
addRepliesToTopic: acc.Update("topics").Set("postCount = postCount + ?, lastReplyBy = ?, lastReplyAt = UTC_TIMESTAMP()").Where("tid = ?").Prepare(), addRepliesToTopic: acc.Update("topics").Set("postCount = postCount + ?, lastReplyBy = ?, lastReplyAt = UTC_TIMESTAMP()").Where("tid = ?").Prepare(),
lock: acc.Update("topics").Set("is_closed = 1").Where("tid = ?").Prepare(), lock: acc.Update("topics").Set("is_closed = 1").Where("tid = ?").Prepare(),

View File

@ -69,10 +69,10 @@ func NewMemoryTopicStore(capacity int) (*MemoryTopicStore, error) {
return &MemoryTopicStore{ return &MemoryTopicStore{
items: make(map[int]*Topic), items: make(map[int]*Topic),
capacity: capacity, capacity: capacity,
get: acc.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", ""), get: acc.Select("topics").Columns("title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data").Where("tid = ?").Prepare(),
exists: acc.SimpleSelect("topics", "tid", "tid = ?", "", ""), exists: acc.Select("topics").Columns("tid").Where("tid = ?").Prepare(),
topicCount: acc.SimpleCount("topics", "", ""), topicCount: acc.Count("topics").Prepare(),
create: acc.SimpleInsert("topics", "parentID, title, content, parsed_content, createdAt, lastReplyAt, lastReplyBy, ipaddress, words, createdBy", "?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?,?"), create: acc.Insert("topics").Columns("parentID, title, content, parsed_content, createdAt, lastReplyAt, lastReplyBy, ipaddress, words, createdBy").Fields("?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?,?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
@ -279,10 +279,10 @@ type SQLTopicStore struct {
func NewSQLTopicStore() (*SQLTopicStore, error) { func NewSQLTopicStore() (*SQLTopicStore, error) {
acc := qgen.Builder.Accumulator() acc := qgen.Builder.Accumulator()
return &SQLTopicStore{ return &SQLTopicStore{
get: acc.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", ""), get: acc.Select("topics").Columns("title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data").Where("tid = ?").Prepare(),
exists: acc.SimpleSelect("topics", "tid", "tid = ?", "", ""), exists: acc.Select("topics").Columns("tid").Where("tid = ?").Prepare(),
topicCount: acc.SimpleCount("topics", "", ""), topicCount: acc.Count("topics").Prepare(),
create: acc.SimpleInsert("topics", "parentID, title, content, parsed_content, createdAt, lastReplyAt, lastReplyBy, ipaddress, words, createdBy", "?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?,?"), create: acc.Insert("topics").Columns("parentID, title, content, parsed_content, createdAt, lastReplyAt, lastReplyBy, ipaddress, words, createdBy").Fields("?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?,?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }

View File

@ -80,8 +80,7 @@ type UserStmts struct {
var userStmts UserStmts var userStmts UserStmts
func init() { func init() {
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
userStmts = UserStmts{ userStmts = UserStmts{
activate: acc.SimpleUpdate("users", "active = 1", "uid = ?"), activate: acc.SimpleUpdate("users", "active = 1", "uid = ?"),
changeGroup: acc.SimpleUpdate("users", "group = ?", "uid = ?"), changeGroup: acc.SimpleUpdate("users", "group = ?", "uid = ?"),

View File

@ -50,8 +50,7 @@ type WidgetStmts struct {
var widgetStmts WidgetStmts var widgetStmts WidgetStmts
func init() { func init() {
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
widgetStmts = WidgetStmts{ widgetStmts = WidgetStmts{
getWidgets: acc.Select("widgets").Columns("position, side, type, active, location, data").Orderby("position ASC").Prepare(), getWidgets: acc.Select("widgets").Columns("position, side, type, active, location, data").Orderby("position ASC").Prepare(),
} }

View File

@ -24,8 +24,7 @@ var filterStmts FilterStmts
func init() { func init() {
WordFilterBox.Store(WordFilterMap(make(map[int]WordFilter))) WordFilterBox.Store(WordFilterMap(make(map[int]WordFilter)))
DbInits.Add(func() error { DbInits.Add(func(acc *qgen.Accumulator) error {
acc := qgen.Builder.Accumulator()
filterStmts = FilterStmts{ filterStmts = FilterStmts{
getWordFilters: acc.Select("word_filters").Columns("wfid, find, replacement").Prepare(), getWordFilters: acc.Select("word_filters").Columns("wfid, find, replacement").Prepare(),
} }

View File

@ -1,64 +1,66 @@
package main package main
import "./common"
func init() { func init() {
// Site Info // Site Info
site.ShortName = "Ts" // This should be less than three letters to fit in the navbar common.Site.ShortName = "Ts" // This should be less than three letters to fit in the navbar
site.Name = "Test Site" common.Site.Name = "Test Site"
site.Email = "" common.Site.Email = ""
site.URL = "localhost" common.Site.URL = "localhost"
site.Port = "8080" // 8080 common.Site.Port = "8080" // 8080
site.EnableSsl = false common.Site.EnableSsl = false
site.EnableEmails = false common.Site.EnableEmails = false
site.HasProxy = false // Cloudflare counts as this, if it's sitting in the middle common.Site.HasProxy = false // Cloudflare counts as this, if it's sitting in the middle
config.SslPrivkey = "" common.Config.SslPrivkey = ""
config.SslFullchain = "" common.Config.SslFullchain = ""
site.Language = "english" common.Site.Language = "english"
// Database details // Database details
dbConfig.Host = "localhost" common.DbConfig.Host = "localhost"
dbConfig.Username = "root" common.DbConfig.Username = "root"
dbConfig.Password = "password" common.DbConfig.Password = "password"
dbConfig.Dbname = "gosora" common.DbConfig.Dbname = "gosora"
dbConfig.Port = "3306" // You probably won't need to change this common.DbConfig.Port = "3306" // You probably won't need to change this
// MySQL Test Database details // MySQL Test Database details
dbConfig.TestHost = "localhost" common.DbConfig.TestHost = "localhost"
dbConfig.TestUsername = "root" common.DbConfig.TestUsername = "root"
dbConfig.TestPassword = "" common.DbConfig.TestPassword = ""
dbConfig.TestDbname = "gosora_test" // The name of the test database, leave blank to disable. DON'T USE YOUR PRODUCTION DATABASE FOR THIS. LEAVE BLANK IF YOU DON'T KNOW WHAT THIS MEANS. common.DbConfig.TestDbname = "gosora_test" // The name of the test database, leave blank to disable. DON'T USE YOUR PRODUCTION DATABASE FOR THIS. LEAVE BLANK IF YOU DON'T KNOW WHAT THIS MEANS.
dbConfig.TestPort = "3306" common.DbConfig.TestPort = "3306"
// Limiters // Limiters
config.MaxRequestSize = 5 * megabyte common.Config.MaxRequestSize = 5 * common.Megabyte
// Caching // Caching
config.CacheTopicUser = CACHE_STATIC common.Config.CacheTopicUser = common.CACHE_STATIC
config.UserCacheCapacity = 120 // The max number of users held in memory common.Config.UserCacheCapacity = 120 // The max number of users held in memory
config.TopicCacheCapacity = 200 // The max number of topics held in memory common.Config.TopicCacheCapacity = 200 // The max number of topics held in memory
// Email // Email
config.SMTPServer = "" common.Config.SMTPServer = ""
config.SMTPUsername = "" common.Config.SMTPUsername = ""
config.SMTPPassword = "" common.Config.SMTPPassword = ""
config.SMTPPort = "25" common.Config.SMTPPort = "25"
// Misc // Misc
config.DefaultRoute = routeTopics common.Config.DefaultRoute = routeTopics
config.DefaultGroup = 3 // Should be a setting in the database common.Config.DefaultGroup = 3 // Should be a setting in the database
config.ActivationGroup = 5 // Should be a setting in the database common.Config.ActivationGroup = 5 // Should be a setting in the database
config.StaffCSS = "staff_post" common.Config.StaffCSS = "staff_post"
config.DefaultForum = 2 common.Config.DefaultForum = 2
config.MinifyTemplates = true common.Config.MinifyTemplates = true
config.MultiServer = false // Experimental: Enable Cross-Server Synchronisation and several other features common.Config.MultiServer = false // Experimental: Enable Cross-Server Synchronisation and several other features
//config.Noavatar = "https://api.adorable.io/avatars/{width}/{id}@{site_url}.png" //common.Config.Noavatar = "https://api.adorable.io/avatars/{width}/{id}@{site_url}.png"
config.Noavatar = "https://api.adorable.io/avatars/285/{id}@{site_url}.png" common.Config.Noavatar = "https://api.adorable.io/avatars/285/{id}@{site_url}.png"
config.ItemsPerPage = 25 common.Config.ItemsPerPage = 25
// Developer flags // Developer flags
dev.DebugMode = true //common.Dev.DebugMode = true
//dev.SuperDebug = true //common.Dev.SuperDebug = true
//dev.TemplateDebug = true //common.Dev.TemplateDebug = true
//dev.Profiling = true //common.Dev.Profiling = true
//dev.TestDB = true //common.Dev.TestDB = true
} }

View File

@ -35,23 +35,23 @@ func initGuilds() (err error) {
acc := qgen.Builder.Accumulator() acc := qgen.Builder.Accumulator()
guilds.ListStmt = acc.SimpleSelect("guilds", "guildID, name, desc, active, privacy, joinable, owner, memberCount, createdAt, lastUpdateTime", "", "", "") guilds.ListStmt = acc.Select("guilds").Columns("guildID, name, desc, active, privacy, joinable, owner, memberCount, createdAt, lastUpdateTime").Prepare()
guilds.GetGuildStmt = acc.SimpleSelect("guilds", "name, desc, active, privacy, joinable, owner, memberCount, mainForum, backdrop, createdAt, lastUpdateTime", "guildID = ?", "", "") guilds.GetGuildStmt = acc.Select("guilds").Columns("name, desc, active, privacy, joinable, owner, memberCount, mainForum, backdrop, createdAt, lastUpdateTime").Where("guildID = ?").Prepare()
guilds.MemberListStmt = acc.SimpleSelect("guilds_members", "guildID, uid, rank, posts, joinedAt", "", "", "") guilds.MemberListStmt = acc.Select("guilds_members").Columns("guildID, uid, rank, posts, joinedAt").Prepare()
guilds.MemberListJoinStmt = acc.SimpleLeftJoin("guilds_members", "users", "users.uid, guilds_members.rank, guilds_members.posts, guilds_members.joinedAt, users.name, users.avatar", "guilds_members.uid = users.uid", "guilds_members.guildID = ?", "guilds_members.rank DESC, guilds_members.joinedat ASC", "") guilds.MemberListJoinStmt = acc.SimpleLeftJoin("guilds_members", "users", "users.uid, guilds_members.rank, guilds_members.posts, guilds_members.joinedAt, users.name, users.avatar", "guilds_members.uid = users.uid", "guilds_members.guildID = ?", "guilds_members.rank DESC, guilds_members.joinedat ASC", "")
guilds.GetMemberStmt = acc.SimpleSelect("guilds_members", "rank, posts, joinedAt", "guildID = ? AND uid = ?", "", "") guilds.GetMemberStmt = acc.Select("guilds_members").Columns("rank, posts, joinedAt").Where("guildID = ? AND uid = ?").Prepare()
guilds.CreateGuildStmt = acc.SimpleInsert("guilds", "name, desc, active, privacy, joinable, owner, memberCount, mainForum, backdrop, createdAt, lastUpdateTime", "?,?,?,?,1,?,1,?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()") guilds.CreateGuildStmt = acc.Insert("guilds").Columns("name, desc, active, privacy, joinable, owner, memberCount, mainForum, backdrop, createdAt, lastUpdateTime").Fields("?,?,?,?,1,?,1,?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP()").Prepare()
guilds.AttachForumStmt = acc.SimpleUpdate("forums", "parentID = ?, parentType = 'guild'", "fid = ?") guilds.AttachForumStmt = acc.Update("forums").Set("parentID = ?, parentType = 'guild'").Where("fid = ?").Prepare()
guilds.UnattachForumStmt = acc.SimpleUpdate("forums", "parentID = 0, parentType = ''", "fid = ?") guilds.UnattachForumStmt = acc.Update("forums").Set("parentID = 0, parentType = ''").Where("fid = ?").Prepare()
guilds.AddMemberStmt = acc.SimpleInsert("guilds_members", "guildID, uid, rank, posts, joinedAt", "?,?,?,0,UTC_TIMESTAMP()") guilds.AddMemberStmt = acc.Insert("guilds_members").Columns("guildID, uid, rank, posts, joinedAt").Fields("?,?,?,0,UTC_TIMESTAMP()").Prepare()
return acc.FirstError() return acc.FirstError()
} }

View File

@ -5,17 +5,17 @@ import (
"database/sql" "database/sql"
) )
type accBuilder struct { type Accumulator struct {
conn *sql.DB conn *sql.DB
adapter DB_Adapter adapter DB_Adapter
firstErr error firstErr error
} }
func (build *accBuilder) SetConn(conn *sql.DB) { func (build *Accumulator) SetConn(conn *sql.DB) {
build.conn = conn build.conn = conn
} }
func (build *accBuilder) SetAdapter(name string) error { func (build *Accumulator) SetAdapter(name string) error {
adap, err := GetAdapter(name) adap, err := GetAdapter(name)
if err != nil { if err != nil {
return err return err
@ -24,15 +24,15 @@ func (build *accBuilder) SetAdapter(name string) error {
return nil return nil
} }
func (build *accBuilder) GetAdapter() DB_Adapter { func (build *Accumulator) GetAdapter() DB_Adapter {
return build.adapter return build.adapter
} }
func (build *accBuilder) FirstError() error { func (build *Accumulator) FirstError() error {
return build.firstErr return build.firstErr
} }
func (build *accBuilder) recordError(err error) { func (build *Accumulator) recordError(err error) {
if err == nil { if err == nil {
return return
} }
@ -41,7 +41,7 @@ func (build *accBuilder) recordError(err error) {
} }
} }
func (build *accBuilder) prepare(res string, err error) *sql.Stmt { func (build *Accumulator) prepare(res string, err error) *sql.Stmt {
if err != nil { if err != nil {
build.recordError(err) build.recordError(err)
return nil return nil
@ -51,7 +51,7 @@ func (build *accBuilder) prepare(res string, err error) *sql.Stmt {
return stmt return stmt
} }
func (build *accBuilder) Tx(handler func(*TransactionBuilder) error) { func (build *Accumulator) Tx(handler func(*TransactionBuilder) error) {
tx, err := build.conn.Begin() tx, err := build.conn.Begin()
if err != nil { if err != nil {
build.recordError(err) build.recordError(err)
@ -66,56 +66,56 @@ func (build *accBuilder) Tx(handler func(*TransactionBuilder) error) {
build.recordError(tx.Commit()) build.recordError(tx.Commit())
} }
func (build *accBuilder) SimpleSelect(table string, columns string, where string, orderby string, limit string) *sql.Stmt { func (build *Accumulator) SimpleSelect(table string, columns string, where string, orderby string, limit string) *sql.Stmt {
return build.prepare(build.adapter.SimpleSelect("_builder", table, columns, where, orderby, limit)) return build.prepare(build.adapter.SimpleSelect("_builder", table, columns, where, orderby, limit))
} }
func (build *accBuilder) SimpleCount(table string, where string, limit string) *sql.Stmt { func (build *Accumulator) SimpleCount(table string, where string, limit string) *sql.Stmt {
return build.prepare(build.adapter.SimpleCount("_builder", table, where, limit)) return build.prepare(build.adapter.SimpleCount("_builder", table, where, limit))
} }
func (build *accBuilder) SimpleLeftJoin(table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) *sql.Stmt { func (build *Accumulator) SimpleLeftJoin(table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) *sql.Stmt {
return build.prepare(build.adapter.SimpleLeftJoin("_builder", table1, table2, columns, joiners, where, orderby, limit)) return build.prepare(build.adapter.SimpleLeftJoin("_builder", table1, table2, columns, joiners, where, orderby, limit))
} }
func (build *accBuilder) SimpleInnerJoin(table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) *sql.Stmt { func (build *Accumulator) SimpleInnerJoin(table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) *sql.Stmt {
return build.prepare(build.adapter.SimpleInnerJoin("_builder", table1, table2, columns, joiners, where, orderby, limit)) return build.prepare(build.adapter.SimpleInnerJoin("_builder", table1, table2, columns, joiners, where, orderby, limit))
} }
func (build *accBuilder) CreateTable(table string, charset string, collation string, columns []DB_Table_Column, keys []DB_Table_Key) *sql.Stmt { func (build *Accumulator) CreateTable(table string, charset string, collation string, columns []DB_Table_Column, keys []DB_Table_Key) *sql.Stmt {
return build.prepare(build.adapter.CreateTable("_builder", table, charset, collation, columns, keys)) return build.prepare(build.adapter.CreateTable("_builder", table, charset, collation, columns, keys))
} }
func (build *accBuilder) SimpleInsert(table string, columns string, fields string) *sql.Stmt { func (build *Accumulator) SimpleInsert(table string, columns string, fields string) *sql.Stmt {
return build.prepare(build.adapter.SimpleInsert("_builder", table, columns, fields)) return build.prepare(build.adapter.SimpleInsert("_builder", table, columns, fields))
} }
func (build *accBuilder) SimpleInsertSelect(ins DB_Insert, sel DB_Select) *sql.Stmt { func (build *Accumulator) SimpleInsertSelect(ins DB_Insert, sel DB_Select) *sql.Stmt {
return build.prepare(build.adapter.SimpleInsertSelect("_builder", ins, sel)) return build.prepare(build.adapter.SimpleInsertSelect("_builder", ins, sel))
} }
func (build *accBuilder) SimpleInsertLeftJoin(ins DB_Insert, sel DB_Join) *sql.Stmt { func (build *Accumulator) SimpleInsertLeftJoin(ins DB_Insert, sel DB_Join) *sql.Stmt {
return build.prepare(build.adapter.SimpleInsertLeftJoin("_builder", ins, sel)) return build.prepare(build.adapter.SimpleInsertLeftJoin("_builder", ins, sel))
} }
func (build *accBuilder) SimpleInsertInnerJoin(ins DB_Insert, sel DB_Join) *sql.Stmt { func (build *Accumulator) SimpleInsertInnerJoin(ins DB_Insert, sel DB_Join) *sql.Stmt {
return build.prepare(build.adapter.SimpleInsertInnerJoin("_builder", ins, sel)) return build.prepare(build.adapter.SimpleInsertInnerJoin("_builder", ins, sel))
} }
func (build *accBuilder) SimpleUpdate(table string, set string, where string) *sql.Stmt { func (build *Accumulator) SimpleUpdate(table string, set string, where string) *sql.Stmt {
return build.prepare(build.adapter.SimpleUpdate("_builder", table, set, where)) return build.prepare(build.adapter.SimpleUpdate("_builder", table, set, where))
} }
func (build *accBuilder) SimpleDelete(table string, where string) *sql.Stmt { func (build *Accumulator) SimpleDelete(table string, where string) *sql.Stmt {
return build.prepare(build.adapter.SimpleDelete("_builder", table, where)) return build.prepare(build.adapter.SimpleDelete("_builder", table, where))
} }
// I don't know why you need this, but here it is x.x // I don't know why you need this, but here it is x.x
func (build *accBuilder) Purge(table string) *sql.Stmt { func (build *Accumulator) Purge(table string) *sql.Stmt {
return build.prepare(build.adapter.Purge("_builder", table)) return build.prepare(build.adapter.Purge("_builder", table))
} }
func (build *accBuilder) prepareTx(tx *sql.Tx, res string, err error) (stmt *sql.Stmt) { func (build *Accumulator) prepareTx(tx *sql.Tx, res string, err error) (stmt *sql.Stmt) {
if err != nil { if err != nil {
build.recordError(err) build.recordError(err)
return nil return nil
@ -126,68 +126,68 @@ func (build *accBuilder) prepareTx(tx *sql.Tx, res string, err error) (stmt *sql
} }
// These ones support transactions // These ones support transactions
func (build *accBuilder) SimpleSelectTx(tx *sql.Tx, table string, columns string, where string, orderby string, limit string) (stmt *sql.Stmt) { func (build *Accumulator) SimpleSelectTx(tx *sql.Tx, table string, columns string, where string, orderby string, limit string) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleSelect("_builder", table, columns, where, orderby, limit) res, err := build.adapter.SimpleSelect("_builder", table, columns, where, orderby, limit)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleCountTx(tx *sql.Tx, table string, where string, limit string) (stmt *sql.Stmt) { func (build *Accumulator) SimpleCountTx(tx *sql.Tx, table string, where string, limit string) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleCount("_builder", table, where, limit) res, err := build.adapter.SimpleCount("_builder", table, where, limit)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleLeftJoinTx(tx *sql.Tx, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (stmt *sql.Stmt) { func (build *Accumulator) SimpleLeftJoinTx(tx *sql.Tx, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleLeftJoin("_builder", table1, table2, columns, joiners, where, orderby, limit) res, err := build.adapter.SimpleLeftJoin("_builder", table1, table2, columns, joiners, where, orderby, limit)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleInnerJoinTx(tx *sql.Tx, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (stmt *sql.Stmt) { func (build *Accumulator) SimpleInnerJoinTx(tx *sql.Tx, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleInnerJoin("_builder", table1, table2, columns, joiners, where, orderby, limit) res, err := build.adapter.SimpleInnerJoin("_builder", table1, table2, columns, joiners, where, orderby, limit)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) CreateTableTx(tx *sql.Tx, table string, charset string, collation string, columns []DB_Table_Column, keys []DB_Table_Key) (stmt *sql.Stmt) { func (build *Accumulator) CreateTableTx(tx *sql.Tx, table string, charset string, collation string, columns []DB_Table_Column, keys []DB_Table_Key) (stmt *sql.Stmt) {
res, err := build.adapter.CreateTable("_builder", table, charset, collation, columns, keys) res, err := build.adapter.CreateTable("_builder", table, charset, collation, columns, keys)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleInsertTx(tx *sql.Tx, table string, columns string, fields string) (stmt *sql.Stmt) { func (build *Accumulator) SimpleInsertTx(tx *sql.Tx, table string, columns string, fields string) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleInsert("_builder", table, columns, fields) res, err := build.adapter.SimpleInsert("_builder", table, columns, fields)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleInsertSelectTx(tx *sql.Tx, ins DB_Insert, sel DB_Select) (stmt *sql.Stmt) { func (build *Accumulator) SimpleInsertSelectTx(tx *sql.Tx, ins DB_Insert, sel DB_Select) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleInsertSelect("_builder", ins, sel) res, err := build.adapter.SimpleInsertSelect("_builder", ins, sel)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleInsertLeftJoinTx(tx *sql.Tx, ins DB_Insert, sel DB_Join) (stmt *sql.Stmt) { func (build *Accumulator) SimpleInsertLeftJoinTx(tx *sql.Tx, ins DB_Insert, sel DB_Join) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleInsertLeftJoin("_builder", ins, sel) res, err := build.adapter.SimpleInsertLeftJoin("_builder", ins, sel)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleInsertInnerJoinTx(tx *sql.Tx, ins DB_Insert, sel DB_Join) (stmt *sql.Stmt) { func (build *Accumulator) SimpleInsertInnerJoinTx(tx *sql.Tx, ins DB_Insert, sel DB_Join) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleInsertInnerJoin("_builder", ins, sel) res, err := build.adapter.SimpleInsertInnerJoin("_builder", ins, sel)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleUpdateTx(tx *sql.Tx, table string, set string, where string) (stmt *sql.Stmt) { func (build *Accumulator) SimpleUpdateTx(tx *sql.Tx, table string, set string, where string) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleUpdate("_builder", table, set, where) res, err := build.adapter.SimpleUpdate("_builder", table, set, where)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) SimpleDeleteTx(tx *sql.Tx, table string, where string) (stmt *sql.Stmt) { func (build *Accumulator) SimpleDeleteTx(tx *sql.Tx, table string, where string) (stmt *sql.Stmt) {
res, err := build.adapter.SimpleDelete("_builder", table, where) res, err := build.adapter.SimpleDelete("_builder", table, where)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
// I don't know why you need this, but here it is x.x // I don't know why you need this, but here it is x.x
func (build *accBuilder) PurgeTx(tx *sql.Tx, table string) (stmt *sql.Stmt) { func (build *Accumulator) PurgeTx(tx *sql.Tx, table string) (stmt *sql.Stmt) {
res, err := build.adapter.Purge("_builder", table) res, err := build.adapter.Purge("_builder", table)
return build.prepareTx(tx, res, err) return build.prepareTx(tx, res, err)
} }
func (build *accBuilder) Delete(table string) *deleteBuilder { func (build *Accumulator) Delete(table string) *deleteBuilder {
return &deleteBuilder{table, "", build} return &deleteBuilder{table, "", build}
} }
@ -195,7 +195,7 @@ type deleteBuilder struct {
table string table string
where string where string
build *accBuilder build *Accumulator
} }
func (delete *deleteBuilder) Where(where string) *deleteBuilder { func (delete *deleteBuilder) Where(where string) *deleteBuilder {
@ -207,7 +207,7 @@ func (delete *deleteBuilder) Prepare() *sql.Stmt {
return delete.build.SimpleDelete(delete.table, delete.where) return delete.build.SimpleDelete(delete.table, delete.where)
} }
func (build *accBuilder) Update(table string) *updateBuilder { func (build *Accumulator) Update(table string) *updateBuilder {
return &updateBuilder{table, "", "", build} return &updateBuilder{table, "", "", build}
} }
@ -216,7 +216,7 @@ type updateBuilder struct {
set string set string
where string where string
build *accBuilder build *Accumulator
} }
func (update *updateBuilder) Set(set string) *updateBuilder { func (update *updateBuilder) Set(set string) *updateBuilder {
@ -233,7 +233,7 @@ func (update *updateBuilder) Prepare() *sql.Stmt {
return update.build.SimpleUpdate(update.table, update.set, update.where) return update.build.SimpleUpdate(update.table, update.set, update.where)
} }
func (build *accBuilder) Select(table string) *selectBuilder { func (build *Accumulator) Select(table string) *selectBuilder {
return &selectBuilder{table, "", "", "", "", build} return &selectBuilder{table, "", "", "", "", build}
} }
@ -244,7 +244,7 @@ type selectBuilder struct {
orderby string orderby string
limit string limit string
build *accBuilder build *Accumulator
} }
func (selectItem *selectBuilder) Columns(columns string) *selectBuilder { func (selectItem *selectBuilder) Columns(columns string) *selectBuilder {
@ -276,10 +276,10 @@ func (selectItem *selectBuilder) Query(args ...interface{}) (*sql.Rows, error) {
if stmt != nil { if stmt != nil {
return stmt.Query(args...) return stmt.Query(args...)
} }
return nil, selectItem.FirstError() return nil, selectItem.build.FirstError()
} }
func (build *accBuilder) Insert(table string) *insertBuilder { func (build *Accumulator) Insert(table string) *insertBuilder {
return &insertBuilder{table, "", "", build} return &insertBuilder{table, "", "", build}
} }
@ -288,7 +288,7 @@ type insertBuilder struct {
columns string columns string
fields string fields string
build *accBuilder build *Accumulator
} }
func (insert *insertBuilder) Columns(columns string) *insertBuilder { func (insert *insertBuilder) Columns(columns string) *insertBuilder {
@ -304,3 +304,29 @@ func (insert *insertBuilder) Fields(fields string) *insertBuilder {
func (insert *insertBuilder) Prepare() *sql.Stmt { func (insert *insertBuilder) Prepare() *sql.Stmt {
return insert.build.SimpleInsert(insert.table, insert.columns, insert.fields) return insert.build.SimpleInsert(insert.table, insert.columns, insert.fields)
} }
func (build *Accumulator) Count(table string) *countBuilder {
return &countBuilder{table, "", "", build}
}
type countBuilder struct {
table string
where string
limit string
build *Accumulator
}
func (count *countBuilder) Where(where string) *countBuilder {
count.where = where
return count
}
func (count *countBuilder) Limit(limit string) *countBuilder {
count.limit = limit
return count
}
func (count *countBuilder) Prepare() *sql.Stmt {
return count.build.SimpleCount(count.table, count.where, count.limit)
}

View File

@ -16,8 +16,8 @@ type builder struct {
adapter DB_Adapter adapter DB_Adapter
} }
func (build *builder) Accumulator() *accBuilder { func (build *builder) Accumulator() *Accumulator {
return &accBuilder{build.conn, build.adapter, nil} return &Accumulator{build.conn, build.adapter, nil}
} }
func (build *builder) SetConn(conn *sql.DB) { func (build *builder) SetConn(conn *sql.DB) {