2025-04-29 06:03:47 +02:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2025-05-08 19:01:55 +02:00
|
|
|
// Pool is a connection pool. e.g. pgxpool
|
2025-04-29 06:03:47 +02:00
|
|
|
type Pool interface {
|
|
|
|
Beginner
|
|
|
|
QueryExecutor
|
2025-05-26 09:31:45 +02:00
|
|
|
Migrator
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
Acquire(ctx context.Context) (Client, error)
|
|
|
|
Close(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
2025-06-17 09:46:01 +02:00
|
|
|
type PoolTest interface {
|
|
|
|
Pool
|
|
|
|
// MigrateTest is the same as [Migrator] but executes the migrations multiple times instead of only once.
|
|
|
|
MigrateTest(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
2025-05-08 19:01:55 +02:00
|
|
|
// Client is a single database connection which can be released back to the pool.
|
2025-04-29 06:03:47 +02:00
|
|
|
type Client interface {
|
|
|
|
Beginner
|
|
|
|
QueryExecutor
|
2025-05-26 09:31:45 +02:00
|
|
|
Migrator
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
Release(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
2025-05-08 19:01:55 +02:00
|
|
|
// Querier is a database client that can execute queries and return rows.
|
2025-04-29 06:03:47 +02:00
|
|
|
type Querier interface {
|
|
|
|
Query(ctx context.Context, stmt string, args ...any) (Rows, error)
|
|
|
|
QueryRow(ctx context.Context, stmt string, args ...any) Row
|
|
|
|
}
|
|
|
|
|
2025-05-08 19:01:55 +02:00
|
|
|
// Executor is a database client that can execute statements.
|
2025-06-17 09:46:01 +02:00
|
|
|
// It returns the number of rows affected or an error
|
2025-04-29 06:03:47 +02:00
|
|
|
type Executor interface {
|
2025-06-17 09:46:01 +02:00
|
|
|
Exec(ctx context.Context, stmt string, args ...any) (int64, error)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-08 19:01:55 +02:00
|
|
|
// QueryExecutor is a database client that can execute queries and statements.
|
2025-04-29 06:03:47 +02:00
|
|
|
type QueryExecutor interface {
|
|
|
|
Querier
|
|
|
|
Executor
|
|
|
|
}
|
|
|
|
|
2025-05-08 19:01:55 +02:00
|
|
|
// Scanner scans a single row of data into the destination.
|
2025-04-29 06:03:47 +02:00
|
|
|
type Scanner interface {
|
|
|
|
Scan(dest ...any) error
|
|
|
|
}
|
|
|
|
|
2025-05-08 19:01:55 +02:00
|
|
|
// Row is an abstraction of sql.Row.
|
2025-04-29 06:03:47 +02:00
|
|
|
type Row interface {
|
|
|
|
Scanner
|
|
|
|
}
|
|
|
|
|
2025-05-08 19:01:55 +02:00
|
|
|
// Rows is an abstraction of sql.Rows.
|
2025-04-29 06:03:47 +02:00
|
|
|
type Rows interface {
|
2025-07-14 21:27:14 +02:00
|
|
|
Scanner
|
2025-04-29 06:03:47 +02:00
|
|
|
Next() bool
|
|
|
|
Close() error
|
|
|
|
Err() error
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
|
|
|
|
type CollectableRows interface {
|
|
|
|
// Collect collects all rows and scans them into dest.
|
|
|
|
// dest must be a pointer to a slice of pointer to structs
|
|
|
|
// e.g. *[]*MyStruct
|
|
|
|
// Rows are closed after this call.
|
|
|
|
Collect(dest any) error
|
|
|
|
// CollectFirst collects the first row and scans it into dest.
|
|
|
|
// dest must be a pointer to a struct
|
|
|
|
// e.g. *MyStruct{}
|
|
|
|
// Rows are closed after this call.
|
|
|
|
CollectFirst(dest any) error
|
|
|
|
// CollectExactlyOneRow collects exactly one row and scans it into dest.
|
|
|
|
// e.g. *MyStruct{}
|
|
|
|
// Rows are closed after this call.
|
|
|
|
CollectExactlyOneRow(dest any) error
|
|
|
|
}
|