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 Grid struct {
|
||||
ID uint
|
||||
|
||||
@@ -13,3 +19,43 @@ type Grid struct {
|
||||
Units string
|
||||
Style string
|
||||
}
|
||||
|
||||
func (g *Grid) Query(data *InsertId[string]) {
|
||||
data.query = `
|
||||
INSERT INTO grid (system_id, type, size, distance, diagonals, thickness
|
||||
alpha, color, units, style)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING id`
|
||||
}
|
||||
|
||||
func (g *Grid) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, g.Type, g.Size, g.Distance, g.Diagonals, g.Thickness,
|
||||
g.Alpha, g.Color, g.Units, g.Style}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&g.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Grid) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, g.Type, g.Size, g.Distance, g.Diagonals, g.Thickness,
|
||||
g.Alpha, g.Color, g.Units, g.Style}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&g.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user