add sqlc and character table.

This commit is contained in:
2021-03-21 20:53:21 +01:00
parent b6b7b8f1b6
commit c5ea598ea1
26 changed files with 1285 additions and 65 deletions
@@ -0,0 +1,235 @@
// Code generated by sqlc. DO NOT EDIT.
// source: characters.sql
package psqlcore
import (
"context"
"github.com/lib/pq"
)
const addCharacterNick = `-- name: AddCharacterNick :exec
UPDATE data_character
SET nicks=append(nicks, $1::text)
WHERE id=$2::text
`
type AddCharacterNickParams struct {
Nick string `json:"nick"`
ID string `json:"id"`
}
func (q *Queries) AddCharacterNick(ctx context.Context, arg AddCharacterNickParams) error {
_, err := q.db.ExecContext(ctx, addCharacterNick, arg.Nick, arg.ID)
return err
}
const deleteCharacter = `-- name: DeleteCharacter :exec
DELETE FROM data_character WHERE id=$1
`
func (q *Queries) DeleteCharacter(ctx context.Context, id string) error {
_, err := q.db.ExecContext(ctx, deleteCharacter, id)
return err
}
const insertCharacter = `-- name: InsertCharacter :exec
INSERT INTO data_character (id, nicks, name, short_name, author, description, ts_vector) VALUES (
$1::text, $2::text[], $3::text,
$4::text, $5::text, $6::text,
to_tsvector(
'english',
$3::text || ' ' || $6::text || ' ' || $5::text || ' '
|| immutable_array_to_string(
$2::text[], ' '
)
)
)
`
type InsertCharacterParams struct {
ID string `json:"id"`
Nicks []string `json:"nicks"`
Name string `json:"name"`
ShortName string `json:"short_name"`
Author string `json:"author"`
Description string `json:"description"`
}
func (q *Queries) InsertCharacter(ctx context.Context, arg InsertCharacterParams) error {
_, err := q.db.ExecContext(ctx, insertCharacter,
arg.ID,
pq.Array(arg.Nicks),
arg.Name,
arg.ShortName,
arg.Author,
arg.Description,
)
return err
}
const removeCharacterNick = `-- name: RemoveCharacterNick :exec
UPDATE data_character
SET nicks=array_remove(nicks, $1::text)
WHERE id=$2::text
`
type RemoveCharacterNickParams struct {
Nick string `json:"nick"`
ID string `json:"id"`
}
func (q *Queries) RemoveCharacterNick(ctx context.Context, arg RemoveCharacterNickParams) error {
_, err := q.db.ExecContext(ctx, removeCharacterNick, arg.Nick, arg.ID)
return err
}
const selectCharacterByID = `-- name: SelectCharacterByID :one
SELECT id, nicks, name, short_name, author, description, ts_vector FROM data_character WHERE id = $1::text
`
func (q *Queries) SelectCharacterByID(ctx context.Context, id string) (DataCharacter, error) {
row := q.db.QueryRowContext(ctx, selectCharacterByID, id)
var i DataCharacter
err := row.Scan(
&i.ID,
pq.Array(&i.Nicks),
&i.Name,
&i.ShortName,
&i.Author,
&i.Description,
&i.TsVector,
)
return i, err
}
const selectCharacterByName = `-- name: SelectCharacterByName :one
SELECT id, nicks, name, short_name, author, description, ts_vector FROM data_character WHERE name = $1::text
`
func (q *Queries) SelectCharacterByName(ctx context.Context, name string) (DataCharacter, error) {
row := q.db.QueryRowContext(ctx, selectCharacterByName, name)
var i DataCharacter
err := row.Scan(
&i.ID,
pq.Array(&i.Nicks),
&i.Name,
&i.ShortName,
&i.Author,
&i.Description,
&i.TsVector,
)
return i, err
}
const selectCharacterByNick = `-- name: SelectCharacterByNick :one
SELECT id, nicks, name, short_name, author, description, ts_vector FROM data_character WHERE nicks <@ ARRAY[$1::text]
`
func (q *Queries) SelectCharacterByNick(ctx context.Context, nick string) (DataCharacter, error) {
row := q.db.QueryRowContext(ctx, selectCharacterByNick, nick)
var i DataCharacter
err := row.Scan(
&i.ID,
pq.Array(&i.Nicks),
&i.Name,
&i.ShortName,
&i.Author,
&i.Description,
&i.TsVector,
)
return i, err
}
const selectCharacters = `-- name: SelectCharacters :many
SELECT id, nicks, name, short_name, author, description, ts_vector FROM data_character
WHERE ($1::bool = false OR id = ANY($2::text[]))
AND ($3::bool = false OR name = ANY($4::text[]))
AND ($5::bool = false OR nicks && ($6::text[]))
AND ($7::bool = false OR author = $8::text)
AND ($9::bool = false OR "ts_vector" @@ to_tsquery($10::text))
LIMIT $11::int
`
type SelectCharactersParams struct {
FilterID bool `json:"filter_id"`
Ids []string `json:"ids"`
FilterName bool `json:"filter_name"`
Names []string `json:"names"`
FilterNick bool `json:"filter_nick"`
Nicks []string `json:"nicks"`
FilterAuthor bool `json:"filter_author"`
Author string `json:"author"`
FilterSearch bool `json:"filter_search"`
Search string `json:"search"`
LimitSize int32 `json:"limit_size"`
}
func (q *Queries) SelectCharacters(ctx context.Context, arg SelectCharactersParams) ([]DataCharacter, error) {
rows, err := q.db.QueryContext(ctx, selectCharacters,
arg.FilterID,
pq.Array(arg.Ids),
arg.FilterName,
pq.Array(arg.Names),
arg.FilterNick,
pq.Array(arg.Nicks),
arg.FilterAuthor,
arg.Author,
arg.FilterSearch,
arg.Search,
arg.LimitSize,
)
if err != nil {
return nil, err
}
defer rows.Close()
items := []DataCharacter{}
for rows.Next() {
var i DataCharacter
if err := rows.Scan(
&i.ID,
pq.Array(&i.Nicks),
&i.Name,
&i.ShortName,
&i.Author,
&i.Description,
&i.TsVector,
); 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 updateCharacter = `-- name: UpdateCharacter :exec
UPDATE data_character
SET name=$1::text,
short_name=$2::text,
description=$3::text
WHERE id=$4::text
`
type UpdateCharacterParams struct {
Name string `json:"name"`
ShortName string `json:"short_name"`
Description string `json:"description"`
ID string `json:"id"`
}
func (q *Queries) UpdateCharacter(ctx context.Context, arg UpdateCharacterParams) error {
_, err := q.db.ExecContext(ctx, updateCharacter,
arg.Name,
arg.ShortName,
arg.Description,
arg.ID,
)
return err
}
+42
View File
@@ -0,0 +1,42 @@
// Code generated by sqlc. DO NOT EDIT.
// source: counter.sql
package psqlcore
import (
"context"
)
const bumpCounter = `-- name: BumpCounter :exec
UPDATE core_counter SET value = value + 1 WHERE id = $1::text AND value <= $2::int
`
type BumpCounterParams struct {
ID string `json:"id"`
Value int32 `json:"value"`
}
func (q *Queries) BumpCounter(ctx context.Context, arg BumpCounterParams) error {
_, err := q.db.ExecContext(ctx, bumpCounter, arg.ID, arg.Value)
return err
}
const ensureCounter = `-- name: EnsureCounter :exec
INSERT INTO core_counter (id, value) VALUES ($1::text, 0) ON CONFLICT DO NOTHING
`
func (q *Queries) EnsureCounter(ctx context.Context, dollar_1 string) error {
_, err := q.db.ExecContext(ctx, ensureCounter, dollar_1)
return err
}
const incrementCounter = `-- name: IncrementCounter :one
UPDATE core_counter SET value = value + 1 WHERE id = $1::text RETURNING value::int
`
func (q *Queries) IncrementCounter(ctx context.Context, dollar_1 string) (int32, error) {
row := q.db.QueryRowContext(ctx, incrementCounter, dollar_1)
var value int32
err := row.Scan(&value)
return value, err
}
+29
View File
@@ -0,0 +1,29 @@
// Code generated by sqlc. DO NOT EDIT.
package psqlcore
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,
}
}
+22
View File
@@ -0,0 +1,22 @@
// Code generated by sqlc. DO NOT EDIT.
package psqlcore
import (
"database/sql"
)
type CoreCounter struct {
ID string `json:"id"`
Value sql.NullInt32 `json:"value"`
}
type DataCharacter struct {
ID string `json:"id"`
Nicks []string `json:"nicks"`
Name string `json:"name"`
ShortName string `json:"short_name"`
Author string `json:"author"`
Description string `json:"description"`
TsVector interface{} `json:"ts_vector"`
}
+4
View File
@@ -0,0 +1,4 @@
//go:generate go run github.com/kyleconroy/sqlc/cmd/sqlc generate -f ../../../sqlc.yaml
// Package psqlcore contains the generated code for the PostgresSQL drivers. It's generated using sqlc.
package psqlcore