db

package
v0.6.32 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: Apache-2.0 Imports: 15 Imported by: 1 module, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package db offers client struct and functions to interact with database connection. It provides encrypting, decrypting, and a way to reset the database.

Index

Constants

View Source
const (
	// InformerObjectCacheDBPath is where SQLite's object database file will be stored relative to process running steve
	// It's given in two parts because the root is used as the suffix for the tempfile, and then we'll add a ".db" after it.
	// In non-test mode, we can append the ".db" extension right here.
	InformerObjectCacheDBPathRoot = "informer_object_cache"
	InformerObjectCacheDBPath     = InformerObjectCacheDBPathRoot + ".db"
)

Variables

This section is empty.

Functions

func Sanitize

func Sanitize(s string) string

Sanitize returns a string that can be used in SQL as a name

Types

type Client

type Client interface {
	WithTransaction(ctx context.Context, forWriting bool, f WithTransactionFunction) error
	Prepare(stmt string) *sql.Stmt
	QueryForRows(ctx context.Context, stmt transaction.Stmt, params ...any) (*sql.Rows, error)
	ReadObjects(rows Rows, typ reflect.Type, shouldDecrypt bool) ([]any, error)
	ReadStrings(rows Rows) ([]string, error)
	ReadStrings2(rows Rows) ([][]string, error)
	ReadInt(rows Rows) (int, error)
	Upsert(tx transaction.Client, stmt *sql.Stmt, key string, obj any, shouldEncrypt bool) error
	CloseStmt(closable Closable) error
	NewConnection(isTemp bool) (string, error)
	Encryptor() Encryptor
	Decryptor() Decryptor
}

Client defines a database client that provides encrypting, decrypting, and database resetting

func NewClient

func NewClient(c Connection, encryptor Encryptor, decryptor Decryptor, useTempDir bool) (Client, string, error)

NewClient returns a client and the path to the database. If the given connection is nil then a default one will be created.

type Closable

type Closable interface {
	Close() error
}

Closable Closes an underlying connection and returns an error on failure.

type Connection

type Connection interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
	Exec(query string, args ...any) (sql.Result, error)
	Prepare(query string) (*sql.Stmt, error)
	Close() error
}

Connection represents a connection pool.

type Decryptor

type Decryptor interface {
	// Decrypt accepts a chunk of encrypted data, the nonce used to encrypt it and the ID of the used key (as it rotates). It returns the decrypted data or an error.
	Decrypt([]byte, []byte, uint32) ([]byte, error)
}

Decryptor decrypts data previously encrypted by Encryptor.

type Encryptor

type Encryptor interface {
	// Encrypt encrypts the specified data, returning: the encrypted data, the nonce used to encrypt the data, and an ID identifying the key that was used (as it rotates). On failure error is returned instead.
	Encrypt([]byte) ([]byte, []byte, uint32, error)
}

Encryptor encrypts data with a key which is rotated to avoid wear-out.

type QueryError

type QueryError struct {
	QueryString string
	Err         error
}

QueryError encapsulates an error while executing a query

func (*QueryError) Error

func (e *QueryError) Error() string

Error returns a string representation of this QueryError

func (*QueryError) Unwrap

func (e *QueryError) Unwrap() error

Unwrap returns the underlying error

type Rows

type Rows interface {
	Next() bool
	Err() error
	Close() error
	Scan(dest ...any) error
}

Rows represents sql rows. It exposes method to navigate the rows, read their outputs, and close them.

type WithTransactionFunction added in v0.5.7

type WithTransactionFunction func(tx transaction.Client) error

WithTransactionFunction is a function that uses a transaction

Directories

Path Synopsis
Package transaction provides mockable interfaces of sql package struct types.
Package transaction provides mockable interfaces of sql package struct types.

Jump to

Keyboard shortcuts

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