Merged in feature/logVersioning (pull request #40)
Can Sync Versioning * started * client * jobcansync * tests
This commit is contained in:
@@ -11,6 +11,23 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const addClientCanSync = `-- name: AddClientCanSync :exec
|
||||
INSERT INTO clientCanSync (canSync, clientId) VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type AddClientCanSyncParams struct {
|
||||
Cansync bool `db:"cansync"`
|
||||
Clientid pgtype.UUID `db:"clientid"`
|
||||
}
|
||||
|
||||
// AddClientCanSync
|
||||
//
|
||||
// INSERT INTO clientCanSync (canSync, clientId) VALUES ($1, $2)
|
||||
func (q *Queries) AddClientCanSync(ctx context.Context, arg *AddClientCanSyncParams) error {
|
||||
_, err := q.db.Exec(ctx, addClientCanSync, arg.Cansync, arg.Clientid)
|
||||
return err
|
||||
}
|
||||
|
||||
const createClient = `-- name: CreateClient :one
|
||||
INSERT INTO clients (name) VALUES ($1) RETURNING id
|
||||
`
|
||||
@@ -26,33 +43,56 @@ func (q *Queries) CreateClient(ctx context.Context, name string) (pgtype.UUID, e
|
||||
}
|
||||
|
||||
const getClient = `-- name: GetClient :one
|
||||
SELECT id, name, canSync FROM clients WHERE id = $1
|
||||
SELECT c.id, c.name, coalesce(cs.canSync, false) as canSync
|
||||
FROM clients as c
|
||||
LEFT JOIN (
|
||||
SELECT clientId, canSync
|
||||
FROM clientCanSync
|
||||
WHERE clientId = $1
|
||||
ORDER BY addedAt DESC
|
||||
LIMIT 1
|
||||
) as cs on cs.clientId = c.id
|
||||
WHERE c.id = $1
|
||||
`
|
||||
|
||||
type GetClientRow struct {
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Name string `db:"name"`
|
||||
Cansync bool `db:"cansync"`
|
||||
}
|
||||
|
||||
// GetClient
|
||||
//
|
||||
// SELECT id, name, canSync FROM clients WHERE id = $1
|
||||
func (q *Queries) GetClient(ctx context.Context, id pgtype.UUID) (*Client, error) {
|
||||
row := q.db.QueryRow(ctx, getClient, id)
|
||||
var i Client
|
||||
// SELECT c.id, c.name, coalesce(cs.canSync, false) as canSync
|
||||
// FROM clients as c
|
||||
// LEFT JOIN (
|
||||
// SELECT clientId, canSync
|
||||
// FROM clientCanSync
|
||||
// WHERE clientId = $1
|
||||
// ORDER BY addedAt DESC
|
||||
// LIMIT 1
|
||||
// ) as cs on cs.clientId = c.id
|
||||
// WHERE c.id = $1
|
||||
func (q *Queries) GetClient(ctx context.Context, clientid pgtype.UUID) (*GetClientRow, error) {
|
||||
row := q.db.QueryRow(ctx, getClient, clientid)
|
||||
var i GetClientRow
|
||||
err := row.Scan(&i.ID, &i.Name, &i.Cansync)
|
||||
return &i, err
|
||||
}
|
||||
|
||||
const updateClient = `-- name: UpdateClient :exec
|
||||
UPDATE clients SET name = $1, canSync = $2 WHERE id = $3
|
||||
UPDATE clients SET name = $1 WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateClientParams struct {
|
||||
Name string `db:"name"`
|
||||
Cansync bool `db:"cansync"`
|
||||
ID pgtype.UUID `db:"id"`
|
||||
Name string `db:"name"`
|
||||
ID pgtype.UUID `db:"id"`
|
||||
}
|
||||
|
||||
// UpdateClient
|
||||
//
|
||||
// UPDATE clients SET name = $1, canSync = $2 WHERE id = $3
|
||||
// UPDATE clients SET name = $1 WHERE id = $2
|
||||
func (q *Queries) UpdateClient(ctx context.Context, arg *UpdateClientParams) error {
|
||||
_, err := q.db.Exec(ctx, updateClient, arg.Name, arg.Cansync, arg.ID)
|
||||
_, err := q.db.Exec(ctx, updateClient, arg.Name, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user