finish setup insert method, refactor sql migration file
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type Ownership struct {
|
||||
ID uint
|
||||
|
||||
@@ -8,6 +14,43 @@ type Ownership struct {
|
||||
Assistant string
|
||||
}
|
||||
|
||||
func (o Ownership) Query(data *InsertId[string]) {
|
||||
data.query = `
|
||||
INSERT INTO ownership (%s, player, trusted, assistant)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
}
|
||||
|
||||
func (o Ownership) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, o.Player, o.Trusted, o.Assistant}
|
||||
|
||||
err := tx.QueryRow(data.query, args...).Scan(&o.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o Ownership) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, o.Player, o.Trusted, o.Assistant}
|
||||
|
||||
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&o.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type OwnershipString struct {
|
||||
ID uint
|
||||
|
||||
|
||||
Reference in New Issue
Block a user