gosora/query_gen/micro_builders.go

284 lines
6.7 KiB
Go
Raw Normal View History

package qgen
type dateCutoff struct {
2022-02-21 03:32:53 +00:00
Column string
Quantity int
Unit string
Type int
}
2017-11-13 00:31:46 +00:00
type prebuilder struct {
2022-02-21 03:32:53 +00:00
adapter Adapter
2017-11-13 00:31:46 +00:00
}
func (b *prebuilder) Select(nlist ...string) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
name := optString(nlist, "")
return &selectPrebuilder{name, "", "", "", "", "", nil, nil, "", b.adapter}
2017-11-13 00:31:46 +00:00
}
func (b *prebuilder) Count(nlist ...string) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
name := optString(nlist, "")
return &selectPrebuilder{name, "", "COUNT(*)", "", "", "", nil, nil, "", b.adapter}
}
func (b *prebuilder) Insert(nlist ...string) *insertPrebuilder {
2022-02-21 03:32:53 +00:00
name := optString(nlist, "")
return &insertPrebuilder{name, "", "", "", b.adapter}
2017-11-13 00:31:46 +00:00
}
func (b *prebuilder) Update(nlist ...string) *updatePrebuilder {
2022-02-21 03:32:53 +00:00
name := optString(nlist, "")
return &updatePrebuilder{name, "", "", "", nil, nil, b.adapter}
2017-11-13 00:31:46 +00:00
}
func (b *prebuilder) Delete(nlist ...string) *deletePrebuilder {
2022-02-21 03:32:53 +00:00
name := optString(nlist, "")
return &deletePrebuilder{name, "", "", nil, b.adapter}
2017-11-13 00:31:46 +00:00
}
type deletePrebuilder struct {
2022-02-21 03:32:53 +00:00
name string
table string
where string
dateCutoff *dateCutoff
2022-02-21 03:32:53 +00:00
build Adapter
}
func (b *deletePrebuilder) Table(table string) *deletePrebuilder {
2022-02-21 03:32:53 +00:00
b.table = table
return b
}
func (b *deletePrebuilder) Where(where string) *deletePrebuilder {
2022-02-21 03:32:53 +00:00
if b.where != "" {
b.where += " AND "
}
b.where += where
return b
}
// TODO: We probably want to avoid the double allocation of two builders somehow
func (b *deletePrebuilder) FromAcc(acc *accDeleteBuilder) *deletePrebuilder {
2022-02-21 03:32:53 +00:00
b.table = acc.table
b.where = acc.where
b.dateCutoff = acc.dateCutoff
return b
}
func (b *deletePrebuilder) Text() (string, error) {
2022-02-21 03:32:53 +00:00
return b.build.SimpleDelete(b.name, b.table, b.where)
}
func (b *deletePrebuilder) Parse() {
2022-02-21 03:32:53 +00:00
b.build.SimpleDelete(b.name, b.table, b.where)
}
type updatePrebuilder struct {
2022-02-21 03:32:53 +00:00
name string
table string
set string
where string
dateCutoff *dateCutoff // We might want to do this in a slightly less hacky way
whereSubQuery *selectPrebuilder
2022-02-21 03:32:53 +00:00
build Adapter
}
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 05:42:41 +00:00
func qUpdate(table string, set string, where string) *updatePrebuilder {
2022-02-21 03:32:53 +00:00
return &updatePrebuilder{table: table, set: set, where: where}
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 05:42:41 +00:00
}
func (b *updatePrebuilder) Table(table string) *updatePrebuilder {
2022-02-21 03:32:53 +00:00
b.table = table
return b
}
func (b *updatePrebuilder) Set(set string) *updatePrebuilder {
2022-02-21 03:32:53 +00:00
b.set = set
return b
}
func (b *updatePrebuilder) Where(where string) *updatePrebuilder {
2022-02-21 03:32:53 +00:00
if b.where != "" {
b.where += " AND "
}
b.where += where
return b
}
func (b *updatePrebuilder) WhereQ(sel *selectPrebuilder) *updatePrebuilder {
2022-02-21 03:32:53 +00:00
b.whereSubQuery = sel
return b
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 05:42:41 +00:00
}
func (b *updatePrebuilder) Text() (string, error) {
2022-02-21 03:32:53 +00:00
return b.build.SimpleUpdate(b)
}
func (b *updatePrebuilder) Parse() {
2022-02-21 03:32:53 +00:00
b.build.SimpleUpdate(b)
}
type selectPrebuilder struct {
2022-02-21 03:32:53 +00:00
name string
table string
columns string
where string
orderby string
limit string
dateCutoff *dateCutoff
inChain *selectPrebuilder
inColumn string // for inChain
2022-02-21 03:32:53 +00:00
build Adapter
}
func (b *selectPrebuilder) Table(table string) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
b.table = table
return b
}
func (b *selectPrebuilder) Columns(columns string) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
b.columns = columns
return b
}
func (b *selectPrebuilder) Where(where string) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
if b.where != "" {
b.where += " AND "
}
b.where += where
return b
}
func (b *selectPrebuilder) InQ(subBuilder *selectPrebuilder) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
b.inChain = subBuilder
return b
}
func (b *selectPrebuilder) Orderby(orderby string) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
b.orderby = orderby
return b
}
func (b *selectPrebuilder) Limit(limit string) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
b.limit = limit
return b
}
// TODO: We probably want to avoid the double allocation of two builders somehow
func (b *selectPrebuilder) FromAcc(acc *AccSelectBuilder) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
b.table = acc.table
if acc.columns != "" {
b.columns = acc.columns
}
b.where = acc.where
b.orderby = acc.orderby
b.limit = acc.limit
b.dateCutoff = acc.dateCutoff
if acc.inChain != nil {
b.inChain = &selectPrebuilder{"", acc.inChain.table, acc.inChain.columns, acc.inChain.where, acc.inChain.orderby, acc.inChain.limit, acc.inChain.dateCutoff, nil, "", b.build}
b.inColumn = acc.inColumn
}
return b
}
func (b *selectPrebuilder) FromCountAcc(acc *accCountBuilder) *selectPrebuilder {
2022-02-21 03:32:53 +00:00
b.table = acc.table
b.where = acc.where
b.limit = acc.limit
2022-02-21 03:32:53 +00:00
b.dateCutoff = acc.dateCutoff
if acc.inChain != nil {
b.inChain = &selectPrebuilder{"", acc.inChain.table, acc.inChain.columns, acc.inChain.where, acc.inChain.orderby, acc.inChain.limit, acc.inChain.dateCutoff, nil, "", b.build}
b.inColumn = acc.inColumn
}
return b
}
// TODO: Add support for dateCutoff
func (b *selectPrebuilder) Text() (string, error) {
2022-02-21 03:32:53 +00:00
return b.build.SimpleSelect(b.name, b.table, b.columns, b.where, b.orderby, b.limit)
}
// TODO: Add support for dateCutoff
func (b *selectPrebuilder) Parse() {
2022-02-21 03:32:53 +00:00
b.build.SimpleSelect(b.name, b.table, b.columns, b.where, b.orderby, b.limit)
}
type insertPrebuilder struct {
2022-02-21 03:32:53 +00:00
name string
table string
columns string
fields string
2022-02-21 03:32:53 +00:00
build Adapter
}
func (b *insertPrebuilder) Table(table string) *insertPrebuilder {
2022-02-21 03:32:53 +00:00
b.table = table
return b
}
func (b *insertPrebuilder) Columns(columns string) *insertPrebuilder {
2022-02-21 03:32:53 +00:00
b.columns = columns
return b
}
func (b *insertPrebuilder) Fields(fields string) *insertPrebuilder {
2022-02-21 03:32:53 +00:00
b.fields = fields
return b
}
func (b *insertPrebuilder) Text() (string, error) {
2022-02-21 03:32:53 +00:00
return b.build.SimpleInsert(b.name, b.table, b.columns, b.fields)
}
func (b *insertPrebuilder) Parse() {
2022-02-21 03:32:53 +00:00
b.build.SimpleInsert(b.name, b.table, b.columns, b.fields)
}
/*type countPrebuilder struct {
2022-02-21 03:32:53 +00:00
name string
table string
where string
limit string
2022-02-21 03:32:53 +00:00
build Adapter
}
func (b *countPrebuilder) Table(table string) *countPrebuilder {
2022-02-21 03:32:53 +00:00
b.table = table
return b
}
func b *countPrebuilder) Where(where string) *countPrebuilder {
2022-02-21 03:32:53 +00:00
if b.where != "" {
b.where += " AND "
}
b.where += where
return b
}
func (b *countPrebuilder) Limit(limit string) *countPrebuilder {
2022-02-21 03:32:53 +00:00
b.limit = limit
return b
}
func (b *countPrebuilder) Text() (string, error) {
2022-02-21 03:32:53 +00:00
return b.build.SimpleCount(b.name, b.table, b.where, b.limit)
}
func (b *countPrebuilder) Parse() {
2022-02-21 03:32:53 +00:00
b.build.SimpleCount(b.name, b.table, b.where, b.limit)
}*/
func optString(nlist []string, defaultStr string) string {
2022-02-21 03:32:53 +00:00
if len(nlist) == 0 {
return defaultStr
}
return nlist[0]
}