sql

package
v0.0.0-...-ba7cdbd Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2014 License: MPL-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ConstraintNotNull = 1 + iota
	ConstraintUnique
	ConstraintPrimaryKey
	ConstraintForeignKey
)
View Source
const (
	KindInvalid = iota
	KindInteger
	KindFloat
	KindDecimal
	KindChar
	KindVarchar
	KindText
	KindBlob
	KindTime
)
View Source
const (
	OptionAutoIncrement = 1 + iota
)

Variables

View Source
var (
	ErrNoRows           = sql.ErrNoRows
	ErrFuncNotSupported = errors.New("function not supported")
)

Functions

func DescField

func DescField(idx *index.Index, field string) bool

func NewIter

func NewIter(m driver.Model, d driver.Driver, r *sql.Rows, err error) driver.Iter

Types

type Backend

type Backend interface {
	// Check performs any required sanity checks on the connection.
	Check(*DB) error
	// Name passsed to database/sql.Open
	Name() string
	// Tag returns the struct tag read by this backend
	Tag() string
	// Capabilities returns the backend capabilities not provided
	// by the SQL driver itself.
	Capabilities() driver.Capability
	// Placeholder returns the placeholder for the n'th position
	Placeholder(int) string
	// Placeholders returns a placeholders string for the given number if parameters
	Placeholders(int) string
	// StringQuote returns the character used for quoting strings.
	StringQuote() byte
	// IdentifierQuote returns the character used for quoting identifiers.
	IdentifierQuote() byte
	// Func returns the function which corresponds to the given name and
	// return type at the database level.
	Func(string, reflect.Type) (string, error)
	// DefaultValues returns the string used to signal that a INSERT has no provided
	// values and the default ones should be used.
	DefaultValues() string
	// Inspect returns the table as it exists in the database for the current model. If
	// the table does not exist, the Backend is expected to return (nil, nil).
	Inspect(*DB, driver.Model) (*Table, error)
	// HasIndex returns wheter an index exists using the provided model, index and name.
	HasIndex(*DB, driver.Model, *index.Index, string) (bool, error)
	// DefineField returns the complete field definition as a string, including name, type, options...
	// Field constraints are returned in the secon argument, each constraint should be an item in the
	// returned slice.
	DefineField(*DB, driver.Model, *Table, *Field) (string, []string, error)
	// AddFields adds the given field to the table for the given model. prevTable is the result
	// of Inspect() on the previous table, while newTable is generated from the model definition.
	AddFields(db *DB, m driver.Model, prevTable *Table, newTable *Table, fields []*Field) error
	// Alter field changes oldField to newField, potentially including the name.
	AlterField(db *DB, m driver.Model, table *Table, oldField *Field, newField *Field) error
	// Insert performs an insert on the given database for the given model fields.
	// Most drivers should just return db.Exec(query, args...).
	Insert(*DB, driver.Model, string, ...interface{}) (driver.Result, error)
	// Returns the db type of the given field (e.g. INTEGER)
	FieldType(reflect.Type, *structs.Tag) (string, error)
	// Types that need to be transformed (e.g. sqlite transforms time.Time and bool to integer)
	Transforms() []reflect.Type
	// Scan an int64 from the db to Go
	ScanInt(val int64, goVal *reflect.Value, t *structs.Tag) error
	// Scan a float64 from the db to Go
	ScanFloat(val float64, goVal *reflect.Value, t *structs.Tag) error
	// Scan a bool from the db to Go
	ScanBool(val bool, goVal *reflect.Value, t *structs.Tag) error
	// Scan a []byte from the db to Go
	ScanByteSlice(val []byte, goVal *reflect.Value, t *structs.Tag) error
	// Scan a string from the db to Go
	ScanString(val string, goVal *reflect.Value, t *structs.Tag) error
	// Scan a *time.Time from the db to Go
	ScanTime(val *time.Time, goVal *reflect.Value, t *structs.Tag) error
	// Transform a value from Go to the database
	TransformOutValue(reflect.Value) (interface{}, error)
}

Backend is the interface implemented by drivers for database/sql orm backends

type Constraint

type Constraint struct {
	Type       ConstraintType
	References Reference
}

func (*Constraint) String

func (c *Constraint) String() string

type ConstraintType

type ConstraintType int

type DB

type DB struct {
	// contains filtered or unexported fields
}

func (*DB) Backend

func (d *DB) Backend() Backend

func (*DB) Begin

func (d *DB) Begin() (*DB, error)

func (*DB) Close

func (d *DB) Close() error

func (*DB) Commit

func (d *DB) Commit() error

func (*DB) DB

func (d *DB) DB() *sql.DB

func (*DB) Driver

func (d *DB) Driver() *Driver

func (*DB) Exec

func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error)

func (*DB) Query

func (d *DB) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*DB) QueryRow

func (d *DB) QueryRow(query string, args ...interface{}) *sql.Row

func (*DB) QuoteIdentifier

func (d *DB) QuoteIdentifier(s string) string

func (*DB) QuoteString

func (d *DB) QuoteString(s string) string

func (*DB) Rollback

func (d *DB) Rollback() error

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

func NewDriver

func NewDriver(b Backend, url *config.URL) (*Driver, error)

func (*Driver) Begin

func (d *Driver) Begin() (driver.Tx, error)

func (*Driver) Capabilities

func (d *Driver) Capabilities() driver.Capability

func (*Driver) Check

func (d *Driver) Check() error

func (*Driver) Close

func (d *Driver) Close() error

func (*Driver) Commit

func (d *Driver) Commit() error

func (*Driver) Connection

func (d *Driver) Connection() interface{}

func (*Driver) Count

func (d *Driver) Count(m driver.Model, q query.Q, limit int, offset int) (uint64, error)

func (*Driver) DB

func (d *Driver) DB() *DB

func (*Driver) Delete

func (d *Driver) Delete(m driver.Model, q query.Q) (driver.Result, error)

func (*Driver) Exists

func (d *Driver) Exists(m driver.Model, q query.Q) (bool, error)

func (*Driver) HasFunc

func (d *Driver) HasFunc(fname string, retType reflect.Type) bool

func (*Driver) Initialize

func (d *Driver) Initialize(ms []driver.Model) error

func (*Driver) Insert

func (d *Driver) Insert(m driver.Model, data interface{}) (driver.Result, error)

func (*Driver) Operate

func (d *Driver) Operate(m driver.Model, q query.Q, ops []*operation.Operation) (driver.Result, error)

func (*Driver) Query

func (d *Driver) Query(m driver.Model, q query.Q, sort []driver.Sort, limit int, offset int) driver.Iter

func (*Driver) Rollback

func (d *Driver) Rollback() error

func (*Driver) Select

func (d *Driver) Select(fields []string, quote bool, m driver.Model, q query.Q, sort []driver.Sort, limit int, offset int) (*bytes.Buffer, []interface{}, error)

func (*Driver) SelectStmt

func (d *Driver) SelectStmt(buf *bytes.Buffer, params *[]interface{}, fields []string, quote bool, m driver.Model) error

func (*Driver) SetLogger

func (d *Driver) SetLogger(logger *log.Logger)

func (*Driver) Tags

func (d *Driver) Tags() []string

func (*Driver) Transaction

func (d *Driver) Transaction(f func(driver.Driver) error) error

func (*Driver) Update

func (d *Driver) Update(m driver.Model, q query.Q, data interface{}) (driver.Result, error)

func (*Driver) Upsert

func (d *Driver) Upsert(m driver.Model, q query.Q, data interface{}) (driver.Result, error)

func (*Driver) Upserts

func (d *Driver) Upserts() bool

type Executor

type Executor interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
}

type Field

type Field struct {
	Name        string
	Type        string
	Default     string
	Options     []FieldOption
	Constraints []*Constraint
}

func (*Field) AddConstraint

func (f *Field) AddConstraint(ct ConstraintType)

func (*Field) AddOption

func (f *Field) AddOption(opt FieldOption)

func (*Field) Constraint

func (f *Field) Constraint(ct ConstraintType) *Constraint

func (*Field) Copy

func (f *Field) Copy() *Field

func (*Field) HasOption

func (f *Field) HasOption(opt FieldOption) bool

func (*Field) SQL

func (f *Field) SQL(db *DB, m driver.Model, table *Table) (string, []string, error)

type FieldOption

type FieldOption int

type Iter

type Iter struct {
	// contains filtered or unexported fields
}

func (*Iter) Close

func (i *Iter) Close() error

func (*Iter) Err

func (i *Iter) Err() error

func (*Iter) Next

func (i *Iter) Next(out ...interface{}) bool

type Kind

type Kind int

func TypeKind

func TypeKind(typ string) (Kind, int)

type Queryier

type Queryier interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

type Reference

type Reference string

func MakeReference

func MakeReference(table string, field string) Reference

func (Reference) Field

func (r Reference) Field() string

func (Reference) Table

func (r Reference) Table() string

type SqlBackend

type SqlBackend struct {
}

func (*SqlBackend) AddFields

func (b *SqlBackend) AddFields(db *DB, m driver.Model, prevTable *Table, newTable *Table, fields []*Field) error

func (*SqlBackend) AlterField

func (b *SqlBackend) AlterField(db *DB, m driver.Model, table *Table, oldField *Field, newField *Field) error

func (*SqlBackend) Capabilities

func (b *SqlBackend) Capabilities() driver.Capability

func (*SqlBackend) Check

func (b *SqlBackend) Check(_ *DB) error

func (*SqlBackend) DefaultValues

func (b *SqlBackend) DefaultValues() string

func (*SqlBackend) DefineField

func (b *SqlBackend) DefineField(db *DB, m driver.Model, table *Table, f *Field) (string, []string, error)

func (*SqlBackend) Func

func (b *SqlBackend) Func(fname string, retType reflect.Type) (string, error)

func (*SqlBackend) IdentifierQuote

func (b *SqlBackend) IdentifierQuote() byte

func (*SqlBackend) Insert

func (b *SqlBackend) Insert(db *DB, m driver.Model, query string, args ...interface{}) (driver.Result, error)

func (*SqlBackend) Inspect

func (b *SqlBackend) Inspect(db *DB, m driver.Model, schema string) (*Table, error)

func (*SqlBackend) Placeholder

func (b *SqlBackend) Placeholder(n int) string

func (*SqlBackend) Placeholders

func (b *SqlBackend) Placeholders(n int) string

func (*SqlBackend) ScanBool

func (b *SqlBackend) ScanBool(val bool, goVal *reflect.Value, t *structs.Tag) error

func (*SqlBackend) ScanByteSlice

func (b *SqlBackend) ScanByteSlice(val []byte, goVal *reflect.Value, t *structs.Tag) error

func (*SqlBackend) ScanFloat

func (b *SqlBackend) ScanFloat(val float64, goVal *reflect.Value, t *structs.Tag) error

func (*SqlBackend) ScanInt

func (b *SqlBackend) ScanInt(val int64, goVal *reflect.Value, t *structs.Tag) error

func (*SqlBackend) ScanString

func (b *SqlBackend) ScanString(val string, goVal *reflect.Value, t *structs.Tag) error

func (*SqlBackend) ScanTime

func (b *SqlBackend) ScanTime(val *time.Time, goVal *reflect.Value, t *structs.Tag) error

func (*SqlBackend) StringQuote

func (b *SqlBackend) StringQuote() byte

func (*SqlBackend) TransformOutValue

func (b *SqlBackend) TransformOutValue(val reflect.Value) (interface{}, error)

func (*SqlBackend) Transforms

func (b *SqlBackend) Transforms() []reflect.Type

type Table

type Table struct {
	Fields      []*Field
	Constraints []*Constraint
}

func (*Table) PrimaryKeys

func (t *Table) PrimaryKeys() []string

func (*Table) SQL

func (t *Table) SQL(db *DB, b Backend, m driver.Model, name string) (string, error)

Jump to

Keyboard shortcuts

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