hiling_go/db/sqlc/store.go

56 lines
1.8 KiB
Go

package db
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
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)
UpdateUser(ctx context.Context, arg UpdateUserParams) (UpdateUserRow, error)
GetUserStats(ctx context.Context, user_id int32) (GetUserStatsRow, 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
GetNewsEventsList(ctx context.Context, arg GetNewsEventsListParams) ([]NewsEventRow, error)
GetListRecentLocationsWithRatings(ctx context.Context, arg GetListRecentLocationsParams) ([]GetListRecentLocationsWithRatingsRow, error)
}
type SQLStore struct {
*Queries
pool *pgxpool.Pool
}
func NewStore(pool *pgxpool.Pool) Store {
return &SQLStore{
pool: pool,
Queries: New(pool),
}
}
// TRANSACTION QUERY FUNCTION
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error {
tx, err := store.pool.Begin(ctx)
if err != nil {
return err
}
q := New(tx)
err = fn(q)
if err != nil {
if rbErr := tx.Rollback(ctx); rbErr != nil {
return fmt.Errorf("tx err: %v, rb err : %v", err, rbErr)
}
return err
}
return tx.Commit(ctx)
}