db

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	ReaderDb *sqlx.DB // Database connection for read operations
	// contains filtered or unexported fields
}

Database manages SQLite database connections with WAL mode and connection pooling. It provides both reader and writer connections with mutex protection for write operations and embedded schema migration support using goose.

func NewDatabase

func NewDatabase(config *SqliteDatabaseConfig, logger logrus.FieldLogger) *Database

NewDatabase creates a new Database instance with the specified configuration and logger. The database connections are not initialized until Init() is called.

func (*Database) ApplyEmbeddedDbSchema

func (d *Database) ApplyEmbeddedDbSchema(version int64) error

ApplyEmbeddedDbSchema applies database schema migrations using embedded SQL files. Supports different migration strategies: -2 (all), -1 (one up), or specific version. Uses goose migration library with allowMissing option for flexible schema management.

func (*Database) Close

func (d *Database) Close() error

Close closes the database writer connection. Should be called during application shutdown to ensure proper cleanup.

func (*Database) DeleteSpammer

func (d *Database) DeleteSpammer(tx *sqlx.Tx, id int64) error

DeleteSpammer removes a spammer record from the database within a transaction. Permanently deletes the spammer and all associated data. Returns an error if the deletion fails or the spammer doesn't exist.

func (*Database) GetSpammer

func (d *Database) GetSpammer(id int64) (*Spammer, error)

GetSpammer retrieves a single spammer by ID from the database. Returns the spammer entity or an error if not found or database access fails.

func (*Database) GetSpammers

func (d *Database) GetSpammers() ([]*Spammer, error)

GetSpammers retrieves all spammers from the database ordered by creation time (newest first). Returns a slice of spammer entities or an error if database access fails.

func (*Database) GetSpamoorState

func (d *Database) GetSpamoorState(key string, returnValue interface{}) (interface{}, error)

GetSpamoorState retrieves and deserializes a state value by key. The returnValue parameter should be a pointer to the target type for unmarshaling. Returns the deserialized value or an error if the key doesn't exist or JSON parsing fails.

func (*Database) Init

func (d *Database) Init() error

Init initializes the database connections with WAL mode and connection pooling. Sets default connection limits (50 max open, 10 max idle) if not specified. Enables WAL mode for better concurrent access and configures connection timeouts.

func (*Database) InsertSpammer

func (d *Database) InsertSpammer(tx *sqlx.Tx, spammer *Spammer) error

InsertSpammer creates a new spammer record in the database within a transaction. Updates the spammer's ID field with the generated database ID after insertion. Returns an error if the insertion fails or transaction is invalid.

func (*Database) RunDBTransaction

func (d *Database) RunDBTransaction(handler func(tx *sqlx.Tx) error) error

RunDBTransaction executes a function within a database transaction with automatic rollback. The transaction is protected by a mutex to ensure sequential write operations. Automatically rolls back on error and commits on success.

func (*Database) SetSpamoorState

func (d *Database) SetSpamoorState(tx *sqlx.Tx, key string, value interface{}) error

SetSpamoorState stores a state value by key with JSON serialization. If tx is nil, creates and manages its own transaction automatically. Uses INSERT OR REPLACE to handle both new entries and updates atomically. Returns an error if JSON marshaling or database operation fails.

func (*Database) UpdateSpammer

func (d *Database) UpdateSpammer(tx *sqlx.Tx, spammer *Spammer) error

UpdateSpammer modifies an existing spammer record in the database within a transaction. Updates all mutable fields: name, description, config, status, and state. The ID and creation timestamp remain unchanged.

type Spammer

type Spammer struct {
	ID          int64  `db:"id"`          // Unique identifier for the spammer instance
	Scenario    string `db:"scenario"`    // Name of the scenario to execute
	Name        string `db:"name"`        // Human-readable name for the spammer
	Description string `db:"description"` // Detailed description of what this spammer does
	Config      string `db:"config"`      // YAML configuration for scenario and wallet settings
	Status      int    `db:"status"`      // Current execution status (see SpammerStatus constants)
	CreatedAt   int64  `db:"created_at"`  // Unix timestamp when the spammer was created
	State       string `db:"state"`       // Persistent state data for scenario execution
}

Spammer represents a database entity for storing spammer configuration and state. Maps to the "spammers" table with fields for scenario definition, execution status, and persistent state management. The status field tracks execution state using SpammerStatus constants.

type SpamoorState

type SpamoorState struct {
	Key   string `db:"key"`   // Unique identifier for the state entry
	Value string `db:"value"` // JSON-serialized value for the state data
}

SpamoorState represents a key-value pair for storing application-wide configuration state. Maps to the "spamoor_state" table for persisting settings like scenario counters, first launch flags, and other global application state that survives restarts.

type SqliteDatabaseConfig

type SqliteDatabaseConfig struct {
	File         string `yaml:"file"`         // Database file path
	MaxOpenConns int    `yaml:"maxOpenConns"` // Maximum number of open connections to the database
	MaxIdleConns int    `yaml:"maxIdleConns"` // Maximum number of idle connections in the pool
}

SqliteDatabaseConfig defines the configuration for SQLite database connections. It specifies the database file path and connection pool limits for managing concurrent database access efficiently.

Jump to

Keyboard shortcuts

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