config

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config provides database configuration types.

Index

Constants

View Source
const (
	LoadBalanceRoundRobin = "round_robin"
	LoadBalanceRandom     = "random"
)
View Source
const (
	ExporterJaeger      = "jaeger"
	ExporterKafka       = "kafka"
	ExporterRedisStream = "redis_stream"

	// Mode selects the mqx producer object topology.
	//   single  → mqx.POS  (single broker / single node)
	//   cluster → mqx.POC  (cluster mode; only meaningful for redis_stream —
	//              kafkax has no POC variant, the kafka client itself
	//              supports multiple brokers via the Addrs array)
	ModeSingle  = "single"
	ModeCluster = "cluster"
)

Variables

This section is empty.

Functions

func Load

func Load(path string) (DBConfig, *TracingConfig, error)

Load auto-detects format based on file basename and returns a DBConfig. If the basename doesn't match a known driver, falls back to dispatching by the first populated section in the file (so db.yaml with `mysql:` block works regardless of filename).

func LoadCluster

func LoadCluster(path string) (*ClusterConfig, *TracingConfig, error)

LoadCluster loads a ClusterConfig from a file.

func LoadGaussDB

func LoadGaussDB(path string) (*GaussDBConfig, *TracingConfig, error)

LoadGaussDB loads a GaussDB config from a file.

func LoadMSSQL

func LoadMSSQL(path string) (*MSSQLConfig, *TracingConfig, error)

LoadMSSQL loads a MSSQL config from a file.

func LoadMariaDB

func LoadMariaDB(path string) (*MariaDBConfig, *TracingConfig, error)

LoadMariaDB loads a MariaDB config from a file.

func LoadMySQL

func LoadMySQL(path string) (*MySQLConfig, *TracingConfig, error)

LoadMySQL loads a MySQL config from a YAML/JSON/TOML file.

func LoadOracle

func LoadOracle(path string) (*OracleConfig, *TracingConfig, error)

LoadOracle loads an Oracle config from a file.

func LoadPostgres

func LoadPostgres(path string) (*PostgresConfig, *TracingConfig, error)

LoadPostgres loads a Postgres config from a file.

func LoadSQLite

func LoadSQLite(path string) (*SQLiteConfig, *TracingConfig, error)

LoadSQLite loads a SQLite config from a file.

func LoadTiDB

func LoadTiDB(path string) (*TiDBConfig, *TracingConfig, error)

LoadTiDB loads a TiDB config from a file.

Types

type BaseDBConfig

type BaseDBConfig struct {
	CommonNetworkConfig `yaml:",inline" json:",inline" toml:",inline"`
	Pool                *PoolConfig    `yaml:"pool,omitempty" json:"pool,omitempty" toml:"pool,omitempty"`
	Tracing             *TracingConfig `yaml:"tracing,omitempty" json:"tracing,omitempty" toml:"tracing,omitempty"`
}

BaseDBConfig embeds CommonNetworkConfig, PoolConfig, and TracingConfig.

func (*BaseDBConfig) DSN

func (b *BaseDBConfig) DSN() string

DSN builds a default MySQL-style DSN from CommonNetworkConfig.

func (*BaseDBConfig) GetPool

func (b *BaseDBConfig) GetPool() *PoolConfig

func (*BaseDBConfig) GetTracing

func (b *BaseDBConfig) GetTracing() *TracingConfig

type ClusterConfig

type ClusterConfig struct {
	Driver      string         `yaml:"driver" json:"driver" toml:"driver"`
	Sources     []NodeConfig   `yaml:"sources" json:"sources" toml:"sources"`
	Replicas    []NodeConfig   `yaml:"replicas,omitempty" json:"replicas,omitempty" toml:"replicas,omitempty"`
	LoadBalance string         `yaml:"load_balance" json:"load_balance" toml:"load_balance"`
	Pool        *PoolConfig    `yaml:"pool,omitempty" json:"pool,omitempty" toml:"pool,omitempty"`
	Tracing     *TracingConfig `yaml:"tracing,omitempty" json:"tracing,omitempty" toml:"tracing,omitempty"`
}

ClusterConfig holds configuration for cluster (read/write splitting) access.

func (*ClusterConfig) Validate

func (cc *ClusterConfig) Validate() error

Validate validates ClusterConfig, setting defaults.

type CommonNetworkConfig

type CommonNetworkConfig struct {
	Host     string `yaml:"host" json:"host" toml:"host"`
	Port     int    `yaml:"port" json:"port" toml:"port"`
	Username string `yaml:"username" json:"username" toml:"username"`
	Password string `yaml:"password" json:"-" toml:"password"`
	Database string `yaml:"database" json:"database" toml:"database"`
}

CommonNetworkConfig holds network connection fields shared by 7/8 databases.

func (*CommonNetworkConfig) DSN

func (c *CommonNetworkConfig) DSN() string

DSN builds a default MySQL-style DSN.

type DBConfig

type DBConfig interface {
	DriverName() string
	DSN() string
	Validate() error
	GetPool() *PoolConfig
	GetTracing() *TracingConfig
}

DBConfig is the unified interface for all 8 database config types.

type GaussDBConfig

type GaussDBConfig struct {
	BaseDBConfig `yaml:",inline" json:",inline" toml:",inline"`
	SSLMode      string `yaml:"ssl_mode" json:"ssl_mode" toml:"ssl_mode"`
	Schema       string `yaml:"schema" json:"schema" toml:"schema"`
}

GaussDBConfig implements DBConfig for Huawei GaussDB (wire-compatible with PostgreSQL).

func (*GaussDBConfig) DSN

func (c *GaussDBConfig) DSN() string

DSN builds a GaussDB DSN string (PostgreSQL wire-compatible, no TimeZone).

func (*GaussDBConfig) DriverName

func (c *GaussDBConfig) DriverName() string

DriverName returns the driver name.

func (*GaussDBConfig) Validate

func (c *GaussDBConfig) Validate() error

Validate validates the config and sets defaults.

type MSSQLConfig

type MSSQLConfig struct {
	BaseDBConfig `yaml:",inline" json:",inline" toml:",inline"`
	Instance     string `yaml:"instance" json:"instance" toml:"instance"`
}

MSSQLConfig implements DBConfig for Microsoft SQL Server.

func (*MSSQLConfig) DSN

func (c *MSSQLConfig) DSN() string

DSN builds a SQL Server connection string.

func (*MSSQLConfig) DriverName

func (c *MSSQLConfig) DriverName() string

DriverName returns the driver name.

func (*MSSQLConfig) Validate

func (c *MSSQLConfig) Validate() error

Validate validates the config and sets defaults.

type MariaDBConfig

type MariaDBConfig struct {
	BaseDBConfig `yaml:",inline" json:",inline" toml:",inline"`
	Charset      string `yaml:"charset" json:"charset" toml:"charset"`
}

MariaDBConfig implements DBConfig for MariaDB (wire-compatible with MySQL).

func (*MariaDBConfig) DSN

func (c *MariaDBConfig) DSN() string

DSN builds a MariaDB DSN string.

func (*MariaDBConfig) DriverName

func (c *MariaDBConfig) DriverName() string

DriverName returns the driver name.

func (*MariaDBConfig) Validate

func (c *MariaDBConfig) Validate() error

Validate validates the config, setting defaults.

type MySQLConfig

type MySQLConfig struct {
	BaseDBConfig `yaml:",inline" json:",inline" toml:",inline"`
	Charset      string `yaml:"charset" json:"charset" toml:"charset"`
}

MySQLConfig implements DBConfig for MySQL.

func (*MySQLConfig) DSN

func (c *MySQLConfig) DSN() string

DSN builds a MySQL DSN string.

func (*MySQLConfig) DriverName

func (c *MySQLConfig) DriverName() string

DriverName returns the driver name.

func (*MySQLConfig) Validate

func (c *MySQLConfig) Validate() error

Validate validates the config, setting defaults.

type NodeConfig

type NodeConfig struct {
	Host     string `yaml:"host" json:"host" toml:"host"`
	Port     int    `yaml:"port" json:"port" toml:"port"`
	Username string `yaml:"username" json:"username" toml:"username"`
	Password string `yaml:"password" json:"-" toml:"password"`
	Database string `yaml:"database" json:"database" toml:"database"`
	// Path is the file path for sqlite cluster nodes (reuses Driver=sqlite).
	Path string `yaml:"path,omitempty" json:"path,omitempty" toml:"path,omitempty"`
	// Expanded fields per driver
	Instance    string `yaml:"instance,omitempty" json:"instance,omitempty" toml:"instance,omitempty"`
	ServiceName string `yaml:"service_name,omitempty" json:"service_name,omitempty" toml:"service_name,omitempty"`
	SID         string `yaml:"sid,omitempty" json:"sid,omitempty" toml:"sid,omitempty"`
	SSLMode     string `yaml:"ssl_mode,omitempty" json:"ssl_mode,omitempty" toml:"ssl_mode,omitempty"`
	Schema      string `yaml:"schema,omitempty" json:"schema,omitempty" toml:"schema,omitempty"`
}

NodeConfig holds a single database node connection info for cluster use. Network fields are used by 7/8 drivers; Path is used by sqlite.

func (*NodeConfig) DSN

func (n *NodeConfig) DSN(driver string) string

DSN builds a DSN for the given driver. The driver is the cluster-level driver (e.g. "mysql", "postgres", "mssql", "oracle", "sqlite"). Network drivers use the network fields; sqlite uses Path.

type OracleConfig

type OracleConfig struct {
	BaseDBConfig `yaml:",inline" json:",inline" toml:",inline"`
	ServiceName  string `yaml:"service_name" json:"service_name" toml:"service_name"`
	SID          string `yaml:"sid" json:"sid" toml:"sid"`
}

OracleConfig implements DBConfig for Oracle Database.

func (*OracleConfig) DSN

func (c *OracleConfig) DSN() string

DSN builds an Oracle connection string.

func (*OracleConfig) DriverName

func (c *OracleConfig) DriverName() string

DriverName returns the driver name.

func (*OracleConfig) Validate

func (c *OracleConfig) Validate() error

Validate validates the config and sets defaults.

type PoolConfig

type PoolConfig struct {
	MaxOpenConns    int `yaml:"max_open_conns" json:"max_open_conns" toml:"max_open_conns"`
	MaxIdleConns    int `yaml:"max_idle_conns" json:"max_idle_conns" toml:"max_idle_conns"`
	ConnMaxLifetime int `yaml:"conn_max_lifetime" json:"conn_max_lifetime" toml:"conn_max_lifetime"`
	ConnMaxIdleTime int `yaml:"conn_max_idle_time" json:"conn_max_idle_time" toml:"conn_max_idle_time"`
}

PoolConfig holds database connection pool settings.

func (*PoolConfig) ConnMaxIdleTimeDuration

func (p *PoolConfig) ConnMaxIdleTimeDuration() time.Duration

ConnMaxIdleTimeDuration returns ConnMaxIdleTime as time.Duration.

func (*PoolConfig) ConnMaxLifetimeDuration

func (p *PoolConfig) ConnMaxLifetimeDuration() time.Duration

ConnMaxLifetimeDuration returns ConnMaxLifetime as time.Duration.

func (*PoolConfig) Validate

func (p *PoolConfig) Validate() error

Validate sets defaults and validates PoolConfig.

type PostgresConfig

type PostgresConfig struct {
	BaseDBConfig `yaml:",inline" json:",inline" toml:",inline"`
	SSLMode      string `yaml:"ssl_mode" json:"ssl_mode" toml:"ssl_mode"`
	TimeZone     string `yaml:"timezone" json:"timezone" toml:"timezone"`
	Schema       string `yaml:"schema" json:"schema" toml:"schema"`
}

PostgresConfig implements DBConfig for PostgreSQL.

func (*PostgresConfig) DSN

func (c *PostgresConfig) DSN() string

DSN builds a PostgreSQL connection string.

func (*PostgresConfig) DriverName

func (c *PostgresConfig) DriverName() string

DriverName returns the driver name.

func (*PostgresConfig) Validate

func (c *PostgresConfig) Validate() error

Validate validates the config and sets defaults.

type SQLiteConfig

type SQLiteConfig struct {
	Path    string         `yaml:"path" json:"path" toml:"path"`
	Pool    *PoolConfig    `yaml:"pool,omitempty" json:"pool,omitempty" toml:"pool,omitempty"`
	Tracing *TracingConfig `yaml:"tracing,omitempty" json:"tracing,omitempty" toml:"tracing,omitempty"`
}

SQLiteConfig implements DBConfig for SQLite (file-based, no network fields).

func (*SQLiteConfig) DSN

func (c *SQLiteConfig) DSN() string

DSN returns the file path.

func (*SQLiteConfig) DriverName

func (c *SQLiteConfig) DriverName() string

DriverName returns the driver name.

func (*SQLiteConfig) GetPool

func (c *SQLiteConfig) GetPool() *PoolConfig

GetPool returns the pool config.

func (*SQLiteConfig) GetTracing

func (c *SQLiteConfig) GetTracing() *TracingConfig

GetTracing returns the tracing config.

func (*SQLiteConfig) Validate

func (c *SQLiteConfig) Validate() error

Validate validates the config.

type TiDBConfig

type TiDBConfig struct {
	BaseDBConfig `yaml:",inline" json:",inline" toml:",inline"`
	Charset      string `yaml:"charset" json:"charset" toml:"charset"`
}

TiDBConfig implements DBConfig for TiDB (wire-compatible with MySQL).

func (*TiDBConfig) DSN

func (c *TiDBConfig) DSN() string

DSN builds a TiDB DSN string.

func (*TiDBConfig) DriverName

func (c *TiDBConfig) DriverName() string

DriverName returns the driver name.

func (*TiDBConfig) Validate

func (c *TiDBConfig) Validate() error

Validate validates the config, setting defaults.

type TracingConfig

type TracingConfig struct {
	Enabled      bool    `yaml:"enabled" json:"enabled" toml:"enabled"`
	Service      string  `yaml:"service" json:"service" toml:"service"`
	Exporter     string  `yaml:"exporter" json:"exporter" toml:"exporter"`
	Endpoint     string  `yaml:"endpoint" json:"endpoint" toml:"endpoint"`
	Protocol     string  `yaml:"protocol" json:"protocol" toml:"protocol"`
	SamplerType  string  `yaml:"sampler_type" json:"sampler_type" toml:"sampler_type"`
	SamplerRatio float64 `yaml:"sampler_ratio" json:"sampler_ratio" toml:"sampler_ratio"`
	// Mode selects single (POS) vs cluster (POC) for the underlying mqx
	// producer. Only `redis_stream` actually branches on Mode — kafka's
	// client supports multiple brokers natively, so kafkax.POS is always
	// used regardless of Mode. The field is accepted for all exporters so
	// yaml schema is uniform.
	Mode string `yaml:"mode,omitempty" json:"mode,omitempty" toml:"mode,omitempty"`
	// Kafka-specific
	Topic                 string `yaml:"topic" json:"topic" toml:"topic"`
	KafkaUsername         string `yaml:"kafka_username,omitempty" json:"kafka_username,omitempty" toml:"kafka_username,omitempty"`
	KafkaPassword         string `yaml:"kafka_password,omitempty" json:"-" toml:"kafka_password,omitempty"`
	KafkaSecurityProtocol string `yaml:"kafka_security_protocol,omitempty" json:"kafka_security_protocol,omitempty" toml:"kafka_security_protocol,omitempty"`
	KafkaSASLMechanism    string `yaml:"kafka_sasl_mechanism,omitempty" json:"kafka_sasl_mechanism,omitempty" toml:"kafka_sasl_mechanism,omitempty"`
	// Redis-specific
	Stream        string `yaml:"stream" json:"stream" toml:"stream"`
	RedisPassword string `yaml:"redis_password" json:"-" toml:"redis_password"`
}

TracingConfig holds OpenTelemetry tracing export configuration.

Field names follow the mqx standard where overlap exists (e.g. `topic`, `stream`, `endpoint`, `sampler_type`). The translation from the user-facing mqx-style yaml to this internal struct happens in dbsql/tracer.go.

func (*TracingConfig) Validate

func (tc *TracingConfig) Validate() error

Validate sets defaults and validates TracingConfig.

Jump to

Keyboard shortcuts

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