Documentation ¶
Index ¶
- Constants
- func Migrate(id string, init func(m *M))
- func RegisterConnectionAdapter(adapter string, ca ConnectionAdapter)
- func RemoveConnection(name string) error
- func Transaction(ctx context.Context, fn func() error) error
- type ActiveRecord
- func (r *ActiveRecord) Context() context.Context
- func (r *ActiveRecord) Copy() *ActiveRecord
- func (r *ActiveRecord) Delete() (*ActiveRecord, error)
- func (v *ActiveRecord) Errors() Errors
- func (r *ActiveRecord) Insert() (*ActiveRecord, error)
- func (r *ActiveRecord) IsValid() bool
- func (r *ActiveRecord) Name() string
- func (r *ActiveRecord) String() string
- func (r *ActiveRecord) ToHash() Hash
- func (r *ActiveRecord) Update() (*ActiveRecord, error)
- func (r *ActiveRecord) Validate() error
- func (r *ActiveRecord) WithContext(ctx context.Context) *ActiveRecord
- type Array
- type Association
- type AssociationAccessors
- type AssociationMethods
- type AssociationReflection
- func (a AssociationReflection) AccessAssociation(assocName string) (*ActiveRecord, error)
- func (a AssociationReflection) AccessCollection(collName string) (*Relation, error)
- func (a AssociationReflection) AssignAssociation(assocName string, target *ActiveRecord) error
- func (a AssociationReflection) AssignCollection(collName string, targets ...*ActiveRecord) error
- func (a AssociationReflection) Association(assocName string) RecordResult
- func (a AssociationReflection) AssociationNames() []string
- func (a AssociationReflection) Collection(collName string) CollectionResult
- func (v AssociationReflection) Errors() Errors
- func (a AssociationReflection) HasAssociation(assocName string) bool
- func (a AssociationReflection) HasAssociations(assocNames ...string) bool
- func (a AssociationReflection) ReflectOnAllAssociations() []*AssociationReflection
- func (a AssociationReflection) ReflectOnAssociation(assocName string) *AssociationReflection
- type Attribute
- type AttributeAccessors
- type AttributeMethods
- type AttributeValidator
- type BelongsTo
- func (a *BelongsTo) AccessAssociation(owner *ActiveRecord) RecordResult
- func (a *BelongsTo) AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult
- func (a *BelongsTo) AssociationForeignKey() string
- func (a *BelongsTo) AssociationName() string
- func (a *BelongsTo) AssociationOwner() *Relation
- func (a *BelongsTo) ForeignKey(fk string)
- func (a *BelongsTo) String() string
- type Boolean
- type CollectionAccessors
- type CollectionAssociation
- type CollectionResult
- type ColumnDefinition
- type ColumnValue
- type Conn
- type ConnectionAdapter
- type DatabaseConfig
- type DatabaseStatements
- type Date
- type DateTime
- type DeleteOperation
- type Dependency
- type ErrAdapterNotFound
- type ErrAssociation
- type ErrConnectionNotEstablished
- type ErrInvalidType
- type ErrInvalidValue
- type ErrRecordNotFound
- type ErrRecordNotUnique
- type ErrTableNotExist
- type ErrType
- type ErrUnknownAssociation
- type ErrUnknownAttribute
- type ErrUnknownPrimaryKey
- type ErrUnsupportedType
- type ErrValidation
- type Errors
- type Exclusion
- type Float64
- type Format
- type HasMany
- type HasOne
- func (a *HasOne) AccessAssociation(owner *ActiveRecord) RecordResult
- func (a *HasOne) AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult
- func (a *HasOne) AssociationForeignKey() string
- func (a *HasOne) AssociationName() string
- func (a *HasOne) AssociationOwner() *Relation
- func (a *HasOne) String() string
- type Inclusion
- type InsertOperation
- type Int64
- type JSON
- type Length
- type M
- type Nil
- type Ownership
- type Persistence
- type Predicate
- type Presence
- type PrimaryKey
- type QueryBuilder
- func (q *QueryBuilder) Args() []interface{}
- func (q *QueryBuilder) From(from string)
- func (q *QueryBuilder) Group(values ...string)
- func (q *QueryBuilder) Join(rel *Relation, assoc Association)
- func (q *QueryBuilder) Limit(num int)
- func (q *QueryBuilder) Operation() *QueryOperation
- func (q *QueryBuilder) Select(columns ...string)
- func (q *QueryBuilder) String() string
- func (q *QueryBuilder) Where(cond string, args ...interface{})
- type QueryMethods
- type QueryOperation
- type R
- func (r *R) BelongsTo(name string, init ...func(*BelongsTo))
- func (r *R) DefineAttribute(name string, t Type, validators ...AttributeValidator)
- func (r *R) HasMany(name string)
- func (r *R) HasOne(name string)
- func (r *R) PrimaryKey(name string)
- func (r *R) TableName(name string)
- func (r *R) Validates(name string, validator AttributeValidator)
- func (r *R) ValidatesPresence(names ...string)
- type RecordResult
- func (r RecordResult) AssignAssociation(name string, target RecordResult) RecordResult
- func (r RecordResult) AssignCollection(name string, targets ...RecordResult) RecordResult
- func (r RecordResult) Association(name string) RecordResult
- func (r RecordResult) Collection(name string) CollectionResult
- func (r RecordResult) Delete() RecordResult
- func (r RecordResult) Insert() RecordResult
- func (r RecordResult) Update() RecordResult
- type References
- type Reflection
- type Relation
- func (a *Relation) AccessAssociation(assocName string) (*ActiveRecord, error)
- func (a *Relation) AccessCollection(collName string) (*Relation, error)
- func (rel *Relation) All() CollectionResult
- func (a *Relation) AssignAssociation(assocName string, target *ActiveRecord) error
- func (a *Relation) AssignCollection(collName string, targets ...*ActiveRecord) error
- func (a *Relation) Association(assocName string) RecordResult
- func (a *Relation) AssociationNames() []string
- func (a *Relation) Collection(collName string) CollectionResult
- func (rel *Relation) ColumnNames() []string
- func (rel *Relation) Connect(conn Conn) *Relation
- func (rel *Relation) Connection() Conn
- func (rel *Relation) Context() context.Context
- func (rel *Relation) Copy() *Relation
- func (rel *Relation) Create(params map[string]interface{}) RecordResult
- func (rel *Relation) Each(fn func(*ActiveRecord) error) error
- func (v *Relation) Errors() Errors
- func (rel *Relation) ExtractRecord(h Hash) (*ActiveRecord, error)
- func (rel *Relation) Find(id interface{}) RecordResult
- func (rel *Relation) FindBy(cond string, arg interface{}) RecordResult
- func (rel *Relation) First() RecordResult
- func (rel *Relation) Group(attrNames ...string) *Relation
- func (a *Relation) HasAssociation(assocName string) bool
- func (a *Relation) HasAssociations(assocNames ...string) bool
- func (rel *Relation) Initialize(params map[string]interface{}) (*ActiveRecord, error)
- func (rel *Relation) InsertAll(params ...map[string]interface{}) (rr []*ActiveRecord, err error)
- func (rel *Relation) IsEmpty() bool
- func (rel *Relation) Joins(assocNames ...string) *Relation
- func (rel *Relation) Limit(num int) *Relation
- func (rel *Relation) Name() string
- func (rel *Relation) New(params ...map[string]interface{}) RecordResult
- func (rel *Relation) PrimaryKey() string
- func (a *Relation) ReflectOnAllAssociations() []*AssociationReflection
- func (a *Relation) ReflectOnAssociation(assocName string) *AssociationReflection
- func (rel *Relation) Select(attrNames ...string) *Relation
- func (rel *Relation) String() string
- func (rel *Relation) TableName() string
- func (rel *Relation) ToA() (Array, error)
- func (rel *Relation) ToSQL() string
- func (rel *Relation) Where(cond string, arg interface{}) *Relation
- func (rel *Relation) WithContext(ctx context.Context) *Relation
- type SchemaStatements
- type SingularAssociation
- type String
- type Table
- func (tb *Table) Columns() (columns []ColumnDefinition)
- func (tb *Table) DateTime(name string)
- func (tb *Table) DefineColumn(name string, t Type)
- func (tb *Table) ForeignKey(target string)
- func (tb *Table) ForeignKeys() []string
- func (tb *Table) Int64(name string)
- func (tb *Table) Name() string
- func (tb *Table) PrimaryKey(primaryKey string)
- func (tb *Table) References(target string, init ...References)
- func (tb *Table) String(name string)
- type Time
- type TransactionStatements
- type Type
- type UpdateOperation
- type Validator
Constants ¶
const (
SchemaMigrationsName = "schema_migrations"
)
Variables ¶
This section is empty.
Functions ¶
func RegisterConnectionAdapter ¶
func RegisterConnectionAdapter(adapter string, ca ConnectionAdapter)
func RemoveConnection ¶
Types ¶
type ActiveRecord ¶
type ActiveRecord struct { AttributeMethods AttributeAccessors AssociationMethods AssociationAccessors CollectionAccessors // contains filtered or unexported fields }
func (*ActiveRecord) Context ¶
func (r *ActiveRecord) Context() context.Context
func (*ActiveRecord) Copy ¶
func (r *ActiveRecord) Copy() *ActiveRecord
func (*ActiveRecord) Delete ¶
func (r *ActiveRecord) Delete() (*ActiveRecord, error)
func (*ActiveRecord) Insert ¶
func (r *ActiveRecord) Insert() (*ActiveRecord, error)
func (*ActiveRecord) IsValid ¶
func (r *ActiveRecord) IsValid() bool
IsValid runs all the validations, returns true if no errors are found, false othewrise. Alias for Validate.
func (*ActiveRecord) Name ¶
func (r *ActiveRecord) Name() string
func (*ActiveRecord) String ¶
func (r *ActiveRecord) String() string
func (*ActiveRecord) ToHash ¶
func (r *ActiveRecord) ToHash() Hash
func (*ActiveRecord) Update ¶
func (r *ActiveRecord) Update() (*ActiveRecord, error)
func (*ActiveRecord) Validate ¶
func (r *ActiveRecord) Validate() error
Validate runs all the validation, returns unpassed validations, nil otherwise.
func (*ActiveRecord) WithContext ¶
func (r *ActiveRecord) WithContext(ctx context.Context) *ActiveRecord
type Array ¶
type Array []*ActiveRecord
func (Array) ToHashArray ¶
func (arr Array) ToHashArray() []activesupport.Hash
type Association ¶
type AssociationAccessors ¶
type AssociationAccessors interface { // AssignAssociation(string, assoc *ActiveRecord) error Association(assocName string) RecordResult AccessAssociation(assocName string) (*ActiveRecord, error) }
type AssociationMethods ¶
type AssociationMethods interface { AssociationNames() []string HasAssociation(assocName string) bool HasAssociations(assocNames ...string) bool ReflectOnAssociation(assocName string) *AssociationReflection ReflectOnAllAssociations() []*AssociationReflection }
type AssociationReflection ¶
type AssociationReflection struct { *Relation Association }
func (AssociationReflection) AccessAssociation ¶
func (a AssociationReflection) AccessAssociation(assocName string) (*ActiveRecord, error)
func (AssociationReflection) AccessCollection ¶
func (AssociationReflection) AssignAssociation ¶
func (a AssociationReflection) AssignAssociation(assocName string, target *ActiveRecord) error
func (AssociationReflection) AssignCollection ¶
func (a AssociationReflection) AssignCollection(collName string, targets ...*ActiveRecord) error
func (AssociationReflection) Association ¶
func (a AssociationReflection) Association(assocName string) RecordResult
func (AssociationReflection) AssociationNames ¶
func (a AssociationReflection) AssociationNames() []string
func (AssociationReflection) Collection ¶
func (a AssociationReflection) Collection(collName string) CollectionResult
func (AssociationReflection) HasAssociation ¶
func (AssociationReflection) HasAssociations ¶
func (AssociationReflection) ReflectOnAllAssociations ¶
func (a AssociationReflection) ReflectOnAllAssociations() []*AssociationReflection
ReflectOnAllAssociations returns an array of AssociationReflection types for all associations in the Relation.
func (AssociationReflection) ReflectOnAssociation ¶
func (a AssociationReflection) ReflectOnAssociation(assocName string) *AssociationReflection
ReflectOnAssociation returns AssociationReflection for the specified association.
type AttributeAccessors ¶
type AttributeMethods ¶
type AttributeValidator ¶
type AttributeValidator interface { ValidateAttribute(r *ActiveRecord, attrName string, value interface{}) error AllowsNil() bool AllowsBlank() bool }
type BelongsTo ¶
type BelongsTo struct {
// contains filtered or unexported fields
}
func (*BelongsTo) AccessAssociation ¶
func (a *BelongsTo) AccessAssociation(owner *ActiveRecord) RecordResult
AccessAssociation returns a record of the target.
activerecord.New("owner", func(r *activerecord.R) { r.BelongsTo("target") })
This association considers the following tables relation:
+------------------------+ +----------------+ | owners | | targets | +------------+-----------+ +------+---------+ | id | integer | +-->| id | integer | pk | target_id | string |*---+ | name | string | | updated_at | timestamp | +------+---------+ +------------+-----------+
func (*BelongsTo) AssignAssociation ¶
func (a *BelongsTo) AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult
func (*BelongsTo) AssociationForeignKey ¶
func (*BelongsTo) AssociationName ¶
func (*BelongsTo) AssociationOwner ¶
func (*BelongsTo) ForeignKey ¶
ForeignKey sets the foreign key used for the association. By default this is guessed to be the name of this relation in lower-case and "_id" suffixed.
So a relation that defines a BelongsTo("person") association will use "person_id" as a default foreign key.
type CollectionAccessors ¶
type CollectionAccessors interface { // AssignCollection(collName string, coll []*ActiveRecord) error Collection(collName string) CollectionResult AccessCollection(collName string) (*Relation, error) }
type CollectionAssociation ¶
type CollectionAssociation interface { Association AssignCollection(owner *ActiveRecord, targets ...*ActiveRecord) RecordResult AccessCollection(owner *ActiveRecord) CollectionResult }
type CollectionResult ¶
type CollectionResult struct { Result[*Relation] }
func ErrCollection ¶
func ErrCollection(err error) CollectionResult
func OkCollection ¶
func OkCollection(rel *Relation) CollectionResult
func (CollectionResult) DeleteAll ¶
func (c CollectionResult) DeleteAll() error
func (CollectionResult) ToA ¶
func (c CollectionResult) ToA() (Array, error)
type ColumnDefinition ¶
type ColumnValue ¶
type Conn ¶
type Conn interface { TransactionStatements DatabaseStatements SchemaStatements Close() error }
func EstablishConnection ¶
func EstablishConnection(c DatabaseConfig) (Conn, error)
EstablishConnection establishes connection to the database. Accepts a configuration as input where Adapter key must be specified with the name of a database adapter (in lower-case).
Example for PostgreSQL database:
activerecord.EstablishConnection(activerecord.DatabaseConfig{ Adapter: "postgresql", Host: "localhost", Username: "pguser", Password: "pgpass", Database: "somedatabase", })
func RetrieveConnection ¶
type ConnectionAdapter ¶
type ConnectionAdapter func(DatabaseConfig) (Conn, error)
type DatabaseConfig ¶
type DatabaseStatements ¶
type DatabaseStatements interface { ExecInsert(ctx context.Context, op *InsertOperation) (id interface{}, err error) ExecUpdate(ctx context.Context, op *UpdateOperation) (err error) ExecDelete(ctx context.Context, op *DeleteOperation) (err error) ExecQuery(ctx context.Context, op *QueryOperation, cb func(activesupport.Hash) bool) (err error) }
type DeleteOperation ¶
type Dependency ¶
type ErrAdapterNotFound ¶
type ErrAdapterNotFound struct {
Adapter string
}
ErrAdapterNotFound is returned when Active Record cannot find database specified database adapter.
func (*ErrAdapterNotFound) Error ¶
func (e *ErrAdapterNotFound) Error() string
type ErrAssociation ¶
type ErrAssociation struct {
Message string
}
func (ErrAssociation) Error ¶
func (e ErrAssociation) Error() string
type ErrConnectionNotEstablished ¶
type ErrConnectionNotEstablished struct {
Name string
}
ErrConnectionNotEstablished is returned when connection to the database is not established.
func (*ErrConnectionNotEstablished) Error ¶
func (e *ErrConnectionNotEstablished) Error() string
type ErrInvalidType ¶
func (ErrInvalidType) Error ¶
func (e ErrInvalidType) Error() string
type ErrInvalidValue ¶
func (ErrInvalidValue) Error ¶
func (e ErrInvalidValue) Error() string
type ErrRecordNotFound ¶
type ErrRecordNotFound struct { PrimaryKey string ID interface{} }
func (*ErrRecordNotFound) Error ¶
func (e *ErrRecordNotFound) Error() string
func (*ErrRecordNotFound) Is ¶
func (e *ErrRecordNotFound) Is(target error) bool
type ErrRecordNotUnique ¶
type ErrRecordNotUnique struct {
Err error
}
func (*ErrRecordNotUnique) Error ¶
func (e *ErrRecordNotUnique) Error() string
func (*ErrRecordNotUnique) Is ¶
func (e *ErrRecordNotUnique) Is(target error) bool
type ErrTableNotExist ¶
type ErrTableNotExist struct {
TableName string
}
func (ErrTableNotExist) Error ¶
func (e ErrTableNotExist) Error() string
type ErrUnknownAssociation ¶
func (ErrUnknownAssociation) Error ¶
func (e ErrUnknownAssociation) Error() string
type ErrUnknownAttribute ¶
ErrUnknownAttribute is returned on attempt to assign unknown attribute to the ActiveRecord.
func (*ErrUnknownAttribute) Error ¶
func (e *ErrUnknownAttribute) Error() string
Error returns a string representation of the error.
type ErrUnknownPrimaryKey ¶
func (*ErrUnknownPrimaryKey) Error ¶
func (e *ErrUnknownPrimaryKey) Error() string
type ErrUnsupportedType ¶
type ErrUnsupportedType struct {
TypeName string
}
func (ErrUnsupportedType) Error ¶
func (e ErrUnsupportedType) Error() string
type ErrValidation ¶
type ErrValidation struct { Model *ActiveRecord Errors Errors }
func (ErrValidation) Error ¶
func (e ErrValidation) Error() string
type Errors ¶
type Errors struct {
// contains filtered or unexported fields
}
func (*Errors) FullMessages ¶
type Exclusion ¶
Exclusion validates that specified value of the attribute is not in a slice.
Supplier := activerecord.New("supplier", func(r *activerecord.R) { r.Validates("password", &activerecord.Exclusion{From: Strings("12345678")}) })
func (*Exclusion) AllowsBlank ¶
AllowsBlank returns true when blank values are allowed, and false otherwise.
func (*Exclusion) AllowsNil ¶
AllowsNil returns true when nil values are allowed, and false otherwise.
func (*Exclusion) ValidateAttribute ¶
func (e *Exclusion) ValidateAttribute(r *ActiveRecord, attrName string, val interface{}) error
ValidateAttribute validates that the specified value is not in a slice. When it is, method returns ErrInvalidValue error.
type Format ¶
type Format struct { With Str Without Str AllowNil bool AllowBlank bool // Message is a customer error message (default is "has invalid format"). Message string // contains filtered or unexported fields }
Format validates that specified value of the attribute is of the correct form, going by the regular expression provided.
Use `With` property to require that the attribute matches the regular expression:
Supplier := activerecord.New("supplier", func(r *activerecord.R) { r.Validates("name", &activerecord.Format{With: `[a-z]+`}) })
Use `Without` property to require that the attribute does not match the regular epxression:
Supplier := activerecord.New("supplier", func(r *activerecord.R) { r.Validates("phone", &activerecord.Format{Without: `[a-zA-Z]`}) })
You must pass either `With` or `Without` as parameters. When both are empty strings or both expressions are specified, an error ErrArgument is returned.
func (*Format) AllowsBlank ¶
AllowsBlank returns true when blank values are allowed, and false otherwise.
func (*Format) Initialize ¶
Initialize ensures the correctness of the parameters and compiles the given regular expression. When parameters are not valid, or regular expression is not compilable, method returns an error.
func (*Format) ValidateAttribute ¶
func (f *Format) ValidateAttribute(r *ActiveRecord, attrName string, val interface{}) error
ValidateAttribute validates that the given value is in the required format.
When value type is not a string, method returns ErrInvlidType error. In case of failed validation, method returns ErrInvalidValue error.
type HasMany ¶
type HasMany struct {
// contains filtered or unexported fields
}
func (*HasMany) AccessCollection ¶
func (a *HasMany) AccessCollection(owner *ActiveRecord) CollectionResult
AccessCollection returns a collection of the target records.
HasMany association indicates a one-to-many association with another model. The association indicates that each instance of the model has zero or more instances of target model.
activerecord.New("owner", func(r *activerecord.R) { r.HasMany("targets") })
This association considers the following tables relation:
+----------------+ +--------------------+ | owners | | targets | +------+---------+ +----------+---------+ | id | integer |<---+ | id | integer | | name | string | +---*| owner_id | integer | +------+---------+ | name | string | +----------+---------+
func (*HasMany) AssignCollection ¶
func (a *HasMany) AssignCollection(owner *ActiveRecord, targets ...*ActiveRecord) RecordResult
func (*HasMany) AssociationForeignKey ¶
func (*HasMany) AssociationName ¶
type HasOne ¶
type HasOne struct {
// contains filtered or unexported fields
}
func (*HasOne) AccessAssociation ¶
func (a *HasOne) AccessAssociation(owner *ActiveRecord) RecordResult
The association indicates that one model has a reference to this model. That "target" model can be fetched through this association.
activerecord.New("owner", func(r *activerecord.R) { r.HasOne("target") })
This association considers the following tables relation:
+----------------+ +--------------------+ | owners | | targets | +------+---------+ +----------+---------+ | id | integer |<---+ | id | integer | | name | string | +---*| owner_id | integer | +------+---------+ | name | string | +----------+---------+
func (*HasOne) AssignAssociation ¶
func (a *HasOne) AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult
func (*HasOne) AssociationForeignKey ¶
func (*HasOne) AssociationName ¶
func (*HasOne) AssociationOwner ¶
type Inclusion ¶
Inclusion validates that the specified value of the attribute is available in a slice.
Supplier := activerecord.New("supplier", func(r *activerecord.R) { r.Validates("state", &activerecord.Inclusion{In: Strings("NY", "MA")}) })
func (*Inclusion) AllowsBlank ¶
AllowsBlank returns true when blank values are allowed, and false otherwise.
func (*Inclusion) AllowsNil ¶
AllowsNil returnd true when nil values are allowed, and false otherwise.
func (*Inclusion) ValidateAttribute ¶
func (i *Inclusion) ValidateAttribute(r *ActiveRecord, attrName string, val interface{}) error
ValidateAttribute validates that the given value is in the provided slice. When it's not, method returns ErrInvalidValue error.
type InsertOperation ¶
type InsertOperation struct { TableName string ColumnValues []ColumnValue OnDuplicate string ConflictTarget string }
type Length ¶
type Length struct { // Minimum is the minimum length of the attribute. Minimum int // Maximum is the maximum length of the attribute. Maximum int // AllowNil skips validation, when attribute is nil. AllowNil bool // AllowBlank skips validation, when attribute is blank. AllowBlank bool }
Length validates that the specified value of the attribute match the length restrictions supplied.
Supplier := activerecord.New("supplier", func(r *activerecord.R) { r.Validates("phone", &activerecord.Length{Minumum: 7, Maximum: 32}) r.Validates("zip_code", &activerecord.Length{Minimum: 5}) })
func (*Length) AllowsBlank ¶
AllowsBlank returns true when blank values are allowed, and false otherwise.
func (*Length) Initialize ¶
Initialize ensures the correctness of the parameters. When parameters are not valid: both `Mininum` and `Maximum` must be positive numbers, and `Minimum < Maximum`, method returns ErrArgument error.
func (*Length) ValidateAttribute ¶
func (l *Length) ValidateAttribute(r *ActiveRecord, attrName string, val interface{}) error
ValidateAttribute validates that the specified value comply the length restrictions.
Method returns ErrInvalidType for non-character-based types and ErrInvalidValue, when length restrictions are not met.
type M ¶
type M struct {
// contains filtered or unexported fields
}
func (*M) AddForeignKey ¶
func (*M) CreateTable ¶
func (*M) TableExists ¶
type Persistence ¶
type Persistence interface { Insert() (*ActiveRecord, error) Update() (*ActiveRecord, error) Delete() error IsPersisted() bool }
type Presence ¶
type Presence struct { AllowNil bool // Message is a custom error message (default is "can't be blank"). Message string }
Presence validates that specified value of the attribute is not blank (as defined by activesupport.IsBlank).
Supplier := activerecord.New("supplier", func(r *activerecord.R) { r.HasOne("account") r.Validates("account", &activegraph.Presence{}) })
The account attribute must be in the object and it cannot be blank.
func (*Presence) AllowsBlank ¶
AllowsBlank returns false, since blank values are not allowed for this validator.
func (*Presence) ValidateAttribute ¶
func (p *Presence) ValidateAttribute(r *ActiveRecord, attrName string, val interface{}) error
ValidateAttribute returns ErrInvalidValue when specified value is blank.
type PrimaryKey ¶
type PrimaryKey struct {
Attribute
}
PrimaryKey makes any specified attribute a primary key.
func (PrimaryKey) PrimaryKey ¶
func (p PrimaryKey) PrimaryKey() bool
PrimaryKey always returns true.
type QueryBuilder ¶
type QueryBuilder struct {
// contains filtered or unexported fields
}
func (*QueryBuilder) Args ¶
func (q *QueryBuilder) Args() []interface{}
func (*QueryBuilder) From ¶
func (q *QueryBuilder) From(from string)
func (*QueryBuilder) Group ¶
func (q *QueryBuilder) Group(values ...string)
func (*QueryBuilder) Join ¶
func (q *QueryBuilder) Join(rel *Relation, assoc Association)
func (*QueryBuilder) Limit ¶
func (q *QueryBuilder) Limit(num int)
func (*QueryBuilder) Operation ¶
func (q *QueryBuilder) Operation() *QueryOperation
func (*QueryBuilder) Select ¶
func (q *QueryBuilder) Select(columns ...string)
func (*QueryBuilder) String ¶
func (q *QueryBuilder) String() string
func (*QueryBuilder) Where ¶
func (q *QueryBuilder) Where(cond string, args ...interface{})
type QueryMethods ¶
type QueryOperation ¶
type R ¶
type R struct {
// contains filtered or unexported fields
}
func (*R) DefineAttribute ¶
func (r *R) DefineAttribute(name string, t Type, validators ...AttributeValidator)
func (*R) PrimaryKey ¶
func (*R) TableName ¶
TableName sets the table name explicitly.
Vertex := activerecord.New("vertex", func(r *activerecord.R) { r.TableName("vertices") })
func (*R) Validates ¶
func (r *R) Validates(name string, validator AttributeValidator)
func (*R) ValidatesPresence ¶
type RecordResult ¶
type RecordResult struct { Result[*ActiveRecord] }
func ErrRecord ¶
func ErrRecord(err error) RecordResult
func OkRecord ¶
func OkRecord(r *ActiveRecord) RecordResult
func ReturnRecord ¶
func ReturnRecord(r *ActiveRecord, err error) RecordResult
func (RecordResult) AssignAssociation ¶
func (r RecordResult) AssignAssociation(name string, target RecordResult) RecordResult
func (RecordResult) AssignCollection ¶
func (r RecordResult) AssignCollection(name string, targets ...RecordResult) RecordResult
func (RecordResult) Association ¶
func (r RecordResult) Association(name string) RecordResult
func (RecordResult) Collection ¶
func (r RecordResult) Collection(name string) CollectionResult
func (RecordResult) Delete ¶
func (r RecordResult) Delete() RecordResult
func (RecordResult) Insert ¶
func (r RecordResult) Insert() RecordResult
func (RecordResult) Update ¶
func (r RecordResult) Update() RecordResult
type References ¶
type References struct {
ForeignKey bool
}
type Reflection ¶
type Reflection struct {
// contains filtered or unexported fields
}
func NewReflection ¶
func NewReflection() *Reflection
func (*Reflection) AddReflection ¶
func (r *Reflection) AddReflection(name string, rel *Relation)
func (*Reflection) Reflection ¶
func (r *Reflection) Reflection(name string) (*Relation, error)
type Relation ¶
type Relation struct { AttributeMethods // contains filtered or unexported fields }
func (*Relation) AccessAssociation ¶
func (a *Relation) AccessAssociation(assocName string) (*ActiveRecord, error)
func (*Relation) AccessCollection ¶
func (*Relation) All ¶
func (rel *Relation) All() CollectionResult
func (*Relation) AssignAssociation ¶
func (a *Relation) AssignAssociation(assocName string, target *ActiveRecord) error
func (*Relation) AssignCollection ¶
func (a *Relation) AssignCollection(collName string, targets ...*ActiveRecord) error
func (*Relation) Association ¶
func (a *Relation) Association(assocName string) RecordResult
func (*Relation) AssociationNames ¶
func (a *Relation) AssociationNames() []string
func (*Relation) Collection ¶
func (a *Relation) Collection(collName string) CollectionResult
func (*Relation) ColumnNames ¶
TODO: move to the Schema type all column-related methods.
func (*Relation) Connection ¶
func (*Relation) Create ¶
func (rel *Relation) Create(params map[string]interface{}) RecordResult
func (*Relation) ExtractRecord ¶
func (rel *Relation) ExtractRecord(h Hash) (*ActiveRecord, error)
func (*Relation) Find ¶
func (rel *Relation) Find(id interface{}) RecordResult
func (*Relation) FindBy ¶
func (rel *Relation) FindBy(cond string, arg interface{}) RecordResult
FindBy returns a record matching the specified condition.
person := Person.FindBy("name", "Bill") // Ok(Some(#<Person id: 1, name: "Bill", occupation: "retired">)) person := Person.FindBy("salary > ?", 10000) // Ok(Some(#<Person id: 3, name: "Jeff", occupation: "CEO">))
func (*Relation) First ¶
func (rel *Relation) First() RecordResult
First find returns the first record.
func (*Relation) HasAssociation ¶
func (*Relation) HasAssociations ¶
func (*Relation) Initialize ¶
func (rel *Relation) Initialize(params map[string]interface{}) (*ActiveRecord, error)
func (*Relation) InsertAll ¶
func (rel *Relation) InsertAll(params ...map[string]interface{}) ( rr []*ActiveRecord, err error, )
func (*Relation) Limit ¶
Limit specifies a limit for the number of records to retrieve.
User.Limit(10) // Generated SQL has 'LIMIT 10'
func (*Relation) New ¶
func (rel *Relation) New(params ...map[string]interface{}) RecordResult
func (*Relation) PrimaryKey ¶
PrimaryKey returns the attribute name of the record's primary key.
func (*Relation) ReflectOnAllAssociations ¶
func (a *Relation) ReflectOnAllAssociations() []*AssociationReflection
ReflectOnAllAssociations returns an array of AssociationReflection types for all associations in the Relation.
func (*Relation) ReflectOnAssociation ¶
func (a *Relation) ReflectOnAssociation(assocName string) *AssociationReflection
ReflectOnAssociation returns AssociationReflection for the specified association.
func (*Relation) Select ¶
Select allows to specify a subset of fields to return.
Method returns a new relation, where a set of attributes is limited by the specified list.
Model.Select("field", "other_field") // #<Model id: 1, field: "value", other_field: "value">
Accessing attributes of a Record that do not have fields retrieved by a select except id with return nil.
model, _ := Model.Select("field").Find(1) model.Attribute("other_field") // Returns nil
func (*Relation) ToA ¶
ToA converts Relation to array. The method access database to retrieve objects.
type SchemaStatements ¶
type SingularAssociation ¶
type SingularAssociation interface { Association AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult AccessAssociation(owner *ActiveRecord) RecordResult }
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
func (*Table) Columns ¶
func (tb *Table) Columns() (columns []ColumnDefinition)
func (*Table) DefineColumn ¶
func (*Table) ForeignKey ¶
func (*Table) ForeignKeys ¶
func (*Table) PrimaryKey ¶
func (*Table) References ¶
func (tb *Table) References(target string, init ...References)
type TransactionStatements ¶
type UpdateOperation ¶
type UpdateOperation struct { TableName string PrimaryKey string ColumnValues []ColumnValue }
type Validator ¶
type Validator interface {
Validate(r *ActiveRecord) error
}