ceous

package module
v0.0.0-...-d79b3d1 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2020 License: MIT Imports: 14 Imported by: 0

README

⚠⚠⚠ THIS IS A WORK IN PROGRESS! DO NOT USE IT YET! Please. ⚠⚠⚠

actions codecov

go-ceous

An ORM experiment in GO.

Guidelines for development

  • Use generation;
  • Strong relation support;
  • Support scenarios;
  • Transaction session;
  • Caching;
  • Minimum overhead;
  • Multi dialect;
  • Custom data types;
  • Ready for cancellation;
  • Avoid escaping;
  • Easy to use;
  • Fun;

Documentation

Index

Constants

View Source
const (
	// ForUpdate marks a use a FOR UPDATE.
	ForUpdate = sq.ForUpdate
	// ForNoKeyUpdate marks a use a FOR NO KEY UPDATE.
	ForNoKeyUpdate = sq.ForNoKeyUpdate
	// ForShare marks a use a FOR SHARE.
	ForShare = sq.ForShare
	// ForKeyShare marks a use a FOR KEY SHARE.
	ForKeyShare = sq.ForKeyShare
)
View Source
const (
	// ForUpdateTypeNone will generate the FOR [OPTION] with no modifier.
	ForUpdateTypeNone = sq.ForUpdateTypeNone
	// SkipLocked will generate the FOR [OPTION] SKIP LOCKED modifier.
	SkipLocked = sq.SkipLocked
	// NoWait will generate the FOR [OPTION] NOWAIT modifier.
	NoWait = sq.NoWait
)

Variables

View Source
var (
	ErrFieldNotFound              = errors.New("field not found")
	ErrInvalidRecordType          = errors.New("invalid record type")
	ErrInconsistentRelationResult = errors.New("inconsistent relation result")

	ErrULIDInvalidStringFormat = errors.New("invalid ulid string format")
)
View Source
var (
	ErrNonNewDocument = errors.New("ceous: cannot insert a non new document")
	ErrNotWritable    = errors.New("ceous: record is not writable")
	ErrNewDocument    = errors.New("ceous: cannot updated a new document")
	ErrNoRowUpdate    = errors.New("ceous: update affected no rows")
	ErrNotFound       = errors.New("ceous: entity not found")
)
View Source
var (
	ErrConditionTypeNotSupported = errors.New("condition type not supported")
)

Functions

func Asc

func Asc(field interface{}) string

func Desc

func Desc(field interface{}) string

func DisableDefaultScenario

func DisableDefaultScenario(q *BaseQuery)

DisableDefaultScenario sets the flag IsDefaultScenarioDisabled to true.

TODO(jota): See more DefaultScenario for queries.

func FieldAlias

func FieldAlias(schema Schema) func(SchemaField) AliasedSchemaField

FieldAlias creates a function that will create a SchemaField that will be bound to the schema passed.

func FieldAutoIncrement

func FieldAutoIncrement(field *BaseSchemaField)

func FieldPK

func FieldPK(field *BaseSchemaField)

func MakeWritable

func MakeWritable(record Record)

func OpEq

func OpEq(a, b interface{}) *eqOperator

func OpNe

func OpNe(a, b interface{}) *neOperator

func OpNot

func OpNot(a Sqlizer) *notOperator

func WrapPK

func WrapPK(prefix string, key PrimaryKey) *pkWrapper

Types

type AliasedSchemaField

type AliasedSchemaField interface {
	SchemaField
	Reference() string
}

func NewAliasSchemaField

func NewAliasSchemaField(schema Schema, field SchemaField) AliasedSchemaField

type BaseConnection

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

func NewConnection

func NewConnection(db DBProxy) *BaseConnection

NewConnection will create a new instance of the `*BaseConnection`.

func (*BaseConnection) Begin

func (conn *BaseConnection) Begin() (*BaseTxRunner, error)

Begin starts a transaction.

func (*BaseConnection) BeginTx

func (conn *BaseConnection) BeginTx(ctx context.Context, opts *sql.TxOptions) (*BaseTxRunner, error)

BeginTx starts a transaction with more options.

func (*BaseConnection) DB

func (conn *BaseConnection) DB() DBProxy

DB returns the real connection object for the database connection.

func (*BaseConnection) Exec

func (conn *BaseConnection) Exec(query string, args ...interface{}) (sql.Result, error)

func (*BaseConnection) ExecContext

func (conn *BaseConnection) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*BaseConnection) Prepare

func (conn *BaseConnection) Prepare(query string) (*sql.Stmt, error)

func (*BaseConnection) PrepareContext

func (conn *BaseConnection) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (*BaseConnection) Query

func (conn *BaseConnection) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*BaseConnection) QueryContext

func (conn *BaseConnection) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

type BaseQuery

type BaseQuery struct {
	Schema Schema
	Runner DBRunner

	Relations []Relation

	RecordScanner RecordScanner

	IsDefaultScenarioDisabled bool
	// contains filtered or unexported fields
}

func NewBaseQuery

func NewBaseQuery(options ...CeousOption) *BaseQuery

func (*BaseQuery) Builder

func (q *BaseQuery) Builder() (*sq.SelectBuilder, error)

Builder will prepare a *sq.SelectBuilder and return it with all fields, conditions and limits.

func (*BaseQuery) Count

func (q *BaseQuery) Count() (int64, error)

func (*BaseQuery) ExcludeFields

func (q *BaseQuery) ExcludeFields(fields ...SchemaField)

func (*BaseQuery) For

func (q *BaseQuery) For(t SelectForType, lockingType ...SelectForLockingType)

func (*BaseQuery) Limit

func (q *BaseQuery) Limit(limit uint64) *BaseQuery

Limit will update the limit directive of this query.

Warning: If you use the method `Builder` directly, be aware that this will affect the builder returned.

func (*BaseQuery) Offset

func (q *BaseQuery) Offset(offset uint64) *BaseQuery

Offset will update the offset directive of this query.

Warning: If you use the method `Builder` directly, be aware that this will affect the builder returned.

func (*BaseQuery) OrderBy

func (q *BaseQuery) OrderBy(fields ...interface{})

func (*BaseQuery) RawQuery

func (q *BaseQuery) RawQuery() (*sql.Rows, error)

func (*BaseQuery) RawQueryContext

func (q *BaseQuery) RawQueryContext(ctx context.Context) (*sql.Rows, error)

func (*BaseQuery) RawQueryRow

func (q *BaseQuery) RawQueryRow() sq.RowScanner

func (*BaseQuery) RawQueryRowContext

func (q *BaseQuery) RawQueryRowContext(ctx context.Context) sq.RowScanner

func (*BaseQuery) Select

func (q *BaseQuery) Select(fields ...SchemaField)

func (*BaseQuery) Where

func (q *BaseQuery) Where(pred interface{}, args ...interface{})

func (*BaseQuery) With

func (q *BaseQuery) With(d interface{})

type BaseRecordScanner

type BaseRecordScanner struct{}

BaseRecordScanner implements a basic and generic `RecordScanner`.

var DefaultRecordScanner BaseRecordScanner

func (*BaseRecordScanner) ScanRecord

func (recordScanner *BaseRecordScanner) ScanRecord(rs ResultSet, model Record) error

ScanRecord uses the ColumnAddress to read records from the given `rs`.

type BaseSchema

type BaseSchema struct {
	ColumnsArr []SchemaField
	// contains filtered or unexported fields
}

func NewBaseSchema

func NewBaseSchema(tableName, alias string, columns ...SchemaField) *BaseSchema

func (*BaseSchema) Alias

func (schema *BaseSchema) Alias() string

func (*BaseSchema) As

func (schema *BaseSchema) As(alias string) Schema

func (*BaseSchema) Columns

func (schema *BaseSchema) Columns() []SchemaField

func (*BaseSchema) PrimaryKey

func (schema *BaseSchema) PrimaryKey() SchemaField

func (*BaseSchema) Table

func (schema *BaseSchema) Table() string

type BaseSchemaField

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

func NewSchemaField

func NewSchemaField(name string, options ...FieldOption) *BaseSchemaField

func (*BaseSchemaField) IsAutoInc

func (field *BaseSchemaField) IsAutoInc() bool

func (*BaseSchemaField) IsPK

func (field *BaseSchemaField) IsPK() bool

func (*BaseSchemaField) QualifiedName

func (field *BaseSchemaField) QualifiedName(schema Schema) string

func (*BaseSchemaField) String

func (field *BaseSchemaField) String() string

type BaseStore

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

BaseStore is the generic implementation of the Store.

TODO(jota): To document it...

func NewStore

func NewStore(schema Schema, options ...CeousOption) *BaseStore

NewStore returns a new base implementation of a Store. That does not aims to be used by itself, but to be used as composition on real stores.

TODO(jota): Improve documentation.

func (*BaseStore) Delete

func (store *BaseStore) Delete(record Record) error

func (*BaseStore) Insert

func (store *BaseStore) Insert(record Record, fields ...SchemaField) error

Insert will insert a record.

`fields` define what fields are going to be used on the insert.

func (*BaseStore) Update

func (store *BaseStore) Update(record Record, fields ...SchemaField) (int64, error)

type BaseTxRunner

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

func NewTx

func NewTx(tx *sql.Tx) *BaseTxRunner

NewTx returns a new transaction abstraction.

func (*BaseTxRunner) Commit

func (tx *BaseTxRunner) Commit() error

func (*BaseTxRunner) Exec

func (tx *BaseTxRunner) Exec(query string, args ...interface{}) (sql.Result, error)

func (*BaseTxRunner) ExecContext

func (tx *BaseTxRunner) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*BaseTxRunner) Prepare

func (tx *BaseTxRunner) Prepare(query string) (*sql.Stmt, error)

func (*BaseTxRunner) PrepareContext

func (tx *BaseTxRunner) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (*BaseTxRunner) Query

func (tx *BaseTxRunner) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*BaseTxRunner) QueryContext

func (tx *BaseTxRunner) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*BaseTxRunner) QueryRow

func (tx *BaseTxRunner) QueryRow(query string, args ...interface{}) *sql.Row

func (*BaseTxRunner) QueryRowContext

func (tx *BaseTxRunner) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

func (*BaseTxRunner) Rollback

func (tx *BaseTxRunner) Rollback() error

type CeousOption

type CeousOption func(q interface{})

func WithConn

func WithConn(conn Connection) CeousOption

WithConn returns a query option for creating .

func WithRunner

func WithRunner(runner DBRunner) CeousOption

WithRunner returns a query option for setting the runner for a transaction.

func WithSchema

func WithSchema(schema Schema) CeousOption

WithSchema returns a query option that will set the schema of a Query. Useful for using aliases.

type ColumnAddresser

type ColumnAddresser interface {
	ColumnAddress(name string) (interface{}, error)
}

Valuer ...

type Columnable

type Columnable interface {
	Columns() []SchemaField
}

type Condition

type Condition func(Schema) Sqlizer

func Eq

func Eq(field SchemaField, value interface{}) Condition

func Ne

func Ne(field SchemaField, value interface{}) Condition

func Not

func Not(cond Condition) Condition

func SqlCondition

func SqlCondition(sql string, args []interface{}) Condition

SqlCondition will return a new condition that will create a sqlCondition.

See more at sqlCondition

type Connection

type Connection interface {
	DB() DBProxy
}

type DBProxy

type DBProxy interface {
	DBRunner
	io.Closer
	Statisticer
	Transactioner
}

type DBRunner

type DBRunner interface {
	sq.Execer
	sq.ExecerContext
	sq.Queryer
	sq.QueryerContext
	sq.Preparer
}

type Embedded

type Embedded struct {
}

type FieldOption

type FieldOption func(*BaseSchemaField)

type Model

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

Valuer ...

func (*Model) IsPersisted

func (model *Model) IsPersisted() bool

func (*Model) IsWritable

func (model *Model) IsWritable() bool

type Pinger

type Pinger interface {
	Ping() error
}

type PingerContext

type PingerContext interface {
	PingContext(ctx context.Context) error
}

type PrimaryKey

type PrimaryKey interface {
	Columnable
	ColumnAddresser
	Valuer
}

type Query

type Query interface {
	RawQuery() (*sql.Rows, error)
	RawQueryContext(context.Context) (*sql.Rows, error)
	RawQueryRow() sq.RowScanner
	RawQueryRowContext(context.Context) sq.RowScanner
}

type Record

type Record interface {
	GetID() interface{}
	IsPersisted() bool

	IsWritable() bool

	ColumnAddresser
	Valuer
	// contains filtered or unexported methods
}

Valuer ...

type RecordScanner

type RecordScanner interface {
	ScanRecord(rs ResultSet, model Record) error
}

RecordScanner describes a scanner for a record.

type RecordScannerColumns

type RecordScannerColumns interface {
	SelectColumns() []SchemaField
}

RecordScannerColumns describes a column selector.

type Relation

type Relation interface {
	Aggregate(model Record) error
	Realize() error
}

type ResultSet

type ResultSet interface {
	Next() bool
	Scan(dest ...interface{}) error
	Columns() ([]string, error)
	io.Closer
}

ResultSet represents the abstraction of a sql.ResultSet struct.

type Schema

type Schema interface {
	PrimaryKey() SchemaField
	Alias() string
	Table() string
	As(string) Schema
	Columnable
}

type SchemaField

type SchemaField interface {
	// String returns the string representation of the field. That is, its name.
	String() string

	// QualifiedString returns the name of the field qualified by the alias of
	// the given schema.
	QualifiedName(Schema) string

	IsPK() bool
	IsAutoInc() bool
}

type SelectForLockingType

type SelectForLockingType = sq.SelectForLockingType

type SelectForType

type SelectForType = sq.SelectForType

type Sqlizer

type Sqlizer interface {
	sq.Sqlizer
}

type Statisticer

type Statisticer interface {
	Stats() sql.DBStats
}

type Transactioner

type Transactioner interface {
	Begin() (*sql.Tx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

type TxProxy

type TxProxy interface {
	DBRunner
	Commit() error
	Rollback() error
}

type ULID

type ULID uuid.UUID

ULID is an ID type provided by kallax that is a lexically sortable UUID. The internal representation is an ULID (https://github.com/oklog/ulid). It already implements sql.Scanner and driver.Valuer, so it's perfectly safe for database usage.

This ULID implementation was copied from the go-kallax library (https://github.com/src-d/go-kallax/blob/c3e1e4d85f44dd6ed4f0b65c7fed60c0c576ba85/model.go).

func MustNewULID

func MustNewULID() ULID

MustNewULID returns a new ULID, which is a lexically sortable UUID.

func NewULID

func NewULID() (ULID, error)

NewULID returns a new ULID, which is a lexically sortable UUID.

func NewULIDFromText

func NewULIDFromText(text string) (ULID, error)

NewULIDFromText creates a new ULID from its string representation. Will return an error if the text is not a valid ULID.

func (ULID) IsEmpty

func (id ULID) IsEmpty() bool

IsEmpty returns whether the ID is empty or not. An empty ID means it has not been set yet.

func (ULID) MarshalText

func (id ULID) MarshalText() ([]byte, error)

MarshalText serializes an ULID for JSON.

func (ULID) Raw

func (id ULID) Raw() interface{}

Raw returns the underlying raw value.

func (*ULID) Scan

func (id *ULID) Scan(src interface{}) error

Scan implements the Scanner interface.

func (ULID) String

func (id ULID) String() string

String returns the string representation of the ID.

func (*ULID) UnmarshalText

func (id *ULID) UnmarshalText(text []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaler interface. Following formats are supported: "6ba7b810-9dad-11d1-80b4-00c04fd430c8", "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" Implements the exact same code as the UUID UnmarshalText removing the version check.

func (ULID) Value

func (id ULID) Value() (driver.Value, error)

Value implements the Valuer interface.

type Valuer

type Valuer interface {
	Value(column string) (interface{}, error)
}

Valuer ...

Directories

Path Synopsis
cmd
tpl
db

Jump to

Keyboard shortcuts

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