396 lines
12 KiB
Go
396 lines
12 KiB
Go
|
|
// Code generated by BobGen mysql v0.42.0. DO NOT EDIT.
|
||
|
|
// This file is meant to be re-generated in place and/or deleted at any time.
|
||
|
|
|
||
|
|
package models
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"io"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/aarondl/opt/omit"
|
||
|
|
"github.com/stephenafamo/bob"
|
||
|
|
"github.com/stephenafamo/bob/dialect/mysql"
|
||
|
|
"github.com/stephenafamo/bob/dialect/mysql/dialect"
|
||
|
|
"github.com/stephenafamo/bob/dialect/mysql/dm"
|
||
|
|
"github.com/stephenafamo/bob/dialect/mysql/sm"
|
||
|
|
"github.com/stephenafamo/bob/dialect/mysql/um"
|
||
|
|
"github.com/stephenafamo/bob/expr"
|
||
|
|
)
|
||
|
|
|
||
|
|
// DjangoSession is an object representing the database table.
|
||
|
|
type DjangoSession struct {
|
||
|
|
SessionKey string `db:"session_key,pk" `
|
||
|
|
SessionData string `db:"session_data" `
|
||
|
|
ExpireDate time.Time `db:"expire_date" `
|
||
|
|
}
|
||
|
|
|
||
|
|
// DjangoSessionSlice is an alias for a slice of pointers to DjangoSession.
|
||
|
|
// This should almost always be used instead of []*DjangoSession.
|
||
|
|
type DjangoSessionSlice []*DjangoSession
|
||
|
|
|
||
|
|
// DjangoSessions contains methods to work with the django_session table
|
||
|
|
var DjangoSessions = mysql.NewTablex[*DjangoSession, DjangoSessionSlice, *DjangoSessionSetter]("django_session", buildDjangoSessionColumns("django_session"), []string{"session_key"})
|
||
|
|
|
||
|
|
// DjangoSessionsQuery is a query on the django_session table
|
||
|
|
type DjangoSessionsQuery = *mysql.ViewQuery[*DjangoSession, DjangoSessionSlice]
|
||
|
|
|
||
|
|
func buildDjangoSessionColumns(alias string) djangoSessionColumns {
|
||
|
|
return djangoSessionColumns{
|
||
|
|
ColumnsExpr: expr.NewColumnsExpr(
|
||
|
|
"session_key", "session_data", "expire_date",
|
||
|
|
).WithParent("django_session"),
|
||
|
|
tableAlias: alias,
|
||
|
|
SessionKey: mysql.Quote(alias, "session_key"),
|
||
|
|
SessionData: mysql.Quote(alias, "session_data"),
|
||
|
|
ExpireDate: mysql.Quote(alias, "expire_date"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type djangoSessionColumns struct {
|
||
|
|
expr.ColumnsExpr
|
||
|
|
tableAlias string
|
||
|
|
SessionKey mysql.Expression
|
||
|
|
SessionData mysql.Expression
|
||
|
|
ExpireDate mysql.Expression
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c djangoSessionColumns) Alias() string {
|
||
|
|
return c.tableAlias
|
||
|
|
}
|
||
|
|
|
||
|
|
func (djangoSessionColumns) AliasedAs(alias string) djangoSessionColumns {
|
||
|
|
return buildDjangoSessionColumns(alias)
|
||
|
|
}
|
||
|
|
|
||
|
|
// DjangoSessionSetter is used for insert/upsert/update operations
|
||
|
|
// All values are optional, and do not have to be set
|
||
|
|
// Generated columns are not included
|
||
|
|
type DjangoSessionSetter struct {
|
||
|
|
SessionKey omit.Val[string] `db:"session_key,pk" `
|
||
|
|
SessionData omit.Val[string] `db:"session_data" `
|
||
|
|
ExpireDate omit.Val[time.Time] `db:"expire_date" `
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s DjangoSessionSetter) SetColumns() []string {
|
||
|
|
vals := make([]string, 0, 3)
|
||
|
|
if s.SessionKey.IsValue() {
|
||
|
|
vals = append(vals, "session_key")
|
||
|
|
}
|
||
|
|
if s.SessionData.IsValue() {
|
||
|
|
vals = append(vals, "session_data")
|
||
|
|
}
|
||
|
|
if s.ExpireDate.IsValue() {
|
||
|
|
vals = append(vals, "expire_date")
|
||
|
|
}
|
||
|
|
return vals
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s DjangoSessionSetter) Overwrite(t *DjangoSession) {
|
||
|
|
if s.SessionKey.IsValue() {
|
||
|
|
t.SessionKey = s.SessionKey.MustGet()
|
||
|
|
}
|
||
|
|
if s.SessionData.IsValue() {
|
||
|
|
t.SessionData = s.SessionData.MustGet()
|
||
|
|
}
|
||
|
|
if s.ExpireDate.IsValue() {
|
||
|
|
t.ExpireDate = s.ExpireDate.MustGet()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *DjangoSessionSetter) Apply(q *dialect.InsertQuery) {
|
||
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||
|
|
return DjangoSessions.BeforeInsertHooks.RunHooks(ctx, exec, s)
|
||
|
|
})
|
||
|
|
|
||
|
|
q.AppendValues(
|
||
|
|
bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||
|
|
if !(s.SessionKey.IsValue()) {
|
||
|
|
return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
|
||
|
|
}
|
||
|
|
return mysql.Arg(s.SessionKey.MustGet()).WriteSQL(ctx, w, d, start)
|
||
|
|
}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||
|
|
if !(s.SessionData.IsValue()) {
|
||
|
|
return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
|
||
|
|
}
|
||
|
|
return mysql.Arg(s.SessionData.MustGet()).WriteSQL(ctx, w, d, start)
|
||
|
|
}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||
|
|
if !(s.ExpireDate.IsValue()) {
|
||
|
|
return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
|
||
|
|
}
|
||
|
|
return mysql.Arg(s.ExpireDate.MustGet()).WriteSQL(ctx, w, d, start)
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s DjangoSessionSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||
|
|
return um.Set(s.Expressions("django_session")...)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s DjangoSessionSetter) Expressions(prefix ...string) []bob.Expression {
|
||
|
|
exprs := make([]bob.Expression, 0, 3)
|
||
|
|
|
||
|
|
if s.SessionKey.IsValue() {
|
||
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||
|
|
mysql.Quote(append(prefix, "session_key")...),
|
||
|
|
mysql.Arg(s.SessionKey),
|
||
|
|
}})
|
||
|
|
}
|
||
|
|
|
||
|
|
if s.SessionData.IsValue() {
|
||
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||
|
|
mysql.Quote(append(prefix, "session_data")...),
|
||
|
|
mysql.Arg(s.SessionData),
|
||
|
|
}})
|
||
|
|
}
|
||
|
|
|
||
|
|
if s.ExpireDate.IsValue() {
|
||
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||
|
|
mysql.Quote(append(prefix, "expire_date")...),
|
||
|
|
mysql.Arg(s.ExpireDate),
|
||
|
|
}})
|
||
|
|
}
|
||
|
|
|
||
|
|
return exprs
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindDjangoSession retrieves a single record by primary key
|
||
|
|
// If cols is empty Find will return all columns.
|
||
|
|
func FindDjangoSession(ctx context.Context, exec bob.Executor, SessionKeyPK string, cols ...string) (*DjangoSession, error) {
|
||
|
|
if len(cols) == 0 {
|
||
|
|
return DjangoSessions.Query(
|
||
|
|
sm.Where(DjangoSessions.Columns.SessionKey.EQ(mysql.Arg(SessionKeyPK))),
|
||
|
|
).One(ctx, exec)
|
||
|
|
}
|
||
|
|
|
||
|
|
return DjangoSessions.Query(
|
||
|
|
sm.Where(DjangoSessions.Columns.SessionKey.EQ(mysql.Arg(SessionKeyPK))),
|
||
|
|
sm.Columns(DjangoSessions.Columns.Only(cols...)),
|
||
|
|
).One(ctx, exec)
|
||
|
|
}
|
||
|
|
|
||
|
|
// DjangoSessionExists checks the presence of a single record by primary key
|
||
|
|
func DjangoSessionExists(ctx context.Context, exec bob.Executor, SessionKeyPK string) (bool, error) {
|
||
|
|
return DjangoSessions.Query(
|
||
|
|
sm.Where(DjangoSessions.Columns.SessionKey.EQ(mysql.Arg(SessionKeyPK))),
|
||
|
|
).Exists(ctx, exec)
|
||
|
|
}
|
||
|
|
|
||
|
|
// AfterQueryHook is called after DjangoSession is retrieved from the database
|
||
|
|
func (o *DjangoSession) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||
|
|
var err error
|
||
|
|
|
||
|
|
switch queryType {
|
||
|
|
case bob.QueryTypeSelect:
|
||
|
|
ctx, err = DjangoSessions.AfterSelectHooks.RunHooks(ctx, exec, DjangoSessionSlice{o})
|
||
|
|
case bob.QueryTypeInsert:
|
||
|
|
ctx, err = DjangoSessions.AfterInsertHooks.RunHooks(ctx, exec, DjangoSessionSlice{o})
|
||
|
|
case bob.QueryTypeUpdate:
|
||
|
|
ctx, err = DjangoSessions.AfterUpdateHooks.RunHooks(ctx, exec, DjangoSessionSlice{o})
|
||
|
|
case bob.QueryTypeDelete:
|
||
|
|
ctx, err = DjangoSessions.AfterDeleteHooks.RunHooks(ctx, exec, DjangoSessionSlice{o})
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// primaryKeyVals returns the primary key values of the DjangoSession
|
||
|
|
func (o *DjangoSession) primaryKeyVals() bob.Expression {
|
||
|
|
return mysql.Arg(o.SessionKey)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o *DjangoSession) pkEQ() dialect.Expression {
|
||
|
|
return mysql.Quote("django_session", "session_key").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||
|
|
return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update uses an executor to update the DjangoSession
|
||
|
|
func (o *DjangoSession) Update(ctx context.Context, exec bob.Executor, s *DjangoSessionSetter) error {
|
||
|
|
_, err := DjangoSessions.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
s.Overwrite(o)
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Delete deletes a single DjangoSession record with an executor
|
||
|
|
func (o *DjangoSession) Delete(ctx context.Context, exec bob.Executor) error {
|
||
|
|
_, err := DjangoSessions.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Reload refreshes the DjangoSession using the executor
|
||
|
|
func (o *DjangoSession) Reload(ctx context.Context, exec bob.Executor) error {
|
||
|
|
o2, err := DjangoSessions.Query(
|
||
|
|
sm.Where(DjangoSessions.Columns.SessionKey.EQ(mysql.Arg(o.SessionKey))),
|
||
|
|
).One(ctx, exec)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
*o = *o2
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// AfterQueryHook is called after DjangoSessionSlice is retrieved from the database
|
||
|
|
func (o DjangoSessionSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||
|
|
var err error
|
||
|
|
|
||
|
|
switch queryType {
|
||
|
|
case bob.QueryTypeSelect:
|
||
|
|
ctx, err = DjangoSessions.AfterSelectHooks.RunHooks(ctx, exec, o)
|
||
|
|
case bob.QueryTypeInsert:
|
||
|
|
ctx, err = DjangoSessions.AfterInsertHooks.RunHooks(ctx, exec, o)
|
||
|
|
case bob.QueryTypeUpdate:
|
||
|
|
ctx, err = DjangoSessions.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||
|
|
case bob.QueryTypeDelete:
|
||
|
|
ctx, err = DjangoSessions.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o DjangoSessionSlice) pkIN() dialect.Expression {
|
||
|
|
if len(o) == 0 {
|
||
|
|
return mysql.Raw("NULL")
|
||
|
|
}
|
||
|
|
|
||
|
|
return mysql.Quote("django_session", "session_key").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||
|
|
pkPairs := make([]bob.Expression, len(o))
|
||
|
|
for i, row := range o {
|
||
|
|
pkPairs[i] = row.primaryKeyVals()
|
||
|
|
}
|
||
|
|
return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
// copyMatchingRows finds models in the given slice that have the same primary key
|
||
|
|
// then it first copies the existing relationships from the old model to the new model
|
||
|
|
// and then replaces the old model in the slice with the new model
|
||
|
|
func (o DjangoSessionSlice) copyMatchingRows(from ...*DjangoSession) {
|
||
|
|
for i, old := range o {
|
||
|
|
for _, new := range from {
|
||
|
|
if new.SessionKey != old.SessionKey {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
o[i] = new
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
|
||
|
|
func (o DjangoSessionSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||
|
|
return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
|
||
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||
|
|
return DjangoSessions.BeforeUpdateHooks.RunHooks(ctx, exec, o)
|
||
|
|
})
|
||
|
|
|
||
|
|
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||
|
|
var err error
|
||
|
|
switch retrieved := retrieved.(type) {
|
||
|
|
case *DjangoSession:
|
||
|
|
o.copyMatchingRows(retrieved)
|
||
|
|
case []*DjangoSession:
|
||
|
|
o.copyMatchingRows(retrieved...)
|
||
|
|
case DjangoSessionSlice:
|
||
|
|
o.copyMatchingRows(retrieved...)
|
||
|
|
default:
|
||
|
|
// If the retrieved value is not a DjangoSession or a slice of DjangoSession
|
||
|
|
// then run the AfterUpdateHooks on the slice
|
||
|
|
_, err = DjangoSessions.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}))
|
||
|
|
|
||
|
|
q.AppendWhere(o.pkIN())
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
||
|
|
func (o DjangoSessionSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
|
||
|
|
return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
|
||
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||
|
|
return DjangoSessions.BeforeDeleteHooks.RunHooks(ctx, exec, o)
|
||
|
|
})
|
||
|
|
|
||
|
|
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
||
|
|
var err error
|
||
|
|
switch retrieved := retrieved.(type) {
|
||
|
|
case *DjangoSession:
|
||
|
|
o.copyMatchingRows(retrieved)
|
||
|
|
case []*DjangoSession:
|
||
|
|
o.copyMatchingRows(retrieved...)
|
||
|
|
case DjangoSessionSlice:
|
||
|
|
o.copyMatchingRows(retrieved...)
|
||
|
|
default:
|
||
|
|
// If the retrieved value is not a DjangoSession or a slice of DjangoSession
|
||
|
|
// then run the AfterDeleteHooks on the slice
|
||
|
|
_, err = DjangoSessions.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}))
|
||
|
|
|
||
|
|
q.AppendWhere(o.pkIN())
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o DjangoSessionSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals DjangoSessionSetter) error {
|
||
|
|
_, err := DjangoSessions.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)
|
||
|
|
|
||
|
|
for i := range o {
|
||
|
|
vals.Overwrite(o[i])
|
||
|
|
}
|
||
|
|
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o DjangoSessionSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
||
|
|
if len(o) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
_, err := DjangoSessions.Delete(o.DeleteMod()).Exec(ctx, exec)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (o DjangoSessionSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
||
|
|
if len(o) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
o2, err := DjangoSessions.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
o.copyMatchingRows(o2...)
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type djangoSessionWhere[Q mysql.Filterable] struct {
|
||
|
|
SessionKey mysql.WhereMod[Q, string]
|
||
|
|
SessionData mysql.WhereMod[Q, string]
|
||
|
|
ExpireDate mysql.WhereMod[Q, time.Time]
|
||
|
|
}
|
||
|
|
|
||
|
|
func (djangoSessionWhere[Q]) AliasedAs(alias string) djangoSessionWhere[Q] {
|
||
|
|
return buildDjangoSessionWhere[Q](buildDjangoSessionColumns(alias))
|
||
|
|
}
|
||
|
|
|
||
|
|
func buildDjangoSessionWhere[Q mysql.Filterable](cols djangoSessionColumns) djangoSessionWhere[Q] {
|
||
|
|
return djangoSessionWhere[Q]{
|
||
|
|
SessionKey: mysql.Where[Q, string](cols.SessionKey),
|
||
|
|
SessionData: mysql.Where[Q, string](cols.SessionData),
|
||
|
|
ExpireDate: mysql.Where[Q, time.Time](cols.ExpireDate),
|
||
|
|
}
|
||
|
|
}
|