1065 lines
27 KiB
Go
1065 lines
27 KiB
Go
// SPDX-FileCopyrightText: 2021 The NGI Pointer Secure-Scuttlebutt Team of 2020/2021
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Code generated by SQLBoiler 4.5.0 (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/v4/boil"
|
|
"github.com/volatiletech/sqlboiler/v4/queries"
|
|
"github.com/volatiletech/sqlboiler/v4/queries/qm"
|
|
"github.com/volatiletech/sqlboiler/v4/queries/qmhelper"
|
|
"github.com/volatiletech/strmangle"
|
|
)
|
|
|
|
// Pin is an object representing the database table.
|
|
type Pin struct {
|
|
ID int64 `boil:"id" json:"id" toml:"id" yaml:"id"`
|
|
Name string `boil:"name" json:"name" toml:"name" yaml:"name"`
|
|
|
|
R *pinR `boil:"-" json:"-" toml:"-" yaml:"-"`
|
|
L pinL `boil:"-" json:"-" toml:"-" yaml:"-"`
|
|
}
|
|
|
|
var PinColumns = struct {
|
|
ID string
|
|
Name string
|
|
}{
|
|
ID: "id",
|
|
Name: "name",
|
|
}
|
|
|
|
// Generated where
|
|
|
|
var PinWhere = struct {
|
|
ID whereHelperint64
|
|
Name whereHelperstring
|
|
}{
|
|
ID: whereHelperint64{field: "\"pins\".\"id\""},
|
|
Name: whereHelperstring{field: "\"pins\".\"name\""},
|
|
}
|
|
|
|
// PinRels is where relationship names are stored.
|
|
var PinRels = struct {
|
|
Notices string
|
|
}{
|
|
Notices: "Notices",
|
|
}
|
|
|
|
// pinR is where relationships are stored.
|
|
type pinR struct {
|
|
Notices NoticeSlice `boil:"Notices" json:"Notices" toml:"Notices" yaml:"Notices"`
|
|
}
|
|
|
|
// NewStruct creates a new relationship struct
|
|
func (*pinR) NewStruct() *pinR {
|
|
return &pinR{}
|
|
}
|
|
|
|
// pinL is where Load methods for each relationship are stored.
|
|
type pinL struct{}
|
|
|
|
var (
|
|
pinAllColumns = []string{"id", "name"}
|
|
pinColumnsWithoutDefault = []string{"name"}
|
|
pinColumnsWithDefault = []string{"id"}
|
|
pinPrimaryKeyColumns = []string{"id"}
|
|
)
|
|
|
|
type (
|
|
// PinSlice is an alias for a slice of pointers to Pin.
|
|
// This should generally be used opposed to []Pin.
|
|
PinSlice []*Pin
|
|
// PinHook is the signature for custom Pin hook methods
|
|
PinHook func(context.Context, boil.ContextExecutor, *Pin) error
|
|
|
|
pinQuery struct {
|
|
*queries.Query
|
|
}
|
|
)
|
|
|
|
// Cache for insert, update and upsert
|
|
var (
|
|
pinType = reflect.TypeOf(&Pin{})
|
|
pinMapping = queries.MakeStructMapping(pinType)
|
|
pinPrimaryKeyMapping, _ = queries.BindMapping(pinType, pinMapping, pinPrimaryKeyColumns)
|
|
pinInsertCacheMut sync.RWMutex
|
|
pinInsertCache = make(map[string]insertCache)
|
|
pinUpdateCacheMut sync.RWMutex
|
|
pinUpdateCache = make(map[string]updateCache)
|
|
pinUpsertCacheMut sync.RWMutex
|
|
pinUpsertCache = 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 pinBeforeInsertHooks []PinHook
|
|
var pinBeforeUpdateHooks []PinHook
|
|
var pinBeforeDeleteHooks []PinHook
|
|
var pinBeforeUpsertHooks []PinHook
|
|
|
|
var pinAfterInsertHooks []PinHook
|
|
var pinAfterSelectHooks []PinHook
|
|
var pinAfterUpdateHooks []PinHook
|
|
var pinAfterDeleteHooks []PinHook
|
|
var pinAfterUpsertHooks []PinHook
|
|
|
|
// doBeforeInsertHooks executes all "before insert" hooks.
|
|
func (o *Pin) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinBeforeInsertHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doBeforeUpdateHooks executes all "before Update" hooks.
|
|
func (o *Pin) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinBeforeUpdateHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doBeforeDeleteHooks executes all "before Delete" hooks.
|
|
func (o *Pin) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinBeforeDeleteHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doBeforeUpsertHooks executes all "before Upsert" hooks.
|
|
func (o *Pin) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinBeforeUpsertHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doAfterInsertHooks executes all "after Insert" hooks.
|
|
func (o *Pin) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinAfterInsertHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doAfterSelectHooks executes all "after Select" hooks.
|
|
func (o *Pin) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinAfterSelectHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doAfterUpdateHooks executes all "after Update" hooks.
|
|
func (o *Pin) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinAfterUpdateHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doAfterDeleteHooks executes all "after Delete" hooks.
|
|
func (o *Pin) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinAfterDeleteHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doAfterUpsertHooks executes all "after Upsert" hooks.
|
|
func (o *Pin) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
|
|
if boil.HooksAreSkipped(ctx) {
|
|
return nil
|
|
}
|
|
|
|
for _, hook := range pinAfterUpsertHooks {
|
|
if err := hook(ctx, exec, o); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// AddPinHook registers your hook function for all future operations.
|
|
func AddPinHook(hookPoint boil.HookPoint, pinHook PinHook) {
|
|
switch hookPoint {
|
|
case boil.BeforeInsertHook:
|
|
pinBeforeInsertHooks = append(pinBeforeInsertHooks, pinHook)
|
|
case boil.BeforeUpdateHook:
|
|
pinBeforeUpdateHooks = append(pinBeforeUpdateHooks, pinHook)
|
|
case boil.BeforeDeleteHook:
|
|
pinBeforeDeleteHooks = append(pinBeforeDeleteHooks, pinHook)
|
|
case boil.BeforeUpsertHook:
|
|
pinBeforeUpsertHooks = append(pinBeforeUpsertHooks, pinHook)
|
|
case boil.AfterInsertHook:
|
|
pinAfterInsertHooks = append(pinAfterInsertHooks, pinHook)
|
|
case boil.AfterSelectHook:
|
|
pinAfterSelectHooks = append(pinAfterSelectHooks, pinHook)
|
|
case boil.AfterUpdateHook:
|
|
pinAfterUpdateHooks = append(pinAfterUpdateHooks, pinHook)
|
|
case boil.AfterDeleteHook:
|
|
pinAfterDeleteHooks = append(pinAfterDeleteHooks, pinHook)
|
|
case boil.AfterUpsertHook:
|
|
pinAfterUpsertHooks = append(pinAfterUpsertHooks, pinHook)
|
|
}
|
|
}
|
|
|
|
// One returns a single pin record from the query.
|
|
func (q pinQuery) One(ctx context.Context, exec boil.ContextExecutor) (*Pin, error) {
|
|
o := &Pin{}
|
|
|
|
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 pins")
|
|
}
|
|
|
|
if err := o.doAfterSelectHooks(ctx, exec); err != nil {
|
|
return o, err
|
|
}
|
|
|
|
return o, nil
|
|
}
|
|
|
|
// All returns all Pin records from the query.
|
|
func (q pinQuery) All(ctx context.Context, exec boil.ContextExecutor) (PinSlice, error) {
|
|
var o []*Pin
|
|
|
|
err := q.Bind(ctx, exec, &o)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "models: failed to assign all query results to Pin slice")
|
|
}
|
|
|
|
if len(pinAfterSelectHooks) != 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 Pin records in the query.
|
|
func (q pinQuery) 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 pins rows")
|
|
}
|
|
|
|
return count, nil
|
|
}
|
|
|
|
// Exists checks if the row exists in the table.
|
|
func (q pinQuery) 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 pins exists")
|
|
}
|
|
|
|
return count > 0, nil
|
|
}
|
|
|
|
// Notices retrieves all the notice's Notices with an executor.
|
|
func (o *Pin) Notices(mods ...qm.QueryMod) noticeQuery {
|
|
var queryMods []qm.QueryMod
|
|
if len(mods) != 0 {
|
|
queryMods = append(queryMods, mods...)
|
|
}
|
|
|
|
queryMods = append(queryMods,
|
|
qm.InnerJoin("\"pin_notices\" on \"notices\".\"id\" = \"pin_notices\".\"notice_id\""),
|
|
qm.Where("\"pin_notices\".\"pin_id\"=?", o.ID),
|
|
)
|
|
|
|
query := Notices(queryMods...)
|
|
queries.SetFrom(query.Query, "\"notices\"")
|
|
|
|
if len(queries.GetSelect(query.Query)) == 0 {
|
|
queries.SetSelect(query.Query, []string{"\"notices\".*"})
|
|
}
|
|
|
|
return query
|
|
}
|
|
|
|
// LoadNotices allows an eager lookup of values, cached into the
|
|
// loaded structs of the objects. This is for a 1-M or N-M relationship.
|
|
func (pinL) LoadNotices(ctx context.Context, e boil.ContextExecutor, singular bool, maybePin interface{}, mods queries.Applicator) error {
|
|
var slice []*Pin
|
|
var object *Pin
|
|
|
|
if singular {
|
|
object = maybePin.(*Pin)
|
|
} else {
|
|
slice = *maybePin.(*[]*Pin)
|
|
}
|
|
|
|
args := make([]interface{}, 0, 1)
|
|
if singular {
|
|
if object.R == nil {
|
|
object.R = &pinR{}
|
|
}
|
|
args = append(args, object.ID)
|
|
} else {
|
|
Outer:
|
|
for _, obj := range slice {
|
|
if obj.R == nil {
|
|
obj.R = &pinR{}
|
|
}
|
|
|
|
for _, a := range args {
|
|
if a == obj.ID {
|
|
continue Outer
|
|
}
|
|
}
|
|
|
|
args = append(args, obj.ID)
|
|
}
|
|
}
|
|
|
|
if len(args) == 0 {
|
|
return nil
|
|
}
|
|
|
|
query := NewQuery(
|
|
qm.Select("\"notices\".id, \"notices\".title, \"notices\".content, \"notices\".language, \"a\".\"pin_id\""),
|
|
qm.From("\"notices\""),
|
|
qm.InnerJoin("\"pin_notices\" as \"a\" on \"notices\".\"id\" = \"a\".\"notice_id\""),
|
|
qm.WhereIn("\"a\".\"pin_id\" in ?", args...),
|
|
)
|
|
if mods != nil {
|
|
mods.Apply(query)
|
|
}
|
|
|
|
results, err := query.QueryContext(ctx, e)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to eager load notices")
|
|
}
|
|
|
|
var resultSlice []*Notice
|
|
|
|
var localJoinCols []int64
|
|
for results.Next() {
|
|
one := new(Notice)
|
|
var localJoinCol int64
|
|
|
|
err = results.Scan(&one.ID, &one.Title, &one.Content, &one.Language, &localJoinCol)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to scan eager loaded results for notices")
|
|
}
|
|
if err = results.Err(); err != nil {
|
|
return errors.Wrap(err, "failed to plebian-bind eager loaded slice notices")
|
|
}
|
|
|
|
resultSlice = append(resultSlice, one)
|
|
localJoinCols = append(localJoinCols, localJoinCol)
|
|
}
|
|
|
|
if err = results.Close(); err != nil {
|
|
return errors.Wrap(err, "failed to close results in eager load on notices")
|
|
}
|
|
if err = results.Err(); err != nil {
|
|
return errors.Wrap(err, "error occurred during iteration of eager loaded relations for notices")
|
|
}
|
|
|
|
if len(noticeAfterSelectHooks) != 0 {
|
|
for _, obj := range resultSlice {
|
|
if err := obj.doAfterSelectHooks(ctx, e); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if singular {
|
|
object.R.Notices = resultSlice
|
|
for _, foreign := range resultSlice {
|
|
if foreign.R == nil {
|
|
foreign.R = ¬iceR{}
|
|
}
|
|
foreign.R.Pins = append(foreign.R.Pins, object)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
for i, foreign := range resultSlice {
|
|
localJoinCol := localJoinCols[i]
|
|
for _, local := range slice {
|
|
if local.ID == localJoinCol {
|
|
local.R.Notices = append(local.R.Notices, foreign)
|
|
if foreign.R == nil {
|
|
foreign.R = ¬iceR{}
|
|
}
|
|
foreign.R.Pins = append(foreign.R.Pins, local)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// AddNotices adds the given related objects to the existing relationships
|
|
// of the pin, optionally inserting them as new records.
|
|
// Appends related to o.R.Notices.
|
|
// Sets related.R.Pins appropriately.
|
|
func (o *Pin) AddNotices(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Notice) error {
|
|
var err error
|
|
for _, rel := range related {
|
|
if insert {
|
|
if err = rel.Insert(ctx, exec, boil.Infer()); err != nil {
|
|
return errors.Wrap(err, "failed to insert into foreign table")
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, rel := range related {
|
|
query := "insert into \"pin_notices\" (\"pin_id\", \"notice_id\") values (?, ?)"
|
|
values := []interface{}{o.ID, rel.ID}
|
|
|
|
if boil.IsDebug(ctx) {
|
|
writer := boil.DebugWriterFrom(ctx)
|
|
fmt.Fprintln(writer, query)
|
|
fmt.Fprintln(writer, values)
|
|
}
|
|
_, err = exec.ExecContext(ctx, query, values...)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to insert into join table")
|
|
}
|
|
}
|
|
if o.R == nil {
|
|
o.R = &pinR{
|
|
Notices: related,
|
|
}
|
|
} else {
|
|
o.R.Notices = append(o.R.Notices, related...)
|
|
}
|
|
|
|
for _, rel := range related {
|
|
if rel.R == nil {
|
|
rel.R = ¬iceR{
|
|
Pins: PinSlice{o},
|
|
}
|
|
} else {
|
|
rel.R.Pins = append(rel.R.Pins, o)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetNotices removes all previously related items of the
|
|
// pin replacing them completely with the passed
|
|
// in related items, optionally inserting them as new records.
|
|
// Sets o.R.Pins's Notices accordingly.
|
|
// Replaces o.R.Notices with related.
|
|
// Sets related.R.Pins's Notices accordingly.
|
|
func (o *Pin) SetNotices(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Notice) error {
|
|
query := "delete from \"pin_notices\" where \"pin_id\" = ?"
|
|
values := []interface{}{o.ID}
|
|
if boil.IsDebug(ctx) {
|
|
writer := boil.DebugWriterFrom(ctx)
|
|
fmt.Fprintln(writer, query)
|
|
fmt.Fprintln(writer, values)
|
|
}
|
|
_, err := exec.ExecContext(ctx, query, values...)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to remove relationships before set")
|
|
}
|
|
|
|
removeNoticesFromPinsSlice(o, related)
|
|
if o.R != nil {
|
|
o.R.Notices = nil
|
|
}
|
|
return o.AddNotices(ctx, exec, insert, related...)
|
|
}
|
|
|
|
// RemoveNotices relationships from objects passed in.
|
|
// Removes related items from R.Notices (uses pointer comparison, removal does not keep order)
|
|
// Sets related.R.Pins.
|
|
func (o *Pin) RemoveNotices(ctx context.Context, exec boil.ContextExecutor, related ...*Notice) error {
|
|
var err error
|
|
query := fmt.Sprintf(
|
|
"delete from \"pin_notices\" where \"pin_id\" = ? and \"notice_id\" in (%s)",
|
|
strmangle.Placeholders(dialect.UseIndexPlaceholders, len(related), 2, 1),
|
|
)
|
|
values := []interface{}{o.ID}
|
|
for _, rel := range related {
|
|
values = append(values, rel.ID)
|
|
}
|
|
|
|
if boil.IsDebug(ctx) {
|
|
writer := boil.DebugWriterFrom(ctx)
|
|
fmt.Fprintln(writer, query)
|
|
fmt.Fprintln(writer, values)
|
|
}
|
|
_, err = exec.ExecContext(ctx, query, values...)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to remove relationships before set")
|
|
}
|
|
removeNoticesFromPinsSlice(o, related)
|
|
if o.R == nil {
|
|
return nil
|
|
}
|
|
|
|
for _, rel := range related {
|
|
for i, ri := range o.R.Notices {
|
|
if rel != ri {
|
|
continue
|
|
}
|
|
|
|
ln := len(o.R.Notices)
|
|
if ln > 1 && i < ln-1 {
|
|
o.R.Notices[i] = o.R.Notices[ln-1]
|
|
}
|
|
o.R.Notices = o.R.Notices[:ln-1]
|
|
break
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func removeNoticesFromPinsSlice(o *Pin, related []*Notice) {
|
|
for _, rel := range related {
|
|
if rel.R == nil {
|
|
continue
|
|
}
|
|
for i, ri := range rel.R.Pins {
|
|
if o.ID != ri.ID {
|
|
continue
|
|
}
|
|
|
|
ln := len(rel.R.Pins)
|
|
if ln > 1 && i < ln-1 {
|
|
rel.R.Pins[i] = rel.R.Pins[ln-1]
|
|
}
|
|
rel.R.Pins = rel.R.Pins[:ln-1]
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Pins retrieves all the records using an executor.
|
|
func Pins(mods ...qm.QueryMod) pinQuery {
|
|
mods = append(mods, qm.From("\"pins\""))
|
|
return pinQuery{NewQuery(mods...)}
|
|
}
|
|
|
|
// FindPin retrieves a single record by ID with an executor.
|
|
// If selectCols is empty Find will return all columns.
|
|
func FindPin(ctx context.Context, exec boil.ContextExecutor, iD int64, selectCols ...string) (*Pin, error) {
|
|
pinObj := &Pin{}
|
|
|
|
sel := "*"
|
|
if len(selectCols) > 0 {
|
|
sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
|
|
}
|
|
query := fmt.Sprintf(
|
|
"select %s from \"pins\" where \"id\"=?", sel,
|
|
)
|
|
|
|
q := queries.Raw(query, iD)
|
|
|
|
err := q.Bind(ctx, exec, pinObj)
|
|
if err != nil {
|
|
if errors.Cause(err) == sql.ErrNoRows {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
return nil, errors.Wrap(err, "models: unable to select from pins")
|
|
}
|
|
|
|
return pinObj, nil
|
|
}
|
|
|
|
// Insert a single record using an executor.
|
|
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
|
|
func (o *Pin) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error {
|
|
if o == nil {
|
|
return errors.New("models: no pins provided for insertion")
|
|
}
|
|
|
|
var err error
|
|
|
|
if err := o.doBeforeInsertHooks(ctx, exec); err != nil {
|
|
return err
|
|
}
|
|
|
|
nzDefaults := queries.NonZeroDefaultSet(pinColumnsWithDefault, o)
|
|
|
|
key := makeCacheKey(columns, nzDefaults)
|
|
pinInsertCacheMut.RLock()
|
|
cache, cached := pinInsertCache[key]
|
|
pinInsertCacheMut.RUnlock()
|
|
|
|
if !cached {
|
|
wl, returnColumns := columns.InsertColumnSet(
|
|
pinAllColumns,
|
|
pinColumnsWithDefault,
|
|
pinColumnsWithoutDefault,
|
|
nzDefaults,
|
|
)
|
|
|
|
cache.valueMapping, err = queries.BindMapping(pinType, pinMapping, wl)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cache.retMapping, err = queries.BindMapping(pinType, pinMapping, returnColumns)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(wl) != 0 {
|
|
cache.query = fmt.Sprintf("INSERT INTO \"pins\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
|
|
} else {
|
|
cache.query = "INSERT INTO \"pins\" %sDEFAULT VALUES%s"
|
|
}
|
|
|
|
var queryOutput, queryReturning string
|
|
|
|
if len(cache.retMapping) != 0 {
|
|
cache.retQuery = fmt.Sprintf("SELECT \"%s\" FROM \"pins\" WHERE %s", strings.Join(returnColumns, "\",\""), strmangle.WhereClause("\"", "\"", 0, pinPrimaryKeyColumns))
|
|
}
|
|
|
|
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)
|
|
}
|
|
result, err := exec.ExecContext(ctx, cache.query, vals...)
|
|
|
|
if err != nil {
|
|
return errors.Wrap(err, "models: unable to insert into pins")
|
|
}
|
|
|
|
var lastID int64
|
|
var identifierCols []interface{}
|
|
|
|
if len(cache.retMapping) == 0 {
|
|
goto CacheNoHooks
|
|
}
|
|
|
|
lastID, err = result.LastInsertId()
|
|
if err != nil {
|
|
return ErrSyncFail
|
|
}
|
|
|
|
o.ID = int64(lastID)
|
|
if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == pinMapping["id"] {
|
|
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 pins")
|
|
}
|
|
|
|
CacheNoHooks:
|
|
if !cached {
|
|
pinInsertCacheMut.Lock()
|
|
pinInsertCache[key] = cache
|
|
pinInsertCacheMut.Unlock()
|
|
}
|
|
|
|
return o.doAfterInsertHooks(ctx, exec)
|
|
}
|
|
|
|
// Update uses an executor to update the Pin.
|
|
// 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 *Pin) 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)
|
|
pinUpdateCacheMut.RLock()
|
|
cache, cached := pinUpdateCache[key]
|
|
pinUpdateCacheMut.RUnlock()
|
|
|
|
if !cached {
|
|
wl := columns.UpdateColumnSet(
|
|
pinAllColumns,
|
|
pinPrimaryKeyColumns,
|
|
)
|
|
|
|
if !columns.IsWhitelist() {
|
|
wl = strmangle.SetComplement(wl, []string{"created_at"})
|
|
}
|
|
if len(wl) == 0 {
|
|
return 0, errors.New("models: unable to update pins, could not build whitelist")
|
|
}
|
|
|
|
cache.query = fmt.Sprintf("UPDATE \"pins\" SET %s WHERE %s",
|
|
strmangle.SetParamNames("\"", "\"", 0, wl),
|
|
strmangle.WhereClause("\"", "\"", 0, pinPrimaryKeyColumns),
|
|
)
|
|
cache.valueMapping, err = queries.BindMapping(pinType, pinMapping, append(wl, pinPrimaryKeyColumns...))
|
|
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 pins row")
|
|
}
|
|
|
|
rowsAff, err := result.RowsAffected()
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "models: failed to get rows affected by update for pins")
|
|
}
|
|
|
|
if !cached {
|
|
pinUpdateCacheMut.Lock()
|
|
pinUpdateCache[key] = cache
|
|
pinUpdateCacheMut.Unlock()
|
|
}
|
|
|
|
return rowsAff, o.doAfterUpdateHooks(ctx, exec)
|
|
}
|
|
|
|
// UpdateAll updates all rows with the specified column values.
|
|
func (q pinQuery) 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 pins")
|
|
}
|
|
|
|
rowsAff, err := result.RowsAffected()
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "models: unable to retrieve rows affected for pins")
|
|
}
|
|
|
|
return rowsAff, nil
|
|
}
|
|
|
|
// UpdateAll updates all rows with the specified column values, using an executor.
|
|
func (o PinSlice) 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)), pinPrimaryKeyMapping)
|
|
args = append(args, pkeyArgs...)
|
|
}
|
|
|
|
sql := fmt.Sprintf("UPDATE \"pins\" SET %s WHERE %s",
|
|
strmangle.SetParamNames("\"", "\"", 0, colNames),
|
|
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, pinPrimaryKeyColumns, 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 pin slice")
|
|
}
|
|
|
|
rowsAff, err := result.RowsAffected()
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all pin")
|
|
}
|
|
return rowsAff, nil
|
|
}
|
|
|
|
// Delete deletes a single Pin record with an executor.
|
|
// Delete will match against the primary key column to find the record to delete.
|
|
func (o *Pin) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
|
|
if o == nil {
|
|
return 0, errors.New("models: no Pin provided for delete")
|
|
}
|
|
|
|
if err := o.doBeforeDeleteHooks(ctx, exec); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), pinPrimaryKeyMapping)
|
|
sql := "DELETE FROM \"pins\" 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 pins")
|
|
}
|
|
|
|
rowsAff, err := result.RowsAffected()
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "models: failed to get rows affected by delete for pins")
|
|
}
|
|
|
|
if err := o.doAfterDeleteHooks(ctx, exec); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return rowsAff, nil
|
|
}
|
|
|
|
// DeleteAll deletes all matching rows.
|
|
func (q pinQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
|
|
if q.Query == nil {
|
|
return 0, errors.New("models: no pinQuery 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 pins")
|
|
}
|
|
|
|
rowsAff, err := result.RowsAffected()
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for pins")
|
|
}
|
|
|
|
return rowsAff, nil
|
|
}
|
|
|
|
// DeleteAll deletes all rows in the slice, using an executor.
|
|
func (o PinSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
|
|
if len(o) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
if len(pinBeforeDeleteHooks) != 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)), pinPrimaryKeyMapping)
|
|
args = append(args, pkeyArgs...)
|
|
}
|
|
|
|
sql := "DELETE FROM \"pins\" WHERE " +
|
|
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, pinPrimaryKeyColumns, 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 pin slice")
|
|
}
|
|
|
|
rowsAff, err := result.RowsAffected()
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for pins")
|
|
}
|
|
|
|
if len(pinAfterDeleteHooks) != 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 *Pin) Reload(ctx context.Context, exec boil.ContextExecutor) error {
|
|
ret, err := FindPin(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 *PinSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error {
|
|
if o == nil || len(*o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
slice := PinSlice{}
|
|
var args []interface{}
|
|
for _, obj := range *o {
|
|
pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), pinPrimaryKeyMapping)
|
|
args = append(args, pkeyArgs...)
|
|
}
|
|
|
|
sql := "SELECT \"pins\".* FROM \"pins\" WHERE " +
|
|
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, pinPrimaryKeyColumns, 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 PinSlice")
|
|
}
|
|
|
|
*o = slice
|
|
|
|
return nil
|
|
}
|
|
|
|
// PinExists checks if the Pin row exists.
|
|
func PinExists(ctx context.Context, exec boil.ContextExecutor, iD int64) (bool, error) {
|
|
var exists bool
|
|
sql := "select exists(select 1 from \"pins\" 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 pins exists")
|
|
}
|
|
|
|
return exists, nil
|
|
}
|