Help us shape the future of Go! Take the 2026 Go Developer Survey

adaptor

package
v0.4.1-rc0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package adaptor defines the database driver abstraction used by sqlx.

An Adaptor is a connected, driver-specific handle: it executes frag.Fragment values (DB), exposes a Dialect for DDL/DML rendering and error classification, and can scan the live schema into a builder.Catalog. Higher layers such as migrator and session depend on this package; public callers usually reach it via github.com/xoctopus/sqlx/pkg/adaptors or session.New.

Interfaces

  • DB: Exec / Query / Tx over Fragments (backed by *sql.DB or a ctx Executor)
  • Adaptor: DB + DriverName / Schema / Dialect / Catalog
  • Connector: Open from a parsed DSN URL
  • Dialect: CreateTable / AddColumn / AddIndex / DBType / conflict detection, etc.

Registration and Open

Driver implementations call Register in init (optionally with aliases). Open parses a DSN (`scheme://...`), looks up the scheme, and delegates to that driver's Connector.Open.

a, err := adaptor.Open(ctx, "mysql://user:pass@tcp(localhost:3306)/db")

Blank-import github.com/xoctopus/sqlx/pkg/adaptors (or a concrete driver package such as mysql) so Register runs before Open.

Execution helpers

Driver packages

  • mysql: full Adaptor + Dialect (registered on import)
  • postgres / sqlite: undone

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DatabaseNameFromDSN

func DatabaseNameFromDSN(u *url.URL) string

DatabaseNameFromDSN returns the database name from u.Path, stripping a leading '/'.

func Register

func Register(a Adaptor, aliases ...string)

Register stores an Adaptor under its DriverName and optional aliases.

func WithExecutor

func WithExecutor(ctx context.Context, e Executor) context.Context

WithExecutor binds e into ctx for nested Exec/Query/Tx reuse.

Types

type Adaptor

type Adaptor interface {
	DB

	// DriverName returns the registered driver scheme (for example "mysql").
	DriverName() string
	// Schema returns the current database/schema name.
	Schema() string

	Dialect() Dialect
	Catalog(context.Context) (builder.Catalog, error)
}

Adaptor is a driver-specific database access handle.

func Open

func Open(ctx context.Context, dsn string) (Adaptor, error)

Open parses dsn and opens an Adaptor whose scheme matches a registered driver.

type Connector

type Connector interface {
	Open(context.Context, *url.URL) (Adaptor, error)
}

Connector opens an Adaptor from a parsed DSN URL.

type DB

type DB interface {
	// D returns the underlying *sql.DB.
	D() *sql.DB

	Exec(context.Context, frag.Fragment) (sql.Result, error)
	Query(context.Context, frag.Fragment) (*sql.Rows, error)
	Tx(context.Context, func(context.Context) error) error
	Close() error
}

DB executes SQL fragments against a database connection pool.

func Wrap

func Wrap(d *sql.DB, ew func(error) error) DB

Wrap adapts *sql.DB to DB. ew is reserved for error wrapping.

type Dialect

type Dialect interface {
	CreateSchema(string) frag.Fragment
	SwitchSchema(string) frag.Fragment

	CreateTableIfNotExists(t builder.Table) []frag.Fragment
	DropTable(t builder.Table) frag.Fragment
	TruncateTable(t builder.Table) frag.Fragment

	AddColumn(builder.Col) frag.Fragment
	DropColumn(builder.Col) frag.Fragment
	RenameColumn(builder.Col, builder.Col) frag.Fragment
	ModifyColumn(builder.Col, builder.Col) frag.Fragment

	AddIndex(key builder.Key) frag.Fragment
	DropIndex(key builder.Key) frag.Fragment

	DBType(builder.ColumnDef) frag.Fragment
	ColDefine(dd *builder.ColumnDef)
	IsUnknownDatabaseError(error) bool
	IsConflictError(err error) bool
}

Dialect renders dialect-specific DDL/DML fragments and classifies driver errors.

type Executor

type Executor interface {
	ExecContext(context.Context, string, ...any) (sql.Result, error)
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}

Executor can run SQL with context, typically *sql.DB or *sql.Tx.

func ExecutorFrom

func ExecutorFrom(ctx context.Context) Executor

ExecutorFrom returns the Executor bound by WithExecutor, if any.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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