finish setup insert method, refactor sql migration file

This commit is contained in:
lbenedar
2026-04-17 17:33:21 +03:00
parent 1b7c7e9ae3
commit 21abe68858
47 changed files with 2054 additions and 1056 deletions

View File

@@ -1,7 +1,51 @@
package db
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
)
type Style struct {
ID uint
Src string
}
func (s *Style) Query(data *InsertId[string]) {
data.query = fmt.Sprintf(`
INSERT INTO style (%s, src)
VALUES ($1, $2)
RETURNING id`, data.fieldName)
}
func (s *Style) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, s.Src}
err := tx.QueryRow(data.query, args...).Scan(&s.ID)
if err != nil {
return err
}
return nil
}
func (s *Style) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, s.Src}
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&s.ID)
if err != nil {
return err
}
return nil
}