sqlstore

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package sqlstore is the database/sql ledgo.Store shared by the SQL backends. Each backend supplies a Dialect and its schema.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect interface {
	// Placeholder is the bind parameter for the nth argument, from 1: "$1",
	// ":1" or "?".
	Placeholder(n int) string
	// Number converts the decimal bound to param, which shopspring/decimal
	// sends as text with a '.' separator, to an exact number.
	Number(param string) string
	// Text formats a numeric expression so it scans into a decimal.Decimal
	// exactly, never through float64.
	Text(expr string) string
	// IgnoreDuplicate rewrites a single-row INSERT so that a duplicate key
	// inserts nothing, for databases where the error would abort the whole
	// transaction.
	IgnoreDuplicate(insert string) string
	// IsDuplicate reports whether err is a unique key violation. The database
	// must have rolled back only the failed statement, so the transaction
	// stays usable.
	IsDuplicate(err error) bool
	// TxOptions starts the transactions that Store.Commit opens itself. They
	// must run at READ COMMITTED: nil if that is the database's default.
	TxOptions() *sql.TxOptions
	// AlreadyExists reports whether a Migrate statement failed because its
	// table or index already exists.
	AlreadyExists(err error) bool
}

Dialect is what differs between the SQL databases. Queries are written with "?" placeholders, which Store rewrites with Placeholder.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store keeps accounts and transactions in a SQL database.

Commits lock only the accounts that have a balance limit, always in ID order, so concurrent commits cannot deadlock on READ COMMITTED and need no retry loop.

func InTx

func InTx(tx *sql.Tx, dialect Dialect) *Store

InTx returns a Store that writes into tx, so postings commit or roll back together with the caller's own changes.

func New

func New(db *sql.DB, dialect Dialect) *Store

New returns a Store on db. Each Commit runs in its own READ COMMITTED transaction, started with the dialect's TxOptions.

func (*Store) Balance

func (s *Store) Balance(ctx context.Context, account string) (decimal.Decimal, error)

Balance is the account's debits minus credits: cached for accounts with a limit, summed from entries otherwise.

func (*Store) Commit

func (s *Store) Commit(ctx context.Context, tx ledgo.Transaction) error

Commit stores tx. See ledgo.Store.Commit.

func (*Store) CreateAccount

func (s *Store) CreateAccount(ctx context.Context, account ledgo.Account) error

CreateAccount registers account. Creating the same account again is a no-op; with different settings it returns ledgo.ErrAccountConflict.

func (*Store) Migrate

func (s *Store) Migrate(ctx context.Context, schema string) error

Migrate runs schema, a list of statements separated by ";", skipping tables and indexes that already exist.

Jump to

Keyboard shortcuts

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