Documentation
¶
Overview ¶
Package dbkit provides unified database connection initialization for PostgreSQL and Dameng DM.
The single entry point for applications:
db, err := dbkit.Open(config)
It returns a standard *sql.DB; connection pooling is managed by database/sql. This library implements neither pooling nor node health checks nor failover - those are provided by the database drivers or the databases' own HA mechanisms.
Index ¶
Constants ¶
const ( ModeStandalone = "standalone" ModeCluster = "cluster" )
Valid mode values in configuration.
const ( DriverPostgres = "postgres" DriverDM = "dm" )
Driver identifiers in configuration (Registry keys).
Variables ¶
var ( // ErrUnknownDriver is returned when the requested driver is not registered. ErrUnknownDriver = errors.New("dbkit: unknown driver") // ErrDuplicateDriver is returned when registering a driver name twice. ErrDuplicateDriver = errors.New("dbkit: duplicate driver") )
Functions ¶
Types ¶
type Config ¶
type Config struct {
Driver string `yaml:"driver"` // postgres | dm
Mode string `yaml:"mode"` // standalone | cluster
Host string `yaml:"host"` // required by standalone
Port int `yaml:"port"` // required by standalone, [1,65535]
Nodes []NodeConfig `yaml:"nodes"` // required by cluster
Options map[string]string `yaml:"options"` // driver-specific passthrough
Username string `yaml:"username"`
Password string `yaml:"password"`
Database string `yaml:"database"`
}
Config describes a database connection. Fields map one-to-one to the YAML structure; YAML decoding is the application's responsibility (this library does not depend on yaml).
type DMDriver ¶
type DMDriver struct{}
DMDriver adapts Dameng DM, backed by the official Go driver (third_party/dm, registered as "dm").
Cluster mode uses the driver's native dynamic service name mechanism: the DSN host position holds a service-name placeholder and the node list is passed as a query parameter of the same name. Node traversal, loginMode primary/standby preference and switchTimes/switchInterval retries are all handled by the driver. This library performs no cluster management.
func (DMDriver) BuildDSN ¶
BuildDSN builds a native DM DSN:
standalone: dm://user:pass@host:port?<params sorted by key> cluster: dm://user:pass@dbkit_svc?dbkit_svc=(h1:p1,h2:p2)&<params sorted by key>
cfg.Database maps to the driver's schema connection parameter (a DM instance holds one database; schemas are the namespaces). The driver ignores a DSN path in cluster mode, while the schema parameter works in both modes. An explicit schema option wins over the injected value.
DSN escaping limits (the driver's parser does not URL-decode, see third_party/dm n.go parseDSN): username/password must not contain '?'; option keys must not contain '&', '=' or '?'; option values must not contain '&' or '?'. '@' ':' '&' '=' are safe in username/password, which the driver parses via LastIndex("@") + SplitN(":", 2).
func (DMDriver) SQLDriverName ¶
SQLDriverName returns the database/sql registered driver name.
type Driver ¶
type Driver interface {
// Name is the driver identifier in configuration (Registry key),
// e.g. postgres, dm.
Name() string
// SQLDriverName is the database/sql registered driver name, e.g. pgx, dm.
// PostgreSQL differs between the two, hence the separate method.
SQLDriverName() string
// BuildDSN builds the DSN; the given Config is assumed to be validated.
BuildDSN(cfg Config) (string, error)
}
Driver is the adapter interface for a database driver. Implementations convert a Config into the driver's native DSN. BuildDSN is a pure function and performs no I/O; DSN format and parameter semantics follow each driver's official documentation.
type NodeConfig ¶
NodeConfig is a single node address in cluster mode.
type PostgresDriver ¶
type PostgresDriver struct{}
PostgresDriver adapts PostgreSQL, backed by pgx/v5 (stdlib registers "pgx").
Cluster mode uses pgx's native multi-host DSN (host=h1,h2 port=p1,p2); node selection, target_session_attrs filtering and failover are handled by pgx itself. This library performs no cluster management.
func (PostgresDriver) BuildDSN ¶
func (PostgresDriver) BuildDSN(cfg Config) (string, error)
BuildDSN builds a libpq keyword=value DSN:
host=h1[,h2...] port=p1[,p2...] user=... password=... dbname=... <options sorted by key>
func (PostgresDriver) Name ¶
func (PostgresDriver) Name() string
Name returns the driver identifier used in configuration.
func (PostgresDriver) SQLDriverName ¶
func (PostgresDriver) SQLDriverName() string
SQLDriverName returns the database/sql registered driver name.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
dm-cluster
command
dm-cluster example: reads config.yaml and connects to Dameng DM via dbkit.Open.
|
dm-cluster example: reads config.yaml and connects to Dameng DM via dbkit.Open. |
|
dm-standalone
command
dm-standalone example: reads config.yaml and connects to Dameng DM via dbkit.Open.
|
dm-standalone example: reads config.yaml and connects to Dameng DM via dbkit.Open. |
|
postgres-cluster
command
postgres-cluster example: reads config.yaml and connects to PostgreSQL via dbkit.Open.
|
postgres-cluster example: reads config.yaml and connects to PostgreSQL via dbkit.Open. |
|
postgres-standalone
command
postgres-standalone example: reads config.yaml and connects to PostgreSQL via dbkit.Open.
|
postgres-standalone example: reads config.yaml and connects to PostgreSQL via dbkit.Open. |
|
third_party
|
|