v1.2.1 Latest Latest

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

Go to latest
Published: Apr 2, 2024 License: BSD-2-Clause Imports: 23 Imported by: 53




View Source
const (
	InvalidRelation = iota


This section is empty.


func Append

func Append(fmter Formatter, b []byte, v interface{}) []byte

func AppendBoolValue added in v0.1.6

func AppendBoolValue(fmter Formatter, b []byte, v reflect.Value) []byte

func AppendFloat32Value added in v0.1.6

func AppendFloat32Value(fmter Formatter, b []byte, v reflect.Value) []byte

func AppendFloat64Value added in v0.1.6

func AppendFloat64Value(fmter Formatter, b []byte, v reflect.Value) []byte

func AppendIntValue

func AppendIntValue(fmter Formatter, b []byte, v reflect.Value) []byte

func AppendJSONValue added in v0.3.4

func AppendJSONValue(fmter Formatter, b []byte, v reflect.Value) []byte

func AppendQueryAppender

func AppendQueryAppender(fmter Formatter, b []byte, app QueryAppender) []byte

func AppendStringValue added in v0.3.4

func AppendStringValue(fmter Formatter, b []byte, v reflect.Value) []byte

func AppendUintValue

func AppendUintValue(fmter Formatter, b []byte, v reflect.Value) []byte

func DiscoverSQLType added in v0.1.17

func DiscoverSQLType(typ reflect.Type) string

func SetTableNameInflector

func SetTableNameInflector(fn func(string) string)

SetTableNameInflector overrides the default func that pluralizes model name to get table name, e.g. my_article becomes my_articles.


type AfterScanRowHook added in v1.0.13

type AfterScanRowHook interface {
	AfterScanRow(context.Context) error

type AppenderFunc

type AppenderFunc func(fmter Formatter, b []byte, v reflect.Value) []byte

func Appender

func Appender(dialect Dialect, typ reflect.Type) AppenderFunc

func FieldAppender

func FieldAppender(dialect Dialect, field *Field) AppenderFunc

func PtrAppender added in v1.0.3

func PtrAppender(fn AppenderFunc) AppenderFunc

type BaseDialect added in v1.0.14

type BaseDialect struct{}

func (BaseDialect) AppendBool added in v1.1.9

func (BaseDialect) AppendBool(b []byte, v bool) []byte

func (BaseDialect) AppendBytes added in v1.0.14

func (BaseDialect) AppendBytes(b, bs []byte) []byte

func (BaseDialect) AppendJSON added in v1.0.14

func (BaseDialect) AppendJSON(b, jsonb []byte) []byte

func (BaseDialect) AppendString added in v1.0.17

func (BaseDialect) AppendString(b []byte, s string) []byte

func (BaseDialect) AppendTime added in v1.0.14

func (BaseDialect) AppendTime(b []byte, tm time.Time) []byte

func (BaseDialect) AppendUint32 added in v1.0.14

func (BaseDialect) AppendUint32(b []byte, n uint32) []byte

func (BaseDialect) AppendUint64 added in v1.0.14

func (BaseDialect) AppendUint64(b []byte, n uint64) []byte

type BaseModel added in v0.3.1

type BaseModel struct{}

type BeforeAppendModelHook added in v1.0.13

type BeforeAppendModelHook interface {
	BeforeAppendModel(ctx context.Context, query Query) error

type BeforeScanRowHook added in v1.0.13

type BeforeScanRowHook interface {
	BeforeScanRow(context.Context) error

type ColumnsAppender

type ColumnsAppender interface {
	AppendColumns(fmter Formatter, b []byte) ([]byte, error)

type CustomAppender added in v0.3.4

type CustomAppender func(typ reflect.Type) AppenderFunc

type Dialect

type Dialect interface {
	Init(db *sql.DB)

	Name() dialect.Name
	Features() feature.Feature

	Tables() *Tables
	OnTable(table *Table)

	IdentQuote() byte

	AppendUint32(b []byte, n uint32) []byte
	AppendUint64(b []byte, n uint64) []byte
	AppendTime(b []byte, tm time.Time) []byte
	AppendString(b []byte, s string) []byte
	AppendBytes(b []byte, bs []byte) []byte
	AppendJSON(b, jsonb []byte) []byte
	AppendBool(b []byte, v bool) []byte

	// AppendSequence adds the appropriate instruction for the driver to create a sequence
	// from which (autoincremented) values for the column will be generated.
	AppendSequence(b []byte, t *Table, f *Field) []byte

	// DefaultVarcharLen should be returned for dialects in which specifying VARCHAR length
	// is mandatory in queries that modify the schema (CREATE TABLE / ADD COLUMN, etc).
	// Dialects that do not have such requirement may return 0, which should be interpreted so by the caller.
	DefaultVarcharLen() int

type Field

type Field struct {
	StructField reflect.StructField
	IsPtr       bool

	Tag          tagparser.Tag
	IndirectType reflect.Type
	Index        []int

	Name    string // SQL name, .e.g. id
	SQLName Safe   // escaped SQL name, e.g. "id"
	GoName  string // struct field name, e.g. Id

	DiscoveredSQLType  string
	UserSQLType        string
	CreateTableSQLType string
	SQLDefault         string

	OnDelete string
	OnUpdate string

	IsPK          bool
	NotNull       bool
	NullZero      bool
	AutoIncrement bool
	Identity      bool

	Append AppenderFunc
	Scan   ScannerFunc
	IsZero IsZeroerFunc

func (*Field) AppendValue

func (f *Field) AppendValue(fmter Formatter, b []byte, strct reflect.Value) []byte

func (*Field) Clone

func (f *Field) Clone() *Field

func (*Field) HasNilValue added in v1.1.1

func (f *Field) HasNilValue(v reflect.Value) bool

func (*Field) HasZeroValue

func (f *Field) HasZeroValue(v reflect.Value) bool

func (*Field) ScanValue

func (f *Field) ScanValue(strct reflect.Value, src interface{}) error

func (*Field) ScanWithCheck

func (f *Field) ScanWithCheck(fv reflect.Value, src interface{}) error

func (*Field) SkipUpdate added in v1.1.6

func (f *Field) SkipUpdate() bool

func (*Field) String

func (f *Field) String() string

func (*Field) Value

func (f *Field) Value(strct reflect.Value) reflect.Value

func (*Field) WithIndex added in v1.2.0

func (f *Field) WithIndex(path []int) *Field

type Formatter

type Formatter struct {
	// contains filtered or unexported fields

func NewFormatter

func NewFormatter(dialect Dialect) Formatter

func NewNopFormatter

func NewNopFormatter() Formatter

func (Formatter) AppendIdent

func (f Formatter) AppendIdent(b []byte, ident string) []byte

func (Formatter) AppendName added in v1.1.15

func (f Formatter) AppendName(b []byte, name string) []byte

func (Formatter) AppendQuery

func (f Formatter) AppendQuery(dst []byte, query string, args ...interface{}) []byte

func (Formatter) AppendValue

func (f Formatter) AppendValue(b []byte, v reflect.Value) []byte

func (Formatter) Dialect

func (f Formatter) Dialect() Dialect

func (Formatter) FormatQuery

func (f Formatter) FormatQuery(query string, args ...interface{}) string

func (Formatter) HasFeature

func (f Formatter) HasFeature(feature feature.Feature) bool

func (Formatter) IdentQuote

func (f Formatter) IdentQuote() byte

func (Formatter) IsNop

func (f Formatter) IsNop() bool

func (Formatter) WithArg

func (f Formatter) WithArg(arg NamedArgAppender) Formatter

func (Formatter) WithNamedArg added in v0.4.3

func (f Formatter) WithNamedArg(name string, value interface{}) Formatter

type Ident

type Ident string

Ident represents a SQL identifier, for example, a fully qualified column name such as `table_name.col_name`.

func (Ident) AppendQuery

func (s Ident) AppendQuery(fmter Formatter, b []byte) ([]byte, error)

type IsZeroerFunc

type IsZeroerFunc func(reflect.Value) bool

type Model added in v1.0.13

type Model interface {
	ScanRows(ctx context.Context, rows *sql.Rows) (int, error)
	Value() interface{}

type Name added in v1.1.15

type Name string

Name represents a single SQL name, for example, a column name.

func (Name) AppendQuery added in v1.1.15

func (s Name) AppendQuery(fmter Formatter, b []byte) ([]byte, error)

type NamedArgAppender added in v0.4.0

type NamedArgAppender interface {
	AppendNamedArg(fmter Formatter, b []byte, name string) ([]byte, bool)

type NullTime added in v0.1.17

type NullTime struct {

NullTime is a time.Time wrapper that marshals zero time as JSON null and SQL NULL.

func (NullTime) AppendQuery added in v0.1.17

func (tm NullTime) AppendQuery(fmter Formatter, b []byte) ([]byte, error)

func (NullTime) MarshalJSON added in v0.1.17

func (tm NullTime) MarshalJSON() ([]byte, error)

func (*NullTime) Scan added in v0.1.17

func (tm *NullTime) Scan(src interface{}) error

func (*NullTime) UnmarshalJSON added in v0.1.17

func (tm *NullTime) UnmarshalJSON(b []byte) error

type Query added in v1.0.5

type Query interface {
	Operation() string
	GetModel() Model
	GetTableName() string

type QueryAppender

type QueryAppender interface {
	AppendQuery(fmter Formatter, b []byte) ([]byte, error)

func In added in v1.1.2

func In(slice interface{}) QueryAppender

func NullZero added in v1.1.15

func NullZero(value interface{}) QueryAppender

type QueryWithArgs

type QueryWithArgs struct {
	Query string
	Args  []interface{}

func SafeQuery

func SafeQuery(query string, args []interface{}) QueryWithArgs

func UnsafeIdent

func UnsafeIdent(ident string) QueryWithArgs

func (QueryWithArgs) AppendQuery

func (q QueryWithArgs) AppendQuery(fmter Formatter, b []byte) ([]byte, error)

func (QueryWithArgs) IsZero

func (q QueryWithArgs) IsZero() bool

type QueryWithSep

type QueryWithSep struct {
	Sep string

func SafeQueryWithSep

func SafeQueryWithSep(query string, args []interface{}, sep string) QueryWithSep

type Relation

type Relation struct {
	Type       int
	Field      *Field
	JoinTable  *Table
	BaseFields []*Field
	JoinFields []*Field
	OnUpdate   string
	OnDelete   string
	Condition  []string

	PolymorphicField *Field
	PolymorphicValue string

	M2MTable      *Table
	M2MBaseFields []*Field
	M2MJoinFields []*Field

func (*Relation) References added in v1.1.17

func (r *Relation) References() bool

References returns true if the table to which the Relation belongs needs to declare a foreign key constraint to create the relation. For other relations, the constraint is created in either the referencing table (1:N, 'has-many' relations) or a mapping table (N:N, 'm2m' relations).

func (*Relation) String

func (r *Relation) String() string

type Safe

type Safe string

Safe represents a safe SQL query.

func (Safe) AppendQuery

func (s Safe) AppendQuery(fmter Formatter, b []byte) ([]byte, error)

type ScannerFunc

type ScannerFunc func(dest reflect.Value, src interface{}) error

func FieldScanner

func FieldScanner(dialect Dialect, field *Field) ScannerFunc

func PtrScanner added in v1.0.3

func PtrScanner(fn ScannerFunc) ScannerFunc

func Scanner

func Scanner(typ reflect.Type) ScannerFunc

type Table

type Table struct {
	Type      reflect.Type
	ZeroValue reflect.Value // reflect.Struct
	ZeroIface interface{}   // struct pointer

	TypeName  string
	ModelName string

	Name              string
	SQLName           Safe
	SQLNameForSelects Safe
	Alias             string
	SQLAlias          Safe

	Fields     []*Field // PKs + DataFields
	PKs        []*Field
	DataFields []*Field

	FieldMap  map[string]*Field
	StructMap map[string]*structField

	Relations map[string]*Relation
	Unique    map[string][]*Field

	SoftDeleteField       *Field
	UpdateSoftDeleteField func(fv reflect.Value, tm time.Time) error
	// contains filtered or unexported fields

Table represents a SQL table created from Go struct.

func (*Table) AppendNamedArg added in v0.4.3

func (t *Table) AppendNamedArg(
	fmter Formatter, b []byte, name string, strct reflect.Value,
) ([]byte, bool)

func (*Table) CheckPKs

func (t *Table) CheckPKs() error

func (*Table) Dialect

func (t *Table) Dialect() Dialect

func (*Table) Field

func (t *Table) Field(name string) (*Field, error)

func (*Table) HasAfterScanHook

func (t *Table) HasAfterScanHook() bool

DEPRECATED. Use HasAfterScanRowHook.

func (*Table) HasAfterScanRowHook added in v1.0.13

func (t *Table) HasAfterScanRowHook() bool

func (*Table) HasBeforeAppendModelHook added in v1.0.13

func (t *Table) HasBeforeAppendModelHook() bool

func (*Table) HasBeforeScanHook

func (t *Table) HasBeforeScanHook() bool

DEPRECATED. Use HasBeforeScanRowHook.

func (*Table) HasBeforeScanRowHook added in v1.0.13

func (t *Table) HasBeforeScanRowHook() bool

func (*Table) HasField

func (t *Table) HasField(name string) bool

func (*Table) LookupField added in v1.2.0

func (t *Table) LookupField(name string) *Field

func (*Table) String

func (t *Table) String() string

type Tables

type Tables struct {
	// contains filtered or unexported fields

func NewTables

func NewTables(dialect Dialect) *Tables

func (*Tables) ByModel

func (t *Tables) ByModel(name string) *Table

func (*Tables) ByName

func (t *Tables) ByName(name string) *Table

func (*Tables) Get

func (t *Tables) Get(typ reflect.Type) *Table

func (*Tables) InProgress added in v1.2.0

func (t *Tables) InProgress(typ reflect.Type) *Table

func (*Tables) Register

func (t *Tables) Register(models ...interface{})

Jump to

Keyboard shortcuts

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