activerecord

package
v0.0.8-b Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SchemaMigrationsName = "schema_migrations"
)

Variables

This section is empty.

Functions

func Migrate

func Migrate(id string, init func(m *M))

func RegisterConnectionAdapter

func RegisterConnectionAdapter(adapter string, ca ConnectionAdapter)

func RemoveConnection

func RemoveConnection(name string) error

func Transaction

func Transaction(ctx context.Context, fn func() error) error

Transaction runs the given block in a database transaction, and returns the result of the function.

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) Errors

func (v *ActiveRecord) Errors() Errors

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 Association interface {
	// AssociationOwner() *Relation
	AssociationName() string
	AssociationForeignKey() string
}

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 (a AssociationReflection) AccessCollection(collName string) (*Relation, error)

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) Errors

func (v AssociationReflection) Errors() Errors

func (AssociationReflection) HasAssociation

func (a AssociationReflection) HasAssociation(assocName string) bool

func (AssociationReflection) HasAssociations

func (a AssociationReflection) HasAssociations(assocNames ...string) bool

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 Attribute

type Attribute interface {
	AttributeName() string
	AttributeType() Type
}

type AttributeAccessors

type AttributeAccessors interface {
	ID() interface{}
	AttributePresent(attrName string) bool
	Attribute(attrName string) interface{}
	// AccessAttribute(attrName string) interface{}
	AssignAttribute(attrName string, val interface{}) error
	AssignAttributes(newAttributes map[string]interface{}) error
}

type AttributeMethods

type AttributeMethods interface {
	AttributeNames() []string
	HasAttribute(attrName string) bool
	HasAttributes(attrNames ...string) bool
	AttributeForInspect(attrName string) Attribute
	AttributesForInspect(attrNames ...string) []Attribute
}

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 (a *BelongsTo) AssociationForeignKey() string

func (*BelongsTo) AssociationName

func (a *BelongsTo) AssociationName() string

func (*BelongsTo) AssociationOwner

func (a *BelongsTo) AssociationOwner() *Relation

func (*BelongsTo) ForeignKey

func (a *BelongsTo) ForeignKey(fk string)

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.

func (*BelongsTo) String

func (a *BelongsTo) String() string

type Boolean

type Boolean struct{}

func (*Boolean) Deserialize

func (b *Boolean) Deserialize(value interface{}) (interface{}, error)

func (*Boolean) NativeType

func (b *Boolean) NativeType() string

func (*Boolean) Serialize

func (*Boolean) Serialize(value interface{}) (interface{}, error)

func (*Boolean) String

func (b *Boolean) String() string

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 ColumnDefinition struct {
	Name         string
	Type         Type
	NotNull      bool
	IsPrimaryKey bool
}

type ColumnValue

type ColumnValue struct {
	Name  string
	Type  Type
	Value interface{}
}

type Conn

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

func RetrieveConnection(name string) (Conn, error)

type ConnectionAdapter

type ConnectionAdapter func(DatabaseConfig) (Conn, error)

type DatabaseConfig

type DatabaseConfig struct {
	Name     string
	Adapter  string
	Host     string
	Username string
	Password string
	Database string
}

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 Date

type Date struct{}

func (*Date) Deserialize

func (d *Date) Deserialize(value interface{}) (interface{}, error)

func (*Date) NativeType

func (*Date) NativeType() string

func (*Date) Serialize

func (d *Date) Serialize(value interface{}) (interface{}, error)

func (*Date) String

func (*Date) String() string

type DateTime

type DateTime struct{}

func (*DateTime) Deserialize

func (dt *DateTime) Deserialize(value interface{}) (interface{}, error)

func (*DateTime) NativeType

func (*DateTime) NativeType() string

func (*DateTime) Serialize

func (dt *DateTime) Serialize(value interface{}) (interface{}, error)

func (*DateTime) String

func (*DateTime) String() string

type DeleteOperation

type DeleteOperation struct {
	TableName  string
	PrimaryKey string
	Value      interface{}
}

type Dependency

type Dependency struct {
	TableName  string
	ForeignKey string
	PrimaryKey string
}

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

type ErrInvalidType

type ErrInvalidType struct {
	AttrName string
	TypeName string
	Value    interface{}
}

func (ErrInvalidType) Error

func (e ErrInvalidType) Error() string

type ErrInvalidValue

type ErrInvalidValue struct {
	AttrName string
	Message  string
	Value    interface{}
}

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 ErrType

type ErrType struct {
	TypeName string
	Value    interface{}
}

func (ErrType) Error

func (e ErrType) Error() string

type ErrUnknownAssociation

type ErrUnknownAssociation struct {
	RecordName string
	Assoc      string
}

func (ErrUnknownAssociation) Error

func (e ErrUnknownAssociation) Error() string

type ErrUnknownAttribute

type ErrUnknownAttribute struct {
	RecordName string
	Attr       string
}

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

type ErrUnknownPrimaryKey struct {
	PrimaryKey  string
	Description string
}

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) Add

func (e *Errors) Add(key string, err error)

func (*Errors) Delete

func (e *Errors) Delete(keys ...string)

func (*Errors) FullMessages

func (e *Errors) FullMessages() []string

func (*Errors) IsEmpty

func (e *Errors) IsEmpty() bool

type Exclusion

type Exclusion struct {
	From Slice

	AllowNil   bool
	AllowBlank bool
}

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

func (e *Exclusion) AllowsBlank() bool

AllowsBlank returns true when blank values are allowed, and false otherwise.

func (*Exclusion) AllowsNil

func (e *Exclusion) AllowsNil() bool

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 Float64

type Float64 struct{}

func (*Float64) Deserialize

func (f64 *Float64) Deserialize(value interface{}) (interface{}, error)

func (*Float64) NativeType

func (*Float64) NativeType() string

func (*Float64) Serialize

func (*Float64) Serialize(value interface{}) (interface{}, error)

func (*Float64) String

func (*Float64) String() string

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

func (f *Format) AllowsBlank() bool

AllowsBlank returns true when blank values are allowed, and false otherwise.

func (*Format) AllowsNil

func (f *Format) AllowsNil() bool

AllowsNil returns true when nil values are allowed, and false otherwise.

func (*Format) Initialize

func (f *Format) Initialize() (err error)

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 (a *HasMany) AssociationForeignKey() string

func (*HasMany) AssociationName

func (a *HasMany) AssociationName() string

func (*HasMany) String

func (a *HasMany) String() string

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 (a *HasOne) AssociationForeignKey() string

func (*HasOne) AssociationName

func (a *HasOne) AssociationName() string

func (*HasOne) AssociationOwner

func (a *HasOne) AssociationOwner() *Relation

func (*HasOne) String

func (a *HasOne) String() string

type Inclusion

type Inclusion struct {
	In Slice

	AllowNil   bool
	AllowBlank bool
}

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

func (i *Inclusion) AllowsBlank() bool

AllowsBlank returns true when blank values are allowed, and false otherwise.

func (*Inclusion) AllowsNil

func (i *Inclusion) AllowsNil() bool

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 Int64

type Int64 struct{}

func (*Int64) Deserialize

func (i64 *Int64) Deserialize(value interface{}) (interface{}, error)

func (*Int64) NativeType

func (*Int64) NativeType() string

func (*Int64) Serialize

func (*Int64) Serialize(value interface{}) (interface{}, error)

func (*Int64) String

func (*Int64) String() string

type JSON

type JSON struct{}

func (*JSON) Deserialize

func (j *JSON) Deserialize(value interface{}) (interface{}, error)

func (*JSON) NativeType

func (*JSON) NativeType() string

func (*JSON) Serialize

func (j *JSON) Serialize(value interface{}) (interface{}, error)

func (*JSON) String

func (*JSON) String() 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

func (l *Length) AllowsBlank() bool

AllowsBlank returns true when blank values are allowed, and false otherwise.

func (*Length) AllowsNil

func (l *Length) AllowsNil() bool

AllowsNil returns true when nil values are allowed, and false otherwise.

func (*Length) Initialize

func (l *Length) Initialize() error

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 *M) AddForeignKey(owner, target string)

func (*M) CreateTable

func (m *M) CreateTable(name string, init func(*Table))

func (*M) TableExists

func (m *M) TableExists(tableName string) Result[bool]

type Nil

type Nil struct {
	Type
}

func (Nil) Deserialize

func (n Nil) Deserialize(value interface{}) (interface{}, error)

func (Nil) String

func (n Nil) String() string

type Ownership

type Ownership interface {
	Move(dst interface{}) error
	Borrow(src interface{}) error
}

type Persistence

type Persistence interface {
	Insert() (*ActiveRecord, error)
	Update() (*ActiveRecord, error)
	Delete() error
	IsPersisted() bool
}

type Predicate

type Predicate struct {
	Cond string
	Args []interface{}
}

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

func (p *Presence) AllowsBlank() bool

AllowsBlank returns false, since blank values are not allowed for this validator.

func (*Presence) AllowsNil

func (p *Presence) AllowsNil() bool

AllowsNil returns true if nil values are allowed, and false otherwise.

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 QueryMethods interface {
	Where(cond string, arg interface{}) *Relation
	Select(attrs ...string) *Relation
	Group(attrs ...string) *Relation
	Joins(assocs ...string) *Relation
	Limit(num int) *Relation
}

type QueryOperation

type QueryOperation struct {
	Text    string
	Args    []interface{}
	Columns []string
}

type R

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

func (*R) BelongsTo

func (r *R) BelongsTo(name string, init ...func(*BelongsTo))

func (*R) DefineAttribute

func (r *R) DefineAttribute(name string, t Type, validators ...AttributeValidator)

func (*R) HasMany

func (r *R) HasMany(name string)

func (*R) HasOne

func (r *R) HasOne(name string)

func (*R) PrimaryKey

func (r *R) PrimaryKey(name string)

func (*R) TableName

func (r *R) TableName(name string)

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

func (r *R) ValidatesPresence(names ...string)

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 Initialize

func Initialize(name string, init func(*R)) (*Relation, error)

func New

func New(name string, init ...func(*R)) *Relation

func (*Relation) AccessAssociation

func (a *Relation) AccessAssociation(assocName string) (*ActiveRecord, error)

func (*Relation) AccessCollection

func (a *Relation) AccessCollection(collName string) (*Relation, error)

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

func (rel *Relation) ColumnNames() []string

TODO: move to the Schema type all column-related methods.

func (*Relation) Connect

func (rel *Relation) Connect(conn Conn) *Relation

func (*Relation) Connection

func (rel *Relation) Connection() Conn

func (*Relation) Context

func (rel *Relation) Context() context.Context

func (*Relation) Copy

func (rel *Relation) Copy() *Relation

func (*Relation) Create

func (rel *Relation) Create(params map[string]interface{}) RecordResult

func (*Relation) Each

func (rel *Relation) Each(fn func(*ActiveRecord) error) error

func (*Relation) Errors

func (v *Relation) Errors() Errors

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) Group

func (rel *Relation) Group(attrNames ...string) *Relation

func (*Relation) HasAssociation

func (a *Relation) HasAssociation(assocName string) bool

func (*Relation) HasAssociations

func (a *Relation) HasAssociations(assocNames ...string) bool

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) IsEmpty

func (rel *Relation) IsEmpty() bool

IsEmpty returns true if there are no records.

func (*Relation) Joins

func (rel *Relation) Joins(assocNames ...string) *Relation

func (*Relation) Limit

func (rel *Relation) Limit(num int) *Relation

Limit specifies a limit for the number of records to retrieve.

User.Limit(10) // Generated SQL has 'LIMIT 10'

func (*Relation) Name

func (rel *Relation) Name() string

func (*Relation) New

func (rel *Relation) New(params ...map[string]interface{}) RecordResult

func (*Relation) PrimaryKey

func (rel *Relation) PrimaryKey() string

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

func (rel *Relation) Select(attrNames ...string) *Relation

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) String

func (rel *Relation) String() string

func (*Relation) TableName

func (rel *Relation) TableName() string

func (*Relation) ToA

func (rel *Relation) ToA() (Array, error)

ToA converts Relation to array. The method access database to retrieve objects.

func (*Relation) ToSQL

func (rel *Relation) ToSQL() string

ToSQL returns sql statement for the relation.

User.Where("name", "Oscar").ToSQL()
// SELECT * FROM "users" WHERE "name" = ?

func (*Relation) Where

func (rel *Relation) Where(cond string, arg interface{}) *Relation

func (*Relation) WithContext

func (rel *Relation) WithContext(ctx context.Context) *Relation

type SchemaStatements

type SchemaStatements interface {
	CreateTable(ctx context.Context, table *Table) error
	AddForeignKey(ctx context.Context, owner, target string) error

	ColumnType(typeName string) (Type, error)
	ColumnDefinitions(ctx context.Context, tableName string) ([]ColumnDefinition, error)
}

type SingularAssociation

type SingularAssociation interface {
	Association
	AssignAssociation(owner *ActiveRecord, target *ActiveRecord) RecordResult
	AccessAssociation(owner *ActiveRecord) RecordResult
}

type String

type String struct{}

func (*String) Deserialize

func (s *String) Deserialize(value interface{}) (interface{}, error)

func (*String) NativeType

func (*String) NativeType() string

func (*String) Serialize

func (*String) Serialize(value interface{}) (interface{}, error)

func (*String) String

func (*String) String() string

type Table

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

func (*Table) Columns

func (tb *Table) Columns() (columns []ColumnDefinition)

func (*Table) DateTime

func (tb *Table) DateTime(name string)

func (*Table) DefineColumn

func (tb *Table) DefineColumn(name string, t Type)

func (*Table) ForeignKey

func (tb *Table) ForeignKey(target string)

func (*Table) ForeignKeys

func (tb *Table) ForeignKeys() []string

func (*Table) Int64

func (tb *Table) Int64(name string)

func (*Table) Name

func (tb *Table) Name() string

func (*Table) PrimaryKey

func (tb *Table) PrimaryKey(primaryKey string)

func (*Table) References

func (tb *Table) References(target string, init ...References)

func (*Table) String

func (tb *Table) String(name string)

type Time

type Time struct{}

func (*Time) Deserialize

func (t *Time) Deserialize(value interface{}) (interface{}, error)

func (*Time) NativeType

func (*Time) NativeType() string

func (*Time) Serialize

func (t *Time) Serialize(value interface{}) (interface{}, error)

func (*Time) String

func (*Time) String() string

type TransactionStatements

type TransactionStatements interface {
	BeginTransaction(ctx context.Context) (Conn, error)
	CommitTransaction(ctx context.Context) error
	RollbackTransaction(ctx context.Context) error
}

type Type

type Type interface {
	fmt.Stringer

	NativeType() string

	Deserialize(value interface{}) (interface{}, error)
	Serialize(value interface{}) (interface{}, error)
}

type UpdateOperation

type UpdateOperation struct {
	TableName    string
	PrimaryKey   string
	ColumnValues []ColumnValue
}

type Validator

type Validator interface {
	Validate(r *ActiveRecord) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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