Adopt sqlc for typed Postgres queries behind store entities.
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: users.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const countAdmins = `-- name: CountAdmins :one
|
||||
SELECT COUNT(*)::bigint AS count
|
||||
FROM users
|
||||
WHERE role = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CountAdmins(ctx context.Context, role string) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, countAdmins, role)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const createUser = `-- name: CreateUser :exec
|
||||
INSERT INTO users (id, username, name, password_hash, role, avatar_url, state, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, '', '', $6)
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
ID string
|
||||
Username string
|
||||
Name string
|
||||
PasswordHash string
|
||||
Role string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createUser,
|
||||
arg.ID,
|
||||
arg.Username,
|
||||
arg.Name,
|
||||
arg.PasswordHash,
|
||||
arg.Role,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const getUserByID = `-- name: GetUserByID :one
|
||||
SELECT id, username, name, role, avatar_url, state, created_at
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
type GetUserByIDRow struct {
|
||||
ID string
|
||||
Username string
|
||||
Name string
|
||||
Role string
|
||||
AvatarUrl string
|
||||
State string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
func (q *Queries) GetUserByID(ctx context.Context, id string) (GetUserByIDRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getUserByID, id)
|
||||
var i GetUserByIDRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.Name,
|
||||
&i.Role,
|
||||
&i.AvatarUrl,
|
||||
&i.State,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByUsername = `-- name: GetUserByUsername :one
|
||||
SELECT id, username, name, role, avatar_url, state, created_at, password_hash
|
||||
FROM users
|
||||
WHERE username = $1
|
||||
`
|
||||
|
||||
type GetUserByUsernameRow struct {
|
||||
ID string
|
||||
Username string
|
||||
Name string
|
||||
Role string
|
||||
AvatarUrl string
|
||||
State string
|
||||
CreatedAt string
|
||||
PasswordHash string
|
||||
}
|
||||
|
||||
func (q *Queries) GetUserByUsername(ctx context.Context, username string) (GetUserByUsernameRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getUserByUsername, username)
|
||||
var i GetUserByUsernameRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.Name,
|
||||
&i.Role,
|
||||
&i.AvatarUrl,
|
||||
&i.State,
|
||||
&i.CreatedAt,
|
||||
&i.PasswordHash,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserRole = `-- name: GetUserRole :one
|
||||
SELECT role
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserRole(ctx context.Context, id string) (string, error) {
|
||||
row := q.db.QueryRowContext(ctx, getUserRole, id)
|
||||
var role string
|
||||
err := row.Scan(&role)
|
||||
return role, err
|
||||
}
|
||||
|
||||
const listUsers = `-- name: ListUsers :many
|
||||
SELECT id, username, name, role, avatar_url, state, created_at
|
||||
FROM users
|
||||
ORDER BY created_at ASC
|
||||
`
|
||||
|
||||
type ListUsersRow struct {
|
||||
ID string
|
||||
Username string
|
||||
Name string
|
||||
Role string
|
||||
AvatarUrl string
|
||||
State string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
func (q *Queries) ListUsers(ctx context.Context) ([]ListUsersRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listUsers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListUsersRow{}
|
||||
for rows.Next() {
|
||||
var i ListUsersRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.Name,
|
||||
&i.Role,
|
||||
&i.AvatarUrl,
|
||||
&i.State,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateUserRole = `-- name: UpdateUserRole :execresult
|
||||
UPDATE users
|
||||
SET role = $1
|
||||
WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateUserRoleParams struct {
|
||||
Role string
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserRole(ctx context.Context, arg UpdateUserRoleParams) (sql.Result, error) {
|
||||
return q.db.ExecContext(ctx, updateUserRole, arg.Role, arg.ID)
|
||||
}
|
||||
|
||||
const updateUserState = `-- name: UpdateUserState :exec
|
||||
UPDATE users
|
||||
SET state = $1
|
||||
WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateUserStateParams struct {
|
||||
State string
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserState(ctx context.Context, arg UpdateUserStateParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateUserState, arg.State, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUserStateAndAvatar = `-- name: UpdateUserStateAndAvatar :exec
|
||||
UPDATE users
|
||||
SET state = $1, avatar_url = $2
|
||||
WHERE id = $3
|
||||
`
|
||||
|
||||
type UpdateUserStateAndAvatarParams struct {
|
||||
State string
|
||||
AvatarUrl string
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserStateAndAvatar(ctx context.Context, arg UpdateUserStateAndAvatarParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateUserStateAndAvatar, arg.State, arg.AvatarUrl, arg.ID)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user