Use sqlc to generate typesafe repository tools
parent
1c15742710
commit
d688900bae
@ -0,0 +1,31 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.13.0
|
||||
|
||||
package namespace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.13.0
|
||||
|
||||
package namespace
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type FileStat struct {
|
||||
ID int64
|
||||
Num int64
|
||||
SizeB int64
|
||||
}
|
||||
|
||||
type Namespace struct {
|
||||
ID int64
|
||||
Name string
|
||||
Lastseen int64
|
||||
AllowanceTime sql.NullInt64
|
||||
QuotaKb sql.NullInt64
|
||||
QuotaUsageKb sql.NullInt64
|
||||
UsageID int64
|
||||
DownloadID int64
|
||||
UploadID int64
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
-- name: CreateNamespace :one
|
||||
INSERT INTO
|
||||
namespace(
|
||||
name,
|
||||
lastseen,
|
||||
allowance_time,
|
||||
quota_kb,
|
||||
quota_usage_kb,
|
||||
usage_id,
|
||||
download_id,
|
||||
upload_id
|
||||
)
|
||||
values(?, ?, ?, ?, ?, ?, ?, ?)
|
||||
returning id;
|
||||
|
||||
-- name: CreateFileStats :one
|
||||
INSERT INTO file_stats(num, size_b)
|
||||
values(?, ?)
|
||||
returning id;
|
||||
|
||||
-- name: AllNamespaces :many
|
||||
SELECT
|
||||
ns.id,
|
||||
ns.name,
|
||||
ns.lastseen,
|
||||
ns.allowance_time,
|
||||
ns.quota_kb,
|
||||
ns.quota_usage_kb,
|
||||
u.num as u_num,
|
||||
u.size_b as u_size_b,
|
||||
d.num as d_num,
|
||||
d.size_b as d_size_b,
|
||||
ul.num as ul_num,
|
||||
ul.size_b as ul_size_b
|
||||
FROM namespace as ns
|
||||
JOIN file_stats as u
|
||||
ON ns.usage_id = u.id
|
||||
JOIN file_stats as d
|
||||
ON ns.download_id = d.id
|
||||
JOIN file_stats as ul
|
||||
ON ns.upload_id = ul.id;
|
||||
|
||||
-- name: GetNamespaceByName :one
|
||||
SELECT
|
||||
ns.id,
|
||||
ns.name,
|
||||
ns.lastseen,
|
||||
ns.allowance_time,
|
||||
ns.quota_kb,
|
||||
ns.quota_usage_kb,
|
||||
u.num as u_num,
|
||||
u.size_b as u_size_b,
|
||||
d.num as d_num,
|
||||
d.size_b as d_size_b,
|
||||
ul.num as ul_num,
|
||||
ul.size_b as ul_size_b
|
||||
FROM namespace as ns
|
||||
JOIN file_stats as u
|
||||
ON ns.usage_id = u.id
|
||||
JOIN file_stats as d
|
||||
ON ns.download_id = d.id
|
||||
JOIN file_stats as ul
|
||||
ON ns.upload_id = ul.id
|
||||
WHERE ns.name = ?;
|
||||
|
||||
|
||||
-- name: GetFileStat :one
|
||||
SELECT * FROM file_stats where id = ?;
|
||||
|
||||
-- name: UpdateFileStat :exec
|
||||
UPDATE file_stats SET num = ?, size_b = ? where id = ?;
|
||||
|
||||
-- name: UpdateNamespace :one
|
||||
UPDATE namespace SET
|
||||
name = ?,
|
||||
lastseen = ?,
|
||||
allowance_time = ?,
|
||||
quota_kb = ?,
|
||||
quota_usage_kb = ?
|
||||
WHERE id = ?
|
||||
RETURNING usage_id, download_id, upload_id;
|
||||
|
||||
-- name: DeleteNameSpace :exec
|
||||
DELETE FROM namespace where id = ?;
|
@ -0,0 +1,280 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.13.0
|
||||
// source: queries.sql
|
||||
|
||||
package namespace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const allNamespaces = `-- name: AllNamespaces :many
|
||||
SELECT
|
||||
ns.id,
|
||||
ns.name,
|
||||
ns.lastseen,
|
||||
ns.allowance_time,
|
||||
ns.quota_kb,
|
||||
ns.quota_usage_kb,
|
||||
u.num as u_num,
|
||||
u.size_b as u_size_b,
|
||||
d.num as d_num,
|
||||
d.size_b as d_size_b,
|
||||
ul.num as ul_num,
|
||||
ul.size_b as ul_size_b
|
||||
FROM namespace as ns
|
||||
JOIN file_stats as u
|
||||
ON ns.usage_id = u.id
|
||||
JOIN file_stats as d
|
||||
ON ns.download_id = d.id
|
||||
JOIN file_stats as ul
|
||||
ON ns.upload_id = ul.id
|
||||
`
|
||||
|
||||
type AllNamespacesRow struct {
|
||||
ID int64
|
||||
Name string
|
||||
Lastseen int64
|
||||
AllowanceTime sql.NullInt64
|
||||
QuotaKb sql.NullInt64
|
||||
QuotaUsageKb sql.NullInt64
|
||||
UNum int64
|
||||
USizeB int64
|
||||
DNum int64
|
||||
DSizeB int64
|
||||
UlNum int64
|
||||
UlSizeB int64
|
||||
}
|
||||
|
||||
func (q *Queries) AllNamespaces(ctx context.Context) ([]AllNamespacesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, allNamespaces)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []AllNamespacesRow
|
||||
for rows.Next() {
|
||||
var i AllNamespacesRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Lastseen,
|
||||
&i.AllowanceTime,
|
||||
&i.QuotaKb,
|
||||
&i.QuotaUsageKb,
|
||||
&i.UNum,
|
||||
&i.USizeB,
|
||||
&i.DNum,
|
||||
&i.DSizeB,
|
||||
&i.UlNum,
|
||||
&i.UlSizeB,
|
||||
); 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 createFileStats = `-- name: CreateFileStats :one
|
||||
INSERT INTO file_stats(num, size_b)
|
||||
values(?, ?)
|
||||
returning id
|
||||
`
|
||||
|
||||
type CreateFileStatsParams struct {
|
||||
Num int64
|
||||
SizeB int64
|
||||
}
|
||||
|
||||
func (q *Queries) CreateFileStats(ctx context.Context, arg CreateFileStatsParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, createFileStats, arg.Num, arg.SizeB)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const createNamespace = `-- name: CreateNamespace :one
|
||||
INSERT INTO
|
||||
namespace(
|
||||
name,
|
||||
lastseen,
|
||||
allowance_time,
|
||||
quota_kb,
|
||||
quota_usage_kb,
|
||||
usage_id,
|
||||
download_id,
|
||||
upload_id
|
||||
)
|
||||
values(?, ?, ?, ?, ?, ?, ?, ?)
|
||||
returning id
|
||||
`
|
||||
|
||||
type CreateNamespaceParams struct {
|
||||
Name string
|
||||
Lastseen int64
|
||||
AllowanceTime sql.NullInt64
|
||||
QuotaKb sql.NullInt64
|
||||
QuotaUsageKb sql.NullInt64
|
||||
UsageID int64
|
||||
DownloadID int64
|
||||
UploadID int64
|
||||
}
|
||||
|
||||
func (q *Queries) CreateNamespace(ctx context.Context, arg CreateNamespaceParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, createNamespace,
|
||||
arg.Name,
|
||||
arg.Lastseen,
|
||||
arg.AllowanceTime,
|
||||
arg.QuotaKb,
|
||||
arg.QuotaUsageKb,
|
||||
arg.UsageID,
|
||||
arg.DownloadID,
|
||||
arg.UploadID,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const deleteNameSpace = `-- name: DeleteNameSpace :exec
|
||||
DELETE FROM namespace where id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteNameSpace(ctx context.Context, id int64) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteNameSpace, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getFileStat = `-- name: GetFileStat :one
|
||||
SELECT id, num, size_b FROM file_stats where id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetFileStat(ctx context.Context, id int64) (FileStat, error) {
|
||||
row := q.db.QueryRowContext(ctx, getFileStat, id)
|
||||
var i FileStat
|
||||
err := row.Scan(&i.ID, &i.Num, &i.SizeB)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getNamespaceByName = `-- name: GetNamespaceByName :one
|
||||
SELECT
|
||||
ns.id,
|
||||
ns.name,
|
||||
ns.lastseen,
|
||||
ns.allowance_time,
|
||||
ns.quota_kb,
|
||||
ns.quota_usage_kb,
|
||||
u.num as u_num,
|
||||
u.size_b as u_size_b,
|
||||
d.num as d_num,
|
||||
d.size_b as d_size_b,
|
||||
ul.num as ul_num,
|
||||
ul.size_b as ul_size_b
|
||||
FROM namespace as ns
|
||||
JOIN file_stats as u
|
||||
ON ns.usage_id = u.id
|
||||
JOIN file_stats as d
|
||||
ON ns.download_id = d.id
|
||||
JOIN file_stats as ul
|
||||
ON ns.upload_id = ul.id
|
||||
WHERE ns.name = ?
|
||||
`
|
||||
|
||||
type GetNamespaceByNameRow struct {
|
||||
ID int64
|
||||
Name string
|
||||
Lastseen int64
|
||||
AllowanceTime sql.NullInt64
|
||||
QuotaKb sql.NullInt64
|
||||
QuotaUsageKb sql.NullInt64
|
||||
UNum int64
|
||||
USizeB int64
|
||||
DNum int64
|
||||
DSizeB int64
|
||||
UlNum int64
|
||||
UlSizeB int64
|
||||
}
|
||||
|
||||
func (q *Queries) GetNamespaceByName(ctx context.Context, name string) (GetNamespaceByNameRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getNamespaceByName, name)
|
||||
var i GetNamespaceByNameRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Lastseen,
|
||||
&i.AllowanceTime,
|
||||
&i.QuotaKb,
|
||||
&i.QuotaUsageKb,
|
||||
&i.UNum,
|
||||
&i.USizeB,
|
||||
&i.DNum,
|
||||
&i.DSizeB,
|
||||
&i.UlNum,
|
||||
&i.UlSizeB,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateFileStat = `-- name: UpdateFileStat :exec
|
||||
UPDATE file_stats SET num = ?, size_b = ? where id = ?
|
||||
`
|
||||
|
||||
type UpdateFileStatParams struct {
|
||||
Num int64
|
||||
SizeB int64
|
||||
ID int64
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateFileStat(ctx context.Context, arg UpdateFileStatParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateFileStat, arg.Num, arg.SizeB, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateNamespace = `-- name: UpdateNamespace :one
|
||||
UPDATE namespace SET
|
||||
name = ?,
|
||||
lastseen = ?,
|
||||
allowance_time = ?,
|
||||
quota_kb = ?,
|
||||
quota_usage_kb = ?
|
||||
WHERE id = ?
|
||||
RETURNING usage_id, download_id, upload_id
|
||||
`
|
||||
|
||||
type UpdateNamespaceParams struct {
|
||||
Name string
|
||||
Lastseen int64
|
||||
AllowanceTime sql.NullInt64
|
||||
QuotaKb sql.NullInt64
|
||||
QuotaUsageKb sql.NullInt64
|
||||
ID int64
|
||||
}
|
||||
|
||||
type UpdateNamespaceRow struct {
|
||||
UsageID int64
|
||||
DownloadID int64
|
||||
UploadID int64
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateNamespace(ctx context.Context, arg UpdateNamespaceParams) (UpdateNamespaceRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, updateNamespace,
|
||||
arg.Name,
|
||||
arg.Lastseen,
|
||||
arg.AllowanceTime,
|
||||
arg.QuotaKb,
|
||||
arg.QuotaUsageKb,
|
||||
arg.ID,
|
||||
)
|
||||
var i UpdateNamespaceRow
|
||||
err := row.Scan(&i.UsageID, &i.DownloadID, &i.UploadID)
|
||||
return i, err
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
CREATE TABLE IF NOT EXISTS file_stats(
|
||||
id BIGINT PRIMARY KEY,
|
||||
num BIGINT NOT NULL,
|
||||
size_b BIGINT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS namespace(
|
||||
id BIGINT PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
lastseen BIGINT NOT NULL,
|
||||
allowance_time BIGINT,
|
||||
quota_kb BIGINT,
|
||||
quota_usage_kb BIGINT,
|
||||
usage_id BIGINT NOT NULL REFERENCES file_stats(Id) ON DELETE CASCADE,
|
||||
download_id BIGINT NOT NULL REFERENCES file_stats(Id) ON DELETE CASCADE,
|
||||
upload_id BIGINT NOT NULL REFERENCES file_stats(Id) ON DELETE CASCADE
|
||||
);
|
Loading…
Reference in New Issue