db

package
v0.0.0-...-86e69cd Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 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 EnumRecord

type EnumRecord[T osn.EnumType] struct {
	// contains filtered or unexported fields
}

The record abstraction for enums is interesting because it is both a single enum value (a one-byte int) and the unique (id, name) row in its backing database table.

func (EnumRecord[T]) Columns

func (EnumRecord[T]) Columns() []string

func (EnumRecord[T]) NamedValues

func (record EnumRecord[T]) NamedValues() ([]driver.NamedValue, error)

func (EnumRecord[T]) ScanRow

func (EnumRecord[T]) ScanRow(row *sql.Row) error

func (EnumRecord[T]) ScanValues

func (EnumRecord[T]) ScanValues(...driver.Value) error

func (EnumRecord[T]) Scannables

func (EnumRecord[T]) Scannables() []any

func (EnumRecord[T]) Values

func (record EnumRecord[T]) Values() ([]any, error)

type EnumTable

type EnumTable[T osn.EnumType] struct {
	// contains filtered or unexported fields
}

Serialized enum types are a recurring pattern in fully defined relational DBs and here we make the simplifying assumption that any enumerated type can fit inside a single byte. If the domain is larger than 255 items, express that relation as a [Table[T Record]], named by a [TableIndex[Record, comparable]].

func MakeEnumTable

func MakeEnumTable[T osn.EnumType](tablename string, values []T) EnumTable[T]

Constructor function for making an enum table with the indicated values.

func (EnumTable) Columns

func (table EnumTable) Columns() []string

func (EnumTable[T]) Get

func (table EnumTable[T]) Get(id uint64) (T, error)

Basic getter is equivalent to T(id) but this also validates. Included to conform with the [Table[T]] interface.

func (EnumTable) GetByName

func (table EnumTable) GetByName(name string) (T, error)

Retrieves the single row that corresponds to the indicated name.

func (EnumTable[T]) GetNamed

func (table EnumTable[T]) GetNamed(name string) (T, error)

Returns an EnumRecord[T] instance that matches the provided name. If there is no value T with that name, it returns the zero (assumed UNKNOWN) value. Unlike the query-backed getter for [Table[T]], this never returns an error.

func (EnumTable[T]) Insert

func (table EnumTable[T]) Insert(record *T) error

EnumTable does not allow insertions, but it provides the method to match [Table[T]].

func (EnumTable) Name

func (table EnumTable) Name() string

func (EnumTable[T]) NewRecord

func (table EnumTable[T]) NewRecord() EnumRecord[T]

func (EnumTable[T]) SelectAll

func (table EnumTable[T]) SelectAll() (chan<- T, error)

Enums are sent to the returned channel in a deterministic (0..limit) order.

func (EnumTable[T]) SqlCreate

func (table EnumTable[T]) SqlCreate() string

func (EnumTable[T]) SqlInit

func (table EnumTable[T]) SqlInit() string

func (EnumTable[T]) Strings

func (table EnumTable[T]) Strings() []string

func (EnumTable[T]) Values

func (table EnumTable[T]) Values() []T

Returns an array of each valid value of the composed enum type.

type LegacyMapRecord

type LegacyMapRecord osn.LegacyMap

func NewMapRecord

func NewMapRecord() *LegacyMapRecord

func (LegacyMapRecord) Columns

func (LegacyMapRecord) Columns() []string

func (LegacyMapRecord) NamedValues

func (record LegacyMapRecord) NamedValues() ([]driver.NamedValue, error)

func (*LegacyMapRecord) ScanRow

func (record *LegacyMapRecord) ScanRow(row *sql.Row) error

func (*LegacyMapRecord) ScanValues

func (record *LegacyMapRecord) ScanValues(values ...driver.Value) error

func (*LegacyMapRecord) Scannables

func (record *LegacyMapRecord) Scannables() []any

This is provided for completeness but it has a flaw in that the details are not recoverable. However, at present this method is only called from the `SELECT * FROM maps;` path, so it actually works out to get an abbreviation.

func (LegacyMapRecord) Values

func (record LegacyMapRecord) Values() ([]any, error)

type LegacyMatchRecord

type LegacyMatchRecord struct {
	// Embedded so that type inference on Table[*LegacyMatchRecord] works.
	osn.LegacyMatch
}

func MakeMatchRecord

func MakeMatchRecord(match osn.LegacyMatch) *LegacyMatchRecord

Constructs a LegacyMatchRecord from an osn.LegacyMatch which it encapsulates.

func NewMatchRecord

func NewMatchRecord() *LegacyMatchRecord

Returns a zero record for populating with match metadata.

func (*LegacyMatchRecord) Columns

func (*LegacyMatchRecord) Columns() []string

func (*LegacyMatchRecord) NamedValues

func (record *LegacyMatchRecord) NamedValues() ([]driver.NamedValue, error)

func (*LegacyMatchRecord) ScanRow

func (record *LegacyMatchRecord) ScanRow(row *sql.Row) error

func (*LegacyMatchRecord) ScanValues

func (record *LegacyMatchRecord) ScanValues(values ...driver.Value) error

func (*LegacyMatchRecord) Scannables

func (record *LegacyMatchRecord) Scannables() []any

func (*LegacyMatchRecord) Values

func (record *LegacyMatchRecord) Values() ([]any, error)

type MutableTable

type MutableTable[T Record] interface {
	Table[T]
	Insert(T) error
	Delete(int64) error
}

Variant on the table type which allows for inserting and deleting records.

func MakeMatchesTable

func MakeMatchesTable(sqldb *sql.DB) MutableTable[*LegacyMatchRecord]

func MakePlayersTable

func MakePlayersTable(sqldb *sql.DB) MutableTable[*PlayerRecord]

func MakeRolesTable

func MakeRolesTable(sqldb *sql.DB) MutableTable[*PlayerRoleRecord]

func MakeStandingsTable

func MakeStandingsTable(sqldb *sql.DB) MutableTable[*StandingsRecord]

type OsnDB

type OsnDB interface {
	MustCreateAndPopulateTables() // Create tables or die trying.
	Close()                       // Closes the database and attached resources.

	MapByID(id uint8) (osn.LegacyMap, error)
	MapByName(name string) (osn.LegacyMap, error)

	Players() MutableTable[*PlayerRecord]
	Matches() MutableTable[*LegacyMatchRecord]
	Standings() MutableTable[*StandingsRecord]

	UpdateMatchStatus(osn.GameID, osn.FetchStatus) error
}

Interface for simplifying the interaction with a backing database.

DB includes match metadata, map identities, player history and replay index.

func OpenOsnDB

func OpenOsnDB(filepath string) OsnDB

Opens a Sqlite db at indicated path and prepares queries. Does not create tables, only prepares the connection and statements.

Asserts db exists and basic queries can be constructed. LOG(FATAL) on any error, database remains unmodified.

type PlayerRecord

type PlayerRecord struct {
	// For it to be a pointer-receiver in the following methods, it needs to be an
	// embedded struct member rather than a simple alias type.
	osn.Player
}

This satisfies the Record interface for use as Table[*Record] while being only a thin wrapper around the Player struct.

func NewPlayerRecord

func NewPlayerRecord() *PlayerRecord

func (*PlayerRecord) Columns

func (player *PlayerRecord) Columns() []string

func (*PlayerRecord) NamedValues

func (player *PlayerRecord) NamedValues() ([]driver.NamedValue, error)

func (*PlayerRecord) ScanRow

func (player *PlayerRecord) ScanRow(row *sql.Row) error

func (*PlayerRecord) ScanValues

func (player *PlayerRecord) ScanValues(values ...driver.Value) error

func (*PlayerRecord) Scannables

func (player *PlayerRecord) Scannables() []any

func (*PlayerRecord) Values

func (player *PlayerRecord) Values() ([]any, error)

type PlayerRoleRecord

type PlayerRoleRecord struct {
	MatchID   int64
	PlayerID  int64
	TurnOrder osn.PlayerColorEnum
}

Relation for which players are participating in which matches, and the turn order they are assigned to. Appropriate for both 1v1 and 2v2 matches.

func NewRoleRecord

func NewRoleRecord() *PlayerRoleRecord

func (*PlayerRoleRecord) Columns

func (*PlayerRoleRecord) Columns() []string

func (*PlayerRoleRecord) NamedValues

func (record *PlayerRoleRecord) NamedValues() ([]driver.NamedValue, error)

func (*PlayerRoleRecord) ScanRow

func (record *PlayerRoleRecord) ScanRow(row *sql.Row) error

func (*PlayerRoleRecord) ScanValues

func (record *PlayerRoleRecord) ScanValues(values ...driver.Value) error

func (*PlayerRoleRecord) Scannables

func (record *PlayerRoleRecord) Scannables() []any

func (*PlayerRoleRecord) Values

func (record *PlayerRoleRecord) Values() ([]any, error)

type Record

type Record interface {
	Columns() []string
	Values() ([]any, error)
	NamedValues() ([]driver.NamedValue, error)

	ScanValues(...driver.Value) error
	ScanRow(*sql.Row) error
	Scannables() []any
}

Abstraction over the structural type of values in a table or group of tables.

type StandingsRecord

type StandingsRecord struct {
	After int64
	Until int64
	osn.PlayerStanding
}

func NewStandingsRecord

func NewStandingsRecord() *StandingsRecord

func (*StandingsRecord) Columns

func (*StandingsRecord) Columns() []string

func (*StandingsRecord) NamedValues

func (record *StandingsRecord) NamedValues() ([]driver.NamedValue, error)

func (*StandingsRecord) ScanRow

func (record *StandingsRecord) ScanRow(row *sql.Row) error

func (*StandingsRecord) ScanValues

func (record *StandingsRecord) ScanValues(values ...driver.Value) error

func (*StandingsRecord) Scannables

func (record *StandingsRecord) Scannables() []any

func (*StandingsRecord) Values

func (record *StandingsRecord) Values() ([]any, error)

type Table

type Table[T Record] interface {
	TableSql
	NewRecord() T

	Get(int64) (T, error)
	GetByName(string) (T, error)
	SelectAll() (<-chan T, error)
}

Common interface for one or more database tables and retrieval of records.

func MakeMapsTable

func MakeMapsTable(sqldb *sql.DB) Table[*LegacyMapRecord]

type TableSql

type TableSql interface {
	Name() string
	Columns() []string
	SqlCreate() string
	SqlInit() string
}

The SQL-specific features of a relational table. Fortunately, these have domains and codomains that aren't parameterized by the table's Record type, even though the computation of CREATE, INDEX and INSERT commands

Jump to

Keyboard shortcuts

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