-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,090 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jackc/pgconn" | ||
"github.com/jackc/pgx/v4" | ||
) | ||
|
||
// Handler - функция, которая выполняется в транзакции | ||
type Handler func(ctx context.Context) error | ||
|
||
// Client клиент для работы с БД | ||
type Client interface { | ||
DB() DB | ||
Close() error | ||
} | ||
|
||
// TxManager менеджер транзакций, который выполняет указанный пользователем обработчик в транзакции | ||
type TxManager interface { | ||
ReadCommitted(ctx context.Context, f Handler) error | ||
} | ||
|
||
// Query обертка над запросом, хранящая имя запроса и сам запрос | ||
// Имя запроса используется для логирования и потенциально может использоваться еще где-то, например, для трейсинга | ||
type Query struct { | ||
Name string | ||
QueryRaw string | ||
} | ||
|
||
// Transactor интерфейс для работы с транзакциями | ||
type Transactor interface { | ||
BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) | ||
} | ||
|
||
// SQLExecer комбинирует NamedExecer и QueryExecer | ||
type SQLExecer interface { | ||
NamedExecer | ||
QueryExecer | ||
} | ||
|
||
// NamedExecer интерфейс для работы с именованными запросами с помощью тегов в структурах | ||
type NamedExecer interface { | ||
ScanOneContext(ctx context.Context, dest interface{}, q Query, args ...interface{}) error | ||
ScanAllContext(ctx context.Context, dest interface{}, q Query, args ...interface{}) error | ||
} | ||
|
||
// QueryExecer интерфейс для работы с обычными запросами | ||
type QueryExecer interface { | ||
ExecContext(ctx context.Context, q Query, args ...interface{}) (pgconn.CommandTag, error) | ||
QueryContext(ctx context.Context, q Query, args ...interface{}) (pgx.Rows, error) | ||
QueryRowContext(ctx context.Context, q Query, args ...interface{}) pgx.Row | ||
} | ||
|
||
// Pinger интерфейс для проверки соединения с БД | ||
type Pinger interface { | ||
Ping(ctx context.Context) error | ||
} | ||
|
||
// DB интерфейс для работы с БД | ||
type DB interface { | ||
SQLExecer | ||
Transactor | ||
Pinger | ||
Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package db | ||
|
||
//go:generate sh -c "rm -rf mocks && mkdir -p mocks" | ||
//go:generate minimock -i TxManager -o ./mocks/ -s "_minimock.go" |
Oops, something went wrong.