Understand database/sql and driver boundaries
Understand database/sql and driver boundaries
database/sql provides a common Go API for relational databases; it does not implement a database engine or ship a production SQL driver. A driver translates operations into a particular engine protocol. These labs use an in-process teaching driver with fixed responses: it does not parse SQL, store rows, or prove database compatibility. All examples run locally with Go 1.23.2. The helper must panics only on unexpected lab errors; applications return and handle errors.
package main
import ( "database/sql" "fmt")
func main() { _, err := sql.Open("unregistered-lab-driver", "") fmt.Println(err != nil)}An unknown driver name fails at Open. Real drivers register a name (often using a blank import) or expose a Connector for sql.OpenDB. Choose the supported driver and version for your actual engine; the data source name format is driver-specific.
Expected output:
trueRegistration and a shared handle
package main
import ( "context" "database/sql" "database/sql/driver" "errors" "fmt" "io")
// fixtureDriver is a teaching double, not a SQL engine.type fixtureDriver struct{}type fixtureConn struct{ mode string }type fixtureTx struct{}type fixtureRows struct { values [][]driver.Value fail bool index int}
func (fixtureDriver) Open(name string) (driver.Conn, error) { if name == "offline" { return nil, errors.New("fixture offline") } return &fixtureConn{mode: name}, nil}func (c *fixtureConn) Prepare(string) (driver.Stmt, error) { return nil, errors.New("prepare not implemented in fixture")}func (c *fixtureConn) Close() error { return nil }func (c *fixtureConn) Begin() (driver.Tx, error) { return &fixtureTx{}, nil }func (c *fixtureConn) Ping(ctx context.Context) error { return ctx.Err() }func (t *fixtureTx) Commit() error { return nil }func (t *fixtureTx) Rollback() error { return nil }func (c *fixtureConn) QueryContext(ctx context.Context, q string, args []driver.NamedValue) (driver.Rows, error) { if err := ctx.Err(); err != nil { return nil, err } switch q { case "SELECT name FROM books WHERE id = ?": if len(args) != 1 { return nil, errors.New("one argument required") } if args[0].Value == int64(7) { return &fixtureRows{values: [][]driver.Value{{"Go"}}}, nil } return &fixtureRows{}, nil case "SELECT name FROM books": return &fixtureRows{values: [][]driver.Value{{"Go"}, {nil}}, fail: c.mode == "broken"}, nil default: return nil, errors.New("unsupported fixture query") }}func (c *fixtureConn) ExecContext(ctx context.Context, q string, args []driver.NamedValue) (driver.Result, error) { if err := ctx.Err(); err != nil { return nil, err } if q != "UPDATE books SET name = ? WHERE id = ?" || len(args) != 2 { return nil, errors.New("unsupported fixture update") } if args[1].Value == int64(7) { return driver.RowsAffected(1), nil } return driver.RowsAffected(0), nil}func (r *fixtureRows) Columns() []string { return []string{"name"} }func (r *fixtureRows) Close() error { return nil }func (r *fixtureRows) Next(dest []driver.Value) error { if r.index == len(r.values) { if r.fail { return errors.New("fixture stream interrupted") } return io.EOF } copy(dest, r.values[r.index]) r.index++ return nil}func must(err error) { if err != nil { panic(err) }}func openFixture(mode string) *sql.DB { db, err := sql.Open("bytemind-fixture", mode) must(err) return db}func init() { sql.Register("bytemind-fixture", fixtureDriver{}) }
func main() { db := openFixture("ready") defer db.Close() fmt.Println(len(sql.Drivers()) == 1) fmt.Println(db.Ping() == nil)}The fixture implements driver.Driver, driver.Conn and optional context-aware interfaces. The lengthy helper exists only to let database/sql manage real pool and row objects without external dependencies. A DB is a concurrency-safe pool handle, not a dedicated physical connection. Reuse it across requests.
Expected output:
truetrueTry it locally
Check that sql.Open rejects another unregistered driver and returns a nil handle. Print both booleans.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Database 01: Understand database/sql and driver boundaries
Check the lesson objective before moving on.
What does database/sql provide?
What does the fixture prove?