moxylib

package module
v0.0.0-...-28c721f Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: MIT Imports: 32 Imported by: 3

README

MoxyLib

This is a small *sql.DB SQLite wrapper to provide extra functionality. The focus of the lib is to:

  • Utilizes Go github.com/ncruces/go-sqlite3 as the default, but can accept other drivers.
  • Take SQLite commands as text and get an output that is renderable to HTML.
  • Transaction wrapper and Up only automatic migration system using PRAGMA user_version.
  • Store and retrieve settings/config in SQLite using a Key/Value table.
  • Key/Value storage functions for Value, Set, List, Hash.
Key/Value methods

Note: all values are saved as strings and use '\n' character as the separator. This makes any method other than the simple KVV not suitable for storage of data containing '\n' characters unless the data is Base64 encoded first.

  • KVV: Basic Key Value methods. Everything in the Value column is a string and will be retrieved and set as such.
  • KVH: Hash Values with fields. Field and Value is separated by '' and only splits on first occurence of separator.
  • KVL: List Values allows for repeats and has push pop functionality.
  • KVS: Sets are lists except they don't allow for duplicates and is sorted.

Special Thanks to the Developers of:

go-sqlite3 - https://github.com/ncruces/go-sqlite3
form,mold,validator - https://github.com/go-playground
argon2id - https://github.com/alexedwards/argon2id
zstd - https://github.com/klauspost/compress

And of course the Go Standard Lib.

sqlite sqlite

Documentation

Index

Constants

View Source
const (
	URIOptimizations = "" /* 199-byte string literal not displayed */
	BackupPath       = "sqlite/backup"
)

URIOptimizations for other SQLite3 drivers.

Variables

View Source
var (
	LimiterBanCount int
	LimiterBurst    int
	LimiterRate     int
)
View Source
var (
	DB        *MDB                               // DB is the global database connection.
	EmailAuth smtp.Auth                          // EmailAuth is the SMTP authentication.
	Settings  = make(map[string]string)          // Settings is a map of key-value pairs from the setting table.
	KVSetting = KVNew("setting", "Key", "Value") // KVSetting is the key-value setting table.
)

Functions

func BasicHash

func BasicHash(plain ...[]byte) string

Basic Hash

func CipherDecrypt

func CipherDecrypt(encrypted []byte) ([]byte, error)

Decrypts and decompresses data.

func CipherEncrypt

func CipherEncrypt(plain []byte) (string, error)

Compresses and encrypts data.

func Close

func Close(err error)

Closes the database connection and logs any errors.

func DeleteFile

func DeleteFile(path, name string) error

Delete a file from the filesystem.

func ExecSQLite

func ExecSQLite(cmd string) [][]string

execSQLite executes a given SQL command and returns the results in a 2D string slice. The result slice contains information about the execution, such as errors, rows affected, and last insert ID.

func FilterSQL

func FilterSQL(sql string) []string

Remove leading/trailing whitespace/comments and split the SQL string into individual commands.

func GetUserVersion

func GetUserVersion() (int, error)

Retrieves the user_version PRAGMA from the database and returns it as an integer. The user_version is used to track the current version of the database schema.

func GoRangeWait

func GoRangeWait[E any](list []E, gofunc func(int, E) error) []error

GoRangeWait is a concurrent function that waits at the end. It will run the gofunc for each element in the list concurrently. This is suitable for mutexed operations in gofunc.

func LimitersDelete

func LimitersDelete(ip string)

func LimitersLoadStore

func LimitersLoadStore(ip string) *bLimiter

func LimitersStore

func LimitersStore(ip string, ban bool)

func Migrate

func Migrate(path string) error

Uses ReadMigrate to read the commands from the SQL file and applies them to the database.

func NewLimiter

func NewLimiter(ban bool) *bLimiter

func PasswordHash

func PasswordHash(key string) (string, error)

Hard to crack password hashing with Argon2id.

func PasswordVerify

func PasswordVerify(key, hash string) (bool, *argon2id.Params, error)

PasswordVerify checks if a key matches a hash.

func QuerySQLite

func QuerySQLite(cmd string, noCalc bool) [][]string

Executes a given SQL query and returns the results in a 2D string slice. The result slice contains the query results, including column headers and data rows. If noCalc is true, the column headers will not include the database type.

func RandomString

func RandomString(length int) string

RandomString generates a random string of a given length.

func ReadMigrations

func ReadMigrations(path string) ([]string, error)

Reads a SQL file containing multiple migration commands and returns a map of versioned SQL strings.

func RowsScan

func RowsScan(rows *sql.Rows, dest any) error

Takes SQL rows and scans them into a slice of structs.

func RunSQLite

func RunSQLite(sql string) [][][]string

Take any string and attempt to execute it as a SQLite command. Returns a 3D array of strings with the result of the command.

func SendHTMLEmail

func SendHTMLEmail(subject, html string, to ...string) error

SendEmail sends an email using the provided recipient with HTML content.

func Setup

func Setup(newDB *sql.DB) error

Connects to the SQLite3 database and sets the global DB variable. nil defaults to ncruces SQLite3 driver. Also initializes the crypto variables and loads the settings table.

func SplitIntoMap

func SplitIntoMap(s, sep string) map[string]string

Helper function to split a string into a map of key-value pairs.

func StructMap

func StructMap(v any) map[string]string

StructValues converts a struct to a slice of string values.

func Transaction

func Transaction(execs ...string) error

Executes a series of SQL commands within a single transaction.

func Transform

func Transform(v any) error

Post parser for form data.

func UpdateSettings

func UpdateSettings(confmap map[string]string) error

Updates the settings table with the given key-value pairs. Update Settings map with the new values.

func ValidateBool

func ValidateBool(setting string) error

ValidateBool checks if a string is a boolean.

func ValidateInt

func ValidateInt(setting string) error

ValidateInt checks if a string is an integer.

Types

type Ban

type Ban struct {
	IP     string `mold:"trim" validate:"required,ipv4"`
	Reason string `mold:"trim" validate:"required"`
}

type FileInfo

type FileInfo struct {
	Name    string
	Path    string
	Size    int64
	Created int64
}

func CreateBackup

func CreateBackup(path, name string) (*FileInfo, error)

Backup Sqlite3 database to a file.

func GetFile

func GetFile(path, name string) (*FileInfo, error)

Get a file from the filesystem.

func ListFiles

func ListFiles(path string) ([]*FileInfo, error)

List files in a directory.

type KVTable

type KVTable struct {
	Table string
	CK    string
	CV    string
}

func KVNew

func KVNew(table, key, value string) *KVTable

Create a KVTable struct

func (*KVTable) DEL

func (t *KVTable) DEL(key string) error

DELETES ROW FROM TABLE. Only this function will delete the row from the table. All other functions will empty the value field.

func (*KVTable) GET

func (t *KVTable) GET(key string) (string, error)

Get Value from a given table and key.

func (*KVTable) GETALL

func (t *KVTable) GETALL() (map[string]string, error)

Retrieves all key-value pairs from a given table and returns them in a map. OPTIONS: ASC, DESC, or blank for no order.

func (*KVTable) GETALLSTRUCT

func (t *KVTable) GETALLSTRUCT(order string, v any) error

Retrieves all key-value pairs from a given table and returns them in the provided STRUCT. OPTIONS: ASC, DESC, or blank for no order.

func (*KVTable) HDEL

func (t *KVTable) HDEL(key string, fields ...string) error

Delete fields from a hash in a given table.

func (*KVTable) HGET

func (t *KVTable) HGET(key string, fields ...string) (map[string]string, error)

Retrieves the values of fields from a hash in a given table. If no fields are specified, all fields are returned.

func (*KVTable) HSET

func (t *KVTable) HSET(key string, kvs map[string]string) error

Sets the values of fields in a hash in a given table.

func (*KVTable) LEXISTS

func (t *KVTable) LEXISTS(key, value string) (bool, error)

Checks if a field exists in a set/list in a given table.

func (*KVTable) LGET

func (t *KVTable) LGET(key string) ([]string, error)

Get list from a given table and key.

func (*KVTable) LPOP

func (t *KVTable) LPOP(key string, end bool) (string, error)

Retrieve and remove the first or last element from a list in a given table.

func (*KVTable) LPUSH

func (t *KVTable) LPUSH(key, field string, end bool) error

Add a field to the beginning or end of a list in a given table.

func (*KVTable) SDEL

func (t *KVTable) SDEL(key string, values ...string) error

Deletes fields from a set in a given table.

func (*KVTable) SET

func (t *KVTable) SET(kvs map[string]string) error

Updates the table with the given key-value pairs. If map is nil then do nothing.

func (*KVTable) SSET

func (t *KVTable) SSET(key string, values ...string) error

Sets values in a set in a given table.

type LimiterStat

type LimiterStat struct {
	IP         string
	Requests   int
	Violations int
	Current    int64
}

func LimitersStats

func LimitersStats(ignore string) []LimiterStat

type MDB

type MDB struct{ *sql.DB }

Jump to

Keyboard shortcuts

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