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 ¶
- Wrap: adapt *sql.DB to DB
- WithExecutor / ExecutorFrom: bind *sql.DB or *sql.Tx into context so nested Exec/Query reuse the same executor (used by DB.Tx)
- DatabaseNameFromDSN: extract database/schema name from a DSN URL
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 ¶
DatabaseNameFromDSN returns the database name from u.Path, stripping a leading '/'.
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.
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.
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 ¶
ExecutorFrom returns the Executor bound by WithExecutor, if any.