finish setup insert method, refactor sql migration file
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type Compatibility struct {
|
||||
ID uint
|
||||
|
||||
@@ -7,3 +14,40 @@ type Compatibility struct {
|
||||
Verified string
|
||||
Maximum string
|
||||
}
|
||||
|
||||
func (c Compatibility) Query(data *InsertId[string]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO compatibility (%s, minimum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (c Compatibility) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, c.Minimum, c.Verified, c.Maximum}
|
||||
|
||||
err := tx.QueryRow(data.query, args...).Scan(&c.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Compatibility) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, c.Minimum, c.Verified, c.Maximum}
|
||||
|
||||
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&c.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user