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
func init() {
DbInits.Add(func() error {
acc := qgen.Builder.Accumulator()
DbInits.Add(func(acc *qgen.Accumulator) error {
logStmts = LogStmts{
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(),

View File

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

View File

@ -2,6 +2,8 @@ package common
import (
"database/sql"
"../query_gen/lib"
)
// 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
}
type dbInits []func() error
type dbInits []func(acc *qgen.Accumulator) error
var DbInits dbInits
func (inits dbInits) Run() error {
for _, init := range inits {
err := init()
acc := qgen.Builder.Accumulator()
err := init(acc)
if err != nil {
return err
}
@ -76,6 +79,6 @@ func (inits dbInits) Run() error {
return nil
}
func (inits dbInits) Add(init ...func() error) {
func (inits dbInits) Add(init ...func(acc *qgen.Accumulator) error) {
DbInits = dbInits(append(DbInits, init...))
}

View File

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

View File

@ -54,10 +54,9 @@ type ForumStmts struct {
var forumStmts ForumStmts
func init() {
DbInits.Add(func() error {
acc := qgen.Builder.Accumulator()
DbInits.Add(func(acc *qgen.Accumulator) error {
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()
})

View File

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

View File

@ -37,10 +37,9 @@ type GroupStmts struct {
var groupStmts GroupStmts
func init() {
DbInits.Add(func() error {
acc := qgen.Builder.Accumulator()
DbInits.Add(func(acc *qgen.Accumulator) error {
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()
})

View File

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

View File

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

View File

@ -71,14 +71,13 @@ type ReplyStmts struct {
}
func init() {
DbInits.Add(func() error {
acc := qgen.Builder.Accumulator()
DbInits.Add(func(acc *qgen.Accumulator) error {
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(),
delete: acc.SimpleDelete("replies", "rid = ?"),
addLikesToReply: acc.SimpleUpdate("replies", "likeCount = likeCount + ?", "rid = ?"),
removeRepliesFromTopic: acc.SimpleUpdate("topics", "postCount = postCount - ?", "tid = ?"),
delete: acc.Delete("replies").Where("rid = ?").Prepare(),
addLikesToReply: acc.Update("replies").Set("likeCount = likeCount + ?").Where("rid = ?").Prepare(),
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 = ?", "", ""),
}
return acc.FirstError()

View File

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

View File

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

View File

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

View File

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

View File

@ -126,8 +126,7 @@ type TopicStmts struct {
var topicStmts TopicStmts
func init() {
DbInits.Add(func() error {
acc := qgen.Builder.Accumulator()
DbInits.Add(func(acc *qgen.Accumulator) error {
topicStmts = TopicStmts{
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(),

View File

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

View File

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

View File

@ -50,8 +50,7 @@ type WidgetStmts struct {
var widgetStmts WidgetStmts
func init() {
DbInits.Add(func() error {
acc := qgen.Builder.Accumulator()
DbInits.Add(func(acc *qgen.Accumulator) error {
widgetStmts = WidgetStmts{
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() {
WordFilterBox.Store(WordFilterMap(make(map[int]WordFilter)))
DbInits.Add(func() error {
acc := qgen.Builder.Accumulator()
DbInits.Add(func(acc *qgen.Accumulator) error {
filterStmts = FilterStmts{
getWordFilters: acc.Select("word_filters").Columns("wfid, find, replacement").Prepare(),
}

View File

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

View File

@ -35,23 +35,23 @@ func initGuilds() (err error) {
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.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()
}

View File

@ -5,17 +5,17 @@ import (
"database/sql"
)
type accBuilder struct {
type Accumulator struct {
conn *sql.DB
adapter DB_Adapter
firstErr error
}
func (build *accBuilder) SetConn(conn *sql.DB) {
func (build *Accumulator) SetConn(conn *sql.DB) {
build.conn = conn
}
func (build *accBuilder) SetAdapter(name string) error {
func (build *Accumulator) SetAdapter(name string) error {
adap, err := GetAdapter(name)
if err != nil {
return err
@ -24,15 +24,15 @@ func (build *accBuilder) SetAdapter(name string) error {
return nil
}
func (build *accBuilder) GetAdapter() DB_Adapter {
func (build *Accumulator) GetAdapter() DB_Adapter {
return build.adapter
}
func (build *accBuilder) FirstError() error {
func (build *Accumulator) FirstError() error {
return build.firstErr
}
func (build *accBuilder) recordError(err error) {
func (build *Accumulator) recordError(err error) {
if err == nil {
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 {
build.recordError(err)
return nil
@ -51,7 +51,7 @@ func (build *accBuilder) prepare(res string, err error) *sql.Stmt {
return stmt
}
func (build *accBuilder) Tx(handler func(*TransactionBuilder) error) {
func (build *Accumulator) Tx(handler func(*TransactionBuilder) error) {
tx, err := build.conn.Begin()
if err != nil {
build.recordError(err)
@ -66,56 +66,56 @@ func (build *accBuilder) Tx(handler func(*TransactionBuilder) error) {
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
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))
}
// 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))
}
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 {
build.recordError(err)
return nil
@ -126,68 +126,68 @@ func (build *accBuilder) prepareTx(tx *sql.Tx, res string, err error) (stmt *sql
}
// 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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
return build.prepareTx(tx, res, err)
}
// 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)
return build.prepareTx(tx, res, err)
}
func (build *accBuilder) Delete(table string) *deleteBuilder {
func (build *Accumulator) Delete(table string) *deleteBuilder {
return &deleteBuilder{table, "", build}
}
@ -195,7 +195,7 @@ type deleteBuilder struct {
table string
where string
build *accBuilder
build *Accumulator
}
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)
}
func (build *accBuilder) Update(table string) *updateBuilder {
func (build *Accumulator) Update(table string) *updateBuilder {
return &updateBuilder{table, "", "", build}
}
@ -216,7 +216,7 @@ type updateBuilder struct {
set string
where string
build *accBuilder
build *Accumulator
}
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)
}
func (build *accBuilder) Select(table string) *selectBuilder {
func (build *Accumulator) Select(table string) *selectBuilder {
return &selectBuilder{table, "", "", "", "", build}
}
@ -244,7 +244,7 @@ type selectBuilder struct {
orderby string
limit string
build *accBuilder
build *Accumulator
}
func (selectItem *selectBuilder) Columns(columns string) *selectBuilder {
@ -276,10 +276,10 @@ func (selectItem *selectBuilder) Query(args ...interface{}) (*sql.Rows, error) {
if stmt != nil {
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}
}
@ -288,7 +288,7 @@ type insertBuilder struct {
columns string
fields string
build *accBuilder
build *Accumulator
}
func (insert *insertBuilder) Columns(columns string) *insertBuilder {
@ -304,3 +304,29 @@ func (insert *insertBuilder) Fields(fields string) *insertBuilder {
func (insert *insertBuilder) Prepare() *sql.Stmt {
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
}
func (build *builder) Accumulator() *accBuilder {
return &accBuilder{build.conn, build.adapter, nil}
func (build *builder) Accumulator() *Accumulator {
return &Accumulator{build.conn, build.adapter, nil}
}
func (build *builder) SetConn(conn *sql.DB) {