Created a base to build on
This commit is contained in:
32
internal/db/db.go
Normal file
32
internal/db/db.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
29
internal/db/models.go
Normal file
29
internal/db/models.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
ID int64
|
||||
Name string
|
||||
PlatformID int64
|
||||
Score int32
|
||||
ReleaseYear pgtype.Date
|
||||
Finished pgtype.Date
|
||||
}
|
||||
|
||||
type Platform struct {
|
||||
ID int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int64
|
||||
Name string
|
||||
Bio pgtype.Text
|
||||
}
|
||||
27
internal/db/querier.go
Normal file
27
internal/db/querier.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
CreateGame(ctx context.Context, arg CreateGameParams) (Game, error)
|
||||
CreatePlatform(ctx context.Context, name string) (Platform, error)
|
||||
CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
|
||||
DeleteGame(ctx context.Context, id int64) error
|
||||
DeletePlatform(ctx context.Context, id int64) error
|
||||
DeleteUser(ctx context.Context, id int64) error
|
||||
GetGame(ctx context.Context, id int64) (Game, error)
|
||||
GetUser(ctx context.Context, id int64) (User, error)
|
||||
ListGames(ctx context.Context) ([]Game, error)
|
||||
ListPlatforms(ctx context.Context) ([]Platform, error)
|
||||
ListUsers(ctx context.Context) ([]User, error)
|
||||
UpdateGame(ctx context.Context, arg UpdateGameParams) (Game, error)
|
||||
UpdatePlatform(ctx context.Context, arg UpdatePlatformParams) (Platform, error)
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
285
internal/db/query.sql.go
Normal file
285
internal/db/query.sql.go
Normal file
@@ -0,0 +1,285 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: query.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createGame = `-- name: CreateGame :one
|
||||
INSERT INTO game (
|
||||
name, platform_id, score, release_year, finished
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5
|
||||
)
|
||||
RETURNING id, name, platform_id, score, release_year, finished
|
||||
`
|
||||
|
||||
type CreateGameParams struct {
|
||||
Name string
|
||||
PlatformID int64
|
||||
Score int32
|
||||
ReleaseYear pgtype.Date
|
||||
Finished pgtype.Date
|
||||
}
|
||||
|
||||
func (q *Queries) CreateGame(ctx context.Context, arg CreateGameParams) (Game, error) {
|
||||
row := q.db.QueryRow(ctx, createGame,
|
||||
arg.Name,
|
||||
arg.PlatformID,
|
||||
arg.Score,
|
||||
arg.ReleaseYear,
|
||||
arg.Finished,
|
||||
)
|
||||
var i Game
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.PlatformID,
|
||||
&i.Score,
|
||||
&i.ReleaseYear,
|
||||
&i.Finished,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const createPlatform = `-- name: CreatePlatform :one
|
||||
INSERT INTO platform (
|
||||
name
|
||||
) VALUES (
|
||||
$1
|
||||
)
|
||||
RETURNING id, name
|
||||
`
|
||||
|
||||
func (q *Queries) CreatePlatform(ctx context.Context, name string) (Platform, error) {
|
||||
row := q.db.QueryRow(ctx, createPlatform, name)
|
||||
var i Platform
|
||||
err := row.Scan(&i.ID, &i.Name)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
INSERT INTO users (
|
||||
name, bio
|
||||
) VALUES (
|
||||
$1, $2
|
||||
)
|
||||
RETURNING id, name, bio
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
Name string
|
||||
Bio pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
||||
row := q.db.QueryRow(ctx, createUser, arg.Name, arg.Bio)
|
||||
var i User
|
||||
err := row.Scan(&i.ID, &i.Name, &i.Bio)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteGame = `-- name: DeleteGame :exec
|
||||
DELETE FROM game
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteGame(ctx context.Context, id int64) error {
|
||||
_, err := q.db.Exec(ctx, deleteGame, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const deletePlatform = `-- name: DeletePlatform :exec
|
||||
DELETE FROM platform
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeletePlatform(ctx context.Context, id int64) error {
|
||||
_, err := q.db.Exec(ctx, deletePlatform, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteUser = `-- name: DeleteUser :exec
|
||||
DELETE FROM users
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteUser(ctx context.Context, id int64) error {
|
||||
_, err := q.db.Exec(ctx, deleteUser, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getGame = `-- name: GetGame :one
|
||||
SELECT id, name, platform_id, score, release_year, finished FROM game
|
||||
WHERE id = $1 LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetGame(ctx context.Context, id int64) (Game, error) {
|
||||
row := q.db.QueryRow(ctx, getGame, id)
|
||||
var i Game
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.PlatformID,
|
||||
&i.Score,
|
||||
&i.ReleaseYear,
|
||||
&i.Finished,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUser = `-- name: GetUser :one
|
||||
SELECT id, name, bio FROM users
|
||||
WHERE id = $1 LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUser(ctx context.Context, id int64) (User, error) {
|
||||
row := q.db.QueryRow(ctx, getUser, id)
|
||||
var i User
|
||||
err := row.Scan(&i.ID, &i.Name, &i.Bio)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listGames = `-- name: ListGames :many
|
||||
SELECT id, name, platform_id, score, release_year, finished FROM game
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
func (q *Queries) ListGames(ctx context.Context) ([]Game, error) {
|
||||
rows, err := q.db.Query(ctx, listGames)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Game
|
||||
for rows.Next() {
|
||||
var i Game
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.PlatformID,
|
||||
&i.Score,
|
||||
&i.ReleaseYear,
|
||||
&i.Finished,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listPlatforms = `-- name: ListPlatforms :many
|
||||
SELECT id, name FROM platform
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
func (q *Queries) ListPlatforms(ctx context.Context) ([]Platform, error) {
|
||||
rows, err := q.db.Query(ctx, listPlatforms)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Platform
|
||||
for rows.Next() {
|
||||
var i Platform
|
||||
if err := rows.Scan(&i.ID, &i.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listUsers = `-- name: ListUsers :many
|
||||
SELECT id, name, bio FROM users
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
|
||||
rows, err := q.db.Query(ctx, listUsers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []User
|
||||
for rows.Next() {
|
||||
var i User
|
||||
if err := rows.Scan(&i.ID, &i.Name, &i.Bio); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateGame = `-- name: UpdateGame :one
|
||||
UPDATE game
|
||||
SET name = $2, platform_id = $3, score = $4, release_year = $5, finished = $6
|
||||
WHERE id = $1
|
||||
RETURNING id, name, platform_id, score, release_year, finished
|
||||
`
|
||||
|
||||
type UpdateGameParams struct {
|
||||
ID int64
|
||||
Name string
|
||||
PlatformID int64
|
||||
Score int32
|
||||
ReleaseYear pgtype.Date
|
||||
Finished pgtype.Date
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateGame(ctx context.Context, arg UpdateGameParams) (Game, error) {
|
||||
row := q.db.QueryRow(ctx, updateGame,
|
||||
arg.ID,
|
||||
arg.Name,
|
||||
arg.PlatformID,
|
||||
arg.Score,
|
||||
arg.ReleaseYear,
|
||||
arg.Finished,
|
||||
)
|
||||
var i Game
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.PlatformID,
|
||||
&i.Score,
|
||||
&i.ReleaseYear,
|
||||
&i.Finished,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updatePlatform = `-- name: UpdatePlatform :one
|
||||
UPDATE platform
|
||||
SET name = $2
|
||||
WHERE id = $1
|
||||
RETURNING id, name
|
||||
`
|
||||
|
||||
type UpdatePlatformParams struct {
|
||||
ID int64
|
||||
Name string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdatePlatform(ctx context.Context, arg UpdatePlatformParams) (Platform, error) {
|
||||
row := q.db.QueryRow(ctx, updatePlatform, arg.ID, arg.Name)
|
||||
var i Platform
|
||||
err := row.Scan(&i.ID, &i.Name)
|
||||
return i, err
|
||||
}
|
||||
Reference in New Issue
Block a user