hiling_go/db/sqlc/store.go

53 lines
1.4 KiB
Go

package db
import (
"context"
"database/sql"
"fmt"
"github.com/yiplee/sqlc"
)
type Store interface {
Querier
GetTopListLocations(ctx context.Context, arg GetTopListLocationsParams) ([]GetTopListLocationsRow, error)
GetImagesByLocation(ctx context.Context, arg GetImagesByLocationParams) ([]GetImagesByLocationRow, error)
GetLocation(ctx context.Context, location_id int32) (GetLocationRow, error)
GetUser(ctx context.Context, username string) (GetUserRow, error)
CreateReview(ctx context.Context, arg CreateReviewParams) (Review, error)
GetListLocationReviews(ctx context.Context, arg GetListLocationReviewsParams) ([]GetListLocationReviewsRow, error)
CreateLocation(ctx context.Context, arg CreateLocationParams) (int32, error)
CreateImage(ctx context.Context, arg []CreateImageParams) error
CreateLocationTx(ctx context.Context, arg CreateLocationTxParams) error
}
type SQLStore struct {
*Queries
db *sql.DB
}
func NewStore(db *sql.DB) Store {
return &SQLStore{
db: db,
Queries: New(sqlc.Wrap(db)),
}
}
// TRANSACTION QUERY FUNCTION
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error {
tx, err := store.db.BeginTx(ctx, nil)
if err != nil {
return err
}
q := New(tx)
err = fn(q)
if err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
return fmt.Errorf("tx err: %v, rb err : %v", err, rbErr)
}
return err
}
return tx.Commit()
}