dbstore

package
v0.0.0-...-03206cb Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2016 License: BSD-3-Clause Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var Debug = false

If Debug is set to true, each Storage method will print a log of the SQL commands being executed.

View Source
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

func (db *Storage) CreateTables(ctxt Context) error

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

func (db *Storage) Delete(ctxt Context, val interface{}) error

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) Insert

func (db *Storage) Insert(ctxt Context, val interface{}) error

Insert inserts the value into the database.

func (*Storage) Read

func (db *Storage) Read(ctxt Context, val interface{}, columns ...string) error

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

func (db *Storage) Select(ctxt Context, val interface{}, query string, args ...interface{}) error

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>

func (*Storage) Write

func (db *Storage) Write(ctxt Context, val interface{}, columns ...string) error

Write writes the named columns from val into the database. The key fields in val must already be set and the value must already exist.

Write executes a command like:

update Structs
set column1 = val.Column1, column2 = val.Column2
where Key1 = val.Key1 AND Key2 = val.Key2

Jump to

Keyboard shortcuts

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