driver

package
v0.1.0-dev.23 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnectionNotReadable = errors.New("connection is not readable")
	ErrConnectionNotWritable = errors.New("connection not writable")
	ErrConnectionIsClosed    = errors.New("connection is closed")
)
View Source
var ErrConnectionPoolOutOfIndex = errors.New("connection pool out of index")
View Source
var ErrDriverNotFound = errors.New("driver not found")

Functions

func Drivers

func Drivers() map[string]Driver

Drivers returns all drivers.

func Register

func Register(driver Driver) error

Register registers a driver.

Types

type Condition

type Condition uint8

Condition is a connection to a database.

const (
	// Eq is the equal condition.
	Eq Condition = iota
	// Ne is the not equal condition.
	Ne
	// Gt is the greater than condition.
	Gt
	// Lt is the less than condition.
	Lt
	// Ge is the greater than or equal condition.
	Ge
	// Le is the less than or equal condition.
	Le
)

func GetConditionFromString

func GetConditionFromString(condition string) Condition

func (Condition) String

func (c Condition) String() string

type Connection

type Connection interface {
	// Close closes the connection.
	Close() error
	// IsClosed returns true if the connection is closed.
	IsClosed() bool
	// Ping pings the database.
	// This is used to check if the connection is still alive.
	// If the connection was closed before, the ErrConnectionIsClosed should be returned.
	Ping() error
	// GetDetails returns the details of the database.
	GetDetails(ctx context.Context) (DatabaseDetail, error)
}

Connection is a connection to a database.

type ConnectionPool

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

ConnectionPool is a pool of connections. It's used to manage connections to the database.

func NewConnectionPool

func NewConnectionPool(connector *Connector) *ConnectionPool

NewConnectionPool returns a new connection pool.

func (*ConnectionPool) CloseAll

func (cp *ConnectionPool) CloseAll() error

CloseAll closes all connections in the pool.

func (*ConnectionPool) CloseConnection

func (cp *ConnectionPool) CloseConnection(index uint) error

CloseConnection closes a connection in the pool.

func (*ConnectionPool) Connect

func (cp *ConnectionPool) Connect(ctx context.Context) (Connection, uint, error)

Connect connects to the database.

func (*ConnectionPool) GetConnection

func (cp *ConnectionPool) GetConnection(index uint) (Connection, error)

GetConnection returns a connection from the pool.

func (*ConnectionPool) GetConnectionLength

func (cp *ConnectionPool) GetConnectionLength() uint

GetConnectionLength returns the length of the connections.

func (*ConnectionPool) GetConnector

func (cp *ConnectionPool) GetConnector() *Connector

GetConnector returns the connector.

type Connector

type Connector struct {
	DefaultSortBuilder
	DefaultFilterBuilder
	// contains filtered or unexported fields
}

Connector is database connector. It's used to connect to the database quickly with predefined credentials, filters, and sorts.

func NewConnector

func NewConnector(driver Driver, dsn string) *Connector

NewConnector returns a new connector.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (Connection, error)

Connect connects to the database.

func (*Connector) GetDriver

func (c *Connector) GetDriver() Driver

GetDriver returns the driver.

func (*Connector) IsReadable

func (c *Connector) IsReadable() bool

IsReadable returns true if the connection is readable.

func (*Connector) IsWritable

func (c *Connector) IsWritable() bool

IsWritable returns true if the connection is writable.

type DataCollectionDetail

type DataCollectionDetail struct {
	DataMap      *data.Map
	Name         string
	DataSetCount int
}

DataCollectionDetail is the details of a data collection.

func (DataCollectionDetail) GetDataCollectionName

func (d DataCollectionDetail) GetDataCollectionName() string

func (DataCollectionDetail) GetDataMap

func (d DataCollectionDetail) GetDataMap() *data.Map

func (DataCollectionDetail) GetDataSetCount

func (d DataCollectionDetail) GetDataSetCount() int

type DatabaseDetail

type DatabaseDetail struct {
	Name            string
	DataCollections []DataCollectionDetail
}

DatabaseDetail is the details of a data batch.

func (DatabaseDetail) AllDataCollectionsExcept

func (d DatabaseDetail) AllDataCollectionsExcept(names ...string) []DataCollectionDetail

AllDataCollectionsExcept returns all data collections except the ones with matching names. If no names are provided, all data collections will be returned. If provided names are not found, they will be ignored.

func (DatabaseDetail) GetDataCollection

func (d DatabaseDetail) GetDataCollection(name string) (DataCollectionDetail, error)

func (DatabaseDetail) GetDataCollections

func (d DatabaseDetail) GetDataCollections() []DataCollectionDetail

func (DatabaseDetail) GetDatabaseName

func (d DatabaseDetail) GetDatabaseName() string

func (DatabaseDetail) OnlyDataCollections

func (d DatabaseDetail) OnlyDataCollections(names ...string) []DataCollectionDetail

OnlyDataCollections returns only the data collections with matching names. If no names are provided, all data collections will be returned. If provided names are not found, they will be ignored.

type DefaultFilterBuilder

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

DefaultFilterBuilder is a default implementation of FilterableConnection. That can be used as embedded struct in a driver to implement simple filterable connection. And also this builder can build sql query from the filters.

func (*DefaultFilterBuilder) BuildFilterSQL

func (fb *DefaultFilterBuilder) BuildFilterSQL(dataCollection string) string

func (*DefaultFilterBuilder) GetAllFilters

func (fb *DefaultFilterBuilder) GetAllFilters() map[string][]*Filter

GetAllFilters returns all data collections filters without applying root filters.

func (*DefaultFilterBuilder) GetFilters

func (fb *DefaultFilterBuilder) GetFilters(dataCollection string) []*Filter

func (*DefaultFilterBuilder) GetRootFilters

func (fb *DefaultFilterBuilder) GetRootFilters() []*Filter

GetRootFilters returns root filters.

func (*DefaultFilterBuilder) ResetAllFilters

func (fb *DefaultFilterBuilder) ResetAllFilters()

func (*DefaultFilterBuilder) ResetFilters

func (fb *DefaultFilterBuilder) ResetFilters(dataCollection string)

func (*DefaultFilterBuilder) ResetRootFilters

func (fb *DefaultFilterBuilder) ResetRootFilters()

func (*DefaultFilterBuilder) Where

func (fb *DefaultFilterBuilder) Where(dataCollection, key string, value string) FilterableConnection

func (*DefaultFilterBuilder) WhereCondition

func (fb *DefaultFilterBuilder) WhereCondition(dataCollection string, condition Condition, key string, value string) FilterableConnection

func (*DefaultFilterBuilder) WhereRoot

func (fb *DefaultFilterBuilder) WhereRoot(key string, value string) FilterableConnection

WhereRoot is a general filter that is not related to any data collection. and will apply to all data collections.

func (*DefaultFilterBuilder) WhereRootCondition

func (fb *DefaultFilterBuilder) WhereRootCondition(condition Condition, key, value string) FilterableConnection

WhereRootCondition is a general filter that is not related to any data collection. and will apply to all data collections.

type DefaultSortBuilder

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

DefaultSortBuilder is the default implementation of SortableConnection. That can be used as embedded struct in a driver to implement simple filterable connection. And also this builder can build sql query from the sorts.

func (*DefaultSortBuilder) BuildSortSQL

func (sb *DefaultSortBuilder) BuildSortSQL(dataCollection string) string

BuildSortSQL builds the sort SQL.

func (*DefaultSortBuilder) GetAllSorts

func (sb *DefaultSortBuilder) GetAllSorts() map[string][]*Sort

GetAllSorts returns all sorts of the builder.

func (*DefaultSortBuilder) GetRootSorts

func (sb *DefaultSortBuilder) GetRootSorts() []*Sort

func (*DefaultSortBuilder) GetSorts

func (sb *DefaultSortBuilder) GetSorts(dataCollection string) []*Sort

GetSorts returns the sorts of the builder.

func (*DefaultSortBuilder) OrderBy

func (sb *DefaultSortBuilder) OrderBy(dataCollection, key string, direction ...Direction) SortableConnection

OrderBy adds a sort to the builder.

func (*DefaultSortBuilder) OrderByRoot

func (sb *DefaultSortBuilder) OrderByRoot(key string, direction ...Direction) SortableConnection

func (*DefaultSortBuilder) ResetAllSorts

func (sb *DefaultSortBuilder) ResetAllSorts()

ResetAllSorts resets all data collection sorts.

func (*DefaultSortBuilder) ResetSorts

func (sb *DefaultSortBuilder) ResetSorts(dataCollection string)

ResetSorts resets the specified sorts of a data collection.

type Direction

type Direction uint8

Direction is direction of a sort.

const (
	Asc  Direction = iota // Asc is the ascending direction.
	Desc                  // Desc is the descending direction.
)

func GetDirectionFromString

func GetDirectionFromString(direction string) Direction

GetDirectionFromString returns the direction from a string.

func (Direction) String

func (d Direction) String() string

String returns the string representation of the direction.

type Driver

type Driver interface {
	// GetDriverName returns the name of the driver.
	GetDriverName() string
	// IsWritable returns true if the driver is writable.
	// If the driver is writable, returned Connection will implement WritableConnection.
	IsWritable() bool
	// IsReadable returns true if the driver is readable.
	// If the driver is readable, returned Connection will implement ReadableConnection.
	IsReadable() bool
	// Open opens a connection to the database.
	Open(ctx context.Context, dsn string) (Connection, error)
}

Driver is a driver for a database.

func GetDriver

func GetDriver(name string) (Driver, error)

GetDriver returns a driver by name.

type Filter

type Filter struct {
	Key       string
	Value     string
	Condition Condition
}

Filter is a filter for a query.

func (*Filter) GetCondition

func (f *Filter) GetCondition() Condition

GetCondition returns the condition of the filter.

func (*Filter) GetKey

func (f *Filter) GetKey() string

GetKey returns the key of the filter.

func (*Filter) GetValue

func (f *Filter) GetValue() string

GetValue returns the value of the filter.

type FilterableConnection

type FilterableConnection interface {
	Where(dataCollection, key string, value string) FilterableConnection
	WhereRoot(key, value string) FilterableConnection
	WhereCondition(dataCollection string, condition Condition, key string, value string) FilterableConnection
	WhereRootCondition(condition Condition, key string, value string) FilterableConnection
	GetFilters(dataCollection string) []*Filter
	GetAllFilters() map[string][]*Filter
	GetRootFilters() []*Filter
	ResetFilters(dataCollection string)
	ResetAllFilters()
	ResetRootFilters()
}

FilterableConnection is a connection that can be filtered.

type Manager

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

Manager is a manager for drivers.

func (*Manager) Drivers

func (dm *Manager) Drivers() map[string]Driver

Drivers returns all drivers.

func (*Manager) GetDriver

func (dm *Manager) GetDriver(driverName string) (Driver, error)

GetDriver returns a driver by name.

func (*Manager) Register

func (dm *Manager) Register(driver Driver) error

Register registers a driver.

type ReadableConnection

type ReadableConnection interface {
	Connection // Embeds Connection
	// Read reads a batch of data from the database.
	Read(ctx context.Context, dataCollection string, startOffset, endOffset uint64) (*data.Batch, error)
}

ReadableConnection is a connection to a database that can read data.

type Sort

type Sort struct {
	Key       string
	Direction Direction
}

Sort is a sort for a query.

func (*Sort) GetDirection

func (s *Sort) GetDirection() Direction

GetDirection returns the direction of the sort.

func (*Sort) GetKey

func (s *Sort) GetKey() string

GetKey returns the key of the sort.

type SortableConnection

type SortableConnection interface {
	OrderBy(dataCollection, key string, direction ...Direction) SortableConnection
	OrderByRoot(key string, direction ...Direction) SortableConnection
	GetSorts(dataCollection string) []*Sort
	GetAllSorts() map[string][]*Sort
	GetRootSorts() []*Sort
	ResetSorts(dataCollection string)
	ResetAllSorts()
}

SortableConnection is a connection that can be sorted.

type WritableConnection

type WritableConnection interface {
	Connection // Embeds Connection
	// Write writes a batch of data to the database.
	// If the data set is duplicated or unique constraint is violated, ErrDataSetDuplicate should be returned.
	Write(ctx context.Context, dataCollection string, dataBatch *data.Batch) error
}

WritableConnection is a connection to a database that can write data.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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