dbinterface

package
v1.13.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2026 License: GPL-2.0 Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildQueryWithPlaceholders added in v1.8.0

func BuildQueryWithPlaceholders(queryTemplate string, placeholdersPerRow int, numRows int) string

BuildQueryWithPlaceholders builds a SQL query string with repeated placeholders queryTemplate should contain %s where the placeholders will be inserted placeholdersPerRow is the number of ? per row (e.g., 12 for file inserts) numRows is how many rows to repeat the placeholders for

func DeferForeignKeyChecks added in v1.8.0

func DeferForeignKeyChecks(tx TxQuerier) error

DeferForeignKeyChecks defers foreign key constraint checks for the given transaction until the end of the transaction. This allows operations that would normally violate foreign key constraints due to ordering.

func GetString added in v1.7.0

func GetString(ctx context.Context, tx TxQuerier, ids ...int64) ([]string, error)

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

func GetStringID(ctx context.Context, tx TxQuerier, values ...string) ([]sql.NullInt64, error)

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 InternEmptyString added in v1.9.0

func InternEmptyString(ctx context.Context, tx TxQuerier) (int64, error)

InternEmptyString ensures the empty string exists in string_pool and returns its ID. This is needed for special cases like localhost bypass auth where an empty username is a valid, intentional value (not NULL). The empty string is created if it doesn't exist.

Unlike InternStrings which rejects empty strings (treating them as invalid input), this function specifically handles the case where empty string is a meaningful value.

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

func InternStrings(ctx context.Context, tx TxQuerier, values ...string) ([]int64, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL