mirror of https://github.com/lightninglabs/loop
loopdb: add reservation sqlc code
parent
c3ce0f55a4
commit
5a16934838
@ -0,0 +1,2 @@
|
|||||||
|
DROP TABLE IF EXISTS reservation_updates;
|
||||||
|
DROP TABLE IF EXISTS reservations;
|
@ -0,0 +1,56 @@
|
|||||||
|
-- reservations contains all the information about a reservation.
|
||||||
|
CREATE TABLE IF NOT EXISTS reservations (
|
||||||
|
-- id is the auto incrementing primary key.
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
|
||||||
|
-- reservation_id is the unique identifier for the reservation.
|
||||||
|
reservation_id BLOB NOT NULL UNIQUE,
|
||||||
|
|
||||||
|
-- client_pubkey is the public key of the client.
|
||||||
|
client_pubkey BLOB NOT NULL,
|
||||||
|
|
||||||
|
-- server_pubkey is the public key of the server.
|
||||||
|
server_pubkey BLOB NOT NULL,
|
||||||
|
|
||||||
|
-- expiry is the absolute expiry height of the reservation.
|
||||||
|
expiry INTEGER NOT NULL,
|
||||||
|
|
||||||
|
-- value is the value of the reservation.
|
||||||
|
value BIGINT NOT NULL,
|
||||||
|
|
||||||
|
-- client_key_family is the key family of the client.
|
||||||
|
client_key_family INTEGER NOT NULL,
|
||||||
|
|
||||||
|
-- client_key_index is the key index of the client.
|
||||||
|
client_key_index INTEGER NOT NULL,
|
||||||
|
|
||||||
|
-- initiation_height is the height at which the reservation was initiated.
|
||||||
|
initiation_height INTEGER NOT NULL,
|
||||||
|
|
||||||
|
-- tx_hash is the hash of the transaction that created the reservation.
|
||||||
|
tx_hash BLOB,
|
||||||
|
|
||||||
|
-- out_index is the index of the output that created the reservation.
|
||||||
|
out_index INTEGER,
|
||||||
|
|
||||||
|
-- confirmation_height is the height at which the reservation was confirmed.
|
||||||
|
confirmation_height INTEGER
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS reservations_reservation_id_idx ON reservations(reservation_id);
|
||||||
|
|
||||||
|
-- reservation_updates contains all the updates to a reservation.
|
||||||
|
CREATE TABLE IF NOT EXISTS reservation_updates (
|
||||||
|
-- id is the auto incrementing primary key.
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
|
||||||
|
-- reservation_id is the unique identifier for the reservation.
|
||||||
|
reservation_id BLOB NOT NULL REFERENCES reservations(reservation_id),
|
||||||
|
|
||||||
|
-- update_state is the state of the reservation at the time of the update.
|
||||||
|
update_state TEXT NOT NULL,
|
||||||
|
|
||||||
|
-- update_timestamp is the timestamp of the update.
|
||||||
|
update_timestamp TIMESTAMP NOT NULL
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,66 @@
|
|||||||
|
-- name: CreateReservation :exec
|
||||||
|
INSERT INTO reservations (
|
||||||
|
reservation_id,
|
||||||
|
client_pubkey,
|
||||||
|
server_pubkey,
|
||||||
|
expiry,
|
||||||
|
value,
|
||||||
|
client_key_family,
|
||||||
|
client_key_index,
|
||||||
|
initiation_height
|
||||||
|
) VALUES (
|
||||||
|
$1,
|
||||||
|
$2,
|
||||||
|
$3,
|
||||||
|
$4,
|
||||||
|
$5,
|
||||||
|
$6,
|
||||||
|
$7,
|
||||||
|
$8
|
||||||
|
);
|
||||||
|
|
||||||
|
-- name: UpdateReservation :exec
|
||||||
|
UPDATE reservations
|
||||||
|
SET
|
||||||
|
tx_hash = $2,
|
||||||
|
out_index = $3,
|
||||||
|
confirmation_height = $4
|
||||||
|
WHERE
|
||||||
|
reservations.reservation_id = $1;
|
||||||
|
|
||||||
|
-- name: InsertReservationUpdate :exec
|
||||||
|
INSERT INTO reservation_updates (
|
||||||
|
reservation_id,
|
||||||
|
update_state,
|
||||||
|
update_timestamp
|
||||||
|
) VALUES (
|
||||||
|
$1,
|
||||||
|
$2,
|
||||||
|
$3
|
||||||
|
);
|
||||||
|
|
||||||
|
-- name: GetReservation :one
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
reservations
|
||||||
|
WHERE
|
||||||
|
reservation_id = $1;
|
||||||
|
|
||||||
|
-- name: GetReservations :many
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
reservations
|
||||||
|
ORDER BY
|
||||||
|
id ASC;
|
||||||
|
|
||||||
|
-- name: GetReservationUpdates :many
|
||||||
|
SELECT
|
||||||
|
reservation_updates.*
|
||||||
|
FROM
|
||||||
|
reservation_updates
|
||||||
|
WHERE
|
||||||
|
reservation_id = $1
|
||||||
|
ORDER BY
|
||||||
|
id ASC;
|
@ -0,0 +1,222 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.17.2
|
||||||
|
// source: reservations.sql
|
||||||
|
|
||||||
|
package sqlc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createReservation = `-- name: CreateReservation :exec
|
||||||
|
INSERT INTO reservations (
|
||||||
|
reservation_id,
|
||||||
|
client_pubkey,
|
||||||
|
server_pubkey,
|
||||||
|
expiry,
|
||||||
|
value,
|
||||||
|
client_key_family,
|
||||||
|
client_key_index,
|
||||||
|
initiation_height
|
||||||
|
) VALUES (
|
||||||
|
$1,
|
||||||
|
$2,
|
||||||
|
$3,
|
||||||
|
$4,
|
||||||
|
$5,
|
||||||
|
$6,
|
||||||
|
$7,
|
||||||
|
$8
|
||||||
|
)
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateReservationParams struct {
|
||||||
|
ReservationID []byte
|
||||||
|
ClientPubkey []byte
|
||||||
|
ServerPubkey []byte
|
||||||
|
Expiry int32
|
||||||
|
Value int64
|
||||||
|
ClientKeyFamily int32
|
||||||
|
ClientKeyIndex int32
|
||||||
|
InitiationHeight int32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateReservation(ctx context.Context, arg CreateReservationParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, createReservation,
|
||||||
|
arg.ReservationID,
|
||||||
|
arg.ClientPubkey,
|
||||||
|
arg.ServerPubkey,
|
||||||
|
arg.Expiry,
|
||||||
|
arg.Value,
|
||||||
|
arg.ClientKeyFamily,
|
||||||
|
arg.ClientKeyIndex,
|
||||||
|
arg.InitiationHeight,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getReservation = `-- name: GetReservation :one
|
||||||
|
SELECT
|
||||||
|
id, reservation_id, client_pubkey, server_pubkey, expiry, value, client_key_family, client_key_index, initiation_height, tx_hash, out_index, confirmation_height
|
||||||
|
FROM
|
||||||
|
reservations
|
||||||
|
WHERE
|
||||||
|
reservation_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetReservation(ctx context.Context, reservationID []byte) (Reservation, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getReservation, reservationID)
|
||||||
|
var i Reservation
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.ReservationID,
|
||||||
|
&i.ClientPubkey,
|
||||||
|
&i.ServerPubkey,
|
||||||
|
&i.Expiry,
|
||||||
|
&i.Value,
|
||||||
|
&i.ClientKeyFamily,
|
||||||
|
&i.ClientKeyIndex,
|
||||||
|
&i.InitiationHeight,
|
||||||
|
&i.TxHash,
|
||||||
|
&i.OutIndex,
|
||||||
|
&i.ConfirmationHeight,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getReservationUpdates = `-- name: GetReservationUpdates :many
|
||||||
|
SELECT
|
||||||
|
reservation_updates.id, reservation_updates.reservation_id, reservation_updates.update_state, reservation_updates.update_timestamp
|
||||||
|
FROM
|
||||||
|
reservation_updates
|
||||||
|
WHERE
|
||||||
|
reservation_id = $1
|
||||||
|
ORDER BY
|
||||||
|
id ASC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetReservationUpdates(ctx context.Context, reservationID []byte) ([]ReservationUpdate, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, getReservationUpdates, reservationID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []ReservationUpdate
|
||||||
|
for rows.Next() {
|
||||||
|
var i ReservationUpdate
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.ReservationID,
|
||||||
|
&i.UpdateState,
|
||||||
|
&i.UpdateTimestamp,
|
||||||
|
); 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 getReservations = `-- name: GetReservations :many
|
||||||
|
SELECT
|
||||||
|
id, reservation_id, client_pubkey, server_pubkey, expiry, value, client_key_family, client_key_index, initiation_height, tx_hash, out_index, confirmation_height
|
||||||
|
FROM
|
||||||
|
reservations
|
||||||
|
ORDER BY
|
||||||
|
id ASC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetReservations(ctx context.Context) ([]Reservation, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, getReservations)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []Reservation
|
||||||
|
for rows.Next() {
|
||||||
|
var i Reservation
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.ReservationID,
|
||||||
|
&i.ClientPubkey,
|
||||||
|
&i.ServerPubkey,
|
||||||
|
&i.Expiry,
|
||||||
|
&i.Value,
|
||||||
|
&i.ClientKeyFamily,
|
||||||
|
&i.ClientKeyIndex,
|
||||||
|
&i.InitiationHeight,
|
||||||
|
&i.TxHash,
|
||||||
|
&i.OutIndex,
|
||||||
|
&i.ConfirmationHeight,
|
||||||
|
); 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 insertReservationUpdate = `-- name: InsertReservationUpdate :exec
|
||||||
|
INSERT INTO reservation_updates (
|
||||||
|
reservation_id,
|
||||||
|
update_state,
|
||||||
|
update_timestamp
|
||||||
|
) VALUES (
|
||||||
|
$1,
|
||||||
|
$2,
|
||||||
|
$3
|
||||||
|
)
|
||||||
|
`
|
||||||
|
|
||||||
|
type InsertReservationUpdateParams struct {
|
||||||
|
ReservationID []byte
|
||||||
|
UpdateState string
|
||||||
|
UpdateTimestamp time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) InsertReservationUpdate(ctx context.Context, arg InsertReservationUpdateParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, insertReservationUpdate, arg.ReservationID, arg.UpdateState, arg.UpdateTimestamp)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateReservation = `-- name: UpdateReservation :exec
|
||||||
|
UPDATE reservations
|
||||||
|
SET
|
||||||
|
tx_hash = $2,
|
||||||
|
out_index = $3,
|
||||||
|
confirmation_height = $4
|
||||||
|
WHERE
|
||||||
|
reservations.reservation_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateReservationParams struct {
|
||||||
|
ReservationID []byte
|
||||||
|
TxHash []byte
|
||||||
|
OutIndex sql.NullInt32
|
||||||
|
ConfirmationHeight sql.NullInt32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateReservation(ctx context.Context, arg UpdateReservationParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, updateReservation,
|
||||||
|
arg.ReservationID,
|
||||||
|
arg.TxHash,
|
||||||
|
arg.OutIndex,
|
||||||
|
arg.ConfirmationHeight,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
Loading…
Reference in New Issue