postgres

package
v0.0.0-...-9d9692a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const MemberDeleteQuery = `
DELETE FROM members
WHERE sid = $1 AND uid = $2;`

MemberDeleteQuery is a query statement for deleting single member by id.

View Source
const MemberInsertQuery = `
INSERT INTO members (sid, uid)
VALUES ($1, $2);`

MemberInsertQuery is query statement for inserting single member.

View Source
const MemberSelectQuery = `
SELECT sid, uid FROM members
WHERE sid = $1 AND uid = $2;`

MemberSelectQuery is a query statement for fetching single member by id.

View Source
const ScountDeleteQuery = `
DELETE FROM scounts
WHERE sid = $1;`

ScountDeleteQuery is a query statement for deleting a single scount by sid.

View Source
const ScountInsertQuery = `` /* 168-byte string literal not displayed */

ScountInsertQuery is a query statement for adding a single scount by id.

View Source
const ScountSelectQuery = `
SELECT sid, owner, title, description
FROM scounts
WHERE sid = $1;`

ScountSelectQuery is a query statement for fetching a single scount by sid.

View Source
const UserByEmailQuery = `
SELECT uid, email, username, password
FROM users
WHERE email = $1;`

UserByEmailQuery is a query statement for fetching single user by email.

View Source
const UserDeleteQuery = `
DELETE FROM users WHERE uid = $1;`

UserDeleteQuery is a query statement for deleting single user by uid.

View Source
const UserInsertQuery = `
INSERT INTO users (uid, email, username, password)
VALUES ($1, $2, $3, $4);`

UserInsertQuery is query statement for inserting single user.

View Source
const UserPasswordQuery = `
UPDATE users
SET password = $2
WHERE uid = $1 AND password = $3;`

UserPasswordQuery is a query statement for updating password of the user.

View Source
const UserSelectQuery = `
SELECT uid, email, username, password
FROM users
WHERE uid = $1;`

UserSelectQuery is a query statement for fetching single user by uid.

Variables

View Source
var MemberSelectTemplate = template.Must(template.New("member-select").
	Funcs(template.FuncMap{"join": JoinSorter}).
	Parse(`
{{ define "filter" }}
	FROM members
	WHERE ($1 OR sid = $2)
	AND ($3 OR uid = $4)
	ORDER BY {{ join .Order "sid, uid" }}
{{ end }}

{{ define "find" }}
	SELECT sid, uid,
	{{ template "filter" }}
	{{ with .Paging }}
		LIMIT {{ .Limit }}
		OFFSET {{ .Offset }}
	{{ end }};
{{ end }}

{{ define "count" }}
	SELECT count(*) AS total
	{{ template "filter" }};
{{ end }}
`))

MemberSelectTemplate is a query template for finding matching members from MemberCollection.

View Source
var ScountSelectTemplate = template.Must(template.New("scount-select").
	Funcs(template.FuncMap{"join": JoinSorter}).
	Parse(`
{{ define "filter" }}
	FROM scounts NATURAL JOIN members
	WHERE ($1 OR sid = $2)
	AND ($3 OR uid = $4)
	AND ($5 OR owner = $6)
	AND ($7 OR title ILIKE $8)
	ORDER BY {{ join .Order "sid, uid" }}
{{ end }}

{{ define "find" }}
	SELECT DISTINCT sid, owner, title, description,
	{{ template "filter" }}
	{{ with .Paging }}
		LIMIT {{ .Limit }}
		OFFSET {{ .Offset }}
	{{ end }};
{{ end }}

{{ define "count" }}
	SELECT count(*) AS total
	{{ template "filter" }};
{{ end }}
`))

ScountSelectTemplate is a query template for finding scounts from ScountCollection.

View Source
var ScountUpdateTemplate = template.Must(template.New("scount-update").
	Funcs(template.FuncMap{"add": Add}).
	Parse(`
UPDATE scounts SET
{{ range $i, $col := . }}
	{{ $col }} = {{ add $i 2 | printf "$%d" }}
{{ end }}
WHERE sid = $1;
`))

ScountUpdateTemplate is a query template for updating scounts from ScountCollection.

View Source
var UserSelectTemplate = template.Must(template.New("user-select").
	Funcs(template.FuncMap{"join": JoinSorter}).
	Parse(`
{{ define "filter" }}
	FROM users
	WHERE ($1 OR uid = $2)
	AND ($3 OR email = $4)
	AND ($5 OR username ILIKE $6)
{{ end }}

{{ define "sort" }}
	ORDER BY {{ join .Order "uid" }}
{{ end }}

{{ define "find" }}
	SELECT DISTINCT uid, email, username, password
	{{ template "filter" }}
	{{ template "sort" }}
	{{ with .Paging }}
		LIMIT {{ .Limit }}
		OFFSET {{ .Offset }}
	{{ end }};
{{ end }}

{{ define "count" }}
	SELECT count(*) AS total
	{{ template "filter" }}
	GROUP BY uid
	{{ template "sort" }};
{{ end }}
`))

UserSelectTemplate is a query template for finding users from UserCollection.

View Source
var UserUpdateTemplate = template.Must(template.New("user-update").
	Funcs(template.FuncMap{"add": Add}).
	Parse(`
UPDATE users SET
{{ range $i, $col := . }}
	{{ $col }} = {{ add $i 2 | printf "$%d" }}
{{ end }}
WHERE uid = $1;
`))

UserUpdateTemplate is a query template for updating users from UserCollection.

Functions

func Add

func Add(x, y int) int

Add defines addition behavior inside templates.

func Error

func Error(err error) error

Error is a utility function for error handling.

func JoinSorter

func JoinSorter(cols []db.Sorter, fallback string) string

JoinSorter defines `join` operation inside templates.

func NewStore

func NewStore(DB *sql.DB) *db.Store

NewStore constructs a db.Store from a SQL (postgres supported) database connection handle.

func Open

func Open(uri string) (*db.Store, error)

Open dials an SQL connection using POSTGRES connection uri and constructs a db.Store. Wraps over NewStore.

func Tx

func Tx[T any](
	ctx context.Context,
	sqldb *sql.DB,
	callback func(*sql.Tx) (T, error),
) (t T, err error)

Tx is a handy callback wrapper for executing transactions.

Types

type MemberCollection

type MemberCollection struct {
	DB *sql.DB
}

MemberCollection provides a convenient way to interact with `members` table.

func (MemberCollection) DeleteOne

func (colln MemberCollection) DeleteOne(ctx context.Context, id *db.MemberId) error

DeleteOne removes exactly 1 member from `members` collection based on matching id.

func (MemberCollection) Find

func (colln MemberCollection) Find(
	ctx context.Context,
	filter *db.MemberFilter,
	projector *db.Projector,
) (list *db.Iterable[db.Member], err error)

Find fetches all the members from colln subject to filter and projection options specified.

func (MemberCollection) FindOne

func (colln MemberCollection) FindOne(
	ctx context.Context,
	id *db.MemberId,
) (m db.Member, err error)

FindOne fetches member from colln by id.

func (MemberCollection) Insert

func (colln MemberCollection) Insert(ctx context.Context, members ...db.Member) error

Insert adds one or more members to colln. db.ErrNoRows if no users to insert.

func (MemberCollection) UpdateOne

UpdateOne is not supported on `members` collection.

type ScountCollection

type ScountCollection struct {
	DB *sql.DB
}

ScountCollection provides a convenient way to interact with `scounts` table.

func (ScountCollection) DeleteOne

func (colln ScountCollection) DeleteOne(ctx context.Context, id *db.ScountId) error

DeleteOne removes exactly 1 scount from `scounts` collection based on sid.

func (ScountCollection) Find

func (colln ScountCollection) Find(
	ctx context.Context,
	filter *db.ScountFilter,
	projector *db.Projector,
) (list *db.Iterable[db.Scount], err error)

Find fetches all the scounts from colln subject to filter and projector options specified.

func (ScountCollection) FindOne

func (colln ScountCollection) FindOne(
	ctx context.Context,
	id *db.ScountId,
) (s db.Scount, err error)

FindOne fetches scount from colln by id.

func (ScountCollection) Insert

func (colln ScountCollection) Insert(ctx context.Context, scounts ...db.Scount) error

Insert adds one or more scounts into colln. db.ErrNoRows if empty scounts.

func (ScountCollection) UpdateOne

func (colln ScountCollection) UpdateOne(
	ctx context.Context,
	id *db.ScountId,
	setter *db.ScountUpdater,
) error

UpdateOne modifies exactly 1 scount from `scounts` collection.

type UserCollection

type UserCollection struct {
	DB *sql.DB // underlying database handle
}

UserCollection provides a convenient way to interact with `users` table.

func (UserCollection) DeleteOne

func (colln UserCollection) DeleteOne(ctx context.Context, id *db.UserId) error

DeleteOne removes exactly 1 user from `users` collection based on id.

func (UserCollection) Find

func (colln UserCollection) Find(
	ctx context.Context,
	filter *db.UserFilter,
	projector *db.Projector,
) (list *db.Iterable[db.User], err error)

Find fetches all the users from colln subject to filter and projector options specified.

func (UserCollection) FindByEmail

func (colln UserCollection) FindByEmail(ctx context.Context, email string) (u db.User, err error)

FindByEmail fetches user from colln by email.

func (UserCollection) FindOne

func (colln UserCollection) FindOne(ctx context.Context, id *db.UserId) (u db.User, err error)

FindOne fetches user from colln by id.

func (UserCollection) Insert

func (colln UserCollection) Insert(ctx context.Context, users ...db.User) error

Insert adds one or more users to colln. db.ErrNoRows if no users to insert.

func (UserCollection) UpdateOne

func (colln UserCollection) UpdateOne(
	ctx context.Context,
	id *db.UserId,
	setter *db.UserUpdater,
) error

UpdateOne modifies exactly 1 user from `users` collection.

func (UserCollection) UpdatePassword

func (colln UserCollection) UpdatePassword(ctx context.Context, updater *db.PasswordUpdater) error

UpdatePassword modifies the password of the matching user record as specified.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL