gosora/query_gen/acc_builders.go
Azareal 3465e4c08f You can now manage the attachments for an opening post by hitting edit.
The update system now uses the database as the source of truth for the last version rather than lastSchema.json
Refactored several structs and bits of code, so we can avoid allocations for contexts where we never use a relative time.
Clicking on the relative times on the topic list and the forum page should now take you to the post on the last page rather than just the last page.
Added the reltime template function.
Fixed some obsolete bits of code.
Fixed some spelling mistakes.
Fixed a bug where MaxBytesReader was capped at the maxFileSize rather than r.ContentLength.
All of the client side templates should work again now.
Shortened some statement names to save some horizontal space.
accUpdateBuilder and SimpleUpdate now use updatePrebuilder behind the scenes to simplify things.
Renamed selectItem to builder in AccSelectBuilder.
Added a Total() method to accCountBuilder to reduce the amount of boilerplate used for row count queries.
The "_builder" strings have been replaced with empty strings to help save memory, to make things slightly faster and to open the door to removing the query name in many contexts down the line.
Added the open_edit and close_edit client hooks.
Removed many query name checks.
Split the attachment logic into separate functions and de-duplicated it between replies and topics.
Improved the UI for editing topics in Nox.
Used type aliases to reduce the amount of boilerplate in tables.go and patches.go
Reduced the amount of boilerplate in the action post logic.
Eliminated a map and a slice in the topic page for users who haven't given any likes. E.g. Guests.
Fixed some long out-dated parts of the update instructions.
Updated the update instructions to remove mention of the obsolete lastSchema.json
Fixed a bug in init.js where /api/me was being loaded for guests.
Added the MiniTopicGet, GlobalCount and CountInTopic methods to AttachmentStore.
Added the MiniAttachment struct.
Split the mod floaters out into their own template to reduce duplication.
Removed a couple of redundant ParseForms.

Added the common.skipUntilIfExistsOrLine function.
Added the NotFoundJS and NotFoundJSQ functions.
Added the lastReplyID and attachCount columns to the topics table.
2018-12-27 15:42:41 +10:00

323 lines
7.8 KiB
Go

package qgen
import (
"database/sql"
"strconv"
)
type accDeleteBuilder struct {
table string
where string
build *Accumulator
}
func (builder *accDeleteBuilder) Where(where string) *accDeleteBuilder {
if builder.where != "" {
builder.where += " AND "
}
builder.where += where
return builder
}
func (builder *accDeleteBuilder) Prepare() *sql.Stmt {
return builder.build.SimpleDelete(builder.table, builder.where)
}
func (builder *accDeleteBuilder) Run(args ...interface{}) (int, error) {
stmt := builder.Prepare()
if stmt == nil {
return 0, builder.build.FirstError()
}
res, err := stmt.Exec(args...)
if err != nil {
return 0, err
}
lastID, err := res.LastInsertId()
return int(lastID), err
}
type accUpdateBuilder struct {
up *updatePrebuilder
build *Accumulator
}
func (update *accUpdateBuilder) Set(set string) *accUpdateBuilder {
update.up.set = set
return update
}
func (update *accUpdateBuilder) Where(where string) *accUpdateBuilder {
if update.up.where != "" {
update.up.where += " AND "
}
update.up.where += where
return update
}
func (update *accUpdateBuilder) WhereQ(sel *selectPrebuilder) *accUpdateBuilder {
update.up.whereSubQuery = sel
return update
}
func (builder *accUpdateBuilder) Prepare() *sql.Stmt {
if builder.up.whereSubQuery != nil {
return builder.build.prepare(builder.build.adapter.SimpleUpdateSelect(builder.up))
}
return builder.build.prepare(builder.build.adapter.SimpleUpdate(builder.up))
}
func (builder *accUpdateBuilder) Exec(args ...interface{}) (res sql.Result, err error) {
query, err := builder.build.adapter.SimpleUpdate(builder.up)
if err != nil {
return res, err
}
return builder.build.exec(query, args...)
}
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 (builder *AccSelectBuilder) Columns(columns string) *AccSelectBuilder {
builder.columns = columns
return builder
}
func (builder *AccSelectBuilder) Cols(columns string) *AccSelectBuilder {
builder.columns = columns
return builder
}
func (builder *AccSelectBuilder) Where(where string) *AccSelectBuilder {
if builder.where != "" {
builder.where += " AND "
}
builder.where += where
return builder
}
// TODO: Don't implement the SQL at the accumulator level but the adapter level
func (selectItem *AccSelectBuilder) In(column string, inList []int) *AccSelectBuilder {
if len(inList) == 0 {
return selectItem
}
var where = column + " IN("
for _, item := range inList {
where += strconv.Itoa(item) + ","
}
where = where[:len(where)-1] + ")"
if selectItem.where != "" {
where += " AND " + selectItem.where
}
selectItem.where = where
return selectItem
}
func (selectItem *AccSelectBuilder) InQ(column string, subBuilder *AccSelectBuilder) *AccSelectBuilder {
selectItem.inChain = subBuilder
selectItem.inColumn = column
return selectItem
}
func (builder *AccSelectBuilder) DateCutoff(column string, quantity int, unit string) *AccSelectBuilder {
builder.dateCutoff = &dateCutoff{column, quantity, unit}
return builder
}
func (builder *AccSelectBuilder) Orderby(orderby string) *AccSelectBuilder {
builder.orderby = orderby
return builder
}
func (builder *AccSelectBuilder) Limit(limit string) *AccSelectBuilder {
builder.limit = limit
return builder
}
func (builder *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 builder.dateCutoff != nil || builder.inChain != nil {
selectBuilder := builder.build.GetAdapter().Builder().Select().FromAcc(builder)
return builder.build.prepare(builder.build.GetAdapter().ComplexSelect(selectBuilder))
}
return builder.build.SimpleSelect(builder.table, builder.columns, builder.where, builder.orderby, builder.limit)
}
func (builder *AccSelectBuilder) query() (string, error) {
// 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 builder.dateCutoff != nil || builder.inChain != nil {
selectBuilder := builder.build.GetAdapter().Builder().Select().FromAcc(builder)
return builder.build.GetAdapter().ComplexSelect(selectBuilder)
}
return builder.build.adapter.SimpleSelect("", builder.table, builder.columns, builder.where, builder.orderby, builder.limit)
}
func (builder *AccSelectBuilder) Query(args ...interface{}) (*sql.Rows, error) {
stmt := builder.Prepare()
if stmt != nil {
return stmt.Query(args...)
}
return nil, builder.build.FirstError()
}
type AccRowWrap struct {
row *sql.Row
err error
}
func (wrap *AccRowWrap) Scan(dest ...interface{}) error {
if wrap.err != nil {
return wrap.err
}
return wrap.row.Scan(dest...)
}
// TODO: Test to make sure the errors are passed up properly
func (builder *AccSelectBuilder) QueryRow(args ...interface{}) *AccRowWrap {
stmt := builder.Prepare()
if stmt != nil {
return &AccRowWrap{stmt.QueryRow(args...), nil}
}
return &AccRowWrap{nil, builder.build.FirstError()}
}
// Experimental, reduces lines
func (builder *AccSelectBuilder) Each(handle func(*sql.Rows) error) error {
query, err := builder.query()
if err != nil {
return err
}
rows, err := builder.build.query(query)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
err = handle(rows)
if err != nil {
return err
}
}
return rows.Err()
}
func (builder *AccSelectBuilder) EachInt(handle func(int) error) error {
query, err := builder.query()
if err != nil {
return err
}
rows, err := builder.build.query(query)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var theInt int
err = rows.Scan(&theInt)
if err != nil {
return err
}
err = handle(theInt)
if err != nil {
return err
}
}
return rows.Err()
}
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)
}
func (builder *accInsertBuilder) Exec(args ...interface{}) (res sql.Result, err error) {
query, err := builder.build.adapter.SimpleInsert("", builder.table, builder.columns, builder.fields)
if err != nil {
return res, err
}
return builder.build.exec(query, args...)
}
func (builder *accInsertBuilder) Run(args ...interface{}) (int, error) {
query, err := builder.build.adapter.SimpleInsert("", builder.table, builder.columns, builder.fields)
if err != nil {
return 0, err
}
res, err := builder.build.exec(query, args...)
if err != nil {
return 0, err
}
lastID, err := res.LastInsertId()
return int(lastID), err
}
type accCountBuilder struct {
table string
where string
limit string
build *Accumulator
}
func (count *accCountBuilder) Where(where string) *accCountBuilder {
if count.where != "" {
count.where += " AND "
}
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)
}
func (count *accCountBuilder) Total() (total int, err error) {
stmt := count.Prepare()
if stmt == nil {
return 0, count.build.FirstError()
}
err = stmt.QueryRow().Scan(&total)
return total, err
}
// TODO: Add a Sum builder for summing viewchunks up into one number for the dashboard?