client

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2023 License: MIT Imports: 17 Imported by: 0

README

Клиент для баз данных.

Компонент для подключения к базе данных, с возможностью выбрать нужную СУБД.

Реализует интерфейс DataBase, который доступен в контейнере объектов.

type DataBase interface {
	Query(string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
	Exec(string, ...interface{}) (sql.Result, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

Список поддерживаемых СУБД:

Объект конфигурации.
type Config struct {
	Driver string
}

Описание полей:

Поле Описание Значение по умолчанию Доступные значения
Driver Имя драйвера СУБД, который следует использовать, этот параметр может быть передан одним из 3-х методов:
- флаг командной строки --db.driver
- переменная окружения DB_DRIVER
- значение в конфиг файле db.driver
mysql - mysql - MySQL;
- sqlite - SQLite;

Documentation

Index

Constants

View Source
const (
	DriverFieldName = "db.driver"

	DriverDefault = driver.MySQL
)

Variables

View Source
var Component = &app.Component{
	Dependencies: app.Components{
		logger.Component,
		configurator.Component,
		mysql.Component,
		sqlite.Component,
		info.Component,
	},
	Constructor: func(container container.Container) error {
		return container.Provides(
			NewConfig,
			NewReConfigurationWithConfigurator,
			func(reConfiguration *ReConfiguration) DataBase { return reConfiguration },
		)
	},
	BindFlags: func(flagSet *pflag.FlagSet, container container.Container) error {
		return container.Invoke(func(config *Config) {
			flagSet.StringVar(&config.Driver, DriverFieldName, DriverDefault, fmt.Sprintf(
				"database connection driver. Available drivers [%s]",
				strings.Join([]string{driver.MySQL, driver.SQLite}, ","),
			))
		})
	},
	Serve: func(c container.Container) error {
		dataBase, err := container.Get[*ReConfiguration](c)
		if err != nil {
			return err
		}

		reConfiguration, err := container.Get[configurator.ReConfiguration](c)
		if err != nil {
			return err
		}

		informer, err := container.Get[logger.Informer](c)
		if err != nil {
			return err
		}

		reConfiguration.Registration(dataBase)
		informer.Info("data_base: registration in the reConfigurator")

		info, err := container.Get[info.Info](c)
		if err != nil {
			return err
		}

		config, err := container.Get[*Config](c)
		if err != nil {
			return err
		}

		info.Registration("data_base", config)

		closer, err := container.Get[closer.Closer](c)
		if err != nil {
			return err
		}

		ctx, cancelFunc := context.WithCancel(closer.GetContext())
		defer cancelFunc()

		<-ctx.Done()

		return dataBase.Close()
	},
}

Functions

func IsRecordNotFound

func IsRecordNotFound(err error) bool

Types

type Config

type Config struct {
	Driver string
}

func Configuration

func Configuration(config *Config, configurator configurator.Configurator) *Config

func NewConfig

func NewConfig() *Config

type DataBase

type DataBase interface {
	io.Closer

	Query(string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
	Exec(string, ...interface{}) (sql.Result, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	SQL() *sql.DB
	DriverName() string
}

func New

func New(config *Config, informer logger.Informer, container container.Container) (DataBase, error)

type Page

type Page struct {
	Number           uint32
	Limit            uint8
	ColumnNameToSort string
	SortDirection    SortDirection
}

type ReConfiguration

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

func NewReConfiguration

func NewReConfiguration(informer logger.Informer, container container.Container, config *Config) (*ReConfiguration, error)

func NewReConfigurationWithConfigurator

func NewReConfigurationWithConfigurator(informer logger.Informer, container container.Container, config *Config, configurator configurator.Configurator) (*ReConfiguration, error)

func (*ReConfiguration) Close

func (reConfiguration *ReConfiguration) Close() error

func (*ReConfiguration) DataBase

func (reConfiguration *ReConfiguration) DataBase() DataBase

func (*ReConfiguration) DriverName

func (reConfiguration *ReConfiguration) DriverName() string

func (*ReConfiguration) Exec

func (reConfiguration *ReConfiguration) Exec(s string, i ...interface{}) (sql.Result, error)

func (*ReConfiguration) ExecContext

func (reConfiguration *ReConfiguration) ExecContext(ctx context.Context, s string, i ...interface{}) (sql.Result, error)

func (*ReConfiguration) Query

func (reConfiguration *ReConfiguration) Query(s string, i ...interface{}) (*sql.Rows, error)

func (*ReConfiguration) QueryContext

func (reConfiguration *ReConfiguration) QueryContext(ctx context.Context, s string, i ...interface{}) (*sql.Rows, error)

func (*ReConfiguration) QueryRow

func (reConfiguration *ReConfiguration) QueryRow(s string, i ...interface{}) *sql.Row

func (*ReConfiguration) QueryRowContext

func (reConfiguration *ReConfiguration) QueryRowContext(ctx context.Context, s string, i ...interface{}) *sql.Row

func (*ReConfiguration) ReConfiguration

func (reConfiguration *ReConfiguration) ReConfiguration(c configurator.Configurator) error

func (*ReConfiguration) SQL

func (reConfiguration *ReConfiguration) SQL() *sql.DB

type RecordNotFound

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

func NewRecordNotFound

func NewRecordNotFound(error string) *RecordNotFound

func (*RecordNotFound) Error

func (recordNotFound *RecordNotFound) Error() string

type SortDirection

type SortDirection uint
const (
	ASC SortDirection = iota
	DESC
)

func (SortDirection) String

func (i SortDirection) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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