gosora/query_gen/lib/acc_builders.go
Azareal 8252c481df Updated the codebeat and codeclimate config files.
Renamed the pre_render_view_forum hook to pre_render_forum.
Renamed the pre_render_ips hook to pre_render_ip_search.
Renamed routeTopicCreate to CreateTopic and moved it into /routes/topic.go
Moved three routes into /routes/user.go
Renamed routeIps to IPSearch and moved it into /routes/moderate.go
Moved the IP Search logic out of the route and into the DefaultIPSearcher.
Added InQ() to the select builders and replaced three hand-rolled queries with it.
De-duplicated some pagination logic.
2018-01-21 11:17:43 +00:00

150 lines
3.8 KiB
Go

package qgen
import "database/sql"
type accDeleteBuilder struct {
table string
where string
build *Accumulator
}
func (delete *accDeleteBuilder) Where(where string) *accDeleteBuilder {
delete.where = where
return delete
}
func (delete *accDeleteBuilder) Prepare() *sql.Stmt {
return delete.build.SimpleDelete(delete.table, delete.where)
}
type accUpdateBuilder struct {
table string
set string
where string
build *Accumulator
}
func (update *accUpdateBuilder) Set(set string) *accUpdateBuilder {
update.set = set
return update
}
func (update *accUpdateBuilder) Where(where string) *accUpdateBuilder {
update.where = where
return update
}
func (update *accUpdateBuilder) Prepare() *sql.Stmt {
return update.build.SimpleUpdate(update.table, update.set, update.where)
}
type accSelectBuilder struct {
table string
columns string
where string
orderby string
limit string
dateCutoff *dateCutoff // We might want to do this in a slightly less hacky way
inChain *accSelectBuilder
inColumn string
build *Accumulator
}
func (selectItem *accSelectBuilder) Columns(columns string) *accSelectBuilder {
selectItem.columns = columns
return selectItem
}
func (selectItem *accSelectBuilder) Where(where string) *accSelectBuilder {
selectItem.where = where
return selectItem
}
func (selectItem *accSelectBuilder) InQ(column string, subBuilder *accSelectBuilder) *accSelectBuilder {
selectItem.inChain = subBuilder
selectItem.inColumn = column
return selectItem
}
func (selectItem *accSelectBuilder) DateCutoff(column string, quantity int, unit string) *accSelectBuilder {
selectItem.dateCutoff = &dateCutoff{column, quantity, unit}
return selectItem
}
func (selectItem *accSelectBuilder) Orderby(orderby string) *accSelectBuilder {
selectItem.orderby = orderby
return selectItem
}
func (selectItem *accSelectBuilder) Limit(limit string) *accSelectBuilder {
selectItem.limit = limit
return selectItem
}
func (selectItem *accSelectBuilder) Prepare() *sql.Stmt {
// TODO: Phase out the procedural API and use the adapter's OO API? The OO API might need a bit more work before we do that and it needs to be rolled out to MSSQL.
if selectItem.dateCutoff != nil || selectItem.inChain != nil {
selectBuilder := selectItem.build.GetAdapter().Builder().Select().FromAcc(selectItem)
return selectItem.build.prepare(selectItem.build.GetAdapter().ComplexSelect(selectBuilder))
}
return selectItem.build.SimpleSelect(selectItem.table, selectItem.columns, selectItem.where, selectItem.orderby, selectItem.limit)
}
func (selectItem *accSelectBuilder) Query(args ...interface{}) (*sql.Rows, error) {
stmt := selectItem.Prepare()
if stmt != nil {
return stmt.Query(args...)
}
return nil, selectItem.build.FirstError()
}
type accInsertBuilder struct {
table string
columns string
fields string
build *Accumulator
}
func (insert *accInsertBuilder) Columns(columns string) *accInsertBuilder {
insert.columns = columns
return insert
}
func (insert *accInsertBuilder) Fields(fields string) *accInsertBuilder {
insert.fields = fields
return insert
}
func (insert *accInsertBuilder) Prepare() *sql.Stmt {
return insert.build.SimpleInsert(insert.table, insert.columns, insert.fields)
}
type accCountBuilder struct {
table string
where string
limit string
build *Accumulator
}
func (count *accCountBuilder) Where(where string) *accCountBuilder {
count.where = where
return count
}
func (count *accCountBuilder) Limit(limit string) *accCountBuilder {
count.limit = limit
return count
}
// TODO: Add QueryRow for this and use it in statistics.go
func (count *accCountBuilder) Prepare() *sql.Stmt {
return count.build.SimpleCount(count.table, count.where, count.limit)
}
// TODO: Add a Sum builder for summing viewchunks up into one number for the dashboard?