Documentation
¶
Index ¶
- Constants
- func AppendBoolValue(gen QueryGen, b []byte, v reflect.Value) []byte
- func AppendFloat32Value(gen QueryGen, b []byte, v reflect.Value) []byte
- func AppendFloat64Value(gen QueryGen, b []byte, v reflect.Value) []byte
- func AppendIntValue(gen QueryGen, b []byte, v reflect.Value) []byte
- func AppendJSONValue(gen QueryGen, b []byte, v reflect.Value) []byte
- func AppendOrder(b []byte, sortDir Order) []byte
- func AppendQueryAppender(gen QueryGen, b []byte, app QueryAppender) []byte
- func AppendStringValue(gen QueryGen, b []byte, v reflect.Value) []byte
- func AppendUintValue(gen QueryGen, b []byte, v reflect.Value) []byte
- func DiscoverSQLType(typ reflect.Type) string
- func SetTableNameInflector(fn func(string) string)
- type AfterScanRowHook
- type AppenderFunc
- type BaseDialect
- func (BaseDialect) AppendBool(b []byte, v bool) []byte
- func (BaseDialect) AppendBytes(b, bs []byte) []byte
- func (BaseDialect) AppendJSON(b, jsonb []byte) []byte
- func (BaseDialect) AppendString(b []byte, s string) []byte
- func (BaseDialect) AppendTime(b []byte, tm time.Time) []byte
- func (BaseDialect) AppendUint32(b []byte, n uint32) []byte
- func (BaseDialect) AppendUint64(b []byte, n uint64) []byte
- type BaseModel
- type BeforeAppendModelHook
- type BeforeScanRowHook
- type ColumnsAppender
- type CustomAppender
- type Dialect
- type Field
- func (f *Field) AppendValue(gen QueryGen, b []byte, strct reflect.Value) []byte
- func (f *Field) Clone() *Field
- func (f *Field) HasNilValue(v reflect.Value) bool
- func (f *Field) HasZeroValue(v reflect.Value) bool
- func (f *Field) ScanValue(strct reflect.Value, src any) error
- func (f *Field) ScanWithCheck(fv reflect.Value, src any) error
- func (f *Field) SkipUpdate() bool
- func (f *Field) String() string
- func (f *Field) Value(strct reflect.Value) reflect.Value
- func (f *Field) WithIndex(path []int) *Field
- type Ident
- type IsZeroerFunc
- type Model
- type Name
- type NamedArgAppender
- type NullTime
- type Order
- type Query
- type QueryAppender
- type QueryGen
- func (gen QueryGen) Append(b []byte, v any) []byte
- func (f QueryGen) AppendIdent(b []byte, ident string) []byte
- func (f QueryGen) AppendName(b []byte, name string) []byte
- func (f QueryGen) AppendQuery(dst []byte, query string, args ...any) []byte
- func (f QueryGen) AppendValue(b []byte, v reflect.Value) []byte
- func (f QueryGen) Dialect() Dialect
- func (f QueryGen) FormatQuery(query string, args ...any) string
- func (f QueryGen) HasFeature(feature feature.Feature) bool
- func (f QueryGen) IdentQuote() byte
- func (f QueryGen) IsNop() bool
- func (f QueryGen) WithArg(arg NamedArgAppender) QueryGen
- func (f QueryGen) WithNamedArg(name string, value any) QueryGen
- type QueryWithArgs
- type QueryWithSep
- type Relation
- type Safe
- type ScannerFunc
- type Table
- func (t *Table) AppendNamedArg(gen QueryGen, b []byte, name string, strct reflect.Value) ([]byte, bool)
- func (t *Table) CheckPKs() error
- func (t *Table) Dialect() Dialect
- func (t *Table) Field(name string) (*Field, error)
- func (t *Table) HasAfterScanRowHook() bool
- func (t *Table) HasBeforeAppendModelHook() bool
- func (t *Table) HasBeforeScanRowHook() bool
- func (t *Table) HasField(name string) bool
- func (t *Table) LookupField(name string) *Field
- func (t *Table) String() string
- type Tables
Constants ¶
const ( InvalidRelation = iota HasOneRelation BelongsToRelation HasManyRelation ManyToManyRelation )
Variables ¶
This section is empty.
Functions ¶
func AppendBoolValue ¶ added in v0.1.6
func AppendFloat32Value ¶ added in v0.1.6
func AppendFloat64Value ¶ added in v0.1.6
func AppendJSONValue ¶ added in v0.3.4
func AppendOrder ¶ added in v1.2.16
func AppendQueryAppender ¶
func AppendQueryAppender(gen QueryGen, b []byte, app QueryAppender) []byte
func AppendStringValue ¶ added in v0.3.4
func DiscoverSQLType ¶ added in v0.1.17
func SetTableNameInflector ¶
SetTableNameInflector overrides the default func that pluralizes model name to get table name, e.g. my_article becomes my_articles.
Types ¶
type AfterScanRowHook ¶ added in v1.0.13
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 BeforeAppendModelHook ¶ added in v1.0.13
type BeforeScanRowHook ¶ added in v1.0.13
type ColumnsAppender ¶
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
// DefaultSchema should returns the name of the default database schema.
DefaultSchema() string
}
type Field ¶
type Field struct {
Table *Table // Contains this field
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 (*Field) SkipUpdate ¶ added in v1.1.6
type Ident ¶
type Ident string
Ident represents a SQL identifier, for example, a fully qualified column name such as `table_name.col_name`.
type IsZeroerFunc ¶
type Name ¶ added in v1.1.15
type Name string
Name represents a single SQL name, for example, a column name.
type NamedArgAppender ¶ added in v0.4.0
type NullTime ¶ added in v0.1.17
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 (NullTime) MarshalJSON ¶ added in v0.1.17
func (*NullTime) UnmarshalJSON ¶ added in v0.1.17
type Query ¶ added in v1.0.5
type Query interface {
QueryAppender
Operation() string
GetModel() Model
GetTableName() string
}
type QueryAppender ¶
func In ¶ added in v1.1.2
func In(slice any) QueryAppender
func NullZero ¶ added in v1.1.15
func NullZero(value any) QueryAppender
type QueryGen ¶ added in v1.2.16
type QueryGen struct {
// contains filtered or unexported fields
}
func NewNopQueryGen ¶ added in v1.2.16
func NewNopQueryGen() QueryGen
func NewQueryGen ¶ added in v1.2.16
func (QueryGen) AppendIdent ¶ added in v1.2.16
func (QueryGen) AppendName ¶ added in v1.2.16
func (QueryGen) AppendQuery ¶ added in v1.2.16
func (QueryGen) AppendValue ¶ added in v1.2.16
func (QueryGen) FormatQuery ¶ added in v1.2.16
func (QueryGen) HasFeature ¶ added in v1.2.16
func (QueryGen) IdentQuote ¶ added in v1.2.16
func (QueryGen) WithArg ¶ added in v1.2.16
func (f QueryGen) WithArg(arg NamedArgAppender) QueryGen
type QueryWithArgs ¶
NOTE: It should not be modified after creation.
func SafeQuery ¶
func SafeQuery(query string, args []any) QueryWithArgs
func UnsafeIdent ¶
func UnsafeIdent(ident string) QueryWithArgs
func (QueryWithArgs) AppendQuery ¶
func (q QueryWithArgs) AppendQuery(gen QueryGen, b []byte) ([]byte, error)
func (QueryWithArgs) IsZero ¶
func (q QueryWithArgs) IsZero() bool
type QueryWithSep ¶
type QueryWithSep struct {
QueryWithArgs
Sep string
}
func SafeQueryWithSep ¶
func SafeQueryWithSep(query string, args []any, sep string) QueryWithSep
type Relation ¶
type Relation struct {
Type int
Field *Field // Has the bun tag defining this relation.
// Base and Join can be explained with this query:
//
// SELECT * FROM base_table JOIN join_table
JoinTable *Table
BasePKs []*Field
JoinPKs []*Field
OnUpdate string
OnDelete string
Condition []string
PolymorphicField *Field
PolymorphicValue string
M2MTable *Table
M2MBasePKs []*Field
M2MJoinPKs []*Field
}
func (*Relation) References ¶ added in v1.1.17
References returns true if the table which defines this Relation needs to declare a foreign key constraint, as is the case for 'has-one' and 'belongs-to' relations. For other relations, the constraint is created either in the referencing table (1:N, 'has-many' relations) or the junction table (N:N, 'm2m' relations).
Usage of `rel:` tag does not always imply creation of foreign keys (when WithForeignKeys() is not set) and can be used exclusively for joining tables at query time. For example:
type User struct {
ID int64 `bun:",pk"`
Profile *Profile `bun:",rel:has-one,join:id=user_id"`
}
Creating a FK users.id -> profiles.user_id would be confusing and incorrect, so for such cases References() returns false. One notable exception to this rule is when a Relation is defined in a junction table, in which case it is perfectly fine for its primary keys to reference other tables. Consider:
// UsersToGroups maps users to groups they follow.
type UsersToGroups struct {
UserID string `bun:"user_id,pk"` // Needs FK to users.id
GroupID string `bun:"group_id,pk"` // Needs FK to groups.id
User *User `bun:"rel:belongs-to,join:user_id=id"`
Group *Group `bun:"rel:belongs-to,join:group_id=id"`
}
Here BooksToReaders has a composite primary key, composed of other primary keys.
type ScannerFunc ¶
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 any // struct pointer
TypeName string
ModelName string
Schema 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
IsM2MTable bool // If true, this table is the "junction table" of an m2m relation.
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.