client

package
v1.8.12 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 18 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,
			reConfiguration.Component,
			mysql.Component,
			sqlite.Component,
			info.Component,
			runner.Component,
		},
		Constructor: func(container container.Container) error {
			return container.Provides(
				NewConfig,
				NewReConfigurationWithConfigurator,
				func(dataBaseClient *ReConfiguration) *Process { return NewProcess(dataBaseClient) },
				func(dataBaseClient *Process) DataBase { return dataBaseClient },
			)
		},
		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}, ","),
				))
			})
		},
		Run: app.Run(func(container container.Container) error {
			return container.Invoke(func(
				dataBaseClient *ReConfiguration,
				reConfiguration reConfiguration.ReConfiguration,
				informer logger.Informer,
				info info.Info,
				config *Config,
				process *Process,
				runner runner.Runner,
			) error {
				reConfiguration.Registration(dataBaseClient)
				informer.Info("data_base: registration in the reConfigurator")
				info.Registration("data_base", config)

				return runner.AddProcesses(process)
			})
		}),
	}

	MySQLComponent = &app.Component{
		Dependencies: app.Components{
			mysql.Component,
			runner.Component,
		},
		Constructor: app.Constructor(func(container container.Container) error {
			return container.Provides(
				func(dataBaseClient *mysql.ReConfiguration) *Process { return NewProcess(dataBaseClient) },
				func(dataBaseClient *Process) DataBase { return dataBaseClient },
			)
		}),
		Run: app.Run(func(container container.Container) error {
			return container.Invoke(func(
				process *Process,
				runner runner.Runner,
			) error {
				return runner.AddProcesses(process)
			})
		}),
	}

	SQLiteComponent = &app.Component{
		Dependencies: app.Components{
			sqlite.Component,
			runner.Component,
		},
		Constructor: app.Constructor(func(container container.Container) error {
			return container.Provides(
				func(dataBaseClient *sqlite.ReConfiguration) *Process { return NewProcess(dataBaseClient) },
				func(dataBaseClient *Process) DataBase { return dataBaseClient },
			)
		}),
		Run: app.Run(func(container container.Container) error {
			return container.Invoke(func(
				process *Process,
				runner runner.Runner,
			) error {
				return runner.AddProcesses(process)
			})
		}),
	}
)

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 Process added in v1.8.12

type Process struct {
	DataBase
}

func NewProcess added in v1.8.12

func NewProcess(dataBase DataBase) *Process

func (*Process) Name added in v1.8.12

func (process *Process) Name() string

func (*Process) Process added in v1.8.12

func (process *Process) Process(ctx context.Context) error

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