db

package
v0.0.0-...-a2a17fb Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2017 License: Apache-2.0 Imports: 16 Imported by: 2

Documentation

Overview

Package db is the base package for database access at stellar. It primarily exposes Session which is a lightweight wrapper around a *sqlx.DB that provides utility methods (See the repo tests for examples).

In addition to the query methods, this package provides query logging and stateful transaction management.

In addition to the lower-level access facilities, this package exposes a system to build queries more dynamically using the help of https://github.com/Masterminds/squirrel. These builder method are access through the `Table` type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Get(dest interface{}, query string, args ...interface{}) error
	Rebind(sql string) string
	Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
	Select(dest interface{}, query string, args ...interface{}) error
}

Conn represents a connection to a single database.

type DeleteBuilder

type DeleteBuilder struct {
	Table *Table
	// contains filtered or unexported fields
}

DeleteBuilder is a helper struct used to construct sql queries of the DELETE variety.

func (*DeleteBuilder) Exec

func (delb *DeleteBuilder) Exec() (sql.Result, error)

Exec executes the query represented by the builder, deleting any rows that match the queries where clauses.

func (*DeleteBuilder) Where

func (delb *DeleteBuilder) Where(
	pred interface{},
	args ...interface{},
) *DeleteBuilder

Where is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#DeleteBuilder.Where

type GetBuilder

type GetBuilder struct {
	Table *Table
	// contains filtered or unexported fields
}

GetBuilder is a helper struct used to construct sql queries of the SELECT variety.

func (*GetBuilder) Exec

func (gb *GetBuilder) Exec() error

Exec executes the query represented by the builder, populating the destination with the results returned by running the query against the current database session.

func (*GetBuilder) Offset

func (gb *GetBuilder) Offset(offset uint64) *GetBuilder

Offset is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Offset

func (*GetBuilder) OrderBy

func (gb *GetBuilder) OrderBy(
	orderBys ...string,
) *GetBuilder

OrderBy is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.OrderBy

func (*GetBuilder) Prefix

func (gb *GetBuilder) Prefix(
	sql string,
	args ...interface{},
) *GetBuilder

Prefix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Prefix

func (*GetBuilder) Suffix

func (gb *GetBuilder) Suffix(
	sql string,
	args ...interface{},
) *GetBuilder

Suffix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Suffix

func (*GetBuilder) Where

func (gb *GetBuilder) Where(
	pred interface{},
	args ...interface{},
) *GetBuilder

Where is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#GetBuilder.Where

type InsertBuilder

type InsertBuilder struct {
	Table *Table
	// contains filtered or unexported fields
}

InsertBuilder is a helper struct used to construct sql queries of the INSERT variety.

func (*InsertBuilder) Exec

func (ib *InsertBuilder) Exec() (sql.Result, error)

Exec executes the query represented by the builder, inserting each val provided to the builder into the database.

func (*InsertBuilder) Rows

func (ib *InsertBuilder) Rows(rows ...interface{}) *InsertBuilder

Rows appends more rows onto the insert statement

type NoRowsError

type NoRowsError struct {
}

NoRowsError is returned when an insert is attempted without providing any values to insert.

func (*NoRowsError) Error

func (err *NoRowsError) Error() string

type SelectBuilder

type SelectBuilder struct {
	Table *Table
	// contains filtered or unexported fields
}

SelectBuilder is a helper struct used to construct sql queries of the SELECT variety.

func (*SelectBuilder) Exec

func (sb *SelectBuilder) Exec() error

Exec executes the query represented by the builder, populating the destination with the results returned by running the query against the current database session.

func (*SelectBuilder) Limit

func (sb *SelectBuilder) Limit(limit uint64) *SelectBuilder

Limit is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Limit

func (*SelectBuilder) Offset

func (sb *SelectBuilder) Offset(offset uint64) *SelectBuilder

Offset is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Offset

func (*SelectBuilder) OrderBy

func (sb *SelectBuilder) OrderBy(
	orderBys ...string,
) *SelectBuilder

OrderBy is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.OrderBy

func (*SelectBuilder) Prefix

func (sb *SelectBuilder) Prefix(
	sql string,
	args ...interface{},
) *SelectBuilder

Prefix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Prefix

func (*SelectBuilder) Suffix

func (sb *SelectBuilder) Suffix(
	sql string,
	args ...interface{},
) *SelectBuilder

Suffix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Suffix

func (*SelectBuilder) Where

func (sb *SelectBuilder) Where(
	pred interface{},
	args ...interface{},
) *SelectBuilder

Where is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Where

type Session

type Session struct {
	// DB is the database connection that queries should be executed against.
	DB *sqlx.DB

	// Ctx is the optional context in which the repo is operating under.
	Ctx context.Context
	// contains filtered or unexported fields
}

Session provides helper methods for making queries against `DB` and provides utilities such as automatic query logging and transaction management. NOTE: A Session is designed to be lightweight and temporarily lived (usually request scoped) which is one reason it is acceptable for it to store a context. It is not presently intended to cross goroutine boundaries and is not concurrency safe.

func Open

func Open(dialect, dsn string) (*Session, error)

Open the database at `dsn` and returns a new *Session using it.

func Wrap

func Wrap(base *sql.DB, dialect string) *Session

Wrap wraps a bare *sql.DB (from the database/sql stdlib package) in a *db.Session instance. It is meant to be used in cases where you do not control the instantiation of the database connection, but would still like to leverage the facilities provided in Session.

func (*Session) Begin

func (s *Session) Begin() error

Begin binds this session to a new transaction.

func (*Session) Clone

func (s *Session) Clone() *Session

Clone clones the receiver, returning a new instance backed by the same context and db. The result will not be bound to any transaction that the source is currently within.

func (*Session) Commit

func (s *Session) Commit() error

Commit commits the current transaction

func (*Session) DeleteRange

func (s *Session) DeleteRange(
	start, end int64,
	table string,
	idCol string,
) error

DeleteRange deletes a range of rows from a sql table between `start` and `end` (exclusive).

func (*Session) Dialect

func (s *Session) Dialect() string

Dialect returns the SQL dialect that this session is configured to use

func (*Session) Exec

func (s *Session) Exec(query sq.Sqlizer) (sql.Result, error)

Exec runs `query`

func (*Session) ExecAll

func (s *Session) ExecAll(script string) error

ExecAll runs all sql commands in `script` against `r` within a single transaction.

func (*Session) ExecRaw

func (s *Session) ExecRaw(query string, args ...interface{}) (sql.Result, error)

ExecRaw runs `query` with `args`

func (*Session) Get

func (s *Session) Get(dest interface{}, query sq.Sqlizer) error

Get runs `query`, setting the first result found on `dest`, if any.

func (*Session) GetRaw

func (s *Session) GetRaw(dest interface{}, query string, args ...interface{}) error

GetRaw runs `query` with `args`, setting the first result found on `dest`, if any.

func (*Session) GetTable

func (s *Session) GetTable(name string) *Table

GetTable translates the provided struct into a Table,

func (*Session) NoRows

func (s *Session) NoRows(err error) bool

NoRows returns true if the provided error resulted from a query that found no results.

func (*Session) Query

func (s *Session) Query(query sq.Sqlizer) (*sqlx.Rows, error)

Query runs `query`, returns a *sqlx.Rows instance

func (*Session) QueryRaw

func (s *Session) QueryRaw(query string, args ...interface{}) (*sqlx.Rows, error)

QueryRaw runs `query` with `args`

func (*Session) ReplacePlaceholders

func (s *Session) ReplacePlaceholders(query string) (string, error)

ReplacePlaceholders replaces the '?' parameter placeholders in the provided sql query with a sql dialect appropriate version. Use '??' to escape a placeholder.

func (*Session) Rollback

func (s *Session) Rollback() error

Rollback rolls back the current transaction

func (*Session) Select

func (s *Session) Select(dest interface{}, query sq.Sqlizer) error

Select runs `query`, setting the results found on `dest`.

func (*Session) SelectRaw

func (s *Session) SelectRaw(
	dest interface{},
	query string,
	args ...interface{},
) error

SelectRaw runs `query` with `args`, setting the results found on `dest`.

type Table

type Table struct {
	// Name is the name of the table
	Name string

	Session *Session
}

Table helps to build sql queries against a given table. It logically represents a SQL table on the database that `Session` is connected to.

func (*Table) Delete

func (tbl *Table) Delete(
	pred interface{},
	args ...interface{},
) *DeleteBuilder

Delete returns a new query builder configured to delete rows from the table.

func (*Table) Get

func (tbl *Table) Get(
	dest interface{},
	pred interface{},
	args ...interface{},
) *GetBuilder

Get returns a new query builder configured to select into the provided `dest`.

Get behaves the same was as Select, but automatically limits the query generated to a single value and only populates a single struct.

func (*Table) Insert

func (tbl *Table) Insert(rows ...interface{}) *InsertBuilder

Insert returns a new query builder configured to insert structs into the table.

Insert takes one or more struct (or pointer to struct) values, each of which represents a single row to be created in the table. The first value provided in a call to this function will operate as the template for the insert and will determine what columns are populated in the query. For this reason, it is highly recommmended that you always use the same struct type for any single call this function.

An InsertBuilder uses the "db" struct tag to determine the column names that a given struct should be mapped to, and by default the unmofdified name of the field will be used. Similar to other struct tags, the value "-" will cause the field to be skipped.

NOTE: using the omitempty option, such as used with json struct tags, is not supported.

func (*Table) Select

func (tbl *Table) Select(
	dest interface{},
	pred interface{},
	args ...interface{},
) *SelectBuilder

Select returns a new query builder configured to select into the provided `dest`.

func (*Table) Update

func (tbl *Table) Update(
	source interface{},
	pred interface{},
	args ...interface{},
) *UpdateBuilder

Update returns a new query builder configured to update rows that match the predicate with the values of the provided source struct. See docs for `UpdateBuildeExec` for more documentation.

type UpdateBuilder

type UpdateBuilder struct {
	Table *Table
	// contains filtered or unexported fields
}

UpdateBuilder is a helper struct used to construct sql queries of the UPDATE variety.

func (*UpdateBuilder) Exec

func (ub *UpdateBuilder) Exec() (sql.Result, error)

Exec executes the query that has been previously configured on the UpdateBuilder.

func (*UpdateBuilder) Limit

func (ub *UpdateBuilder) Limit(limit uint64) *UpdateBuilder

Limit is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Limit

func (*UpdateBuilder) Offset

func (ub *UpdateBuilder) Offset(offset uint64) *UpdateBuilder

Offset is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Offset

func (*UpdateBuilder) OrderBy

func (ub *UpdateBuilder) OrderBy(
	orderBys ...string,
) *UpdateBuilder

OrderBy is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.OrderBy

func (*UpdateBuilder) Prefix

func (ub *UpdateBuilder) Prefix(
	sql string,
	args ...interface{},
) *UpdateBuilder

Prefix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Prefix

func (*UpdateBuilder) Set

func (ub *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder

Set is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Suffix

func (*UpdateBuilder) SetMap

func (ub *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder

SetMap is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Suffix

func (*UpdateBuilder) Suffix

func (ub *UpdateBuilder) Suffix(
	sql string,
	args ...interface{},
) *UpdateBuilder

Suffix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Suffix

func (*UpdateBuilder) Where

func (ub *UpdateBuilder) Where(
	pred interface{},
	args ...interface{},
) *UpdateBuilder

Where is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Where

Directories

Path Synopsis
Package dbtest is a package to ease the pain of developing test code that works against external databases.
Package dbtest is a package to ease the pain of developing test code that works against external databases.
Package sqlutils contains utility functions for manipulating strings of SQL
Package sqlutils contains utility functions for manipulating strings of SQL

Jump to

Keyboard shortcuts

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