Documentation
¶
Overview ¶
Package dbinterface provides database interfaces to avoid import cycles. This package has no dependencies and can be imported by both database implementations and models/stores.
Index ¶
- func GetString(ctx context.Context, tx TxQuerier, ids ...int64) ([]string, error)
- func GetStringID(ctx context.Context, tx TxQuerier, values ...string) ([]sql.NullInt64, error)
- func InternStringNullable(ctx context.Context, tx TxQuerier, values ...*string) ([]sql.NullInt64, error)
- func InternStrings(ctx context.Context, tx TxQuerier, values ...string) ([]int64, error)
- type Querier
- type TxQuerier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetString ¶ added in v1.7.0
GetString retrieves one or more string values from the string_pool by their IDs. This is designed for use within transactions. Returns strings in the same order as the input IDs.
func GetStringID ¶ added in v1.7.0
GetStringID retrieves the IDs of string values from the string_pool without creating them. Returns sql.NullInt64{Valid: false} for strings that do not exist. This is designed for use within transactions. For multiple strings, uses batch operations for optimal performance.
func InternStringNullable ¶ added in v1.7.0
func InternStringNullable(ctx context.Context, tx TxQuerier, values ...*string) ([]sql.NullInt64, error)
InternStringNullable interns one or more optional string values and returns their IDs as sql.NullInt64. Returns sql.NullInt64{Valid: false} for any value pointer that is nil or points to an empty string. This is designed for use within transactions.
Performance: For a single string, uses a fast-path. For multiple strings, collects non-empty values and delegates to InternStrings for efficient batch processing.
func InternStrings ¶ added in v1.7.0
InternStrings interns one or more string values efficiently and returns their IDs. This is designed for use within transactions. All values are required (non-empty). Returns error if any value is empty.
Performance: Uses INSERT + SELECT instead of INSERT...RETURNING for massive speedup. RETURNING causes expensive B-tree traversals. For 180k torrents, this optimization provides 5-10x faster string interning by separating insert from ID retrieval. For multiple strings, uses batch operations with deduplication for optimal performance.
Types ¶
type Querier ¶
type Querier interface {
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
BeginTx(ctx context.Context, opts *sql.TxOptions) (TxQuerier, error)
}
Querier is the centralized interface for database operations. It is implemented by *database.DB and provides all database capabilities including queries, transactions, and string interning.
type TxQuerier ¶ added in v1.7.0
type TxQuerier interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
Commit() error
Rollback() error
}
TxQuerier is the interface for database transaction operations. It is implemented by *database.Tx and provides transaction-specific query methods with prepared statement caching, plus transaction control methods.