driver

package
v1.27.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package driver is the contract a database driver module implements to plug into pkg/db. It is a LEAF package: it imports nothing but the standard library, so a driver module can implement it without compiling the framework — the same reason pkg/storage/provider and pkg/auth/backend are leaves (ADR-025, ADR-026).

A driver module does two things in its init(), and both matter:

  • it registers the database/sql driver, so sql.Open finds it;
  • it registers how that driver reports a unique-constraint violation.

The second is the one that is easy to forget and expensive to omit. Without it, IsUniqueViolation does not fail — it answers false, and the branch that turns "that email is taken" into a 409 becomes dead code on that engine. A wrong answer is worse than a missing one, so a driver module that registers the driver without its classifier is a bug in the module, not a degraded mode of the framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasEngine

func HasEngine(engine string) bool

HasEngine reports whether engine registered a classifier.

func MustRegisterUniqueViolation

func MustRegisterUniqueViolation(engine string, fn UniqueViolationFunc)

MustRegisterUniqueViolation is RegisterUniqueViolation for use in an init(), where there is no caller to hand an error back to.

func RegisterUniqueViolation

func RegisterUniqueViolation(engine string, fn UniqueViolationFunc) error

RegisterUniqueViolation records how the driver registered under engine — the database/sql driver name, e.g. "pgx", "mysql", "sqlite" — reports a unique-constraint violation.

It returns an error rather than panicking on a duplicate so that a module linked twice through different paths fails loudly at init in the module's own code, where the import that caused it is visible.

func RegisteredEngines

func RegisteredEngines() []string

RegisteredEngines returns the engines that have a classifier, sorted. It is what an error message uses to tell an operator what IS linked in.

Types

type UniqueViolationFunc

type UniqueViolationFunc func(error) bool

UniqueViolationFunc reports whether err — as produced by one specific driver — was caused by a unique or primary-key constraint.

It must match on the CODE the driver reports, through errors.As, never on substrings of the message: PostgreSQL, MySQL, Oracle and SQL Server all translate their messages when the server runs in another language, so a substring check silently returns false on exactly the deployments where it matters. It must also walk the Unwrap chain, so a wrapped error still classifies.

It must return false for an error that did not come from its own driver: classifiers are consulted in turn, and one that claims errors it does not recognise would answer for another engine.

func UniqueViolationFuncs

func UniqueViolationFuncs() []UniqueViolationFunc

UniqueViolationFuncs returns the registered classifiers, in a stable order so that classification does not depend on map iteration.

Directories

Path Synopsis
Package drivertest is a conformance kit for database driver modules.
Package drivertest is a conformance kit for database driver modules.

Jump to

Keyboard shortcuts

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