hiling_go/db/sqlc/store.go

54 lines
1.5 KiB
Go
Raw Normal View History

2023-09-08 22:24:58 +07:00
package db
import (
2023-09-17 16:32:48 +07:00
"context"
2023-09-08 22:24:58 +07:00
"database/sql"
2023-10-03 19:44:31 +07:00
"fmt"
2023-09-14 13:49:03 +07:00
"github.com/yiplee/sqlc"
2023-09-08 22:24:58 +07:00
)
type Store interface {
Querier
2023-09-17 16:32:48 +07:00
GetTopListLocations(ctx context.Context, arg GetTopListLocationsParams) ([]GetTopListLocationsRow, error)
2023-09-19 21:49:48 +07:00
GetImagesByLocation(ctx context.Context, arg GetImagesByLocationParams) ([]GetImagesByLocationRow, error)
GetLocation(ctx context.Context, location_id int32) (GetLocationRow, error)
2023-09-23 15:24:21 +07:00
GetUser(ctx context.Context, username string) (GetUserRow, error)
2023-10-04 19:51:29 +07:00
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)
2023-10-03 14:56:57 +07:00
CreateLocation(ctx context.Context, arg CreateLocationParams) (int32, error)
CreateImage(ctx context.Context, arg []CreateImageParams) error
2023-10-03 19:44:31 +07:00
CreateLocationTx(ctx context.Context, arg CreateLocationTxParams) error
2023-09-08 22:24:58 +07:00
}
type SQLStore struct {
*Queries
db *sql.DB
}
func NewStore(db *sql.DB) Store {
return &SQLStore{
db: db,
2023-09-14 13:49:03 +07:00
Queries: New(sqlc.Wrap(db)),
2023-09-08 22:24:58 +07:00
}
}
// TRANSACTION QUERY FUNCTION
2023-10-03 19:44:31 +07:00
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error {
tx, err := store.db.BeginTx(ctx, nil)
if err != nil {
return err
}
2023-09-08 22:24:58 +07:00
2023-10-03 19:44:31 +07:00
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()
}