Documentation ¶
Overview ¶
Package dbstore stores and retrieves Go data structures as rows in a SQL database.
Each struct type is stored in its own table, and each field is a separate column of that table. This package makes it easy to store structs into such a database and to read them back out.
Index ¶
- Variables
- type Context
- type Storage
- func (db *Storage) CreateTables(ctxt Context) error
- func (db *Storage) Delete(ctxt Context, val interface{}) error
- func (db *Storage) Insert(ctxt Context, val interface{}) error
- func (db *Storage) Read(ctxt Context, val interface{}, columns ...string) error
- func (db *Storage) Register(val interface{})
- func (db *Storage) Select(ctxt Context, val interface{}, query string, args ...interface{}) error
- func (db *Storage) Write(ctxt Context, val interface{}, columns ...string) error
Constants ¶
This section is empty.
Variables ¶
var Debug = false
If Debug is set to true, each Storage method will print a log of the SQL commands being executed.
var ErrNotFound = errors.New("database record not found")
ErrNotFound is the error returned by Read, Select, and Write when there are no matching records in the database.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context interface { Exec(query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (*sql.Rows, error) }
A Context represents the underlying SQL database. Typically a *sql.DB is used as the Context implementation, but the interface allows debugging adapters to be substituted.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
A Storage records information about the data structures being stored. It must be initialized by one or more calls to Register before the other methods are called.
func (*Storage) CreateTables ¶
CreateTables creates the tables to hold the registered types. It only needs to be called when creating a new database. Each table is named for the type it stores, in the form "full/import/path.TypeName".
func (*Storage) Delete ¶
Delete deletes the value from the database. The unique identification fields in val must be set.
Delete executes a command like:
delete from Structs where Key1 = val.Key1 and Key2 = val.Key2
func (*Storage) Read ¶
Read reads the named columns from the database into val. The key fields in val must already be set.
Read executes a command like:
select columns from Structs where Key1 = val.Key1 AND Key2 = val.Key2
func (*Storage) Register ¶
func (db *Storage) Register(val interface{})
Register records that the storage should store values with the type of val, which should be a pointer to a struct with exported fields.
func (*Storage) Select ¶
Select executes a select command to read one or more rows into val. To read values of type Example, val may take any of these types:
*Example - read a single Example, returning ErrNotFound if not found **Example - allocate and read a single Example, setting it to nil if not found *[]Example - read a slice of Examples *[]*Example - read a slice of Examples
Select executes a command like
select Key1, Key2, Field3, Field4 from Structs <query here>