finish setup data insertion, add world insertion

This commit is contained in:
lbenedar
2026-04-22 18:18:43 +03:00
parent 21abe68858
commit f46ff8333e
66 changed files with 2257 additions and 676 deletions

View File

@@ -2,6 +2,7 @@ package db
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
)
@@ -15,10 +16,10 @@ type Ownership struct {
}
func (o Ownership) Query(data *InsertId[string]) {
data.query = `
data.query = fmt.Sprintf(`
INSERT INTO ownership (%s, player, trusted, assistant)
VALUES ($1, $2, $3, $4)
RETURNING id`
RETURNING id`, data.fieldName)
}
func (o Ownership) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
@@ -28,7 +29,7 @@ func (o Ownership) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
args := []any{data.id, o.Player, o.Trusted, o.Assistant}
err := tx.QueryRow(data.query, args...).Scan(&o.ID)
err := tx.QueryRowx(data.query, args...).Scan(&o.ID)
if err != nil {
return err
}
@@ -43,7 +44,7 @@ func (o Ownership) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[st
args := []any{data.id, o.Player, o.Trusted, o.Assistant}
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&o.ID)
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&o.ID)
if err != nil {
return err
}
@@ -57,3 +58,40 @@ type OwnershipString struct {
Key string
Value int
}
func (o OwnershipString) Query(data *InsertId[string]) {
data.query = fmt.Sprintf(`
INSERT INTO ownership_string (%s, key_, value)
VALUES ($1, $2, $3)
RETURNING id`, data.fieldName)
}
func (o OwnershipString) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, o.Key, o.Value}
err := tx.QueryRowx(data.query, args...).Scan(&o.ID)
if err != nil {
return err
}
return nil
}
func (o OwnershipString) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, o.Key, o.Value}
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&o.ID)
if err != nil {
return err
}
return nil
}