Documentation ¶
Overview ¶
Package depot implements a small abstraction layer for accessing sql (and potentially other) databases.
depot is build around two concepts: values and clauses. See the respective descriptions for details.
depot also provides a code generator used to generate repository types from regulare go structs with field tags. The resulting code uses no reflection and provides a typesafe interface to interact with the database.
Index ¶
- Variables
- type Clause
- type ColsClause
- type Factory
- type FactoryOptions
- type OrderByClause
- type Scanner
- type Session
- func (s *Session) Commit() error
- func (s *Session) DeleteMany(from TableClause, where ...Clause) error
- func (s *Session) Error(err error)
- func (s *Session) Exec(query string, args ...interface{}) error
- func (s *Session) InsertOne(into TableClause, values Values) error
- func (s *Session) QueryCount(from TableClause, clauses ...Clause) (count int, err error)
- func (s *Session) QueryMany(cols ColsClause, from TableClause, clauses ...Clause) ([]Values, error)
- func (s *Session) QueryOne(cols ColsClause, from TableClause, where ...Clause) (Values, error)
- func (s *Session) Rollback() (err error)
- func (s *Session) UpdateMany(table TableClause, values Values, where ...Clause) error
- type TableClause
- type Values
- func (v Values) GetBool(key string) (bool, bool)
- func (v Values) GetBytes(key string) ([]byte, bool)
- func (v Values) GetFloat32(key string) (float32, bool)
- func (v Values) GetFloat64(key string) (float64, bool)
- func (v Values) GetInt(key string) (int, bool)
- func (v Values) GetInt16(key string) (int16, bool)
- func (v Values) GetInt32(key string) (int32, bool)
- func (v Values) GetInt64(key string) (int64, bool)
- func (v Values) GetInt8(key string) (int8, bool)
- func (v Values) GetString(key string) (string, bool)
- func (v Values) GetTime(key string) (time.Time, bool)
- func (v Values) GetUInt(key string) (uint, bool)
- func (v Values) GetUInt16(key string) (uint16, bool)
- func (v Values) GetUInt32(key string) (uint32, bool)
- func (v Values) GetUInt64(key string) (uint64, bool)
- func (v Values) GetUInt8(key string) (uint8, bool)
- func (v Values) IsNull(key string) bool
- type WhereClause
- func EQ(column string, value interface{}) WhereClause
- func GE(column string, value interface{}) WhereClause
- func GT(column string, value interface{}) WhereClause
- func In(column string, values ...interface{}) WhereClause
- func LE(column string, value interface{}) WhereClause
- func LT(column string, value interface{}) WhereClause
- func Where(column string, value interface{}) WhereClause
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoResult is returned when queries execpted to match (at least) on row // match no row at all. ErrNoResult = errors.New("no result") // ErrRollback is returned when trying to commit a session that has already been // rolled back. ErrRollback = errors.New("rolled back") )
Functions ¶
This section is empty.
Types ¶
type Clause ¶
type Clause interface { // SQL returns the SQL query part expressed by this clause. SQL() string // Args returns any positional arguments used by this clause. Args() []interface{} }
Clause defines the interface implemented by all clauses used to describe different parts of a query. A clause captures optional arguments.
type ColsClause ¶
type ColsClause interface { Clause // Names returns the list of column names to select in query order. Names() []string // contains filtered or unexported methods }
ColsClause defines a Clause used to select columns.
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
Factory provides functions to create new Sessions.
func NewSessionFactory ¶
func NewSessionFactory(pool *sql.DB, options *FactoryOptions) *Factory
NewSessionFactory creates a new Factory using connections from the given pool. When providing nil for the options, default options are used.
type FactoryOptions ¶ added in v0.2.0
type FactoryOptions struct { // When set to true all SQL statements will be logged using the log package. LogSQL bool }
FactoryOptions defines the additional options for a Factory.
type OrderByClause ¶
type OrderByClause interface { Clause // contains filtered or unexported methods }
OrderByClause defines the interface used to sort rows.
func Asc ¶
func Asc(column string) OrderByClause
Asc returns an OrderByClause sorting by the given column in ascending order.
func Desc ¶
func Desc(column string) OrderByClause
Desc returns an OrderByClause sorting by the given column in descending order.
func OrderBy ¶
func OrderBy(column string, asc bool) OrderByClause
OrderBy constructs a new OrderByClause.
type Scanner ¶
type Scanner interface {
Scan(vals ...interface{}) error
}
Scanner defines the Scan method provided by sql.Rows and sql.Row, as the sql package does not define such an interface.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session defines an interaction session with the database. A session uses a single transaction and is bound to a single Context. A session provides an abstract interface built around Values and Clauses.
func GetSession ¶
GetSession returns the Session associated with the given Context and a boolean flag (ok) indicating if a session has been registered with the given context.
func MustGetSession ¶ added in v0.2.0
MustGetSession returns the Session associated with the given Context. This function panics when no Session has been stored in the Context.
func (*Session) Commit ¶
Commit commits the session's transaction and returns an error if the commit fails.
func (*Session) DeleteMany ¶
func (s *Session) DeleteMany(from TableClause, where ...Clause) error
DeleteMany deletes all matching rows from the database.
func (*Session) Error ¶
Error marks the transaction as failed so it cannot be committed later on. Calling Error with a nil error clears the error state of the transaction.
func (*Session) Exec ¶ added in v0.2.0
Exec executes the given query passing the given args and returns the resulting error or nil. This is just a wrapper for calling ExexContext on the wrapped transaction.
func (*Session) InsertOne ¶
func (s *Session) InsertOne(into TableClause, values Values) error
InsertOne inserts a single row.
func (*Session) QueryCount ¶
func (s *Session) QueryCount(from TableClause, clauses ...Clause) (count int, err error)
QueryCount executes a counting query and returns the number of matching rows.
func (*Session) QueryMany ¶
func (s *Session) QueryMany(cols ColsClause, from TableClause, clauses ...Clause) ([]Values, error)
QueryMany executes a query that is expected to match any number of rows. The rows are returned as Values. If the query did not match any row an empty slice is returned.
func (*Session) QueryOne ¶
func (s *Session) QueryOne(cols ColsClause, from TableClause, where ...Clause) (Values, error)
QueryOne executes a query that is expected to return a single result. The columns, table and selection criteria are given as Clauses. QueryOne returns the selected values which is never nil. ErrNoResult is returned when the query did not match any rows.
func (*Session) Rollback ¶
Rollback rolls the session's transaction back and returns any error raised during the rollback.
func (*Session) UpdateMany ¶
func (s *Session) UpdateMany(table TableClause, values Values, where ...Clause) error
UpdateMany updates all matching rows with the same values given.
type TableClause ¶
type TableClause interface { Clause // contains filtered or unexported methods }
TableClause implements a clause used to name a table.
func From ¶
func From(table string) TableClause
From is an alias for Table supporting a more DSL style interface.
func Into ¶
func Into(table string) TableClause
Into is an alias for Table supporting a more DSL style interface.
func Table ¶
func Table(table string) TableClause
Table creates a TableClause from the single table name.
type Values ¶
type Values map[string]interface{}
Values contains the persistent column values for an entity either after reading the values from the database to re-create the entity value or to persist the entity's values in the database (either for insertion or update).
func (Values) GetBytes ¶ added in v0.2.0
GetBytes returns the value associated with key as a byte slice.
func (Values) GetFloat32 ¶ added in v0.2.0
GetFloat32 returns the value associated with key as a float32.
func (Values) GetFloat64 ¶ added in v0.2.0
GetFloat64 returns the value associated with key as a float64.
func (Values) GetInt16 ¶ added in v0.2.0
GetInt16 returns the value associated with key as an int16.
func (Values) GetInt32 ¶ added in v0.2.0
GetInt32 returns the value associated with key as an int32.
func (Values) GetInt64 ¶ added in v0.2.0
GetInt64 returns the value associated with key as an int64.
func (Values) GetTime ¶ added in v0.2.0
GetTime returns the value associated with key as a time.Time.
func (Values) GetUInt16 ¶ added in v0.2.0
GetUInt16 returns the value associated with key as a uint16.
func (Values) GetUInt32 ¶ added in v0.2.0
GetUInt32 returns the value associated with key as an uint32.
func (Values) GetUInt64 ¶ added in v0.2.0
GetUInt64 returns the value associated with key as an uint64.
type WhereClause ¶
type WhereClause interface { Clause // contains filtered or unexported methods }
WhereClause defines the interface implemented by all clauses that contribute a "where" condition.
func EQ ¶
func EQ(column string, value interface{}) WhereClause
EQ creates a WhereClause comparing a column's value for equality.
func GE ¶
func GE(column string, value interface{}) WhereClause
GE creates a WhereClause comparing a column's value for greater or equal.
func GT ¶
func GT(column string, value interface{}) WhereClause
GT creates a WhereClause comparing a column's value for greater than.
func In ¶
func In(column string, values ...interface{}) WhereClause
In creates a WhereClause using the `in` operator.
func LE ¶
func LE(column string, value interface{}) WhereClause
LE creates a WhereClause comparing a column's value for less equal.
func LT ¶
func LT(column string, value interface{}) WhereClause
LT creates a WhereClause comparing a column's value for less than.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
depot
Package main contains the CLI for the generate command.
|
Package main contains the CLI for the generate command. |
Package main contains a cli demonstrating the generated repo's usage.
|
Package main contains a cli demonstrating the generated repo's usage. |
models
Package models contains the model definitions.
|
Package models contains the model definitions. |
internal
|
|
generate
Package generate implements code generation for repository types.
|
Package generate implements code generation for repository types. |
utils
Package utils contains utility functions for handling database interaction and mapping.
|
Package utils contains utility functions for handling database interaction and mapping. |