skeleton/internal/database/user.xo.go
2019-08-22 00:48:27 +02:00

186 lines
3.8 KiB
Go

// Package database contains the types for schema 'public'.
package database
// Code generated by xo. DO NOT EDIT.
import (
"errors"
"time"
)
// User represents a row from '"public"."user"'.
type User struct {
ID int64 `db:"id"` // id
IsAdmin bool `db:"is_admin"` // is_admin
Password []byte `db:"password"` // password
CreatedAt time.Time `db:"created_at"` // created_at
// xo fields
_exists, _deleted bool
}
// Exists determines if the User exists in the database.
func (u *User) Exists() bool {
return u._exists
}
// Deleted provides information if the User has been deleted from the database.
func (u *User) Deleted() bool {
return u._deleted
}
// Insert inserts the User to the database.
func (u *User) Insert(db XODB) error {
var err error
// if already exist, bail
if u._exists {
return errors.New("insert failed: already exists")
}
// sql insert query, primary key provided by sequence
const sqlstr = `INSERT INTO "public"."user" (` +
`"is_admin", "password", "created_at"` +
`) VALUES (` +
`$1, $2, $3` +
`) RETURNING "id"`
// run query
XOLog(sqlstr, u.IsAdmin, u.Password, u.CreatedAt)
err = db.QueryRow(sqlstr, u.IsAdmin, u.Password, u.CreatedAt).Scan(&u.ID)
if err != nil {
return err
}
// set existence
u._exists = true
return nil
}
// Update updates the User in the database.
func (u *User) Update(db XODB) error {
var err error
// if doesn't exist, bail
if !u._exists {
return errors.New("update failed: does not exist")
}
// if deleted, bail
if u._deleted {
return errors.New("update failed: marked for deletion")
}
// sql query
const sqlstr = `UPDATE "public"."user" SET (` +
`"is_admin", "password", "created_at"` +
`) = ( ` +
`$1, $2, $3` +
`) WHERE "id" = $4`
// run query
XOLog(sqlstr, u.IsAdmin, u.Password, u.CreatedAt, u.ID)
_, err = db.Exec(sqlstr, u.IsAdmin, u.Password, u.CreatedAt, u.ID)
return err
}
// Save saves the User to the database.
func (u *User) Save(db XODB) error {
if u.Exists() {
return u.Update(db)
}
return u.Insert(db)
}
// Upsert performs an upsert for User.
//
// NOTE: PostgreSQL 9.5+ only
func (u *User) Upsert(db XODB) error {
var err error
// if already exist, bail
if u._exists {
return errors.New("insert failed: already exists")
}
// sql query
const sqlstr = `INSERT INTO "public"."user" (` +
`"id", "is_admin", "password", "created_at"` +
`) VALUES (` +
`$1, $2, $3, $4` +
`) ON CONFLICT ("id") DO UPDATE SET (` +
`"id", "is_admin", "password", "created_at"` +
`) = (` +
`EXCLUDED."id", EXCLUDED."is_admin", EXCLUDED."password", EXCLUDED."created_at"` +
`)`
// run query
XOLog(sqlstr, u.ID, u.IsAdmin, u.Password, u.CreatedAt)
_, err = db.Exec(sqlstr, u.ID, u.IsAdmin, u.Password, u.CreatedAt)
if err != nil {
return err
}
// set existence
u._exists = true
return nil
}
// Delete deletes the User from the database.
func (u *User) Delete(db XODB) error {
var err error
// if doesn't exist, bail
if !u._exists {
return nil
}
// if deleted, bail
if u._deleted {
return nil
}
// sql query
const sqlstr = `DELETE FROM "public"."user" WHERE "id" = $1`
// run query
XOLog(sqlstr, u.ID)
_, err = db.Exec(sqlstr, u.ID)
if err != nil {
return err
}
// set deleted
u._deleted = true
return nil
}
// UserByID retrieves a row from '"public"."user"' as a User.
//
// Generated from index 'user_pkey'.
func UserByID(db XODB, id int64) (*User, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`"id", "is_admin", "password", "created_at" ` +
`FROM "public"."user" ` +
`WHERE "id" = $1`
// run query
XOLog(sqlstr, id)
u := User{
_exists: true,
}
err = db.QueryRow(sqlstr, id).Scan(&u.ID, &u.IsAdmin, &u.Password, &u.CreatedAt)
if err != nil {
return nil, err
}
return &u, nil
}