Documentation
¶
Overview ¶
Package sqlstore is the database/sql ledgo.Store shared by the SQL backends. Each backend supplies a Dialect and its schema.
Index ¶
- type Dialect
- type Store
- func (s *Store) Balance(ctx context.Context, account string) (decimal.Decimal, error)
- func (s *Store) Commit(ctx context.Context, tx ledgo.Transaction) error
- func (s *Store) CreateAccount(ctx context.Context, account ledgo.Account) error
- func (s *Store) Migrate(ctx context.Context, schema string) error
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 ¶
InTx returns a Store that writes into tx, so postings commit or roll back together with the caller's own changes.
func New ¶
New returns a Store on db. Each Commit runs in its own READ COMMITTED transaction, started with the dialect's TxOptions.
func (*Store) Balance ¶
Balance is the account's debits minus credits: cached for accounts with a limit, summed from entries otherwise.
func (*Store) CreateAccount ¶
CreateAccount registers account. Creating the same account again is a no-op; with different settings it returns ledgo.ErrAccountConflict.