Documentation ¶
Index ¶
- Constants
- Variables
- func ContextBackend(ctx context.Context, be *Backend) context.Context
- func ContextConn(ctx context.Context, conn *sql.Conn) context.Context
- func ContextDB(ctx context.Context, db *sql.DB) context.Context
- func ContextTx(ctx context.Context, tx *TxProxy) context.Context
- func Count[T any](ctx context.Context, where any) (int, error)
- func DefineMagicType(typ string, definition string)
- func Delete[T any](ctx context.Context, where any, opts ...*FetchOptions) (sql.Result, error)
- func DeleteOne[T any](ctx context.Context, where any, opts ...*FetchOptions) error
- func ErrorNumber(err error) uint16
- func Escape(val any) string
- func EscapeTx(ctx context.Context) (context.Context, bool)
- func Exec(q *SQLQuery) error
- func Factory[T any]() *T
- func Fetch[T any](ctx context.Context, where any, opts ...*FetchOptions) ([]*T, error)
- func FetchGrouped[T any](ctx context.Context, where map[string]any, key string, opts ...*FetchOptions) (map[string][]*T, error)
- func FetchMapped[T any](ctx context.Context, where any, key string, opts ...*FetchOptions) (map[string]*T, error)
- func FetchOne[T any](ctx context.Context, target *T, where any, opts ...*FetchOptions) error
- func Get[T any](ctx context.Context, where any, opts ...*FetchOptions) (*T, error)
- func HasChanged[T any](obj *T) bool
- func Init(dsn string) error
- func InitCfg(cfg *mysql.Config) error
- func Insert[T any](ctx context.Context, target ...*T) error
- func InsertIgnore[T any](ctx context.Context, target ...*T) error
- func IsNotExist(err error) bool
- func Query(q *SQLQuery, cb func(*sql.Rows) error) error
- func QueryContext(ctx context.Context, q *SQLQuery, cb func(*sql.Rows) error) error
- func QuoteName(v string) string
- func Replace[T any](ctx context.Context, target ...*T) error
- func SetLogger(l Logger)
- func Tx(ctx context.Context, cb func(ctx context.Context) error) error
- func Update[T any](ctx context.Context, target ...*T) error
- func V(v driver.Value) driver.Value
- type Backend
- type Comparison
- type Engine
- type Error
- type EscapeTableable
- type EscapeValueable
- type FetchOptions
- type FindInSet
- type Future
- type Hex
- type Key
- type Like
- type Logger
- type Name
- type Not
- type QueryBuilder
- func (q *QueryBuilder) AlsoSelect(fields ...any) *QueryBuilder
- func (q *QueryBuilder) Delete() *QueryBuilder
- func (q *QueryBuilder) ExecQuery(ctx context.Context) (sql.Result, error)
- func (q *QueryBuilder) From(table any) *QueryBuilder
- func (q *QueryBuilder) Insert(fields ...any) *QueryBuilder
- func (q *QueryBuilder) Into(table EscapeTableable) *QueryBuilder
- func (q *QueryBuilder) Limit(v ...int) *QueryBuilder
- func (q *QueryBuilder) OrderBy(field ...SortValueable) *QueryBuilder
- func (q *QueryBuilder) Prepare(ctx context.Context) (*sql.Stmt, error)
- func (q *QueryBuilder) Render() (string, error)
- func (q *QueryBuilder) RenderArgs() (string, []any, error)
- func (q *QueryBuilder) Replace(table EscapeTableable) *QueryBuilder
- func (q *QueryBuilder) RunQuery(ctx context.Context) (*sql.Rows, error)
- func (q *QueryBuilder) Select(fields ...any) *QueryBuilder
- func (q *QueryBuilder) Set(fields ...any) *QueryBuilder
- func (q *QueryBuilder) Table(table any) *QueryBuilder
- func (q *QueryBuilder) Update(table any) *QueryBuilder
- func (q *QueryBuilder) Where(where ...any) *QueryBuilder
- type SQLQuery
- type Set
- type ShowFieldsResult
- type ShowIndexResult
- type SortValueable
- type TableMeta
- func (t *TableMeta[T]) Count(ctx context.Context, where any) (int, error)
- func (t *TableMeta[T]) Delete(ctx context.Context, where any, opts ...*FetchOptions) (sql.Result, error)
- func (t *TableMeta[T]) DeleteOne(ctx context.Context, where any, opts ...*FetchOptions) error
- func (t *TableMeta[T]) Factory() *T
- func (t *TableMeta[T]) Fetch(ctx context.Context, where any, opts ...*FetchOptions) ([]*T, error)
- func (t *TableMeta[T]) FetchGrouped(ctx context.Context, where any, key string, opts ...*FetchOptions) (map[string][]*T, error)
- func (t *TableMeta[T]) FetchMapped(ctx context.Context, where any, key string, opts ...*FetchOptions) (map[string]*T, error)
- func (t *TableMeta[T]) FetchOne(ctx context.Context, target *T, where any, opts ...*FetchOptions) error
- func (t *TableMeta[T]) Get(ctx context.Context, where any, opts ...*FetchOptions) (*T, error)
- func (t *TableMeta[T]) HasChanged(obj *T) bool
- func (t *TableMeta[T]) Insert(ctx context.Context, targets ...*T) error
- func (t *TableMeta[T]) InsertIgnore(ctx context.Context, targets ...*T) error
- func (t *TableMeta[T]) Name() string
- func (t *TableMeta[T]) Replace(ctx context.Context, targets ...*T) error
- func (t *TableMeta[T]) ScanTo(row *sql.Rows, v *T) error
- func (t *TableMeta[T]) Update(ctx context.Context, target ...*T) error
- type TableMetaIntf
- type TxProxy
- type WhereAND
- type WhereOR
Constants ¶
const ( // Use " for ANSI SQL, and ` for MySQL's own thing NameQuoteChar = `"` NameQuoteRune = '"' )
Variables ¶
var ( ErrNotReady = errors.New("database is not ready (no connection is available)") ErrNotNillable = errors.New("field is nil but cannot be nil") ErrTxAlreadyProcessed = errors.New("transaction has already been committed or rollbacked") ErrDeleteBadAssert = errors.New("delete operation failed assertion") )
var FetchLock = &FetchOptions{Lock: true}
var FormatTableName = formatCamelSnakeCase
Functions ¶
func ContextBackend ¶ added in v0.4.0
func ContextConn ¶ added in v0.1.10
func DefineMagicType ¶ added in v0.2.10
func Delete ¶ added in v0.3.1
Delete will delete values from the table matching the where parameters
func DeleteOne ¶ added in v0.3.3
DeleteOne will operate the deletion in a separate transaction and ensure only 1 row was deleted or it will rollback the deletion and return an error. This is useful when working with important data and security is more important than performance.
func ErrorNumber ¶
func Escape ¶
Escape takes any value and transforms it into a string that can be included in a MySQL query
func EscapeTx ¶ added in v0.2.6
EscapeTx allows obtaining the context underlying a current transaction, this can be useful if a query needs to be run outside of a transaction (for example to log something, etc)
func FetchGrouped ¶ added in v0.2.9
func FetchMapped ¶ added in v0.2.9
func Get ¶ added in v0.1.8
Get will instanciate a new object of type T and return a pointer to it after loading from database
func HasChanged ¶ added in v0.1.2
func Insert ¶
Insert is a short way to insert objects into database
psql.Insert(ctx, obj)
Is equivalent to:
psql.Table(obj).Insert(ctx, obj)
All passed objects must be of the same type
func InsertIgnore ¶ added in v0.1.7
func IsNotExist ¶
IsNotExist returns true if the error is relative to a table not existing.
See: https://mariadb.com/kb/en/mariadb-error-codes/
Example: Error 1146: Table 'test.Test_Table1' doesn't exist
func Query ¶
Query performs a query and use a callback to advance results, meaning there is no need to call sql.Rows.Close()
err = psql.Query(psql.Q("SELECT ..."), func(row *sql.Rows) error { ... })
func QueryContext ¶
func Replace ¶ added in v0.1.7
Replace is a short way to replace objects into database
psql.Replace(ctx, obj)
Is equivalent to:
psql.Table(obj).Replace(ctx, obj)
All passed objects must be of the same type
func SetLogger ¶ added in v0.2.11
func SetLogger(l Logger)
SetLogger sets a global logger for debugging psql This can be called easily as follows using go's log package:
psql.SetLogger(log.Default())
Or a better option:
psql.SetLogger(log.New(os.Stderr, "psql: ", log.LstdFlags|log.Lmsgprefix))
func Tx ¶ added in v0.1.10
Tx can be used to run a function inside a sql transaction for isolation/etc
Types ¶
type Backend ¶ added in v0.4.0
type Backend struct {
// contains filtered or unexported fields
}
var (
DefaultBackend *Backend
)
func GetBackend ¶ added in v0.4.0
GetBackend will attempt to find a backend in the provided context and return it, or it will return DefaultBackend if no backend was found.
type Comparison ¶
func (*Comparison) EscapeValue ¶
func (c *Comparison) EscapeValue() string
type EscapeTableable ¶
type EscapeTableable interface {
EscapeTable() string
}
EscapeTableable is a type of value that can be used as a table
type EscapeValueable ¶
type EscapeValueable interface {
EscapeValue() string
}
func Between ¶ added in v0.2.10
func Between(a, start, end any) EscapeValueable
Between is a BETWEEN SQL operation. The BETWEEN operator is inclusive: begin and end values are included.
func Equal ¶
func Equal(a, b any) EscapeValueable
func F ¶
func F(field ...string) EscapeValueable
F allows passing a field name to the query builder. It can be used in multiple ways:
psql.F("field") psql.F("table.field") psql.F("", "field.with.dots") psql.F("table", "field") psql.F("table.with.dots", "field.with.dots") and more...
func Gt ¶ added in v0.2.0
func Gt(a, b any) EscapeValueable
func Gte ¶ added in v0.2.0
func Gte(a, b any) EscapeValueable
func Lt ¶ added in v0.2.0
func Lt(a, b any) EscapeValueable
func Lte ¶ added in v0.2.0
func Lte(a, b any) EscapeValueable
func Raw ¶
func Raw(s string) EscapeValueable
type FetchOptions ¶ added in v0.1.13
type FetchOptions struct { Lock bool LimitCount int // number of results to return if >0 LimitStart int // seek first record if >0 Sort []SortValueable // fields to sort by }
func Limit ¶ added in v0.1.15
func Limit(cnt int) *FetchOptions
func LimitFrom ¶ added in v0.1.15
func LimitFrom(start, cnt int) *FetchOptions
func Sort ¶ added in v0.1.15
func Sort(fields ...SortValueable) *FetchOptions
type FindInSet ¶ added in v0.3.6
func (*FindInSet) EscapeValue ¶ added in v0.3.6
type Future ¶ added in v0.3.4
type Future[T any] struct { // contains filtered or unexported fields }
func Lazy ¶ added in v0.3.4
Lazy returns an instance of Future that will be resolved in the future. Multiple calls to Lazy in different goroutines will return the same value until it is resolved. This will also attempt to group requests to the same table in the future.
func (*Future[T]) MarshalContextJSON ¶ added in v0.3.4
func (*Future[T]) MarshalJSON ¶ added in v0.3.4
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Name allows specifying the table name when associating a table with a struct
For example: type X struct { KeyName psql.Key `sql:",type=UNIQUE,fields='A,B'"` ... }
type Like ¶
func (*Like) EscapeValue ¶
type Name ¶
type Name struct {
// contains filtered or unexported fields
}
Name allows specifying the table name when associating a table with a struct
For example: type X struct { TableName psql.Name `sql:"X"` ... }
type QueryBuilder ¶
type QueryBuilder struct { Query string Fields []any Tables []EscapeTableable FieldsSet []any WhereData WhereAND GroupBy []any OrderByData []SortValueable LimitData []int // flags Distinct bool CalcFoundRows bool UpdateIgnore bool InsertIgnore bool ForUpdate bool // contains filtered or unexported fields }
func B ¶
func B() *QueryBuilder
func (*QueryBuilder) AlsoSelect ¶
func (q *QueryBuilder) AlsoSelect(fields ...any) *QueryBuilder
func (*QueryBuilder) Delete ¶
func (q *QueryBuilder) Delete() *QueryBuilder
func (*QueryBuilder) From ¶
func (q *QueryBuilder) From(table any) *QueryBuilder
func (*QueryBuilder) Insert ¶
func (q *QueryBuilder) Insert(fields ...any) *QueryBuilder
func (*QueryBuilder) Into ¶
func (q *QueryBuilder) Into(table EscapeTableable) *QueryBuilder
func (*QueryBuilder) Limit ¶
func (q *QueryBuilder) Limit(v ...int) *QueryBuilder
func (*QueryBuilder) OrderBy ¶
func (q *QueryBuilder) OrderBy(field ...SortValueable) *QueryBuilder
func (*QueryBuilder) Render ¶
func (q *QueryBuilder) Render() (string, error)
func (*QueryBuilder) RenderArgs ¶ added in v0.2.0
func (q *QueryBuilder) RenderArgs() (string, []any, error)
func (*QueryBuilder) Replace ¶
func (q *QueryBuilder) Replace(table EscapeTableable) *QueryBuilder
func (*QueryBuilder) Select ¶
func (q *QueryBuilder) Select(fields ...any) *QueryBuilder
func (*QueryBuilder) Set ¶
func (q *QueryBuilder) Set(fields ...any) *QueryBuilder
func (*QueryBuilder) Table ¶
func (q *QueryBuilder) Table(table any) *QueryBuilder
func (*QueryBuilder) Update ¶
func (q *QueryBuilder) Update(table any) *QueryBuilder
func (*QueryBuilder) Where ¶
func (q *QueryBuilder) Where(where ...any) *QueryBuilder
type ShowFieldsResult ¶
type ShowFieldsResult struct { Virtual Name `sql:",check=0"` Field string `sql:",type=VARCHAR,size=256"` Type string `sql:",type=VARCHAR,size=256"` Collation *string `sql:",type=VARCHAR,size=256,null=1"` Null string `sql:",type=VARCHAR,size=3"` // "YES" or "NO" Key string `sql:",type=VARCHAR,size=3"` // "PRI", "UNI", "MUL" or "" Default *string `sql:",type=VARCHAR,size=256"` Extra string `sql:",type=VARCHAR,size=256"` Privileges string `sql:",type=VARCHAR,size=256"` // "select,insert,update,references" Comment string `sql:",type=VARCHAR,size=256"` }
type ShowIndexResult ¶
type ShowIndexResult struct { Virtual Name `sql:",check=0"` Table string `sql:",type=VARCHAR,size=256"` NonUnique bool `sql:"Non_unique"` KeyName string `sql:"Key_name,type=VARCHAR,size=256"` SeqInIndex int64 `sql:"Seq_in_index"` ColumnName string `sql:"Column_name,type=VARCHAR,size=256"` Collation string `sql:",type=VARCHAR,size=256"` Cardinality int64 SubPart *int64 `sql:"Sub_part"` // Packed? Null string `sql:",type=VARCHAR,size=3"` // "YES" or "" IndexType string `sql:"Index_type,type=VARCHAR,size=256"` // BTREE, HASH Comment string `sql:",type=VARCHAR,size=256"` IndexComment string `sql:"Index_comment,type=VARCHAR,size=256"` }
type SortValueable ¶ added in v0.1.19
type SortValueable interface {
// contains filtered or unexported methods
}
SortValueable is a kind of value that can be used for sorting
func S ¶ added in v0.1.19
func S(field ...string) SortValueable
type TableMeta ¶
type TableMeta[T any] struct { // contains filtered or unexported fields }
func Table ¶
Table returns the table object for T against DefaultBackend unless the provided ctx value has a backend.
func (*TableMeta[T]) Factory ¶ added in v0.3.6
func (t *TableMeta[T]) Factory() *T
Factory returns a new object T pre-initialized with its defaults
func (*TableMeta[T]) FetchGrouped ¶ added in v0.2.9
func (*TableMeta[T]) FetchMapped ¶ added in v0.2.9
func (*TableMeta[T]) HasChanged ¶ added in v0.1.2
func (*TableMeta[T]) InsertIgnore ¶ added in v0.1.7
type TableMetaIntf ¶ added in v0.1.2
type TableMetaIntf interface {
Name() string
}
type TxProxy ¶ added in v0.1.17
Source Files ¶
- backend.go
- between.go
- builder.go
- change.go
- check.go
- comparison.go
- context.go
- count.go
- delete.go
- error.go
- escape.go
- export.go
- factory.go
- fetch.go
- fetchmap.go
- field.go
- fieldname.go
- findinset.go
- formatname.go
- hex.go
- insert.go
- key.go
- lazy.go
- like.go
- log.go
- magic.go
- main.go
- mysql.go
- name.go
- query.go
- render.go
- replace.go
- rowstate.go
- set.go
- setter.go
- table.go
- tag.go
- tx.go
- types.go
- update.go
- util.go
- where.go