database

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotConnected is returned when trying to use a driver that hasn't been connected
	ErrNotConnected = errors.New("database: not connected")

	// ErrDriverNotFound is returned when a driver is not registered
	ErrDriverNotFound = errors.New("database: driver not found")

	// ErrDriverAlreadyRegistered is returned when trying to register a driver with an existing name
	ErrDriverAlreadyRegistered = errors.New("database: driver already registered")
)
View Source
var ProviderDisplayNames = map[string]string{
	"postgresql":  "PostgreSQL",
	"postgres":    "PostgreSQL",
	"mysql":       "MySQL",
	"sqlite":      "SQLite",
	"sqlserver":   "SQL Server",
	"mongodb":     "MongoDB",
	"cockroachdb": "CockroachDB",
}

ProviderDisplayNames maps provider names to their display names

Functions

func GetProviderDisplayName

func GetProviderDisplayName(provider string) string

GetProviderDisplayName returns the formatted display name for a provider

func Has

func Has(name string) bool

Has checks if a driver is registered

func List

func List() []string

List returns all registered driver names

func MustRegister

func MustRegister(name string, factory DriverFactory)

MustRegister registers a driver and panics on error

func Register

func Register(name string, factory DriverFactory) error

Register registers a driver factory with a name

Types

type BaseDriver

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

BaseDriver provides common functionality for SQL-based drivers

func (*BaseDriver) Close

func (d *BaseDriver) Close() error

Close closes the database connection

func (*BaseDriver) Connect

func (d *BaseDriver) Connect(cfg *Config) error

Connect is a no-op for BaseDriver (used when connection is already established)

func (*BaseDriver) DB

func (d *BaseDriver) DB() *sql.DB

DB returns the underlying *sql.DB

func (*BaseDriver) Exec

func (d *BaseDriver) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query without returning rows

func (*BaseDriver) Name

func (d *BaseDriver) Name() string

Name returns the driver name

func (*BaseDriver) Ping

func (d *BaseDriver) Ping() error

Ping verifies the connection is alive

func (*BaseDriver) Query

func (d *BaseDriver) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows

func (*BaseDriver) QueryRow

func (d *BaseDriver) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query that returns at most one row

func (*BaseDriver) SetDB

func (d *BaseDriver) SetDB(db *sql.DB)

SetDB sets the underlying database connection

func (*BaseDriver) SetName

func (d *BaseDriver) SetName(name string)

SetName sets the driver name

type Client

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

Client is a high-level wrapper around DBDriver

func NewClient

func NewClient(driverName string) (*Client, error)

NewClient creates a new database client for the specified driver

func NewClientFromDSN

func NewClientFromDSN(driverName, dsn string) (*Client, error)

NewClientFromDSN creates a new client and connects using a DSN string driverName should be the Prisma provider name (postgresql, mysql, etc.)

func (*Client) Close

func (c *Client) Close() error

Close closes the database connection

func (*Client) Config

func (c *Client) Config() *Config

Config returns the connection config

func (*Client) Connect

func (c *Client) Connect(cfg *Config) error

Connect connects to the database

func (*Client) DB

func (c *Client) DB() *sql.DB

DB returns the underlying *sql.DB for advanced operations

func (*Client) Driver

func (c *Client) Driver() DBDriver

Driver returns the underlying driver

func (*Client) DriverName

func (c *Client) DriverName() string

DriverName returns the name of the driver

func (*Client) Exec

func (c *Client) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query without returning rows

func (*Client) Ping

func (c *Client) Ping() error

Ping verifies the connection is alive

func (*Client) Query

func (c *Client) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows

func (*Client) QueryRow

func (c *Client) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query that returns at most one row

type Config

type Config struct {
	Host     string
	Port     int
	User     string
	Password string
	Database string
	SSLMode  string            // disable, require, verify-ca, verify-full
	Extra    map[string]string // DBMS-specific options
}

Config holds database connection configuration

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config with defaults

func (*Config) MySQLDSN

func (c *Config) MySQLDSN() string

MySQLDSN returns a MySQL connection string

func (*Config) PostgresDSN

func (c *Config) PostgresDSN() string

PostgresDSN returns a PostgreSQL connection string

func (*Config) WithDatabase

func (c *Config) WithDatabase(database string) *Config

WithDatabase sets the database name

func (*Config) WithExtra

func (c *Config) WithExtra(key, value string) *Config

WithExtra sets an extra option

func (*Config) WithHost

func (c *Config) WithHost(host string) *Config

WithHost sets the host

func (*Config) WithPassword

func (c *Config) WithPassword(password string) *Config

WithPassword sets the password

func (*Config) WithPort

func (c *Config) WithPort(port int) *Config

WithPort sets the port

func (*Config) WithSSLMode

func (c *Config) WithSSLMode(mode string) *Config

WithSSLMode sets the SSL mode

func (*Config) WithUser

func (c *Config) WithUser(user string) *Config

WithUser sets the user

type DBDriver

type DBDriver interface {
	// Name returns the driver name (e.g., "postgres", "mysql")
	Name() string

	// Connect establishes a connection using the provided config
	Connect(cfg *Config) error

	// Close closes the database connection
	Close() error

	// Ping verifies the connection is alive
	Ping() error

	// Query executes a query that returns rows
	Query(query string, args ...any) (*sql.Rows, error)

	// QueryRow executes a query that returns at most one row
	QueryRow(query string, args ...any) *sql.Row

	// Exec executes a query without returning rows
	Exec(query string, args ...any) (sql.Result, error)

	// DB returns the underlying *sql.DB for advanced operations
	DB() *sql.DB
}

DBDriver defines the interface for database drivers (Strategy pattern)

func Get

func Get(name string) (DBDriver, error)

Get returns a new driver instance by name

type DriverFactory

type DriverFactory func() DBDriver

DriverFactory is a function that creates a new DBDriver instance

type Registry

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

Registry manages database driver registration

func (*Registry) Get

func (r *Registry) Get(name string) (DBDriver, error)

Get returns a new driver instance by name

func (*Registry) Has

func (r *Registry) Has(name string) bool

Has checks if a driver is registered

func (*Registry) List

func (r *Registry) List() []string

List returns all registered driver names

func (*Registry) Register

func (r *Registry) Register(name string, factory DriverFactory) error

Register registers a driver factory with a name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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