231 lines
4.5 KiB
Go
231 lines
4.5 KiB
Go
// Package database contains the types for schema 'public'.
|
|
package database
|
|
|
|
// Code generated by xo. DO NOT EDIT.
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// Email represents a row from '"public"."email"'.
|
|
type Email struct {
|
|
Address string `db:"address"` // address
|
|
UserID int64 `db:"user_id"` // user_id
|
|
CreatedAt time.Time `db:"created_at"` // created_at
|
|
|
|
// xo fields
|
|
_exists, _deleted bool
|
|
}
|
|
|
|
// Exists determines if the Email exists in the database.
|
|
func (e *Email) Exists() bool {
|
|
return e._exists
|
|
}
|
|
|
|
// Deleted provides information if the Email has been deleted from the database.
|
|
func (e *Email) Deleted() bool {
|
|
return e._deleted
|
|
}
|
|
|
|
// Insert inserts the Email to the database.
|
|
func (e *Email) Insert(db XODB) error {
|
|
var err error
|
|
|
|
// if already exist, bail
|
|
if e._exists {
|
|
return errors.New("insert failed: already exists")
|
|
}
|
|
|
|
// sql insert query, primary key must be provided
|
|
const sqlstr = `INSERT INTO "public"."email" (` +
|
|
`"address", "user_id", "created_at"` +
|
|
`) VALUES (` +
|
|
`$1, $2, $3` +
|
|
`)`
|
|
|
|
// run query
|
|
XOLog(sqlstr, e.Address, e.UserID, e.CreatedAt)
|
|
_, err = db.Exec(sqlstr, e.Address, e.UserID, e.CreatedAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// set existence
|
|
e._exists = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// Update updates the Email in the database.
|
|
func (e *Email) Update(db XODB) error {
|
|
var err error
|
|
|
|
// if doesn't exist, bail
|
|
if !e._exists {
|
|
return errors.New("update failed: does not exist")
|
|
}
|
|
|
|
// if deleted, bail
|
|
if e._deleted {
|
|
return errors.New("update failed: marked for deletion")
|
|
}
|
|
|
|
// sql query
|
|
const sqlstr = `UPDATE "public"."email" SET (` +
|
|
`"user_id", "created_at"` +
|
|
`) = ( ` +
|
|
`$1, $2` +
|
|
`) WHERE "address" = $3`
|
|
|
|
// run query
|
|
XOLog(sqlstr, e.UserID, e.CreatedAt, e.Address)
|
|
_, err = db.Exec(sqlstr, e.UserID, e.CreatedAt, e.Address)
|
|
return err
|
|
}
|
|
|
|
// Save saves the Email to the database.
|
|
func (e *Email) Save(db XODB) error {
|
|
if e.Exists() {
|
|
return e.Update(db)
|
|
}
|
|
|
|
return e.Insert(db)
|
|
}
|
|
|
|
// Upsert performs an upsert for Email.
|
|
//
|
|
// NOTE: PostgreSQL 9.5+ only
|
|
func (e *Email) Upsert(db XODB) error {
|
|
var err error
|
|
|
|
// if already exist, bail
|
|
if e._exists {
|
|
return errors.New("insert failed: already exists")
|
|
}
|
|
|
|
// sql query
|
|
const sqlstr = `INSERT INTO "public"."email" (` +
|
|
`"address", "user_id", "created_at"` +
|
|
`) VALUES (` +
|
|
`$1, $2, $3` +
|
|
`) ON CONFLICT ("address") DO UPDATE SET (` +
|
|
`"address", "user_id", "created_at"` +
|
|
`) = (` +
|
|
`EXCLUDED."address", EXCLUDED."user_id", EXCLUDED."created_at"` +
|
|
`)`
|
|
|
|
// run query
|
|
XOLog(sqlstr, e.Address, e.UserID, e.CreatedAt)
|
|
_, err = db.Exec(sqlstr, e.Address, e.UserID, e.CreatedAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// set existence
|
|
e._exists = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete deletes the Email from the database.
|
|
func (e *Email) Delete(db XODB) error {
|
|
var err error
|
|
|
|
// if doesn't exist, bail
|
|
if !e._exists {
|
|
return nil
|
|
}
|
|
|
|
// if deleted, bail
|
|
if e._deleted {
|
|
return nil
|
|
}
|
|
|
|
// sql query
|
|
const sqlstr = `DELETE FROM "public"."email" WHERE "address" = $1`
|
|
|
|
// run query
|
|
XOLog(sqlstr, e.Address)
|
|
_, err = db.Exec(sqlstr, e.Address)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// set deleted
|
|
e._deleted = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// User returns the User associated with the Email's UserID (user_id).
|
|
//
|
|
// Generated from foreign key 'email_user_id_fkey'.
|
|
func (e *Email) User(db XODB) (*User, error) {
|
|
return UserByID(db, e.UserID)
|
|
}
|
|
|
|
// EmailByAddress retrieves a row from '"public"."email"' as a Email.
|
|
//
|
|
// Generated from index 'email_pkey'.
|
|
func EmailByAddress(db XODB, address string) (*Email, error) {
|
|
var err error
|
|
|
|
// sql query
|
|
const sqlstr = `SELECT ` +
|
|
`"address", "user_id", "created_at" ` +
|
|
`FROM "public"."email" ` +
|
|
`WHERE "address" = $1`
|
|
|
|
// run query
|
|
XOLog(sqlstr, address)
|
|
e := Email{
|
|
_exists: true,
|
|
}
|
|
|
|
err = db.QueryRow(sqlstr, address).Scan(&e.Address, &e.UserID, &e.CreatedAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &e, nil
|
|
}
|
|
|
|
// EmailsByUserID retrieves a row from '"public"."email"' as a Email.
|
|
//
|
|
// Generated from index 'email_user_id_idx'.
|
|
func EmailsByUserID(db XODB, userID int64) ([]*Email, error) {
|
|
var err error
|
|
|
|
// sql query
|
|
const sqlstr = `SELECT ` +
|
|
`"address", "user_id", "created_at" ` +
|
|
`FROM "public"."email" ` +
|
|
`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 := []*Email{}
|
|
for q.Next() {
|
|
e := Email{
|
|
_exists: true,
|
|
}
|
|
|
|
// scan
|
|
err = q.Scan(&e.Address, &e.UserID, &e.CreatedAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res = append(res, &e)
|
|
}
|
|
|
|
return res, nil
|
|
}
|