219 lines
4.7 KiB
Go
219 lines
4.7 KiB
Go
// Package database contains the types for schema 'public'.
|
|
package database
|
|
|
|
// Code generated by xo. DO NOT EDIT.
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// Reset represents a row from '"public"."reset"'.
|
|
type Reset struct {
|
|
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 Reset exists in the database.
|
|
func (r *Reset) Exists() bool {
|
|
return r._exists
|
|
}
|
|
|
|
// Deleted provides information if the Reset has been deleted from the database.
|
|
func (r *Reset) Deleted() bool {
|
|
return r._deleted
|
|
}
|
|
|
|
// Insert inserts the Reset to the database.
|
|
func (r *Reset) Insert(db XODB) error {
|
|
var err error
|
|
|
|
// if already exist, bail
|
|
if r._exists {
|
|
return errors.New("insert failed: already exists")
|
|
}
|
|
|
|
// sql insert query, primary key must be provided
|
|
const sqlstr = `INSERT INTO "public"."reset" (` +
|
|
`"user_id", "selector", "verifier", "expires_at"` +
|
|
`) VALUES (` +
|
|
`$1, $2, $3, $4` +
|
|
`)`
|
|
|
|
// run query
|
|
XOLog(sqlstr, r.UserID, r.Selector, r.Verifier, r.ExpiresAt)
|
|
_, err = db.Exec(sqlstr, r.UserID, r.Selector, r.Verifier, r.ExpiresAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// set existence
|
|
r._exists = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// Update updates the Reset in the database.
|
|
func (r *Reset) Update(db XODB) error {
|
|
var err error
|
|
|
|
// if doesn't exist, bail
|
|
if !r._exists {
|
|
return errors.New("update failed: does not exist")
|
|
}
|
|
|
|
// if deleted, bail
|
|
if r._deleted {
|
|
return errors.New("update failed: marked for deletion")
|
|
}
|
|
|
|
// sql query
|
|
const sqlstr = `UPDATE "public"."reset" SET (` +
|
|
`"user_id", "verifier", "expires_at"` +
|
|
`) = ( ` +
|
|
`$1, $2, $3` +
|
|
`) WHERE "selector" = $4`
|
|
|
|
// run query
|
|
XOLog(sqlstr, r.UserID, r.Verifier, r.ExpiresAt, r.Selector)
|
|
_, err = db.Exec(sqlstr, r.UserID, r.Verifier, r.ExpiresAt, r.Selector)
|
|
return err
|
|
}
|
|
|
|
// Save saves the Reset to the database.
|
|
func (r *Reset) Save(db XODB) error {
|
|
if r.Exists() {
|
|
return r.Update(db)
|
|
}
|
|
|
|
return r.Insert(db)
|
|
}
|
|
|
|
// Upsert performs an upsert for Reset.
|
|
//
|
|
// NOTE: PostgreSQL 9.5+ only
|
|
func (r *Reset) Upsert(db XODB) error {
|
|
var err error
|
|
|
|
// if already exist, bail
|
|
if r._exists {
|
|
return errors.New("insert failed: already exists")
|
|
}
|
|
|
|
// sql query
|
|
const sqlstr = `INSERT INTO "public"."reset" (` +
|
|
`"user_id", "selector", "verifier", "expires_at"` +
|
|
`) VALUES (` +
|
|
`$1, $2, $3, $4` +
|
|
`) ON CONFLICT ("selector") DO UPDATE SET (` +
|
|
`"user_id", "selector", "verifier", "expires_at"` +
|
|
`) = (` +
|
|
`EXCLUDED."user_id", EXCLUDED."selector", EXCLUDED."verifier", EXCLUDED."expires_at"` +
|
|
`)`
|
|
|
|
// run query
|
|
XOLog(sqlstr, r.UserID, r.Selector, r.Verifier, r.ExpiresAt)
|
|
_, err = db.Exec(sqlstr, r.UserID, r.Selector, r.Verifier, r.ExpiresAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// set existence
|
|
r._exists = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete deletes the Reset from the database.
|
|
func (r *Reset) Delete(db XODB) error {
|
|
var err error
|
|
|
|
// if doesn't exist, bail
|
|
if !r._exists {
|
|
return nil
|
|
}
|
|
|
|
// if deleted, bail
|
|
if r._deleted {
|
|
return nil
|
|
}
|
|
|
|
// sql query
|
|
const sqlstr = `DELETE FROM "public"."reset" WHERE "selector" = $1`
|
|
|
|
// run query
|
|
XOLog(sqlstr, r.Selector)
|
|
_, err = db.Exec(sqlstr, r.Selector)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// set deleted
|
|
r._deleted = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// User returns the User associated with the Reset's UserID (user_id).
|
|
//
|
|
// Generated from foreign key 'reset_user_id_fkey'.
|
|
func (r *Reset) User(db XODB) (*User, error) {
|
|
return UserByID(db, r.UserID)
|
|
}
|
|
|
|
// ResetBySelector retrieves a row from '"public"."reset"' as a Reset.
|
|
//
|
|
// Generated from index 'reset_pkey'.
|
|
func ResetBySelector(db XODB, selector string) (*Reset, error) {
|
|
var err error
|
|
|
|
// sql query
|
|
const sqlstr = `SELECT ` +
|
|
`"user_id", "selector", "verifier", "expires_at" ` +
|
|
`FROM "public"."reset" ` +
|
|
`WHERE "selector" = $1`
|
|
|
|
// run query
|
|
XOLog(sqlstr, selector)
|
|
r := Reset{
|
|
_exists: true,
|
|
}
|
|
|
|
err = db.QueryRow(sqlstr, selector).Scan(&r.UserID, &r.Selector, &r.Verifier, &r.ExpiresAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &r, nil
|
|
}
|
|
|
|
// ResetByUserID retrieves a row from '"public"."reset"' as a Reset.
|
|
//
|
|
// Generated from index 'reset_user_id_idx'.
|
|
func ResetByUserID(db XODB, userID int64) (*Reset, error) {
|
|
var err error
|
|
|
|
// sql query
|
|
const sqlstr = `SELECT ` +
|
|
`"user_id", "selector", "verifier", "expires_at" ` +
|
|
`FROM "public"."reset" ` +
|
|
`WHERE "user_id" = $1`
|
|
|
|
// run query
|
|
XOLog(sqlstr, userID)
|
|
r := Reset{
|
|
_exists: true,
|
|
}
|
|
|
|
err = db.QueryRow(sqlstr, userID).Scan(&r.UserID, &r.Selector, &r.Verifier, &r.ExpiresAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &r, nil
|
|
}
|