skeleton/internal/database/confirmation.xo.go

233 lines
5.4 KiB
Go

// Package database contains the types for schema 'public'.
package database
// Code generated by xo. DO NOT EDIT.
import (
"errors"
"time"
)
// Confirmation represents a row from '"public"."confirmation"'.
type Confirmation struct {
EmailAddress string `db:"email_address"` // email_address
UserID int64 `db:"user_id"` // user_id
Selector string `db:"selector"` // selector
Verifier []byte `db:"verifier"` // verifier
ExpiresAt time.Time `db:"expires_at"` // expires_at
// xo fields
_exists, _deleted bool
}
// Exists determines if the Confirmation exists in the database.
func (c *Confirmation) Exists() bool {
return c._exists
}
// Deleted provides information if the Confirmation has been deleted from the database.
func (c *Confirmation) Deleted() bool {
return c._deleted
}
// Insert inserts the Confirmation to the database.
func (c *Confirmation) Insert(db XODB) error {
var err error
// if already exist, bail
if c._exists {
return errors.New("insert failed: already exists")
}
// sql insert query, primary key must be provided
const sqlstr = `INSERT INTO "public"."confirmation" (` +
`"email_address", "user_id", "selector", "verifier", "expires_at"` +
`) VALUES (` +
`$1, $2, $3, $4, $5` +
`)`
// run query
XOLog(sqlstr, c.EmailAddress, c.UserID, c.Selector, c.Verifier, c.ExpiresAt)
_, err = db.Exec(sqlstr, c.EmailAddress, c.UserID, c.Selector, c.Verifier, c.ExpiresAt)
if err != nil {
return err
}
// set existence
c._exists = true
return nil
}
// Update updates the Confirmation in the database.
func (c *Confirmation) Update(db XODB) error {
var err error
// if doesn't exist, bail
if !c._exists {
return errors.New("update failed: does not exist")
}
// if deleted, bail
if c._deleted {
return errors.New("update failed: marked for deletion")
}
// sql query
const sqlstr = `UPDATE "public"."confirmation" SET (` +
`"email_address", "user_id", "verifier", "expires_at"` +
`) = ( ` +
`$1, $2, $3, $4` +
`) WHERE "selector" = $5`
// run query
XOLog(sqlstr, c.EmailAddress, c.UserID, c.Verifier, c.ExpiresAt, c.Selector)
_, err = db.Exec(sqlstr, c.EmailAddress, c.UserID, c.Verifier, c.ExpiresAt, c.Selector)
return err
}
// Save saves the Confirmation to the database.
func (c *Confirmation) Save(db XODB) error {
if c.Exists() {
return c.Update(db)
}
return c.Insert(db)
}
// Upsert performs an upsert for Confirmation.
//
// NOTE: PostgreSQL 9.5+ only
func (c *Confirmation) Upsert(db XODB) error {
var err error
// if already exist, bail
if c._exists {
return errors.New("insert failed: already exists")
}
// sql query
const sqlstr = `INSERT INTO "public"."confirmation" (` +
`"email_address", "user_id", "selector", "verifier", "expires_at"` +
`) VALUES (` +
`$1, $2, $3, $4, $5` +
`) ON CONFLICT ("selector") DO UPDATE SET (` +
`"email_address", "user_id", "selector", "verifier", "expires_at"` +
`) = (` +
`EXCLUDED."email_address", EXCLUDED."user_id", EXCLUDED."selector", EXCLUDED."verifier", EXCLUDED."expires_at"` +
`)`
// run query
XOLog(sqlstr, c.EmailAddress, c.UserID, c.Selector, c.Verifier, c.ExpiresAt)
_, err = db.Exec(sqlstr, c.EmailAddress, c.UserID, c.Selector, c.Verifier, c.ExpiresAt)
if err != nil {
return err
}
// set existence
c._exists = true
return nil
}
// Delete deletes the Confirmation from the database.
func (c *Confirmation) Delete(db XODB) error {
var err error
// if doesn't exist, bail
if !c._exists {
return nil
}
// if deleted, bail
if c._deleted {
return nil
}
// sql query
const sqlstr = `DELETE FROM "public"."confirmation" WHERE "selector" = $1`
// run query
XOLog(sqlstr, c.Selector)
_, err = db.Exec(sqlstr, c.Selector)
if err != nil {
return err
}
// set deleted
c._deleted = true
return nil
}
// User returns the User associated with the Confirmation's UserID (user_id).
//
// Generated from foreign key 'confirmation_user_id_fkey'.
func (c *Confirmation) User(db XODB) (*User, error) {
return UserByID(db, c.UserID)
}
// ConfirmationBySelector retrieves a row from '"public"."confirmation"' as a Confirmation.
//
// Generated from index 'confirmation_pkey'.
func ConfirmationBySelector(db XODB, selector string) (*Confirmation, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`"email_address", "user_id", "selector", "verifier", "expires_at" ` +
`FROM "public"."confirmation" ` +
`WHERE "selector" = $1`
// run query
XOLog(sqlstr, selector)
c := Confirmation{
_exists: true,
}
err = db.QueryRow(sqlstr, selector).Scan(&c.EmailAddress, &c.UserID, &c.Selector, &c.Verifier, &c.ExpiresAt)
if err != nil {
return nil, err
}
return &c, nil
}
// ConfirmationsByUserID retrieves a row from '"public"."confirmation"' as a Confirmation.
//
// Generated from index 'confirmation_user_id_idx'.
func ConfirmationsByUserID(db XODB, userID int64) ([]*Confirmation, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`"email_address", "user_id", "selector", "verifier", "expires_at" ` +
`FROM "public"."confirmation" ` +
`WHERE "user_id" = $1`
// run query
XOLog(sqlstr, userID)
q, err := db.Query(sqlstr, userID)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Confirmation{}
for q.Next() {
c := Confirmation{
_exists: true,
}
// scan
err = q.Scan(&c.EmailAddress, &c.UserID, &c.Selector, &c.Verifier, &c.ExpiresAt)
if err != nil {
return nil, err
}
res = append(res, &c)
}
return res, nil
}