Principle:4paradigm OpenMLDB Database Driver Implementation
| Knowledge Sources | |
|---|---|
| Domains | SDK, Database |
| Last Updated | 2026-10-12 12:00 GMT |
Overview
A driver implementation pattern that adapts a database's wire protocol (HTTP/JSON) to a standard SQL driver interface, enabling seamless integration with language-native database APIs.
Description
Database driver implementation bridges a database's native protocol with a language's standard database interface. In OpenMLDB, the Go SDK driver implements Go's database/sql/driver interfaces by translating standard SQL operations into HTTP POST requests to the OpenMLDB API server. The driver handles DSN parsing, connection management, query mode selection (offline/online), parameter serialization, and response deserialization. This allows Go applications to use OpenMLDB through the familiar database/sql API without knowing the underlying HTTP protocol.
The key problem solved is API compatibility: applications written for standard SQL databases (PostgreSQL, MySQL) can switch to OpenMLDB by changing only the driver import and DSN, without rewriting query logic.
Usage
Use this principle when providing SDK access to a database that uses a non-standard wire protocol. Implement the standard driver interfaces of the target language to enable drop-in compatibility with existing database clients.
Theoretical Basis
The driver pattern follows the adapter pattern from software design:
The driver implements these interfaces:
// Abstract interface description (from database/sql/driver)
type Driver interface {
Open(name string) (Conn, error)
}
type Conn interface {
QueryContext(ctx, query, args) (Rows, error)
ExecContext(ctx, query, args) (Result, error)
Ping(ctx) error
Close() error
}
type Rows interface {
Columns() []string
Next(dest []Value) error
Close() error
}
The driver translates each call:
Open(dsn)→ parse DSN, createconnstructQueryContext→ HTTP POST with SQL and parameters as JSONNext()→ iterate pre-parsed JSON response rowsPing→ executeSELECT 1via HTTP
Practical Guide
- Use side-effect import (
_ "github.com/4paradigm/openmldb/go") to register the driver - The DSN format is
openmldb://host:port/db?mode=offsync|offasync|online mode=onlineis for real-time feature serving;mode=offsyncis for synchronous offline jobs- The driver does not support
Prepare()orBegin()— useQueryContextdirectly