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 BuildQueryWithPlaceholders(queryTemplate string, placeholdersPerRow int, numRows int) string
- func DeferForeignKeyChecks(tx TxQuerier) error
- func GetString(ctx context.Context, tx TxQuerier, ids ...int64) ([]string, error)
- func GetStringID(ctx context.Context, tx TxQuerier, values ...string) ([]sql.NullInt64, error)
- func InternEmptyString(ctx context.Context, tx TxQuerier) (int64, 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 BuildQueryWithPlaceholders ¶ added in v1.8.0
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
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
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 InternEmptyString ¶ added in v1.9.0
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
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.