embedded

package module
v1.84.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: Apache-2.0 Imports: 32 Imported by: 4

README

Dolt Database Driver

This package provides a database/sql compatible driver for embedding Dolt inside a Go application. It allows you to access local Dolt databases via the file system, akin to SQLite, without running a Dolt server process.

For details of the database/sql package see this tutorial. Below I will cover things that are specific to using dolt with this database driver.

Ways to Set up a Dolt Database

Create a directory to house one or more dolt databases. Once this is created we'll create a directory for our first database using the dolt cli

mkdir dbs
mkdir dbs/testdb
cd dbs/testdb
dolt init

Alternatively you could clone a database from dolthub or a different remote:

mkdir dbs
cd dbs
dolt clone <REMOTE URL>

Finally, you can create the dbs directory as shown above and then create the database in code using a SQL CREATE TABLE statement

Connecting to the Database

First we'll import the dolt driver so that it will be registered

_ "github.com/dolthub/driver"

Then we will open a connection to the database (recommended):

import (
    "context"
    "database/sql"
    "time"

    "github.com/cenkalti/backoff/v4"
    embedded "github.com/dolthub/driver"
)

cfg, err := embedded.ParseDSN("file:///path/to/dbs?commitname=Your%20Name&[email protected]&database=databasename")
if err != nil {
    // handle error
}

// Optional: configure retries during engine open (e.g. lock contention).
// Retries are bounded by context (e.g. PingContext).
cfg.BackOff = backoff.NewExponentialBackOff()

connector, err := embedded.NewConnector(cfg)
if err != nil {
    // handle error
}

db := sql.OpenDB(connector)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = db.PingContext(ctx)

Now you can use your db as you would normally, however you have access to all of dolt's special features as well.

Dolt Data Source Names

The Dolt driver requires a DSN containing the directory where your databases live, and the name and email that are used in the commit log.

commitname - The name of the committer seen in the dolt commit log
commitemail - The email of the committer seen in the dolt commit log
database - The initial database to connect to
multistatements - If set to true, allows multiple statements in one query
clientfoundrows - If set to true, returns the number of matching rows instead of the number of changed rows in UPDATE queries
Retries on open (driver retries)

Retries during embedded engine open are configured via Config.BackOff passed to embedded.NewConnector. Retries are bounded by the context passed to Connect (typically via db.PingContext(...) or the first operation).

Example DSN

file:///path/to/dbs?commitname=Your%20Name&[email protected]&database=databasename

Multi-Statement Support

If you pass the multistatements=true parameter in the DSN, you can execute multiple statements in one query. The returned rows allow you to iterate over the returned result sets by using the NextResultSet method, just like you can with the MySQL driver.

rows, err := db.Query("SELECT * from someTable; SELECT * from anotherTable;")
// If an error is returned, it means it came from the first statement
if err != nil {
	panic(err)
}

for rows.Next() {
	// process the first result set
}

if rows.NextResultSet() {
    for rows.Next() {
        // process the second result set
    }
} else {
	// If NextResultSet returns false when there were more statements, it means there was an error,
	// which you can access through rows.Err()
	panic(rows.Err())
}

Documentation

Index

Constants

View Source
const (
	DoltDriverName = "dolt"

	CommitNameParam      = "commitname"
	CommitEmailParam     = "commitemail"
	DatabaseParam        = "database"
	MultiStatementsParam = "multistatements"
	ClientFoundRowsParam = "clientfoundrows"

	// The following params are passed through to Dolt's local DB loading layer via
	// engine.SqlEngineConfig.DBLoadParams. They are presence-based flags (values are ignored).
	DisableSingletonCacheParam    = "disable_singleton_cache"
	FailOnJournalLockTimeoutParam = "fail_on_journal_lock_timeout"
)

Variables

This section is empty.

Functions

func LoadMultiEnvFromDir

func LoadMultiEnvFromDir(
	ctx context.Context,
	cfg config.ReadWriteConfig,
	fs filesys.Filesys,
	path, version string,
) (*env.MultiRepoEnv, error)

LoadMultiEnvFromDir looks at each subfolder of the given path as a Dolt repository and attempts to return a MultiRepoEnv with initialized environments for each of those subfolder data repositories. subfolders whose name starts with '.' are skipped.

Types

type Config added in v1.83.8

type Config struct {
	// DSN is the original datasource name string used to create this config (optional).
	DSN string

	// Directory is the filesystem directory containing one or more Dolt databases (required).
	Directory string

	// CommitName and CommitEmail are used for Dolt commit metadata (required).
	CommitName  string
	CommitEmail string

	// Database is the initial database to connect to (optional).
	Database string

	// MultiStatements enables multi-statement support.
	MultiStatements bool

	// ClientFoundRows toggles the MySQL CLIENT_FOUND_ROWS capability in the session.
	ClientFoundRows bool

	// Params is the lower-cased DSN query param map as parsed from the DSN (optional).
	// This is preserved for forward-compat / feature flags while moving away from
	// DSN-driven configuration.
	Params map[string][]string

	// BackOff enables bounded retries when opening the embedded engine.
	//
	// If nil, engine open is attempted once.
	// If non-nil, NewConnector will retry opens for retryable errors using this BackOff.
	//
	// Note: BackOff implementations are stateful; callers should generally provide a
	// fresh instance per connector, and the connector will call Reset() before use.
	BackOff backoff.BackOff

	// Version is the Dolt environment version string used when loading repos (optional).
	// If empty, the connector will use a reasonable default.
	Version string
}

Config configures an embedded Dolt SQL connector / driver.

Typical usage is:

  • Parse a DSN with ParseDSN (populates the DSN-derived fields)
  • Optionally set BackOff to enable retries when opening the embedded engine
  • Pass the config to NewConnector, then use sql.OpenDB(connector)

DSN-derived fields are included so callers can parse once, adjust settings, and construct a connector without re-parsing string DSNs throughout the codebase.

func ParseDSN added in v1.83.8

func ParseDSN(dsn string) (Config, error)

ParseDSN parses the provided DSN string into a Config suitable for NewConnector.

It performs basic validation (required params, directory exists) and preserves the raw parsed param map (lower-cased keys) in Config.Params.

type Connector added in v1.83.8

type Connector struct {
	// contains filtered or unexported fields
}

Connector is a database/sql driver connector for embedded Dolt.

Callers should construct it with NewConnector and then pass it to sql.OpenDB. The connector shares a single underlying embedded engine across connections, and creates a per-connection session context on each Connect.

func NewConnector added in v1.83.8

func NewConnector(cfg Config) (*Connector, error)

NewConnector constructs a new connector for embedded Dolt. The embedded engine is opened lazily on first Connect (and shared thereafter).

If cfg.BackOff is non-nil, opening the engine will be retried for retryable open errors (e.g. lock contention) using that backoff, bounded by the Connect context.

func (*Connector) Close added in v1.83.8

func (c *Connector) Close() error

Close closes the shared embedded engine, if it has been opened. It is safe to call multiple times.

func (*Connector) Connect added in v1.83.8

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

Connect implements driver.Connector.

func (*Connector) Driver added in v1.83.8

func (c *Connector) Driver() driver.Driver

Driver implements driver.Connector.

type DoltConn

type DoltConn struct {
	// contains filtered or unexported fields
}

DoltConn is a driver.Conn implementation that represents a connection to a dolt database located on the filesystem

func (*DoltConn) Begin deprecated

func (d *DoltConn) Begin() (driver.Tx, error)

Begin starts and returns a new transaction.

Deprecated: Use BeginTx instead

func (*DoltConn) BeginTx

func (d *DoltConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

BeginTx starts and returns a new transaction. If the context is canceled by the user the sql package will call Tx.Rollback before discarding and closing the connection.

func (*DoltConn) Close

func (d *DoltConn) Close() error

Close releases the resources held by the DoltConn instance

func (*DoltConn) ExecContext added in v1.84.1

func (d *DoltConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)

ExecContext implements driver.ExecerContext.

func (*DoltConn) IsValid added in v1.84.1

func (d *DoltConn) IsValid() bool

func (*DoltConn) Ping added in v1.84.1

func (d *DoltConn) Ping(ctx context.Context) error

Ping implements driver.Pinger. It verifies the connection is still alive by executing a lightweight query against the engine.

func (*DoltConn) Prepare

func (d *DoltConn) Prepare(query string) (driver.Stmt, error)

Prepare packages up |query| as a *doltStmt so it can be executed. If multistatements mode has been enabled, then a *doltMultiStmt will be returned, capable of executing multiple statements.

func (*DoltConn) PrepareContext added in v1.84.1

func (d *DoltConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)

PrepareContext implements driver.ConnPrepareContext. The supplied context governs the preparation step only; the returned statement inherits the connection's ambient context for its executions.

func (*DoltConn) QueryContext added in v1.84.1

func (d *DoltConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)

QueryContext implements driver.QueryerContext.

func (*DoltConn) ResetSession added in v1.84.1

func (d *DoltConn) ResetSession(ctx context.Context) error

type DoltDataSource

type DoltDataSource struct {
	Directory string
	Params    map[string][]string
}

DoltDataSource provides access to the data provided by the connection string

func ParseDataSource

func ParseDataSource(dataSource string) (*DoltDataSource, error)

ParseDataSource takes the connection string and parses out the parameters and the local filesys directory where the dolt database lives

func (*DoltDataSource) ParamIsTrue

func (ds *DoltDataSource) ParamIsTrue(paramName string) bool

type QuerySplitter

type QuerySplitter struct {
	// contains filtered or unexported fields
}

func NewQuerySplitter

func NewQuerySplitter(str string) *QuerySplitter

func (*QuerySplitter) HasMore

func (qs *QuerySplitter) HasMore() bool

func (*QuerySplitter) Next

func (qs *QuerySplitter) Next() (string, error)

type RuneStack

type RuneStack struct {
	// contains filtered or unexported fields
}

RuneStack is a simple stack of runes

func NewRuneStack added in v1.84.1

func NewRuneStack() *RuneStack

NewRuneStack returns a new RuneStack object

func (*RuneStack) Peek

func (bs *RuneStack) Peek() rune

Peek returns the value at the top of the stack

func (*RuneStack) Pop

func (bs *RuneStack) Pop() rune

Pop takes the top value of the top of the stack and returns it

func (*RuneStack) Push

func (bs *RuneStack) Push(b rune)

Push pushes a new rune on the stack

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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