// Code generated by SQLBoiler 3.6.1 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT. // This file is meant to be re-generated in place and/or deleted at any time. package models import ( "context" "database/sql" "fmt" "reflect" "strings" "sync" "time" "github.com/friendsofgo/errors" "github.com/volatiletech/sqlboiler/boil" "github.com/volatiletech/sqlboiler/queries" "github.com/volatiletech/sqlboiler/queries/qm" "github.com/volatiletech/sqlboiler/queries/qmhelper" "github.com/volatiletech/sqlboiler/strmangle" ) // Session is an object representing the database table. type Session struct { ID string `boil:"id" json:"id" toml:"id" yaml:"id"` Mtime int64 `boil:"mtime" json:"mtime" toml:"mtime" yaml:"mtime"` UserID string `boil:"user_id" json:"user_id" toml:"user_id" yaml:"user_id"` R *sessionR `boil:"-" json:"-" toml:"-" yaml:"-"` L sessionL `boil:"-" json:"-" toml:"-" yaml:"-"` } var SessionColumns = struct { ID string Mtime string UserID string }{ ID: "id", Mtime: "mtime", UserID: "user_id", } // Generated where type whereHelperstring struct{ field string } func (w whereHelperstring) EQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) } func (w whereHelperstring) NEQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.NEQ, x) } func (w whereHelperstring) LT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) } func (w whereHelperstring) LTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LTE, x) } func (w whereHelperstring) GT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) } func (w whereHelperstring) GTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GTE, x) } func (w whereHelperstring) IN(slice []string) qm.QueryMod { values := make([]interface{}, 0, len(slice)) for _, value := range slice { values = append(values, value) } return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) } var SessionWhere = struct { ID whereHelperstring Mtime whereHelperint64 UserID whereHelperstring }{ ID: whereHelperstring{field: "\"session\".\"id\""}, Mtime: whereHelperint64{field: "\"session\".\"mtime\""}, UserID: whereHelperstring{field: "\"session\".\"user_id\""}, } // SessionRels is where relationship names are stored. var SessionRels = struct { }{} // sessionR is where relationships are stored. type sessionR struct { } // NewStruct creates a new relationship struct func (*sessionR) NewStruct() *sessionR { return &sessionR{} } // sessionL is where Load methods for each relationship are stored. type sessionL struct{} var ( sessionAllColumns = []string{"id", "mtime", "user_id"} sessionColumnsWithoutDefault = []string{"id", "mtime", "user_id"} sessionColumnsWithDefault = []string{} sessionPrimaryKeyColumns = []string{"id"} ) type ( // SessionSlice is an alias for a slice of pointers to Session. // This should generally be used opposed to []Session. SessionSlice []*Session // SessionHook is the signature for custom Session hook methods SessionHook func(context.Context, boil.ContextExecutor, *Session) error sessionQuery struct { *queries.Query } ) // Cache for insert, update and upsert var ( sessionType = reflect.TypeOf(&Session{}) sessionMapping = queries.MakeStructMapping(sessionType) sessionPrimaryKeyMapping, _ = queries.BindMapping(sessionType, sessionMapping, sessionPrimaryKeyColumns) sessionInsertCacheMut sync.RWMutex sessionInsertCache = make(map[string]insertCache) sessionUpdateCacheMut sync.RWMutex sessionUpdateCache = make(map[string]updateCache) sessionUpsertCacheMut sync.RWMutex sessionUpsertCache = make(map[string]insertCache) ) var ( // Force time package dependency for automated UpdatedAt/CreatedAt. _ = time.Second // Force qmhelper dependency for where clause generation (which doesn't // always happen) _ = qmhelper.Where ) var sessionBeforeInsertHooks []SessionHook var sessionBeforeUpdateHooks []SessionHook var sessionBeforeDeleteHooks []SessionHook var sessionBeforeUpsertHooks []SessionHook var sessionAfterInsertHooks []SessionHook var sessionAfterSelectHooks []SessionHook var sessionAfterUpdateHooks []SessionHook var sessionAfterDeleteHooks []SessionHook var sessionAfterUpsertHooks []SessionHook // doBeforeInsertHooks executes all "before insert" hooks. func (o *Session) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionBeforeInsertHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doBeforeUpdateHooks executes all "before Update" hooks. func (o *Session) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionBeforeUpdateHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doBeforeDeleteHooks executes all "before Delete" hooks. func (o *Session) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionBeforeDeleteHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doBeforeUpsertHooks executes all "before Upsert" hooks. func (o *Session) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionBeforeUpsertHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterInsertHooks executes all "after Insert" hooks. func (o *Session) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionAfterInsertHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterSelectHooks executes all "after Select" hooks. func (o *Session) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionAfterSelectHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterUpdateHooks executes all "after Update" hooks. func (o *Session) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionAfterUpdateHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterDeleteHooks executes all "after Delete" hooks. func (o *Session) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionAfterDeleteHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterUpsertHooks executes all "after Upsert" hooks. func (o *Session) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range sessionAfterUpsertHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // AddSessionHook registers your hook function for all future operations. func AddSessionHook(hookPoint boil.HookPoint, sessionHook SessionHook) { switch hookPoint { case boil.BeforeInsertHook: sessionBeforeInsertHooks = append(sessionBeforeInsertHooks, sessionHook) case boil.BeforeUpdateHook: sessionBeforeUpdateHooks = append(sessionBeforeUpdateHooks, sessionHook) case boil.BeforeDeleteHook: sessionBeforeDeleteHooks = append(sessionBeforeDeleteHooks, sessionHook) case boil.BeforeUpsertHook: sessionBeforeUpsertHooks = append(sessionBeforeUpsertHooks, sessionHook) case boil.AfterInsertHook: sessionAfterInsertHooks = append(sessionAfterInsertHooks, sessionHook) case boil.AfterSelectHook: sessionAfterSelectHooks = append(sessionAfterSelectHooks, sessionHook) case boil.AfterUpdateHook: sessionAfterUpdateHooks = append(sessionAfterUpdateHooks, sessionHook) case boil.AfterDeleteHook: sessionAfterDeleteHooks = append(sessionAfterDeleteHooks, sessionHook) case boil.AfterUpsertHook: sessionAfterUpsertHooks = append(sessionAfterUpsertHooks, sessionHook) } } // One returns a single session record from the query. func (q sessionQuery) One(ctx context.Context, exec boil.ContextExecutor) (*Session, error) { o := &Session{} queries.SetLimit(q.Query, 1) err := q.Bind(ctx, exec, o) if err != nil { if errors.Cause(err) == sql.ErrNoRows { return nil, sql.ErrNoRows } return nil, errors.Wrap(err, "models: failed to execute a one query for session") } if err := o.doAfterSelectHooks(ctx, exec); err != nil { return o, err } return o, nil } // All returns all Session records from the query. func (q sessionQuery) All(ctx context.Context, exec boil.ContextExecutor) (SessionSlice, error) { var o []*Session err := q.Bind(ctx, exec, &o) if err != nil { return nil, errors.Wrap(err, "models: failed to assign all query results to Session slice") } if len(sessionAfterSelectHooks) != 0 { for _, obj := range o { if err := obj.doAfterSelectHooks(ctx, exec); err != nil { return o, err } } } return o, nil } // Count returns the count of all Session records in the query. func (q sessionQuery) Count(ctx context.Context, exec boil.ContextExecutor) (int64, error) { var count int64 queries.SetSelect(q.Query, nil) queries.SetCount(q.Query) err := q.Query.QueryRowContext(ctx, exec).Scan(&count) if err != nil { return 0, errors.Wrap(err, "models: failed to count session rows") } return count, nil } // Exists checks if the row exists in the table. func (q sessionQuery) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) { var count int64 queries.SetSelect(q.Query, nil) queries.SetCount(q.Query) queries.SetLimit(q.Query, 1) err := q.Query.QueryRowContext(ctx, exec).Scan(&count) if err != nil { return false, errors.Wrap(err, "models: failed to check if session exists") } return count > 0, nil } // Sessions retrieves all the records using an executor. func Sessions(mods ...qm.QueryMod) sessionQuery { mods = append(mods, qm.From("\"session\"")) return sessionQuery{NewQuery(mods...)} } // FindSession retrieves a single record by ID with an executor. // If selectCols is empty Find will return all columns. func FindSession(ctx context.Context, exec boil.ContextExecutor, iD string, selectCols ...string) (*Session, error) { sessionObj := &Session{} sel := "*" if len(selectCols) > 0 { sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") } query := fmt.Sprintf( "select %s from \"session\" where \"id\"=?", sel, ) q := queries.Raw(query, iD) err := q.Bind(ctx, exec, sessionObj) if err != nil { if errors.Cause(err) == sql.ErrNoRows { return nil, sql.ErrNoRows } return nil, errors.Wrap(err, "models: unable to select from session") } return sessionObj, nil } // Insert a single record using an executor. // See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. func (o *Session) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { if o == nil { return errors.New("models: no session provided for insertion") } var err error if err := o.doBeforeInsertHooks(ctx, exec); err != nil { return err } nzDefaults := queries.NonZeroDefaultSet(sessionColumnsWithDefault, o) key := makeCacheKey(columns, nzDefaults) sessionInsertCacheMut.RLock() cache, cached := sessionInsertCache[key] sessionInsertCacheMut.RUnlock() if !cached { wl, returnColumns := columns.InsertColumnSet( sessionAllColumns, sessionColumnsWithDefault, sessionColumnsWithoutDefault, nzDefaults, ) cache.valueMapping, err = queries.BindMapping(sessionType, sessionMapping, wl) if err != nil { return err } cache.retMapping, err = queries.BindMapping(sessionType, sessionMapping, returnColumns) if err != nil { return err } if len(wl) != 0 { cache.query = fmt.Sprintf("INSERT INTO \"session\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) } else { cache.query = "INSERT INTO \"session\" () VALUES ()%s%s" } var queryOutput, queryReturning string if len(cache.retMapping) != 0 { cache.retQuery = fmt.Sprintf("SELECT \"%s\" FROM \"session\" WHERE %s", strings.Join(returnColumns, "\",\""), strmangle.WhereClause("\"", "\"", 0, sessionPrimaryKeyColumns)) } cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning) } value := reflect.Indirect(reflect.ValueOf(o)) vals := queries.ValuesFromMapping(value, cache.valueMapping) if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, cache.query) fmt.Fprintln(writer, vals) } _, err = exec.ExecContext(ctx, cache.query, vals...) if err != nil { return errors.Wrap(err, "models: unable to insert into session") } var identifierCols []interface{} if len(cache.retMapping) == 0 { goto CacheNoHooks } identifierCols = []interface{}{ o.ID, } if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, cache.retQuery) fmt.Fprintln(writer, identifierCols...) } err = exec.QueryRowContext(ctx, cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) if err != nil { return errors.Wrap(err, "models: unable to populate default values for session") } CacheNoHooks: if !cached { sessionInsertCacheMut.Lock() sessionInsertCache[key] = cache sessionInsertCacheMut.Unlock() } return o.doAfterInsertHooks(ctx, exec) } // Update uses an executor to update the Session. // See boil.Columns.UpdateColumnSet documentation to understand column list inference for updates. // Update does not automatically update the record in case of default values. Use .Reload() to refresh the records. func (o *Session) Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error) { var err error if err = o.doBeforeUpdateHooks(ctx, exec); err != nil { return 0, err } key := makeCacheKey(columns, nil) sessionUpdateCacheMut.RLock() cache, cached := sessionUpdateCache[key] sessionUpdateCacheMut.RUnlock() if !cached { wl := columns.UpdateColumnSet( sessionAllColumns, sessionPrimaryKeyColumns, ) if !columns.IsWhitelist() { wl = strmangle.SetComplement(wl, []string{"created_at"}) } if len(wl) == 0 { return 0, errors.New("models: unable to update session, could not build whitelist") } cache.query = fmt.Sprintf("UPDATE \"session\" SET %s WHERE %s", strmangle.SetParamNames("\"", "\"", 0, wl), strmangle.WhereClause("\"", "\"", 0, sessionPrimaryKeyColumns), ) cache.valueMapping, err = queries.BindMapping(sessionType, sessionMapping, append(wl, sessionPrimaryKeyColumns...)) if err != nil { return 0, err } } values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping) if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, cache.query) fmt.Fprintln(writer, values) } var result sql.Result result, err = exec.ExecContext(ctx, cache.query, values...) if err != nil { return 0, errors.Wrap(err, "models: unable to update session row") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: failed to get rows affected by update for session") } if !cached { sessionUpdateCacheMut.Lock() sessionUpdateCache[key] = cache sessionUpdateCacheMut.Unlock() } return rowsAff, o.doAfterUpdateHooks(ctx, exec) } // UpdateAll updates all rows with the specified column values. func (q sessionQuery) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) { queries.SetUpdate(q.Query, cols) result, err := q.Query.ExecContext(ctx, exec) if err != nil { return 0, errors.Wrap(err, "models: unable to update all for session") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: unable to retrieve rows affected for session") } return rowsAff, nil } // UpdateAll updates all rows with the specified column values, using an executor. func (o SessionSlice) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) { ln := int64(len(o)) if ln == 0 { return 0, nil } if len(cols) == 0 { return 0, errors.New("models: update all requires at least one column argument") } colNames := make([]string, len(cols)) args := make([]interface{}, len(cols)) i := 0 for name, value := range cols { colNames[i] = name args[i] = value i++ } // Append all of the primary key values for each column for _, obj := range o { pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), sessionPrimaryKeyMapping) args = append(args, pkeyArgs...) } sql := fmt.Sprintf("UPDATE \"session\" SET %s WHERE %s", strmangle.SetParamNames("\"", "\"", 0, colNames), strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, sessionPrimaryKeyColumns, len(o))) if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, sql) fmt.Fprintln(writer, args...) } result, err := exec.ExecContext(ctx, sql, args...) if err != nil { return 0, errors.Wrap(err, "models: unable to update all in session slice") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all session") } return rowsAff, nil } // Delete deletes a single Session record with an executor. // Delete will match against the primary key column to find the record to delete. func (o *Session) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) { if o == nil { return 0, errors.New("models: no Session provided for delete") } if err := o.doBeforeDeleteHooks(ctx, exec); err != nil { return 0, err } args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), sessionPrimaryKeyMapping) sql := "DELETE FROM \"session\" WHERE \"id\"=?" if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, sql) fmt.Fprintln(writer, args...) } result, err := exec.ExecContext(ctx, sql, args...) if err != nil { return 0, errors.Wrap(err, "models: unable to delete from session") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: failed to get rows affected by delete for session") } if err := o.doAfterDeleteHooks(ctx, exec); err != nil { return 0, err } return rowsAff, nil } // DeleteAll deletes all matching rows. func (q sessionQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { if q.Query == nil { return 0, errors.New("models: no sessionQuery provided for delete all") } queries.SetDelete(q.Query) result, err := q.Query.ExecContext(ctx, exec) if err != nil { return 0, errors.Wrap(err, "models: unable to delete all from session") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for session") } return rowsAff, nil } // DeleteAll deletes all rows in the slice, using an executor. func (o SessionSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { if len(o) == 0 { return 0, nil } if len(sessionBeforeDeleteHooks) != 0 { for _, obj := range o { if err := obj.doBeforeDeleteHooks(ctx, exec); err != nil { return 0, err } } } var args []interface{} for _, obj := range o { pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), sessionPrimaryKeyMapping) args = append(args, pkeyArgs...) } sql := "DELETE FROM \"session\" WHERE " + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, sessionPrimaryKeyColumns, len(o)) if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, sql) fmt.Fprintln(writer, args) } result, err := exec.ExecContext(ctx, sql, args...) if err != nil { return 0, errors.Wrap(err, "models: unable to delete all from session slice") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for session") } if len(sessionAfterDeleteHooks) != 0 { for _, obj := range o { if err := obj.doAfterDeleteHooks(ctx, exec); err != nil { return 0, err } } } return rowsAff, nil } // Reload refetches the object from the database // using the primary keys with an executor. func (o *Session) Reload(ctx context.Context, exec boil.ContextExecutor) error { ret, err := FindSession(ctx, exec, o.ID) if err != nil { return err } *o = *ret return nil } // ReloadAll refetches every row with matching primary key column values // and overwrites the original object slice with the newly updated slice. func (o *SessionSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error { if o == nil || len(*o) == 0 { return nil } slice := SessionSlice{} var args []interface{} for _, obj := range *o { pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), sessionPrimaryKeyMapping) args = append(args, pkeyArgs...) } sql := "SELECT \"session\".* FROM \"session\" WHERE " + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, sessionPrimaryKeyColumns, len(*o)) q := queries.Raw(sql, args...) err := q.Bind(ctx, exec, &slice) if err != nil { return errors.Wrap(err, "models: unable to reload all in SessionSlice") } *o = slice return nil } // SessionExists checks if the Session row exists. func SessionExists(ctx context.Context, exec boil.ContextExecutor, iD string) (bool, error) { var exists bool sql := "select exists(select 1 from \"session\" where \"id\"=? limit 1)" if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, sql) fmt.Fprintln(writer, iD) } row := exec.QueryRowContext(ctx, sql, iD) err := row.Scan(&exists) if err != nil { return false, errors.Wrap(err, "models: unable to check if session exists") } return exists, nil }